rishi1985 commited on
Commit
62abd5e
1 Parent(s): cf9ef5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +479 -254
app.py CHANGED
@@ -1,10 +1,12 @@
1
  import io
2
  from fastapi import FastAPI, File, UploadFile
 
3
 
4
  import subprocess
5
  import os
6
  import requests
7
  import random
 
8
 
9
  import shutil
10
  import json
@@ -23,6 +25,7 @@ from selenium.webdriver.chrome.service import Service
23
  import threading
24
  import random
25
  import string
 
26
  import time
27
 
28
 
@@ -77,150 +80,132 @@ from pymongo.mongo_client import MongoClient
77
 
78
  @app.on_event("startup")
79
  async def startup_event():
 
 
80
  print("on startup")
81
 
82
 
83
  # t = threading.Thread(target=makeqimg)
84
  # t.start()
 
85
 
 
86
 
 
87
 
 
88
 
89
-
90
- mycol = None
91
-
92
-
93
- @app.post("/url")
94
- async def get_url(request: Request ):
95
- return "k"
96
-
97
- # data = await request.json()
98
- # text = data['url']
99
- # mongo_url=text
100
- # print("mongo url ",text)
101
- # global mycol
102
- # if mycol==None:
103
- # myclient = MongoClient(mongo_url)
104
-
105
- # try:
106
- # myclient.admin.command('ping')
107
- # print("Pinged your deployment. You successfully connected to MongoDB!")
108
- # except Exception as e:
109
- # print(e)
110
-
111
- # mydb = myclient['open-ai-api-keys']
112
-
113
- # mycol= mydb['key']
114
- # extract()
115
-
116
-
117
 
118
 
119
- def extract():
 
120
  options = ChromeOptions()
 
121
  options.add_argument('--no-sandbox')
122
  options.add_argument('-headless')
123
- service = Service()
 
124
  driver = webdriver.Chrome(options= options,service=service)
125
-
126
-
127
- global mycol
128
- # global driver
129
-
130
- if True:
131
- # time.sleep(60)
132
- try:
133
- driver.get("https://talkai.info/chat/")
134
- element = driver.find_element(By.CSS_SELECTOR,".chat")
135
- api_key = element.get_attribute("data-api-key")
136
- dict={"key":"open-ai"}
137
- mycol.delete_one(dict)
138
- dict={"key":"open-ai","value":api_key}
139
- mycol.insert_one(dict)
140
- print(api_key)
141
- driver.delete_all_cookies()
142
- driver.quit()
143
-
144
- except Exception as e:
145
- print('error in extract ',e)
146
- pass
147
- # time.sleep(60)
148
-
149
-
150
-
151
- from queue import Queue
152
- chatq = Queue()
153
- imgq= Queue()
154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
- def makeqchat():
 
 
 
 
 
157
 
158
- while chatq.qsize()<2:
159
- print("appending in chat queue")
160
- options = ChromeOptions()
161
- options.add_argument('--no-sandbox')
162
- options.add_argument('-headless')
163
- service = Service()
164
- driver = webdriver.Chrome(options= options,service=service)
165
- driver.get("https://talkai.info/chat/")
166
- chatq.put(driver)
167
-
 
 
 
 
 
168
 
169
 
170
- def makeqimg():
171
-
172
- while imgq.qsize()<2:
173
- print("appending in img queue")
174
- options = ChromeOptions()
175
- options.add_argument('--no-sandbox')
176
- options.add_argument('-headless')
177
- service = Service()
178
- driver = webdriver.Chrome(options= options,service=service)
179
- driver.get("https://talkai.info/image/")
180
- imgq.put(driver)
181
-
182
-
183
 
184
 
185
 
186
 
 
187
 
188
- @app.post("/")
189
- async def get_answer(request: Request ):
 
190
  data = await request.json()
191
-
192
  text = data['text']
193
- host= ''
194
-
195
- temperature=-1
196
-
197
- try:
198
- temperature= data['temperature']
199
- temperature= float(temperature)
200
- temperature= round(temperature,1)
201
-
202
- except:
203
- print("No temperature")
204
-
205
-
206
- # N = 20
207
- # res = ''.join(random.choices(string.ascii_uppercase +
208
- # string.digits, k=N))
209
- # res= res+ str(time.time())
210
-
211
- id= ''
212
-
213
-
214
- # t = threading.Thread(target=do_ML, args=(id,text,host,0))
215
- # t.start()
216
-
217
- res= do_ML(id,text,host,0,temperature)
218
 
 
219
 
220
- dict={"ChatGPT":res}
221
- # dict= {"id":id}
222
-
223
-
224
  return JSONResponse(dict)
225
 
226
 
@@ -228,68 +213,52 @@ async def get_answer(request: Request ):
228
 
229
 
230
 
231
- def do_ML(id:str,text:str,host:str, trycount:int,temperature:float):
232
-
 
 
 
 
 
 
 
233
  try:
234
- starttime=time.time()
235
- options = ChromeOptions()
236
- options.add_argument('--no-sandbox')
237
- options.add_argument('-headless')
238
- service = Service()
239
- driver = webdriver.Chrome(options= options,service=service)
240
- driver.get("https://talkai.info/chat/")
241
- if temperature>=0 and temperature<=2:
242
  try:
243
- print("setting temperature ",temperature)
244
- while True:
245
- currtime= time.time()
246
- if(currtime>starttime+10):
247
- return "Requested Could not be proceed"
248
-
249
- try:
250
- setting_button = driver.find_element(By.ID, "openSettings")
251
- setting_button.click()
252
- break
253
- except:
254
- time.sleep(0.2)
255
 
256
- while True:
257
- currtime= time.time()
258
- if(currtime>starttime+10):
259
- return "Requested Could not be proceed"
260
- try:
261
- input_element = driver.find_element(By.CLASS_NAME,"styled-slider")
262
- new_value = temperature
263
- driver.execute_script("arguments[0].value = arguments[1]", input_element, new_value)
264
- break
265
- except:
266
- time.sleep(0.2)
267
- while True:
268
- currtime= time.time()
269
- if(currtime>starttime+10):
270
- return "Requested Could not be proceed"
271
- try:
272
- confirm_button = driver.find_element(By.CLASS_NAME, "settingsButtonConfirm")
273
- confirm_button.click()
274
- break
275
- except:
276
- time.sleep(0.2)
277
  except:
278
- print("could not set temperature")
279
-
280
 
281
  while True:
282
  currtime= time.time()
283
- if(currtime>starttime+10):
 
 
284
  return "Requested Could not be proceed"
285
  try:
286
- textarea = driver.find_element(By.CSS_SELECTOR, "textarea")
287
- textarea.send_keys(text)
288
-
289
- button = driver.find_element(By.CLASS_NAME, "sectionChatFormButton")
290
- button.click()
 
 
291
  break
292
- except:
 
293
  time.sleep(0.2)
294
 
295
 
@@ -297,152 +266,408 @@ def do_ML(id:str,text:str,host:str, trycount:int,temperature:float):
297
 
298
  # time.sleep(2)
299
  while True:
300
- time.sleep(0.2)
301
- # currtime= time.time()
302
- # if(currtime>starttime+18.5):
 
 
 
303
 
304
- # return "Requested Could not be proceed"
305
 
306
- value=""
307
  try:
308
- messages = driver.find_elements(By.CLASS_NAME, 'messageContain')
309
- last_message_contain = messages[len(messages)-2]
310
- value = last_message_contain.text
311
- value = value[8:len(value)]
312
- print(value)
313
- if value=="Please, wait...":
314
- continue
315
- except:
 
 
 
 
 
 
 
316
  continue
317
 
318
 
319
 
320
- driver.delete_all_cookies()
321
- driver.quit()
322
- return value
323
 
324
 
325
 
326
  except:
327
  print("Error")
328
- driver.delete_all_cookies()
329
- if trycount>3:
330
 
331
- return
 
 
332
  driver.quit()
333
- return do_ML(id,text,host,trycount+1)
334
-
335
 
336
 
 
337
 
338
 
339
 
340
- @app.post("/image")
341
- async def get_answer(q: Query ):
342
-
343
- text = q.text
344
- host= q.host
345
 
346
- # N = 20
347
- # res = ''.join(random.choices(string.ascii_uppercase +
348
- # string.digits, k=N))
349
- # res= res+ str(time.time())
350
 
351
- id= ''
352
-
353
- # t = threading.Thread(target=do_ML2, args=(id,text,host,0))
354
- # t.start()
355
 
356
- url = do_ML2(id,text,host,0)
 
 
 
 
 
357
 
358
- dict= {"url":url}
359
 
360
-
361
- # dict= {"id":id}
362
-
363
-
364
  return JSONResponse(dict)
365
 
366
-
367
-
368
 
369
 
370
- def do_ML2(id:str,text:str,host:str, trycount:int):
371
 
 
 
 
372
  starttime=time.time()
373
-
374
  options = ChromeOptions()
375
  options.add_argument('--no-sandbox')
376
  options.add_argument('-headless')
377
- service = Service()
 
378
  driver = webdriver.Chrome(options= options,service=service)
379
- driver.get("https://talkai.info/image/")
380
  try:
381
-
382
  while True:
383
  currtime= time.time()
384
- if(currtime>starttime+5):
385
- raise ValueError("h")
386
- # return "Requested Could not be proceed"
 
387
  try:
388
- textarea = driver.find_element(By.CSS_SELECTOR, "textarea")
389
- textarea.send_keys(text)
390
- time.sleep(0.1)
391
- button = driver.find_element(By.CLASS_NAME, "sectionChatFormButton")
392
- button.click()
 
 
 
393
  break
394
  except:
395
- time.sleep(0.2)
 
396
 
 
 
397
  # time.sleep(2)
398
  while True:
 
399
  currtime= time.time()
400
- if(currtime>starttime+40):
401
- raise ValueError("j")
402
- return "Requested Could not be proceed"
 
403
 
404
- time.sleep(0.2)
405
- # currtime= time.time()
406
- # if(currtime>starttime+18.5):
407
  try:
408
- messages = driver.find_elements(By.CLASS_NAME,"messageContain")
409
- x= messages[len(messages)-2]
410
- # Extract the text from the 'p' element within the 'div' with class 'messageContain'
411
- message_text = x.find_element(By.TAG_NAME,"p").text
412
- if message_text=='The request could not be processed':
413
- return "The request could not be processed"
 
 
 
 
 
 
 
414
  except Exception as e:
415
  print(e)
416
- pass
417
-
418
 
419
- # return "Request Could not be proceed"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420
  try:
421
- messages = driver.find_elements(By.XPATH, "//div[@class='messageContain']/p/img")
422
- last_message_contain = messages[len(messages)-2]
423
- src = last_message_contain.get_attribute("src")
424
- print(src)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
425
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
426
  driver.delete_all_cookies()
427
  driver.quit()
428
-
429
- return src
 
 
 
 
 
 
 
430
  break
431
  except:
432
  continue
433
-
434
- except:
435
- print("Error")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436
  driver.delete_all_cookies()
437
- if trycount>2:
438
-
439
- return "Request Could not be proceed"
440
  driver.quit()
441
- return do_ML2(id,text,host,trycount+1)
442
-
443
-
444
 
445
 
446
-
 
 
447
 
448
 
 
 
 
 
 
1
  import io
2
  from fastapi import FastAPI, File, UploadFile
3
+ from fastapi import FastAPI, Request, Response, BackgroundTasks
4
 
5
  import subprocess
6
  import os
7
  import requests
8
  import random
9
+ # import undetected_chromedriver as uc
10
 
11
  import shutil
12
  import json
 
25
  import threading
26
  import random
27
  import string
28
+ from selenium.webdriver.common.keys import Keys
29
  import time
30
 
31
 
 
80
 
81
  @app.on_event("startup")
82
  async def startup_event():
83
+ x=requests.get('https://open-ai-ping-eight.vercel.app/proxy')
84
+ print("response ", x.text)
85
  print("on startup")
86
 
87
 
88
  # t = threading.Thread(target=makeqimg)
89
  # t.start()
90
+ proxy =None
91
 
92
+ llama_last_error=0
93
 
94
+ # LLAMA
95
 
96
+ chrome_driver_path = '/usr/local/bin/chromedriver-linux64/chromedriver'
97
 
98
+ @app.post("/llama")
99
+ async def get_answer_llama(request: Request ):
100
+ data = await request.json()
101
+ text = data['text']
102
+ # print("recived ",text)
103
+ res= ""
104
+ res= do_ML_LLAMA70b(text,0)
105
+ dict={"LLAMA":res}
106
+ return JSONResponse(dict)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
 
109
+ def do_ML_LLAMA70b(text:str, trycount:int):
110
+ starttime=time.time()
111
  options = ChromeOptions()
112
+ global llama_last_error
113
  options.add_argument('--no-sandbox')
114
  options.add_argument('-headless')
115
+ options.add_argument("start-maximized")
116
+ service = Service(executable_path=chrome_driver_path)
117
  driver = webdriver.Chrome(options= options,service=service)
118
+ driver.get("https://huggingface-projects-llama-2-7b-chat.hf.space")
119
+ try:
120
+
121
+ while True:
122
+ currtime= time.time()
123
+ if(currtime>starttime+30):
124
+ driver.delete_all_cookies()
125
+ driver.quit()
126
+ return "Requested Could not be proceed"
127
+ try:
128
+ # span_element = driver.find_element(By.CLASS_NAME, 'svelte-s1r2yt')
129
+ # span_element.click()
130
+ # input_element = driver.find_elements(By.CSS_SELECTOR, 'input[data-testid="number-input"]')
131
+ # new_value = "2000"
132
+ # input_element[1].clear()
133
+ # input_element[1].send_keys(new_value)
134
+ xpath_expression = '//textarea[@data-testid="textbox"]'
135
+ textarea_element = driver.find_element(By.XPATH, xpath_expression)
136
+ for line in text.split('\n'):
137
+ textarea_element.send_keys(line)
138
+ textarea_element.send_keys(Keys.SHIFT + Keys.ENTER)
139
+ textarea_element.send_keys('\n')
140
+
141
+
142
+ break
143
+ except Exception as e:
144
+ print(e)
145
+ continue
 
146
 
147
+
148
+ prev =""
149
+
150
+ time.sleep(2)
151
+ while True:
152
+ time.sleep(0.5)
153
+ currtime= time.time()
154
+
155
+ if(currtime>starttime+170):
156
+ driver.delete_all_cookies()
157
+ driver.quit()
158
+
159
+ return prev
160
+
161
+ element_selector = '.wrap.default.minimal.svelte-119qaqt.translucent.generating'
162
 
163
+ try:
164
+ element = driver.find_element(By.CSS_SELECTOR,element_selector)
165
+ print("generating")
166
+ continue
167
+ except:
168
+ print("Element is not present.")
169
 
170
+ parent_element = driver.find_elements(By.CLASS_NAME,'message')
171
+ extracted_text = parent_element[1].text
172
+ element_text= extracted_text
173
+ if element_text=="" or element_text=='Loading content ':
174
+ pritn("empty text exception")
175
+ continue
176
+
177
+ driver.quit()
178
+
179
+ return element_text
180
+
181
+ driver.delete_all_cookies()
182
+ driver.quit()
183
+ llama_last_error= time.time()
184
+ return " --Error Occurred-- "
185
 
186
 
187
+ except Exception as e:
188
+ print(e)
189
+ driver.delete_all_cookies()
190
+ driver.quit()
191
+ llama_last_error= time.time()
192
+ return "Error Occurred "
 
 
 
 
 
 
 
193
 
194
 
195
 
196
 
197
+ ########## MPT
198
 
199
+
200
+ @app.post("/mpt")
201
+ async def get_answer_mpt(request: Request ):
202
  data = await request.json()
 
203
  text = data['text']
204
+ print("recived ",text)
205
+ res= do_ML_MPT(text,0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
 
207
+ dict={"MPT":res}
208
 
 
 
 
 
209
  return JSONResponse(dict)
210
 
211
 
 
213
 
214
 
215
 
216
+ def do_ML_MPT(text:str, trycount:int):
217
+ starttime=time.time()
218
+ options = ChromeOptions()
219
+ options.add_argument('--no-sandbox')
220
+ options.add_argument('-headless')
221
+ options.add_argument("start-maximized")
222
+ service = Service(executable_path=chrome_driver_path)
223
+ driver = webdriver.Chrome(options= options,service=service)
224
+ driver.get("https://mosaicml-mpt-30b-chat.hf.space")
225
  try:
226
+
227
+ while True:
228
+ currtime= time.time()
229
+ if(currtime>starttime+20):
230
+ driver.delete_all_cookies()
231
+ driver.quit()
232
+ return "Requested Could not be proceed"
 
233
  try:
234
+ textarea_xpath = "//textarea[@data-testid='textbox' and @class='scroll-hide svelte-1pie7s6' and @placeholder='Chat Message Box']"
235
+ textarea_element = driver.find_element(By.XPATH,textarea_xpath)
 
 
 
 
 
 
 
 
 
 
236
 
237
+ for line in text.split('\n'):
238
+ textarea_element.send_keys(line)
239
+ # Simulate pressing the 'Shift + Enter' keys to add a newline without triggering submit
240
+ textarea_element.send_keys(Keys.SHIFT + Keys.ENTER)
241
+ break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
  except:
243
+ continue
 
244
 
245
  while True:
246
  currtime= time.time()
247
+ if(currtime>starttime+20):
248
+ driver.delete_all_cookies()
249
+ driver.quit()
250
  return "Requested Could not be proceed"
251
  try:
252
+
253
+
254
+ button_id = 'component-9' # Replace 'comonent-7' with the actual ID
255
+ button_element = driver.find_element(By.ID,button_id)
256
+
257
+ # Perform actions on the element (e.g., clicking the button)
258
+ button_element.click()
259
  break
260
+ except Exception as e:
261
+ print(e)
262
  time.sleep(0.2)
263
 
264
 
 
266
 
267
  # time.sleep(2)
268
  while True:
269
+ time.sleep(0.5)
270
+ currtime= time.time()
271
+
272
+ if(currtime>starttime+120):
273
+ driver.delete_all_cookies()
274
+ driver.quit()
275
 
276
+ return "Requested Could not be proceed"
277
 
278
+ # value=""
279
  try:
280
+ element = driver.find_element(By.XPATH,'//div[@data-testid="bot" and contains(@class, "message bot")]')
281
+
282
+ x=(element.text)
283
+
284
+ print("From text ",x)
285
+ if x=="":
286
+ raise ValueError(" k")
287
+ driver.quit()
288
+ return x
289
+
290
+
291
+
292
+
293
+ except Exception as e:
294
+ print(e)
295
  continue
296
 
297
 
298
 
299
+ driver.quit()
300
+ return " --Error Occurred-- "
 
301
 
302
 
303
 
304
  except:
305
  print("Error")
306
+
307
+ if trycount>1:
308
 
309
+ driver.delete_all_cookies()
310
+ driver.quit()
311
+ return "Error"
312
  driver.quit()
313
+ return do_ML_MPT(text,trycount+1)
 
314
 
315
 
316
+ '''Falcon 40 b intruct'''
317
 
318
 
319
 
 
 
 
 
 
320
 
321
+ '''Star Code'''
 
 
 
322
 
 
 
 
 
323
 
324
+ @app.post("/starcode")
325
+ async def get_answer_falcon(request: Request ):
326
+ data = await request.json()
327
+ text = data['text']
328
+ print("recived ",text)
329
+ res= do_ML_STARCODE(text,0)
330
 
331
+ dict={"RESULT":res}
332
 
 
 
 
 
333
  return JSONResponse(dict)
334
 
 
 
335
 
336
 
 
337
 
338
+
339
+
340
+ def do_ML_STARCODE(text:str, trycount:int):
341
  starttime=time.time()
 
342
  options = ChromeOptions()
343
  options.add_argument('--no-sandbox')
344
  options.add_argument('-headless')
345
+ options.add_argument("start-maximized")
346
+ service = Service(executable_path=chrome_driver_path)
347
  driver = webdriver.Chrome(options= options,service=service)
348
+ driver.get("https://huggingfaceh4-starchat-playground.hf.space/")
349
  try:
350
+
351
  while True:
352
  currtime= time.time()
353
+ if(currtime>starttime+20):
354
+ driver.delete_all_cookies()
355
+ driver.quit()
356
+ return "Requested Could not be proceed"
357
  try:
358
+ textarea_element = driver.find_element(By.CSS_SELECTOR,'textarea[placeholder="Enter your message here"]')
359
+
360
+ for line in text.split('\n'):
361
+ textarea_element.send_keys(line)
362
+ # Simulate pressing the 'Shift + Enter' keys to add a newline without triggering submit
363
+ textarea_element.send_keys(Keys.SHIFT + Keys.ENTER)
364
+ time.sleep(0.5)
365
+ textarea_element.send_keys('\n')
366
  break
367
  except:
368
+ continue
369
+
370
 
371
+ prev =""
372
+
373
  # time.sleep(2)
374
  while True:
375
+ time.sleep(0.5)
376
  currtime= time.time()
377
+
378
+ if(currtime>starttime+170):
379
+ driver.delete_all_cookies()
380
+ driver.quit()
381
 
382
+ return "Requested Could not be proceed"
383
+
 
384
  try:
385
+ div_element = driver.find_element(By.CLASS_NAME,'svelte-j1gjts.generating')
386
+ continue
387
+ except:
388
+ pass
389
+ # value=""
390
+ try:
391
+ div_element = driver.find_element(By.CSS_SELECTOR,'div[data-testid="bot"]')
392
+ text_content = div_element.text
393
+ print(text_content)
394
+ driver.delete_all_cookies()
395
+ driver.quit()
396
+ return text_content
397
+
398
  except Exception as e:
399
  print(e)
400
+ continue
 
401
 
402
+
403
+
404
+ driver.quit()
405
+ return " --Error Occurred-- "
406
+
407
+
408
+
409
+ except:
410
+ print("Error")
411
+
412
+ driver.delete_all_cookies()
413
+ driver.quit()
414
+ return "Error Occureed"
415
+
416
+
417
+
418
+
419
+ @app.post("/falcon180b")
420
+ async def get_answer_falcon180(request: Request, background_tasks: BackgroundTasks ):
421
+ data = await request.json()
422
+ text= data['text']
423
+ print("recievoed , ", text )
424
+ return StreamingResponse(do_falcon180(text), media_type="text/plain")
425
+
426
+
427
+
428
+ import asyncio
429
+ from fastapi.responses import StreamingResponse
430
+
431
+ async def do_falcon180(text):
432
+ starttime=time.time()
433
+ options = ChromeOptions()
434
+ options.add_argument('--no-sandbox')
435
+ options.add_argument('-headless')
436
+ options.add_argument("start-maximized")
437
+ service = Service(executable_path=chrome_driver_path)
438
+ driver = webdriver.Chrome(options= options,service=service)
439
+ # driver.get("https://tiiuae-falcon-180b-demo.hf.space")
440
+ # driver.get("https://ysharma-falcon-180b-demo.hf.space/")
441
+ driver.get("https://lunarflu-falcon-180b-demo-duplicate.hf.space")
442
+ try:
443
+ while True:
444
+ try:
445
+ curr= time.time()
446
+ if curr- starttime>20:
447
+ break
448
+ xpath_expression = "//span[@class='svelte-s1r2yt' and contains(text(), 'Additional Inputs')]"
449
+ element = driver.find_element(By.XPATH, xpath_expression)
450
+ element.click()
451
+ element = driver.find_elements(By.CSS_SELECTOR, 'input[data-testid="number-input"]')
452
+ element[1].clear() # Clear any existing value
453
+ element[1].send_keys('1900')
454
+ textarea_element = driver.find_element(By.XPATH,'//textarea[@placeholder="Type a message..."]')
455
+ for line in text.split('\n'):
456
+ textarea_element.send_keys(line)
457
+ textarea_element.send_keys(Keys.SHIFT + Keys.ENTER)
458
+ textarea_element.send_keys('\n')
459
+ break
460
+ except Exception as e:
461
+ print(e)
462
+ continue
463
+ old_text=""
464
+ while True:
465
+ # await asyncio.sleep(0.1)
466
+ curr= time.time()
467
+ if curr- starttime>180:
468
+ driver.quit()
469
+ break
470
+ done = False
471
  try:
472
+ xpath_expression = "//span[contains(@class, 'chatbot')]"
473
+ element = driver.find_elements(By.XPATH, xpath_expression)
474
+ text = element[1].text
475
+ # print(text)
476
+ while True:
477
+ text = element[1].text
478
+ send_text= text[len(old_text):]
479
+ old_text= text
480
+ yield send_text.encode()
481
+ try:
482
+ element = driver.find_element(By.CLASS_NAME,"generating")
483
+ # print("generating")
484
+ except:
485
+ # print("generated")
486
+ driver.quit()
487
+ done= True
488
+ break
489
+ if done:
490
+ break
491
+ except Exception as e:
492
+ print(e)
493
+ continue
494
+ except:
495
+ driver.quit()
496
 
497
+
498
+
499
+
500
+ @app.post("/wizlm70b")
501
+ async def get_answer_falcon180(request: Request):
502
+ data = await request.json()
503
+ text= data['text']
504
+ print("recievoed , ", text )
505
+ return do_wizard70b(text)
506
+
507
+
508
+ def do_wizard70b(text):
509
+ starttime=time.time()
510
+ options = ChromeOptions()
511
+ options.add_argument('--no-sandbox')
512
+ options.add_argument('-headless')
513
+ options.add_argument("start-maximized")
514
+ service = Service(executable_path=chrome_driver_path)
515
+ driver = webdriver.Chrome(options= options,service=service)
516
+ driver.get("https://chat.lmsys.org/")
517
+ while True:
518
+ curr= time.time()
519
+ if curr- starttime>10:
520
+ driver.delete_all_cookies()
521
+ driver.quit()
522
+ return "Error-- 1"
523
+ try:
524
+ button = driver.find_element(By.XPATH, "//button[contains(@class, 'svelte-kqij2n') and text()='Single Model']")
525
+ button.click()
526
+ break
527
+ except Exception as e:
528
+ print(e)
529
+ time.sleep(0.5)
530
+ # return driver.page_source
531
+
532
+ # while True:
533
+ # curr= time.time()
534
+ # if curr- starttime>50:
535
+ # driver.delete_all_cookies()
536
+ # driver.quit()
537
+ # return "Error-- 2"
538
+ # try:
539
+ # input_element = driver.find_elements(By.CLASS_NAME,'border-none')
540
+ # # Send text to the <input> element
541
+ # input_element[2].clear()
542
+ # input_element[2].send_keys('wizardlm-70b\n')
543
+ # span_element = driver.find_elements(By.XPATH,'//span[text()="Parameters"]')
544
+ # span_element[2].click()
545
+ # input_element = driver.find_elements(By.XPATH ,'//input[@data-testid="number-input"]')
546
+ # input_element[8].clear() # Clear any existing value
547
+ # input_element[8].send_keys('1024')
548
+ # break
549
+ # except Exception as e:
550
+ # print(e)
551
+ # time.sleep(0.5)
552
+ time.sleep(1)
553
+ while True:
554
+ curr= time.time()
555
+ if curr- starttime>60:
556
+ driver.delete_all_cookies()
557
+ driver.quit()
558
+ return "Error-- 3"
559
+ try:
560
+ textarea = driver.find_elements(By.XPATH,'//textarea[@data-testid="textbox"]') # You can also use other locators like by_id, by_name, by_class_name, etc.
561
+ textarea[2].clear()
562
+ for line in text.split('\n'):
563
+ textarea[2].send_keys(line)
564
+ textarea[2].send_keys('\n')
565
+ except Exception as e:
566
+ print(e)
567
+ time.sleep(0.5)
568
+
569
+ time.sleep(2)
570
+ while True:
571
+ curr= time.time()
572
+ if curr- starttime>178:
573
+ driver.delete_all_cookies()
574
+ driver.quit()
575
+ return "Error-- 3 -- timeout"
576
+ try:
577
+ element = driver.find_element(By.CSS_SELECTOR, '.generating')
578
+ print("geberating")
579
+ time.sleep(0.5)
580
+ except:
581
+ print("Element with 'generating' keyword in class is not present.")
582
+ div_element = driver.find_element(By.XPATH,'//div[@data-testid="bot"]')
583
+ extracted_text = div_element.text
584
+ driver.delete_all_cookies()
585
+ driver.quit()
586
+ return extracted_text
587
+
588
+
589
+
590
+
591
+
592
+ #####PALM 2
593
+
594
+ @app.post("/palm2")
595
+ async def get_answer_llama(request: Request ):
596
+ data = await request.json()
597
+ text = data['text']
598
+ # print("recived ",text)
599
+ res= ""
600
+ res= do_ML_PALM(text,0)
601
+ dict={"BOT":res}
602
+ return JSONResponse(dict)
603
+
604
+
605
+ def do_ML_PALM(text:str, trycount:int):
606
+ starttime=time.time()
607
+ options = ChromeOptions()
608
+ options.add_argument('--no-sandbox')
609
+ options.add_argument('-headless')
610
+ options.add_argument("start-maximized")
611
+ service = Service(executable_path=chrome_driver_path)
612
+ driver = webdriver.Chrome(options= options,service=service)
613
+ driver.get('https://chansung-palm-with-gradio-chat.hf.space')
614
+ try:
615
+ while True:
616
+ curr = time.time()
617
+ if curr-starttime>178:
618
  driver.delete_all_cookies()
619
  driver.quit()
620
+ return 'Error -1'
621
+
622
+ try:
623
+ xpath = "//textarea[@data-testid='textbox' and @placeholder='Ask anything']"
624
+ textarea = driver.find_element(By.XPATH,xpath)
625
+ textarea.clear()
626
+ for line in text.split('\n'):
627
+ textarea.send_keys(line)
628
+ textarea.send_keys("\n")
629
  break
630
  except:
631
  continue
632
+ time.sleep(1.5)
633
+ while True:
634
+ if curr-starttime>178:
635
+ driver.delete_all_cookies()
636
+ driver.quit()
637
+ return 'Error -2'
638
+ try:
639
+ div_element = driver.find_element(By.CSS_SELECTOR,'div.wrap.default.full.svelte-zlszon.generating')
640
+ continue
641
+ except:
642
+ print('fail')
643
+ try:
644
+ div_element = driver.find_elements(By.CLASS_NAME,'md.svelte-9tftx4.chatbot')
645
+ div_element= div_element[1]
646
+ # Extract and print the text content of the div element
647
+ div_text = div_element.text
648
+ if div_text=="":
649
+ continue
650
+ driver.delete_all_cookies()
651
+ driver.quit()
652
+ return div_text
653
+ except:
654
+ time.sleep(0.5)
655
+ continue
656
+
657
+
658
+ except Exception as e:
659
+ print(e)
660
  driver.delete_all_cookies()
 
 
 
661
  driver.quit()
662
+ return "Error 3"
 
 
663
 
664
 
665
+
666
+
667
+
668
 
669
 
670
+
671
+
672
+
673
+