EnigmaOfTheWorld commited on
Commit
e4b2e32
·
1 Parent(s): 73b8442

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -8
app.py CHANGED
@@ -42,6 +42,7 @@ stability_api = client.StabilityInference(
42
  whisper_from_pipeline = pipeline("automatic-speech-recognition",model="openai/whisper-medium")
43
  EMBEDIDNGS = None
44
  DATAFRAME_FILE = None
 
45
  RANDOM_USER = ''.join(chr(random.randint(65,90)) for i in range(8))+f'{random.randint(1,10000000000)}'
46
  print(f'{RANDOM_USER} chat started')
47
 
@@ -140,9 +141,9 @@ def search_internet(user_query:str,*,key_number:int) -> str:
140
 
141
  def search_document_uploaded(user_query:str) -> str:
142
  print('Searching uploaded document......')
143
- docsearch = FAISS.load_local(folder_path = f'/tmp/{RANDOM_USER}embeddings',embeddings=EMBEDIDNGS)
144
  chain = load_qa_chain(OpenAI(), chain_type="stuff")
145
- docs = docsearch.similarity_search(user_query)
146
  return chain.run(input_documents=docs, question=user_query)
147
 
148
 
@@ -152,7 +153,7 @@ def ask_dataframes(user_query):
152
  ############# GET OPENAI RESPONSE
153
  def get_open_ai_reponse(user_query:str)->Union[tuple,str]:
154
  print(EMBEDIDNGS)
155
- if EMBEDIDNGS is not None:
156
  print('Searching document')
157
  return search_document_uploaded(user_query)
158
 
@@ -286,17 +287,22 @@ def build_embeddings(file_name,file_ext):
286
 
287
  global EMBEDIDNGS
288
  EMBEDIDNGS = OpenAIEmbeddings(openai_api_key=os.environ['OPENAI_API_KEY'])
289
- docsearch = FAISS.from_texts(texts, EMBEDIDNGS)
290
- if not os.path.exists(f'/tmp/{RANDOM_USER}embeddings'):
291
- os.mkdir(f'/tmp/{RANDOM_USER}embeddings')
292
- docsearch.save_local(f'/tmp/{RANDOM_USER}embeddings')
293
- print(f'Embeddings created to /tmp/{RANDOM_USER}embeddings')
 
294
 
295
 
296
  def ask_questions_abt_dataframes(file,file_ext):
297
  print(file_ext)
298
  global EMBEDIDNGS
299
  EMBEDIDNGS = None
 
 
 
 
300
 
301
  reader_function = { '.csv': pd.read_csv, '.xlsx': pd.read_excel }.get(file_ext)
302
  print(reader_function.__name__)
 
42
  whisper_from_pipeline = pipeline("automatic-speech-recognition",model="openai/whisper-medium")
43
  EMBEDIDNGS = None
44
  DATAFRAME_FILE = None
45
+ DOCSEARCH = None
46
  RANDOM_USER = ''.join(chr(random.randint(65,90)) for i in range(8))+f'{random.randint(1,10000000000)}'
47
  print(f'{RANDOM_USER} chat started')
48
 
 
141
 
142
  def search_document_uploaded(user_query:str) -> str:
143
  print('Searching uploaded document......')
144
+ # docsearch = FAISS.load_local(folder_path = f'/tmp/{RANDOM_USER}embeddings',embeddings=EMBEDIDNGS)
145
  chain = load_qa_chain(OpenAI(), chain_type="stuff")
146
+ docs = DOCSEARCH.similarity_search(user_query)
147
  return chain.run(input_documents=docs, question=user_query)
148
 
149
 
 
153
  ############# GET OPENAI RESPONSE
154
  def get_open_ai_reponse(user_query:str)->Union[tuple,str]:
155
  print(EMBEDIDNGS)
156
+ if (EMBEDIDNGS is not None) and (DOCSEARCH is not None):
157
  print('Searching document')
158
  return search_document_uploaded(user_query)
159
 
 
287
 
288
  global EMBEDIDNGS
289
  EMBEDIDNGS = OpenAIEmbeddings(openai_api_key=os.environ['OPENAI_API_KEY'])
290
+ global DOCSEARCH
291
+ DOCSEARCH = FAISS.from_texts(texts, EMBEDIDNGS)
292
+ # if not os.path.exists(f'/tmp/{RANDOM_USER}embeddings'):
293
+ # os.mkdir(f'/tmp/{RANDOM_USER}embeddings')
294
+ # docsearch.save_local(f'/tmp/{RANDOM_USER}embeddings')
295
+ # print(f'Embeddings created to /tmp/{RANDOM_USER}embeddings')
296
 
297
 
298
  def ask_questions_abt_dataframes(file,file_ext):
299
  print(file_ext)
300
  global EMBEDIDNGS
301
  EMBEDIDNGS = None
302
+ global DOCSEARCH
303
+ DOCSEARCH = None
304
+
305
+
306
 
307
  reader_function = { '.csv': pd.read_csv, '.xlsx': pd.read_excel }.get(file_ext)
308
  print(reader_function.__name__)