rishi1985 commited on
Commit
6320774
·
1 Parent(s): c177869

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -267
app.py CHANGED
@@ -6,6 +6,7 @@ import os
6
  import requests
7
  import random
8
 
 
9
  import shutil
10
  import json
11
  # from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
@@ -86,267 +87,6 @@ async def startup_event():
86
 
87
 
88
 
89
- @app.post("/")
90
- async def get_answer(request: Request ):
91
- data = await request.json()
92
-
93
- text = data['text']
94
- host= ''
95
-
96
- temperature=-1
97
-
98
- try:
99
- temperature= data['temperature']
100
- temperature= float(temperature)
101
- temperature= round(temperature,1)
102
-
103
- except:
104
- print("No temperature")
105
-
106
-
107
- # N = 20
108
- # res = ''.join(random.choices(string.ascii_uppercase +
109
- # string.digits, k=N))
110
- # res= res+ str(time.time())
111
-
112
- id= ''
113
-
114
-
115
- # t = threading.Thread(target=do_ML, args=(id,text,host,0))
116
- # t.start()
117
- temperature= temperature*2
118
-
119
- res= do_ML(id,text,host,0,temperature)
120
-
121
-
122
- dict={"ChatGPT":res}
123
- # dict= {"id":id}
124
-
125
-
126
- return JSONResponse(dict)
127
-
128
-
129
-
130
-
131
-
132
-
133
- def do_ML(id:str,text:str,host:str, trycount:int,temperature:float):
134
- starttime=time.time()
135
- options = ChromeOptions()
136
- options.add_argument('--no-sandbox')
137
- options.add_argument('-headless')
138
- service = Service()
139
- driver = webdriver.Chrome(options= options,service=service)
140
- driver.get("https://talkai.info/chat/")
141
- try:
142
-
143
- if temperature>=0 and temperature<=2:
144
- try:
145
- print("setting temperature ",temperature)
146
- while True:
147
- currtime= time.time()
148
- if(currtime>starttime+10):
149
- return "Requested Could not be proceed"
150
-
151
- try:
152
- setting_button = driver.find_element(By.ID, "openSettings")
153
- setting_button.click()
154
- break
155
- except:
156
- time.sleep(0.2)
157
-
158
- while True:
159
- currtime= time.time()
160
- if(currtime>starttime+10):
161
- return "Requested Could not be proceed"
162
- try:
163
- input_element = driver.find_element(By.CLASS_NAME,"styled-slider")
164
- new_value = temperature
165
- driver.execute_script("arguments[0].value = arguments[1]", input_element, new_value)
166
- break
167
- except:
168
- time.sleep(0.2)
169
- while True:
170
- currtime= time.time()
171
- if(currtime>starttime+10):
172
- return "Requested Could not be proceed"
173
- try:
174
- confirm_button = driver.find_element(By.CLASS_NAME, "settingsButtonConfirm")
175
- confirm_button.click()
176
- break
177
- except:
178
- time.sleep(0.2)
179
- except:
180
- print("could not set temperature")
181
-
182
-
183
- while True:
184
- currtime= time.time()
185
- if(currtime>starttime+10):
186
- return "Requested Could not be proceed"
187
- try:
188
- textarea = driver.find_element(By.CSS_SELECTOR, "textarea")
189
- textarea.send_keys(text)
190
-
191
- button = driver.find_element(By.CLASS_NAME, "sectionChatFormButton")
192
- button.click()
193
- break
194
- except:
195
- time.sleep(0.2)
196
-
197
-
198
- prev =""
199
-
200
- # time.sleep(2)
201
- while True:
202
- time.sleep(0.5)
203
- # currtime= time.time()
204
-
205
- if(currtime>starttime+120):
206
- driver.delete_all_cookies()
207
- driver.quit()
208
-
209
- return "Requested Could not be proceed"
210
-
211
- value=""
212
- try:
213
- messages = driver.find_elements(By.CLASS_NAME, 'messageContain')
214
- last_message_contain = messages[len(messages)-2]
215
- value = last_message_contain.text
216
- value = value[8:len(value)]
217
- print(value)
218
- if value=="Please, wait...":
219
- continue
220
- except:
221
- continue
222
-
223
-
224
-
225
- driver.delete_all_cookies()
226
- driver.quit()
227
- return value
228
-
229
-
230
-
231
- except:
232
- print("Error")
233
- driver.delete_all_cookies()
234
- if trycount>1:
235
-
236
- return
237
- driver.quit()
238
- return do_ML(id,text,host,trycount+1)
239
-
240
-
241
-
242
-
243
-
244
-
245
- @app.post("/image")
246
- async def get_answer(q: Query ):
247
-
248
- text = q.text
249
- host= q.host
250
-
251
- # N = 20
252
- # res = ''.join(random.choices(string.ascii_uppercase +
253
- # string.digits, k=N))
254
- # res= res+ str(time.time())
255
-
256
- id= ''
257
-
258
- # t = threading.Thread(target=do_ML2, args=(id,text,host,0))
259
- # t.start()
260
-
261
- url = do_ML2(id,text,host,0)
262
-
263
- dict= {"url":url}
264
-
265
-
266
- # dict= {"id":id}
267
-
268
-
269
- return JSONResponse(dict)
270
-
271
-
272
-
273
-
274
-
275
- def do_ML2(id:str,text:str,host:str, trycount:int):
276
-
277
- starttime=time.time()
278
-
279
- options = ChromeOptions()
280
- options.add_argument('--no-sandbox')
281
- options.add_argument('-headless')
282
- service = Service()
283
- driver = webdriver.Chrome(options= options,service=service)
284
- driver.get("https://talkai.info/image/")
285
- try:
286
-
287
- while True:
288
- currtime= time.time()
289
- if(currtime>starttime+5):
290
- raise ValueError("h")
291
- # return "Requested Could not be proceed"
292
- try:
293
- textarea = driver.find_element(By.CSS_SELECTOR, "textarea")
294
- textarea.send_keys(text)
295
- time.sleep(0.1)
296
- button = driver.find_element(By.CLASS_NAME, "sectionChatFormButton")
297
- button.click()
298
- break
299
- except:
300
- time.sleep(0.2)
301
-
302
- # time.sleep(2)
303
- while True:
304
- currtime= time.time()
305
- if(currtime>starttime+40):
306
- raise ValueError("j")
307
- return "Requested Could not be proceed"
308
-
309
- time.sleep(0.2)
310
- # currtime= time.time()
311
- # if(currtime>starttime+18.5):
312
- try:
313
- messages = driver.find_elements(By.CLASS_NAME,"messageContain")
314
- x= messages[len(messages)-2]
315
- # Extract the text from the 'p' element within the 'div' with class 'messageContain'
316
- message_text = x.find_element(By.TAG_NAME,"p").text
317
- if message_text=='The request could not be processed':
318
- return "The request could not be processed"
319
- except Exception as e:
320
- print(e)
321
- pass
322
-
323
-
324
- # return "Request Could not be proceed"
325
- try:
326
- messages = driver.find_elements(By.XPATH, "//div[@class='messageContain']/p/img")
327
- last_message_contain = messages[len(messages)-2]
328
- src = last_message_contain.get_attribute("src")
329
- print(src)
330
-
331
- driver.delete_all_cookies()
332
- driver.quit()
333
-
334
- return src
335
- break
336
- except:
337
- continue
338
-
339
- except:
340
- print("Error")
341
- driver.delete_all_cookies()
342
- if trycount>2:
343
-
344
- return "Request Could not be proceed"
345
- driver.quit()
346
- return do_ML2(id,text,host,trycount+1)
347
-
348
-
349
-
350
  # LLAMA
351
 
352
 
@@ -371,6 +111,7 @@ def do_ML_LLAMA(text:str, trycount:int):
371
  options = ChromeOptions()
372
  options.add_argument('--no-sandbox')
373
  options.add_argument('-headless')
 
374
  service = Service()
375
  driver = webdriver.Chrome(options= options,service=service)
376
  driver.get("https://ysharma-explore-llamav2-with-tgi.hf.space/")
@@ -386,10 +127,11 @@ def do_ML_LLAMA(text:str, trycount:int):
386
 
387
  textarea_element.send_keys(text)
388
 
389
- button_id = 'component-7'
390
  button_element = driver.find_element(By.ID,button_id)
391
 
392
  button_element.click()
 
393
  break
394
  except Exception as e:
395
  print(e)
@@ -411,11 +153,10 @@ def do_ML_LLAMA(text:str, trycount:int):
411
 
412
  # value=""
413
  try:
414
- div_id = 'message' # Replace 'your_div_id' with the actual ID of the div element
415
- div_element = driver.find_elements(By.CLASS_NAME,div_id)
416
- div_element= div_element[-1]
417
  # Extract the text from the div element
418
- text = div_element.text
419
  print("text from llama",text)
420
  if prev!="":
421
 
@@ -425,7 +166,7 @@ def do_ML_LLAMA(text:str, trycount:int):
425
  except:
426
  pass
427
 
428
-
429
  driver.quit()
430
  return text
431
  prev= text
 
6
  import requests
7
  import random
8
 
9
+
10
  import shutil
11
  import json
12
  # from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
 
87
 
88
 
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  # LLAMA
91
 
92
 
 
111
  options = ChromeOptions()
112
  options.add_argument('--no-sandbox')
113
  options.add_argument('-headless')
114
+ options.add_argument("start-maximized")
115
  service = Service()
116
  driver = webdriver.Chrome(options= options,service=service)
117
  driver.get("https://ysharma-explore-llamav2-with-tgi.hf.space/")
 
127
 
128
  textarea_element.send_keys(text)
129
 
130
+ button_id = 'component-8'
131
  button_element = driver.find_element(By.ID,button_id)
132
 
133
  button_element.click()
134
+ # print("sent hii")
135
  break
136
  except Exception as e:
137
  print(e)
 
153
 
154
  # value=""
155
  try:
156
+ element = driver.find_element(By.XPATH,'//div[@data-testid="bot" and contains(@class, "message bot")]')
157
+ print(element.text)
 
158
  # Extract the text from the div element
159
+ text = element.text
160
  print("text from llama",text)
161
  if prev!="":
162
 
 
166
  except:
167
  pass
168
 
169
+ driver.delete_all_cookies()
170
  driver.quit()
171
  return text
172
  prev= text