rishi1985 commited on
Commit
abbb8d9
·
1 Parent(s): f0a2f7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +768 -234
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,463 @@ 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 +544,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 +597,386 @@ 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
+ text = text.replace('\n', ' : ')
103
+ # print("recived ",text)
104
+ proxy=None
105
 
106
+ try:
107
+ proxy = data['proxy']
108
+ except:
109
+ pass
110
+ print('proxy ip ', proxy )
111
+ currtime= time.time()
112
+ res= ""
113
+ # if currtime-llama_last_error<=3600:
114
+ # res = do_ML_LLAMA70b(text,0,proxy)
115
+ # else:
116
+ res= do_ML_LLAMA70b_chori(text,0, proxy)
117
+ dict={"LLAMA":res}
118
+ return JSONResponse(dict)
119
 
120
 
121
+ @app.post("/llama2")
122
+ async def get_answer_llama2(request: Request ):
123
+ data = await request.json()
124
+ text = data['text']
125
+ text = text.replace('\n', ' : ')
126
+ # print("recived ",text)
127
+ proxy=None
128
+
129
+ try:
130
+ proxy = data['proxy']
131
+ except:
132
+ pass
133
+ print('proxy ip ', proxy )
134
+ currtime= time.time()
135
+ res= ""
136
+
137
+ res = do_ML_LLAMA70b_chori(text,0,proxy)
138
+
139
+
140
+ dict={"LLAMA":res}
141
+
142
+ return JSONResponse(dict)
143
+
144
+ @app.post("/llama3")
145
+ async def get_answer_llama3(request: Request ):
146
+ data = await request.json()
147
+ text = data['text']
148
+ text = text.replace('\n', ' : ')
149
+ # print("recived ",text)
150
+ proxy=None
151
+
152
+ # try:
153
+ # proxy = data['proxy']
154
+ # except:
155
+ # pass
156
+ # print('proxy ip ', proxy )
157
+ # currtime= time.time()
158
+ # res= ""
159
+
160
+ res = do_ML_LLAMA70b(text,0,'')
161
+
162
+
163
+ dict={"LLAMA":res}
164
+
165
+ return JSONResponse(dict)
166
+
167
+
168
+
169
+
170
+ def do_ML_LLAMA70b_chori(text:str, trycount:int, proxy:str):
171
+ starttime=time.time()
172
+ options = ChromeOptions()
173
+ global llama_last_error
174
+ options.add_argument('--no-sandbox')
175
+ options.add_argument('-headless')
176
+ options.add_argument("start-maximized")
177
+ # options.add_argument("--ignore-certificate-errors")
178
+ service = Service(executable_path=chrome_driver_path)
179
+ driver = webdriver.Chrome(options= options,service=service)
180
+ driver.get("https://chansung-llama2-with-gradio-chat.hf.space/")
181
+ try:
182
+
183
+ while True:
184
+ currtime= time.time()
185
+ if(currtime>starttime+20):
186
+ driver.delete_all_cookies()
187
+ llama_last_error= currtime
188
+ driver.quit()
189
+ return "Requested Could not be proceed"
190
+ try:
191
+ control_panel_span = driver.find_element(By.XPATH,'//span[@class="svelte-s1r2yt" and contains(text(), "Control Panel")]')
192
+ control_panel_span.click()
193
+ time.sleep(0.5)
194
+ input_element = driver.find_elements(By.CSS_SELECTOR,'[data-testid="number-input"]')
195
+ new_value = "3500" # Replace this with the value you want to set
196
+ input_element[3].clear() # Clear the current value if needed
197
+ input_element[3].send_keys(new_value)
198
+ xpath = "//textarea[@data-testid='textbox' and @placeholder='Ask anything']"
199
+ textarea = driver.find_element(By.XPATH,xpath)
200
+ textarea.clear()
201
+ textarea.send_keys(text)
202
+ textarea.send_keys("\n")
203
+
204
+ break
205
+ except Exception as e:
206
+ print(e)
207
+ continue
208
+
209
+
210
+ prev =""
211
 
212
+ time.sleep(2)
213
+ while True:
214
+ time.sleep(0.5)
215
+ currtime= time.time()
216
+
217
+ if(currtime>starttime+170):
218
+ driver.delete_all_cookies()
219
+ driver.quit()
220
+ llama_last_error= currtime
221
+ return "Requested Could not be proceed"
222
+
223
+ try:
224
+ div_element = driver.find_element(By.CSS_SELECTOR,'div.wrap.default.full.svelte-zlszon.generating')
225
+ print("generating")
226
+ continue;
227
+ except:
228
+ print('done gen ')
229
+
230
+ while True:
231
+
232
+ try:
233
+ div_element = driver.find_elements(By.CLASS_NAME,'md.svelte-9tftx4.chatbot')
234
+ div_element= div_element[len(div_element)-1]
235
+ # Extract and print the text content of the div element
236
+ div_text = div_element.text
237
+ print(div_text)
238
+ driver.delete_all_cookies()
239
+ driver.quit()
240
+ print("Lllama chori")
241
+ return div_text
242
+ except Exception as e:
243
+ return "Error Occureed"
244
+
245
+
246
+ driver.delete_all_cookies()
247
+ driver.quit()
248
+ return " --Error Occurred-- "
249
+
250
+
251
 
252
+ except Exception as e:
253
+ print(e)
254
+ driver.delete_all_cookies()
255
+ driver.quit()
256
+ currtime= time.time()
257
+ llama_last_error= currtime
258
+ return "Error Occurred "
259
 
260
 
261
 
262
+ def do_ML_LLAMA70b(text:str, trycount:int, proxy:str):
263
+ starttime=time.time()
264
  options = ChromeOptions()
265
+ global llama_last_error
266
  options.add_argument('--no-sandbox')
267
  options.add_argument('-headless')
268
+ options.add_argument("start-maximized")
269
+ options.add_argument("--ignore-certificate-errors")
270
+ # proxy_port = "8080"
271
+ # proxy_ip= proxy
272
+ # print("pxoxy ip ",proxy_ip)
273
+ # proxy_ip = f"{proxy_ip}:{proxy_port}"
274
+ # print("proxy final ", proxy_ip)
275
+ # options.add_argument(f'--proxy-server=http://{proxy_ip}')
276
+ service = Service(executable_path=chrome_driver_path)
277
  driver = webdriver.Chrome(options= options,service=service)
278
+ driver.get("https://huggingface.co/chat/")
279
+ try:
280
+
281
+ while True:
282
+ currtime= time.time()
283
+ if(currtime>starttime+20):
284
+ driver.delete_all_cookies()
285
+ driver.quit()
286
+ llama_last_error= currtime
287
+ return "Requested Could not be proceed"
288
+ try:
289
+ # xpath_expression = '//textarea[@data-testid="tex
290
+ textarea_element = driver.find_element(By.XPATH, "//textarea[@placeholder='Ask anything']")
291
+
292
+
293
+ # for line in text.split('\n'):
294
+ textarea_element.send_keys(text)
295
+ # textarea_element.send_keys(Keys.SHIFT + Keys.ENTER)
296
+
297
+ textarea_element.send_keys('\n')
298
+
299
+ break
300
+ except Exception as e:
301
+ print(e)
302
+ continue
303
+
304
+
305
+ prev =""
306
+
307
+ time.sleep(0.6)
308
+ while True:
309
+ time.sleep(0.5)
310
+ currtime= time.time()
311
+
312
+ if(currtime>starttime+170):
313
+ driver.delete_all_cookies()
314
+ driver.quit()
315
+
316
+ return "Requested Could not be proceed"
317
+
318
+ try:
319
+ class_name = "-ml-1"
320
+ svg_elements = driver.find_element(By.CLASS_NAME, class_name)
321
+ continue
322
+
323
+ except:
324
+ print("not found")
325
+
326
+ element = driver.find_element(By.CLASS_NAME, "prose") # Find the element by its class name
327
+ element_text = element.text # Extract the text from the element
328
  driver.delete_all_cookies()
329
  driver.quit()
330
+ print("llama huugiing chat")
331
+ return element_text
332
+
333
+
334
+ driver.delete_all_cookies()
335
+ driver.quit()
336
+ llama_last_error= time.time()
337
+ return " --Error Occurred-- "
338
+
339
 
 
 
 
 
340
 
341
+ except:
342
+ driver.delete_all_cookies()
343
+ driver.quit()
344
+ llama_last_error= time.time()
345
+ return "Error Occurred "
346
 
 
 
 
347
 
348
 
 
349
 
 
 
 
 
 
 
 
 
 
 
 
350
 
351
+ def do_ML_LLAMA7b(text:str, trycount:int):
352
+ starttime=time.time()
353
+ options = ChromeOptions()
354
+ options.add_argument('--no-sandbox')
355
+ options.add_argument('-headless')
356
+ options.add_argument("start-maximized")
357
+ service = Service(executable_path=chrome_driver_path)
358
+ driver = webdriver.Chrome(options= options,service=service)
359
+ driver.get("https://huggingface-projects-llama-2-7b-chat.hf.space")
360
+ try:
361
+
362
+ while True:
363
+ currtime= time.time()
364
+ if(currtime>starttime+20):
365
+ driver.delete_all_cookies()
366
+ driver.quit()
367
+ return "Requested Could not be proceed"
368
+ try:
369
+ xpath_expression = '//textarea[@data-testid="textbox"]'
370
+ textarea_element = driver.find_element(By.XPATH, xpath_expression)
371
+
372
+ for line in text.split('\n'):
373
+ textarea_element.send_keys(line)
374
+ # Simulate pressing the 'Shift + Enter' keys to add a newline without triggering submit
375
+ textarea_element.send_keys(Keys.SHIFT + Keys.ENTER)
376
+
377
+ span_element = driver.find_element(By.CLASS_NAME, 'svelte-s1r2yt')
378
+
379
+ span_element.click()
380
+ input_element = driver.find_element(By.CSS_SELECTOR, 'input[data-testid="number-input"]')
381
+ new_value = "2048" # Replace this with the value you want to set
382
+ input_element.clear()
383
+ input_element.send_keys(new_value)
384
+
385
+ textarea_element.send_keys('\n')
386
+
387
+ break
388
+ except:
389
+ continue
390
+
391
+
392
+ prev =""
393
 
394
+ time.sleep(0.6)
395
+ while True:
396
+ time.sleep(0.5)
397
+ currtime= time.time()
398
+
399
+ if(currtime>starttime+170):
400
+ driver.delete_all_cookies()
401
+ driver.quit()
402
+
403
+ return "Requested Could not be proceed"
404
+
405
+ # value=""
406
+ try:
407
+ div_element = driver.find_element(By.CLASS_NAME, 'wrap.default.minimal.svelte-zlszon.translucent.hide')
408
+ element = driver.find_element(By.XPATH,'//div[@data-testid="bot" and contains(@class, "message bot")]')
409
 
410
+ x=(element.text)
411
+ driver.delete_all_cookies()
412
+ driver.quit()
413
+ return x
414
+ except:
415
+ continue
416
 
417
+
418
+
419
+ driver.delete_all_cookies()
420
+ driver.quit()
421
+ return " --Error Occurred-- "
422
+
423
+
424
 
425
+ except:
426
+ driver.delete_all_cookies()
427
+ driver.quit()
428
+ return "Error Occurred "
429
 
430
 
431
 
 
 
 
 
 
 
432
 
 
433
 
434
+ def do_ML_LLAMA13b(text:str, trycount:int):
435
+ starttime=time.time()
436
+ options = ChromeOptions()
437
+ options.add_argument('--no-sandbox')
438
+ options.add_argument('-headless')
439
+ options.add_argument("start-maximized")
440
+ service = Service(executable_path=chrome_driver_path)
441
+ driver = webdriver.Chrome(options= options,service=service)
442
+ driver.get("https://huggingface-projects-llama-2-13b-chat--mkxgl.hf.space/")
443
  try:
444
+
445
+ while True:
446
+ currtime= time.time()
447
+ if(currtime>starttime+20):
448
+ driver.delete_all_cookies()
449
+ driver.quit()
450
+ return "Requested Could not be proceed"
451
+ try:
452
+ xpath_expression = '//textarea[@data-testid="textbox"]'
453
+ textarea_element = driver.find_element(By.XPATH, xpath_expression)
454
+
455
+ for line in text.split('\n'):
456
+ textarea_element.send_keys(line)
457
+ # Simulate pressing the 'Shift + Enter' keys to add a newline without triggering submit
458
+ textarea_element.send_keys(Keys.SHIFT + Keys.ENTER)
459
+
460
+ span_element = driver.find_element(By.CLASS_NAME, 'svelte-s1r2yt')
461
+
462
+ span_element.click()
463
+ input_element = driver.find_element(By.CSS_SELECTOR, 'input[data-testid="number-input"]')
464
+ new_value = "2048" # Replace this with the value you want to set
465
+ input_element.clear()
466
+ input_element.send_keys(new_value)
467
+
468
+ textarea_element.send_keys('\n')
469
+
470
+ break
471
+ except:
472
+ continue
473
+
474
+
475
+ prev =""
476
 
477
+ time.sleep(0.6)
478
+ while True:
479
+ time.sleep(0.5)
480
+ currtime= time.time()
481
+
482
+ if(currtime>starttime+170):
483
+ driver.delete_all_cookies()
484
+ driver.quit()
485
+
486
+ return "Requested Could not be proceed"
487
+
488
+ try:
489
+ queue_element = driver.find_element(By.CLASS_NAME,'progress-text')
490
+ queue_text = queue_element.text
491
+ print(queue_text)
492
+ if "queue" in queue_text:
493
+ print("Queue is present")
494
+ driver.delete_all_cookies()
495
+ driver.quit()
496
+ return do_ML_LLAMA7b(text, trycount)
497
+ except:
498
+ pass
499
+
500
+ # value=""
501
+ try:
502
+ div_element = driver.find_element(By.CLASS_NAME, 'wrap.default.minimal.svelte-zlszon.translucent.hide')
503
+ element = driver.find_element(By.XPATH,'//div[@data-testid="bot" and contains(@class, "message bot")]')
504
 
505
+ x=(element.text)
506
+ driver.delete_all_cookies()
507
+ driver.quit()
508
+ return x
509
+ except:
510
+ continue
511
 
512
+
513
+ driver.delete_all_cookies()
514
+ driver.quit()
515
+
516
+ return " --Error Occurred-- "
517
+
518
+
519
 
520
+ except:
521
+ driver.delete_all_cookies()
522
+ driver.quit()
523
+ return "Error Occurred "
 
524
 
 
 
525
 
526
+
527
+
528
+ ########## MPT
529
+
530
+
531
+ @app.post("/mpt")
532
+ async def get_answer_mpt(request: Request ):
533
+ data = await request.json()
534
+ text = data['text']
535
+ print("recived ",text)
536
+ res= do_ML_MPT(text,0)
537
 
538
+ dict={"MPT":res}
539
+
540
  return JSONResponse(dict)
541
 
542
 
 
544
 
545
 
546
 
547
+ def do_ML_MPT(text:str, trycount:int):
548
+ starttime=time.time()
549
+ options = ChromeOptions()
550
+ options.add_argument('--no-sandbox')
551
+ options.add_argument('-headless')
552
+ options.add_argument("start-maximized")
553
+ service = Service(executable_path=chrome_driver_path)
554
+ driver = webdriver.Chrome(options= options,service=service)
555
+ driver.get("https://mosaicml-mpt-30b-chat.hf.space")
556
  try:
557
+
558
+ while True:
559
+ currtime= time.time()
560
+ if(currtime>starttime+20):
561
+ driver.delete_all_cookies()
562
+ driver.quit()
563
+ return "Requested Could not be proceed"
 
564
  try:
565
+ textarea_xpath = "//textarea[@data-testid='textbox' and @class='scroll-hide svelte-1pie7s6' and @placeholder='Chat Message Box']"
566
+ textarea_element = driver.find_element(By.XPATH,textarea_xpath)
 
 
 
 
 
 
 
 
 
 
567
 
568
+ for line in text.split('\n'):
569
+ textarea_element.send_keys(line)
570
+ # Simulate pressing the 'Shift + Enter' keys to add a newline without triggering submit
571
+ textarea_element.send_keys(Keys.SHIFT + Keys.ENTER)
572
+ break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
573
  except:
574
+ continue
 
575
 
576
  while True:
577
  currtime= time.time()
578
+ if(currtime>starttime+20):
579
+ driver.delete_all_cookies()
580
+ driver.quit()
581
  return "Requested Could not be proceed"
582
  try:
583
+
584
+
585
+ button_id = 'component-9' # Replace 'comonent-7' with the actual ID
586
+ button_element = driver.find_element(By.ID,button_id)
587
+
588
+ # Perform actions on the element (e.g., clicking the button)
589
+ button_element.click()
590
  break
591
+ except Exception as e:
592
+ print(e)
593
  time.sleep(0.2)
594
 
595
 
 
597
 
598
  # time.sleep(2)
599
  while True:
600
+ time.sleep(0.5)
601
+ currtime= time.time()
602
+
603
+ if(currtime>starttime+120):
604
+ driver.delete_all_cookies()
605
+ driver.quit()
606
 
607
+ return "Requested Could not be proceed"
608
 
609
+ # value=""
610
  try:
611
+ element = driver.find_element(By.XPATH,'//div[@data-testid="bot" and contains(@class, "message bot")]')
612
+
613
+ x=(element.text)
614
+
615
+ print("From text ",x)
616
+ if x=="":
617
+ raise ValueError(" k")
618
+ driver.quit()
619
+ return x
620
+
621
+
622
+
623
+
624
+ except Exception as e:
625
+ print(e)
626
  continue
627
 
628
 
629
 
630
+ driver.quit()
631
+ return " --Error Occurred-- "
 
632
 
633
 
634
 
635
  except:
636
  print("Error")
637
+
638
+ if trycount>1:
639
 
640
+ driver.delete_all_cookies()
641
+ driver.quit()
642
+ return "Error"
643
  driver.quit()
644
+ return do_ML_MPT(text,trycount+1)
 
645
 
646
 
647
+ '''Falcon 40 b intruct'''
648
 
649
 
650
+ @app.post("/falcon")
651
+ async def get_answer_falcon(request: Request ):
652
+ data = await request.json()
653
+ text = data['text']
654
+ print("recived ",text)
655
+ res= do_ML_FALCON(text,0)
656
 
657
+ dict={"FALCON":res}
 
 
 
 
 
 
658
 
659
+ return JSONResponse(dict)
 
 
 
660
 
 
 
 
661
 
 
 
 
 
 
662
 
 
 
663
 
664
 
 
665
 
666
+ def do_ML_FALCON(text:str, trycount:int):
667
  starttime=time.time()
 
668
  options = ChromeOptions()
669
  options.add_argument('--no-sandbox')
670
  options.add_argument('-headless')
671
+ options.add_argument("start-maximized")
672
+ service = Service(executable_path=chrome_driver_path)
673
  driver = webdriver.Chrome(options= options,service=service)
674
+ driver.get("https://huggingfaceh4-falcon-chat.hf.space")
675
  try:
676
+
677
  while True:
678
  currtime= time.time()
679
+ if(currtime>starttime+20):
680
+ driver.delete_all_cookies()
681
+ driver.quit()
682
+ return "Requested Could not be proceed"
683
  try:
684
+ textarea_element = driver.find_element(By.CSS_SELECTOR,'textarea[placeholder="Hello Falcon !!"]')
685
+
686
+ for line in text.split('\n'):
687
+ textarea_element.send_keys(line)
688
+ # Simulate pressing the 'Shift + Enter' keys to add a newline without triggering submit
689
+ textarea_element.send_keys(Keys.SHIFT + Keys.ENTER)
690
+ textarea_element.send_keys('\n')
691
  break
692
  except:
693
+ continue
694
+
695
 
696
+ prev =""
697
+
698
  # time.sleep(2)
699
  while True:
700
+ time.sleep(0.5)
701
  currtime= time.time()
702
+
703
+ if(currtime>starttime+170):
704
+ driver.delete_all_cookies()
705
+ driver.quit()
706
+
707
  return "Requested Could not be proceed"
708
 
709
+ # value=""
 
 
710
  try:
711
+ div_element = driver.find_element(By.CSS_SELECTOR,'div[data-testid="bot"]')
712
+ div_text = div_element.text
713
+ print(div_text)
714
+ if prev==div_text and prev !="":
715
+ driver.quit()
716
+ return div_text
717
+
718
+ prev= div_text
719
+
720
+
721
  except Exception as e:
722
  print(e)
723
+ continue
 
724
 
725
+
726
+
727
+ driver.quit()
728
+ return " --Error Occurred-- "
729
+
730
+
731
+
732
+ except:
733
+ print("Error")
734
+ driver.delete_all_cookies()
735
+ driver.quit()
736
+
737
+ return "Error Occureed"
738
+
739
+
740
 
741
+ '''Star Code'''
742
+
743
+
744
+ @app.post("/starcode")
745
+ async def get_answer_falcon(request: Request ):
746
+ data = await request.json()
747
+ text = data['text']
748
+ print("recived ",text)
749
+ res= do_ML_STARCODE(text,0)
750
+
751
+ dict={"RESULT":res}
752
+
753
+ return JSONResponse(dict)
754
+
755
+
756
+
757
+
758
+
759
+
760
+ def do_ML_STARCODE(text:str, trycount:int):
761
+ starttime=time.time()
762
+ options = ChromeOptions()
763
+ options.add_argument('--no-sandbox')
764
+ options.add_argument('-headless')
765
+ options.add_argument("start-maximized")
766
+ service = Service(executable_path=chrome_driver_path)
767
+ driver = webdriver.Chrome(options= options,service=service)
768
+ driver.get("https://huggingfaceh4-starchat-playground.hf.space/")
769
+ try:
770
+
771
+ while True:
772
+ currtime= time.time()
773
+ if(currtime>starttime+20):
774
  driver.delete_all_cookies()
775
  driver.quit()
776
+ return "Requested Could not be proceed"
777
+ try:
778
+ textarea_element = driver.find_element(By.CSS_SELECTOR,'textarea[placeholder="Enter your message here"]')
779
 
780
+ for line in text.split('\n'):
781
+ textarea_element.send_keys(line)
782
+ # Simulate pressing the 'Shift + Enter' keys to add a newline without triggering submit
783
+ textarea_element.send_keys(Keys.SHIFT + Keys.ENTER)
784
+ time.sleep(0.5)
785
+ textarea_element.send_keys('\n')
786
  break
787
  except:
788
  continue
789
+
790
+
791
+ prev =""
792
 
793
+ # time.sleep(2)
794
+ while True:
795
+ time.sleep(0.5)
796
+ currtime= time.time()
797
+
798
+ if(currtime>starttime+170):
799
+ driver.delete_all_cookies()
800
+ driver.quit()
801
+
802
+ return "Requested Could not be proceed"
803
+
804
+ try:
805
+ div_element = driver.find_element(By.CLASS_NAME,'svelte-j1gjts.generating')
806
+ continue
807
+ except:
808
+ pass
809
+ # value=""
810
+ try:
811
+ div_element = driver.find_element(By.CSS_SELECTOR,'div[data-testid="bot"]')
812
+ text_content = div_element.text
813
+ print(text_content)
814
+ driver.delete_all_cookies()
815
+ driver.quit()
816
+ return text_content
817
+
818
+ except Exception as e:
819
+ print(e)
820
+ continue
821
+
822
+
823
+
824
+ driver.quit()
825
+ return " --Error Occurred-- "
826
+
827
+
828
+
829
  except:
830
  print("Error")
 
 
831
 
832
+ driver.delete_all_cookies()
833
+ driver.quit()
834
+ return "Error Occureed"
835
+
836
+
837
+ @app.post("/chatgpt")
838
+ async def get_answer_llama3(request: Request ):
839
+ data = await request.json()
840
+ msges= data['messages']
841
+ print("recievoed , ", msges )
842
+ return do_chat_gpt(msges)
843
+
844
+
845
+
846
+
847
+
848
+ def do_chat_gpt(msges):
849
+ starttime=time.time()
850
+ options = ChromeOptions()
851
+ options.add_argument('--no-sandbox')
852
+ options.add_argument('-headless')
853
+ options.add_argument("start-maximized")
854
+ service = Service(executable_path=chrome_driver_path)
855
+ driver = webdriver.Chrome(options= options,service=service)
856
+ driver.get("https://openskyml-pigeon-chat.hf.space/")
857
+ base=""
858
+ for message in msges:
859
+ base += "%s: %s \n" % (message["role"], message["content"])
860
+ base += "assistant:"
861
+ print("base ",base)
862
+ try:
863
+ while True:
864
+ curr= time.time()
865
+ if curr- starttime>20:
866
+ driver.quit()
867
+ return "Error -1"
868
+ try:
869
+ textarea_element = driver.find_element(By.XPATH,'//textarea[@placeholder="Type a message..."]')
870
+ for line in base.split('\n'):
871
+ textarea_element.send_keys(line)
872
+ textarea_element.send_keys(Keys.SHIFT + Keys.ENTER)
873
+ time.sleep(0.5)
874
+ textarea_element.send_keys('\n')
875
+ break
876
+ except Exception as e:
877
+ print(e)
878
+ time.sleep(0.5)
879
+ continue
880
+ while True:
881
+ curr= time.time()
882
+ if curr- starttime>175:
883
+ driver.quit()
884
+ return "Error -1"
885
+ try:
886
+ div_element = driver.find_element(By.CSS_SELECTOR,'div[data-testid="bot"]')
887
+ text = div_element.text
888
+ print(text)
889
+ if text=="":
890
+ raise ValueError("Empty text")
891
+ driver.quit()
892
+ return text
893
+ except Exception as e:
894
+ print(e)
895
+ time.sleep(0.5)
896
+ except:
897
+ driver.quit()
898
+ return "Error"
899
+
900
+
901
+ @app.post("/falcon180b")
902
+ async def get_answer_falcon180(request: Request, background_tasks: BackgroundTasks ):
903
+ data = await request.json()
904
+ text= data['text']
905
+ print("recievoed , ", text )
906
+ return StreamingResponse(do_falcon180(text), media_type="text/plain")
907
+
908
+
909
+
910
+ import asyncio
911
+ from fastapi.responses import StreamingResponse
912
+
913
+ async def do_falcon180(text):
914
+ starttime=time.time()
915
+ options = ChromeOptions()
916
+ options.add_argument('--no-sandbox')
917
+ options.add_argument('-headless')
918
+ options.add_argument("start-maximized")
919
+ service = Service(executable_path=chrome_driver_path)
920
+ driver = webdriver.Chrome(options= options,service=service)
921
+ driver.get("https://ysharma-falcon-180b-demo.hf.space/")
922
+ try:
923
+ while True:
924
+ try:
925
+ curr= time.time()
926
+ if curr- starttime>20:
927
+ break
928
+ xpath_expression = "//span[@class='svelte-s1r2yt' and contains(text(), 'Additional Inputs')]"
929
+ element = driver.find_element(By.XPATH, xpath_expression)
930
+ element.click()
931
+ element = driver.find_elements(By.CSS_SELECTOR, 'input[data-testid="number-input"]')
932
+ element[1].clear() # Clear any existing value
933
+ element[1].send_keys('4000')
934
+ textarea_element = driver.find_element(By.XPATH,'//textarea[@placeholder="Type a message..."]')
935
+ for line in text.split('\n'):
936
+ textarea_element.send_keys(line)
937
+ textarea_element.send_keys(Keys.SHIFT + Keys.ENTER)
938
+ textarea_element.send_keys('\n')
939
+ break
940
+ except Exception as e:
941
+ print(e," --1")
942
+ continue
943
+ old_text=""
944
+ while True:
945
+ # await asyncio.sleep(0.1)
946
+ curr= time.time()
947
+ if curr- starttime>180:
948
+ driver.quit()
949
+ break
950
+ done = False
951
+ try:
952
+ xpath_expression = "//span[contains(@class, 'chatbot')]"
953
+ element = driver.find_elements(By.XPATH, xpath_expression)
954
+ text = element[1].text
955
+ # print(text)
956
+ while True:
957
+ text = element[1].text
958
+ send_text= text[len(old_text):]
959
+ old_text= text
960
+ yield send_text.encode()
961
+ try:
962
+ element = driver.find_element(By.CLASS_NAME,"generating")
963
+ # print("generating")
964
+ except:
965
+ # print("generated")
966
+ driver.quit()
967
+ done= True
968
+ break
969
+ if done:
970
+ break
971
+ except Exception as e:
972
+ print(e)
973
+ continue
974
+ except:
975
  driver.quit()
 
976
 
977
 
978
 
979
 
980
+
 
981
 
982
+