rishi1985 commited on
Commit
4b90636
1 Parent(s): 983e2bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -1
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import io
2
  from fastapi import FastAPI, File, UploadFile
 
3
 
4
  import subprocess
5
  import os
@@ -897,7 +898,82 @@ def do_chat_gpt(msges):
897
  return "Error"
898
 
899
 
900
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
901
 
902
 
903
 
 
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
 
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://tiiuae-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 = "//div[@class='label-wrap svelte-s1r2yt']"
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)
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.CSS_SELECTOR, 'div.wrap.default.minimal.svelte-zlszon.translucent.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