rishi1985 commited on
Commit
73c385a
1 Parent(s): 14f318a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +151 -49
app.py CHANGED
@@ -73,15 +73,52 @@ app.add_middleware(
73
  # db = firestore.client()
74
  # data_frame = pd.read_csv('data.csv')
75
 
76
-
77
 
78
  @app.on_event("startup")
79
  async def startup_event():
80
- print("on startup")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
 
83
-
84
- from selenium.webdriver.common.by import By
85
 
86
  @app.post("/")
87
  async def get_answer(q: Query ):
@@ -89,18 +126,22 @@ async def get_answer(q: Query ):
89
  text = q.text
90
  host= q.host
91
 
92
- N = 20
93
- res = ''.join(random.choices(string.ascii_uppercase +
94
- string.digits, k=N))
95
- res= res+ str(time.time())
96
 
97
- id= res
98
 
99
- t = threading.Thread(target=do_ML, args=(id,text,host,0))
100
- t.start()
 
101
 
 
102
 
103
- dict= {"id":id}
 
 
104
 
105
 
106
  return JSONResponse(dict)
@@ -111,15 +152,34 @@ async def get_answer(q: Query ):
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)
@@ -129,9 +189,9 @@ def do_ML(id:str,text:str,host:str, trycount:int):
129
 
130
  # time.sleep(2)
131
  while True:
132
- time.sleep(2+trycount)
133
  currtime= time.time()
134
- if(currtime>starttime+600):
135
  data=data={"id":id,"result":"","status":"Error"}
136
  x= requests.post(host,data= data)
137
  return
@@ -144,16 +204,20 @@ def do_ML(id:str,text:str,host:str, trycount:int):
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
156
-
 
 
 
 
157
  data=data={"id":id,"result":value,"status":"Generating --keep refreshing"}
158
  x= requests.post(host,data= data)
159
  print(x.text)
@@ -166,7 +230,7 @@ def do_ML(id:str,text:str,host:str, trycount:int):
166
  x= requests.post(host,data= data)
167
  return
168
  driver.quit()
169
- do_ML(id,text,host,trycount+1)
170
 
171
 
172
 
@@ -179,18 +243,22 @@ async def get_answer(q: Query ):
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)
@@ -200,15 +268,43 @@ async def get_answer(q: Query ):
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)
@@ -216,9 +312,9 @@ def do_ML2(id:str,text:str,host:str, trycount:int):
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
@@ -227,6 +323,12 @@ def do_ML2(id:str,text:str,host:str, trycount:int):
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()
@@ -243,7 +345,7 @@ def do_ML2(id:str,text:str,host:str, trycount:int):
243
  x= requests.post(host,data= data)
244
  return
245
  driver.quit()
246
- do_ML2(id,text,host,trycount+1)
247
 
248
 
249
 
 
73
  # db = firestore.client()
74
  # data_frame = pd.read_csv('data.csv')
75
 
76
+ from selenium.webdriver.common.by import By
77
 
78
  @app.on_event("startup")
79
  async def startup_event():
80
+ print("on startup")
81
+ t = threading.Thread(target=makeqchat)
82
+ t.start()
83
+ t = threading.Thread(target=makeqimg)
84
+ t.start()
85
+
86
+ from queue import Queue
87
+ chatq = Queue()
88
+ imgq= Queue()
89
+
90
+
91
+ def makeqchat():
92
+
93
+ while chatq.qsize()<10:
94
+ print("appending in chat queue")
95
+ options = ChromeOptions()
96
+ options.add_argument('--no-sandbox')
97
+ options.add_argument('-headless')
98
+ service = Service()
99
+ driver = webdriver.Chrome(options= options,service=service)
100
+ driver.get("https://talkai.info/chat/")
101
+ chatq.put(driver)
102
+
103
+
104
+
105
+ def makeqimg():
106
+
107
+ while imgq.qsize()<10:
108
+ print("appending in img queue")
109
+ options = ChromeOptions()
110
+ options.add_argument('--no-sandbox')
111
+ options.add_argument('-headless')
112
+ service = Service()
113
+ driver = webdriver.Chrome(options= options,service=service)
114
+ driver.get("https://talkai.info/image/")
115
+ imgq.put(driver)
116
+
117
+
118
+
119
+
120
 
121
 
 
 
122
 
123
  @app.post("/")
124
  async def get_answer(q: Query ):
 
126
  text = q.text
127
  host= q.host
128
 
129
+ # N = 20
130
+ # res = ''.join(random.choices(string.ascii_uppercase +
131
+ # string.digits, k=N))
132
+ # res= res+ str(time.time())
133
 
134
+ id= ''
135
 
136
+
137
+ # t = threading.Thread(target=do_ML, args=(id,text,host,0))
138
+ # t.start()
139
 
140
+ res= do_ML(id,text,host,0)
141
 
142
+
143
+ dict={"ChatGPT":res}
144
+ # dict= {"id":id}
145
 
146
 
147
  return JSONResponse(dict)
 
152
 
153
 
154
  def do_ML(id:str,text:str,host:str, trycount:int):
155
+ # options = ChromeOptions()
156
+ # options.add_argument('--no-sandbox')
157
+ # options.add_argument('-headless')
158
+ # service = Service()
159
+ # driver = webdriver.Chrome(options= options,service=service)
160
  try:
161
  starttime=time.time()
162
+ # driver.get("https://talkai.info/chat/")
163
+ # time.sleep(2+trycount)
164
+ driver= None
165
+ while True:
166
+ try:
167
+ currtime= time.time()
168
+ if(currtime>starttime+10):
169
+ return "Error"
170
+
171
+ driver= chatq.get()
172
+ # chatq.pop()[0]
173
+ t = threading.Thread(target=makeqchat)
174
+ t.start()
175
+ break;
176
+ except Exception as error:
177
+ print("Error in popping ", error )
178
+ t = threading.Thread(target=makeqchat)
179
+ t.start()
180
+ time.sleep(0.5)
181
+
182
+
183
  textarea = driver.find_element(By.CSS_SELECTOR, "textarea")
184
  textarea.send_keys(text)
185
  time.sleep(0.1)
 
189
 
190
  # time.sleep(2)
191
  while True:
192
+ time.sleep(0.2)
193
  currtime= time.time()
194
+ if(currtime>starttime+60):
195
  data=data={"id":id,"result":"","status":"Error"}
196
  x= requests.post(host,data= data)
197
  return
 
204
  if value=="Please, wait...":
205
  continue
206
 
207
+ # if prev!="":
208
+ # if value==prev:
209
+ # data={"id":id,"result":value,"status":"Generated"}
210
 
211
+ # driver.delete_all_cookies()
212
+ # driver.quit()
213
+ # requests.post(host, data=data)
214
+ # break
215
+ # prev= value
216
+
217
+ driver.delete_all_cookies()
218
+ driver.quit()
219
+ return value
220
+
221
  data=data={"id":id,"result":value,"status":"Generating --keep refreshing"}
222
  x= requests.post(host,data= data)
223
  print(x.text)
 
230
  x= requests.post(host,data= data)
231
  return
232
  driver.quit()
233
+ return do_ML(id,text,host,trycount+1)
234
 
235
 
236
 
 
243
  text = q.text
244
  host= q.host
245
 
246
+ # N = 20
247
+ # res = ''.join(random.choices(string.ascii_uppercase +
248
+ # string.digits, k=N))
249
+ # res= res+ str(time.time())
250
 
251
+ id= ''
252
 
253
+ # t = threading.Thread(target=do_ML2, args=(id,text,host,0))
254
+ # t.start()
255
 
256
+ url = do_ML2(id,text,host,0)
257
 
258
+ dict= {"url":url}
259
+
260
+
261
+ # dict= {"id":id}
262
 
263
 
264
  return JSONResponse(dict)
 
268
 
269
 
270
  def do_ML2(id:str,text:str,host:str, trycount:int):
271
+ # options = ChromeOptions()
272
+ # options.add_argument('--no-sandbox')
273
+ # options.add_argument('-headless')
274
+ # service = Service()
275
+ # driver = webdriver.Chrome(options= options,service=service)
276
  try:
277
  starttime=time.time()
278
+ # driver.get("https://talkai.info/image/")
279
+
280
+
281
+ driver= None
282
+ while True:
283
+ try:
284
+ currtime= time.time()
285
+ if(currtime>starttime+10):
286
+ return "Error"
287
+
288
+ driver= imgq.get()
289
+ # chatq.pop()[0]
290
+ t = threading.Thread(target=makeqimg)
291
+ t.start()
292
+ break;
293
+ except Exception as error:
294
+ print("Error in popping ", error )
295
+ t = threading.Thread(target=makeqimg)
296
+ t.start()
297
+ time.sleep(0.5)
298
+
299
+
300
+
301
+
302
+
303
+
304
+
305
+
306
+
307
+ # time.sleep(2+trycount)
308
  textarea = driver.find_element(By.CSS_SELECTOR, "textarea")
309
  textarea.send_keys(text)
310
  time.sleep(0.1)
 
312
  button.click()
313
  # time.sleep(2)
314
  while True:
315
+ time.sleep(0.2+trycount)
316
  currtime= time.time()
317
+ if(currtime>starttime+60):
318
  data=data={"id":id,"result":"","status":"Error"}
319
  x= requests.post(host,data= data)
320
  return
 
323
  last_message_contain = messages[len(messages)-2]
324
  src = last_message_contain.get_attribute("src")
325
  print(src)
326
+
327
+ driver.delete_all_cookies()
328
+ driver.quit()
329
+ return src
330
+
331
+
332
  data={"id":id,"result":src,"status":"Generated"}
333
  requests.post(host, data=data)
334
  driver.delete_all_cookies()
 
345
  x= requests.post(host,data= data)
346
  return
347
  driver.quit()
348
+ return do_ML2(id,text,host,trycount+1)
349
 
350
 
351