Update app.py
Browse files
app.py
CHANGED
@@ -39,12 +39,7 @@ import time
|
|
39 |
# driver.get("https://yuntian-deng-chatgpt.hf.space/")
|
40 |
|
41 |
|
42 |
-
options = ChromeOptions()
|
43 |
-
options.add_argument('--no-sandbox')
|
44 |
-
options.add_argument('-headless')
|
45 |
-
service = Service()
|
46 |
|
47 |
-
driver = webdriver.Chrome(options= options,service=service)
|
48 |
|
49 |
# driver.get("https://yuntian-deng-chatgpt.hf.space/")
|
50 |
|
@@ -113,19 +108,26 @@ async def get_answer(q: Query ):
|
|
113 |
|
114 |
|
115 |
|
|
|
|
|
116 |
def do_ML(id:str,text:str,host:str, trycount:int):
|
|
|
|
|
|
|
|
|
|
|
117 |
try:
|
118 |
starttime=time.time()
|
119 |
driver.get("https://talkai.info/chat/")
|
120 |
-
time.sleep(
|
121 |
textarea = driver.find_element(By.CSS_SELECTOR, "textarea")
|
122 |
textarea.send_keys(text)
|
123 |
-
time.sleep(1
|
124 |
button = driver.find_element(By.CLASS_NAME, "sectionChatFormButton")
|
125 |
button.click()
|
126 |
prev =""
|
127 |
|
128 |
-
time.sleep(2)
|
129 |
while True:
|
130 |
time.sleep(2+trycount)
|
131 |
currtime= time.time()
|
@@ -138,14 +140,16 @@ def do_ML(id:str,text:str,host:str, trycount:int):
|
|
138 |
last_message_contain = messages[len(messages)-2]
|
139 |
value = last_message_contain.text
|
140 |
value = value[8:len(value)]
|
141 |
-
|
142 |
if value=="Please, wait...":
|
143 |
continue
|
144 |
|
145 |
if prev!="":
|
146 |
if value==prev:
|
147 |
data={"id":id,"result":value,"status":"Generated"}
|
|
|
148 |
driver.delete_all_cookies()
|
|
|
149 |
requests.post(host, data=data)
|
150 |
break
|
151 |
prev= value
|
@@ -155,18 +159,94 @@ def do_ML(id:str,text:str,host:str, trycount:int):
|
|
155 |
print(x.text)
|
156 |
|
157 |
except:
|
|
|
158 |
driver.delete_all_cookies()
|
159 |
if trycount>3:
|
160 |
data=data={"id":id,"result":"","status":"Error"}
|
161 |
x= requests.post(host,data= data)
|
162 |
return
|
|
|
163 |
do_ML(id,text,host,trycount+1)
|
164 |
|
165 |
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
|
172 |
|
|
|
39 |
# driver.get("https://yuntian-deng-chatgpt.hf.space/")
|
40 |
|
41 |
|
|
|
|
|
|
|
|
|
42 |
|
|
|
43 |
|
44 |
# driver.get("https://yuntian-deng-chatgpt.hf.space/")
|
45 |
|
|
|
108 |
|
109 |
|
110 |
|
111 |
+
|
112 |
+
|
113 |
def do_ML(id:str,text:str,host:str, trycount:int):
|
114 |
+
options = ChromeOptions()
|
115 |
+
options.add_argument('--no-sandbox')
|
116 |
+
options.add_argument('-headless')
|
117 |
+
service = Service()
|
118 |
+
driver = webdriver.Chrome(options= options,service=service)
|
119 |
try:
|
120 |
starttime=time.time()
|
121 |
driver.get("https://talkai.info/chat/")
|
122 |
+
time.sleep(2+trycount)
|
123 |
textarea = driver.find_element(By.CSS_SELECTOR, "textarea")
|
124 |
textarea.send_keys(text)
|
125 |
+
time.sleep(0.1)
|
126 |
button = driver.find_element(By.CLASS_NAME, "sectionChatFormButton")
|
127 |
button.click()
|
128 |
prev =""
|
129 |
|
130 |
+
# time.sleep(2)
|
131 |
while True:
|
132 |
time.sleep(2+trycount)
|
133 |
currtime= time.time()
|
|
|
140 |
last_message_contain = messages[len(messages)-2]
|
141 |
value = last_message_contain.text
|
142 |
value = value[8:len(value)]
|
143 |
+
print(value)
|
144 |
if value=="Please, wait...":
|
145 |
continue
|
146 |
|
147 |
if prev!="":
|
148 |
if value==prev:
|
149 |
data={"id":id,"result":value,"status":"Generated"}
|
150 |
+
|
151 |
driver.delete_all_cookies()
|
152 |
+
driver.quit()
|
153 |
requests.post(host, data=data)
|
154 |
break
|
155 |
prev= value
|
|
|
159 |
print(x.text)
|
160 |
|
161 |
except:
|
162 |
+
print("Error")
|
163 |
driver.delete_all_cookies()
|
164 |
if trycount>3:
|
165 |
data=data={"id":id,"result":"","status":"Error"}
|
166 |
x= requests.post(host,data= data)
|
167 |
return
|
168 |
+
driver.quit()
|
169 |
do_ML(id,text,host,trycount+1)
|
170 |
|
171 |
|
172 |
+
|
173 |
+
|
174 |
+
|
175 |
+
|
176 |
+
@app.post("/image")
|
177 |
+
async def get_answer(q: Query ):
|
178 |
+
|
179 |
+
text = q.text
|
180 |
+
host= q.host
|
181 |
+
|
182 |
+
N = 20
|
183 |
+
res = ''.join(random.choices(string.ascii_uppercase +
|
184 |
+
string.digits, k=N))
|
185 |
+
res= res+ str(time.time())
|
186 |
+
|
187 |
+
id= res
|
188 |
+
|
189 |
+
t = threading.Thread(target=do_ML2, args=(id,text,host,0))
|
190 |
+
t.start()
|
191 |
+
|
192 |
+
|
193 |
+
dict= {"id":id}
|
194 |
+
|
195 |
+
|
196 |
+
return JSONResponse(dict)
|
197 |
+
|
198 |
|
199 |
|
200 |
+
|
201 |
+
|
202 |
+
def do_ML2(id:str,text:str,host:str, trycount:int):
|
203 |
+
options = ChromeOptions()
|
204 |
+
options.add_argument('--no-sandbox')
|
205 |
+
options.add_argument('-headless')
|
206 |
+
service = Service()
|
207 |
+
driver = webdriver.Chrome(options= options,service=service)
|
208 |
+
try:
|
209 |
+
starttime=time.time()
|
210 |
+
driver.get("https://talkai.info/image/")
|
211 |
+
time.sleep(2+trycount)
|
212 |
+
textarea = driver.find_element(By.CSS_SELECTOR, "textarea")
|
213 |
+
textarea.send_keys(text)
|
214 |
+
time.sleep(0.1)
|
215 |
+
button = driver.find_element(By.CLASS_NAME, "sectionChatFormButton")
|
216 |
+
button.click()
|
217 |
+
# time.sleep(2)
|
218 |
+
while True:
|
219 |
+
time.sleep(1+trycount)
|
220 |
+
currtime= time.time()
|
221 |
+
if(currtime>starttime+600):
|
222 |
+
data=data={"id":id,"result":"","status":"Error"}
|
223 |
+
x= requests.post(host,data= data)
|
224 |
+
return
|
225 |
+
try:
|
226 |
+
messages = driver.find_elements(By.XPATH, "//div[@class='messageContain']/p/img")
|
227 |
+
last_message_contain = messages[len(messages)-2]
|
228 |
+
src = last_message_contain.get_attribute("src")
|
229 |
+
print(src)
|
230 |
+
data={"id":id,"result":src,"status":"Generated"}
|
231 |
+
requests.post(host, data=data)
|
232 |
+
driver.delete_all_cookies()
|
233 |
+
driver.quit()
|
234 |
+
break
|
235 |
+
except:
|
236 |
+
continue
|
237 |
+
|
238 |
+
except:
|
239 |
+
print("Error")
|
240 |
+
driver.delete_all_cookies()
|
241 |
+
if trycount>3:
|
242 |
+
data=data={"id":id,"result":"","status":"Error"}
|
243 |
+
x= requests.post(host,data= data)
|
244 |
+
return
|
245 |
+
driver.quit()
|
246 |
+
do_ML2(id,text,host,trycount+1)
|
247 |
+
|
248 |
+
|
249 |
+
|
250 |
|
251 |
|
252 |
|