-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathask_gemini.py
78 lines (71 loc) · 2.17 KB
/
ask_gemini.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
from hashlib import md5
from time import sleep
import requests
import pandas as pd
from parser import (
AI,
HttpErr,
)
class ResourceExhaustedErr(HttpErr):
pass
def md5sum_file(filename: str):
if not os.path.exists(filename):
return
hash_md5 = md5()
with open(filename, 'rb') as f:
for chunk in iter(lambda: f.read(4096), b''):
hash_md5.update(chunk)
return hash_md5.hexdigest()
class Gemini(AI):
def ask(self, prompt: str) -> str: # Override
d = dict(contents=[dict(parts=[dict(text=prompt)])])
r = requests.post(self.conf['ai']['url'], json=d)
if r.status_code == 200:
d = r.json()
s = d['candidates'][0]['content']['parts'][0]['text']
return s.rstrip()
print('*' * 20)
print(r.text)
if r.status_code == 429:
raise ResourceExhaustedErr(r)
raise HttpErr(r)
def parse(self): # Override
conf = self.conf['ai']
step = 10
wait_seconds = 20
if os.path.exists(self.output_file):
df = pd.read_csv(self.output_file)
count = len(df)
limit = count // step * step + step
else:
limit = step
is_error = False
while True:
old_md5 = md5sum_file(self.output_file)
try:
super().parse()
is_error = False
wait_seconds = 20
except ResourceExhaustedErr as e:
print('*' * 20)
print('* Quota error')
if wait_seconds > 60:
raise e
wait_seconds += 10
is_error = True
except HttpErr as e:
if is_error:
raise e
is_error = True
if is_error:
print(f'Tunggu {wait_seconds} detik ...')
sleep(wait_seconds)
continue
new_md5 = md5sum_file(self.output_file)
if old_md5 == new_md5:
if new_md5:
print('Selesai.')
break
continue
limit += step