techasad commited on
Commit
4038c3b
·
verified ·
1 Parent(s): 995ef97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -28
app.py CHANGED
@@ -28,17 +28,16 @@ from langchain.prompts import (
28
  )
29
 
30
 
31
- from dotenv import load_dotenv
32
-
33
- # Load environment variables from .env file
34
- load_dotenv()
35
-
36
- # Access the API key
37
- google_api_key = os.getenv("GOOGLE_API_KEY")
38
-
39
-
40
-
41
 
 
 
 
 
 
 
 
 
 
42
  #api_key2 = st.secrets["OPENAI_API_KEY"]
43
  os.environ["GOOGLE_API_KEY"] = google_api_key
44
 
@@ -183,9 +182,9 @@ def main():
183
 
184
  for msg in st.session_state.messages:
185
  st.chat_message(msg["role"]).write(msg["content"])
186
- if msg["role"] == "Assistant":
187
 
188
- st.chat_message(msg["role"]).audio(msg["audio_content"], format='audio/wav')
189
  #st.audio(audio_msg, format='audio/mp3').audio(audio_msg)
190
 
191
 
@@ -225,12 +224,11 @@ def main():
225
  compliment = ['thank you', 'thanks', 'thanks a lot', 'thanks a bunch', 'great', 'ok', 'ok thanks', 'okay', 'great', 'awesome', 'nice']
226
 
227
  prompt_template =dedent(r"""
228
- You are a helpful assistant to help user find information from his documents.
229
  talk humbly. Answer the question from the provided context. Do not answer from your own training data.
230
  Use the following pieces of context to answer the question at the end.
231
  If you don't know the answer, just say that you don't know. Do not makeup any answer.
232
- Do not answer hypothetically. Do not answer in more than 100 words.
233
- Please Do Not say: "Based on the provided context"
234
  Always use the context to find the answer.
235
 
236
  this is the context from study material:
@@ -290,19 +288,19 @@ def main():
290
 
291
  elif uploaded_file:
292
  with st.spinner('Bot is typing ...'):
293
- docs = db.similarity_search(prompt, k=5, fetch_k=10)
294
  response = chain.run(input_documents=docs, question=prompt)
295
 
296
 
297
  lang = detect(response)
298
 
299
- audio_buffer = BytesIO()
300
- audio_file = gTTS(text=response, lang=lang, slow=False)
301
- audio_file.write_to_fp(audio_buffer)
302
- audio_buffer.seek(0)
303
  # st.audio(audio_buffer, format='audio/mp3')
304
  #st.session_state.audio.append({"role": "Assistant", "audio": audio_buffer})
305
- st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
306
 
307
  assistant_message = {"role": "assistant", "content": response}
308
  else:
@@ -312,20 +310,20 @@ def main():
312
  response = chain.invoke({"chat_history": memory, "question": prompt}).content
313
 
314
 
315
- lang = detect(response)
316
 
317
- audio_buffer = BytesIO()
318
- audio_file = gTTS(text=response, lang=lang, slow=False)
319
- audio_file.write_to_fp(audio_buffer)
320
- audio_buffer.seek(0)
321
  #st.audio(audio_buffer, format='audio/mp3')
322
  #st.session_state.audio.append({"role": "Assistant", "audio": audio_buffer})
323
- st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
324
 
325
  assistant_message = {"role": "assistant", "content": response}
326
 
327
  st.write(response)
328
- st.audio(audio_buffer, format='audio/wav')
329
 
330
 
331
  except Exception as e:
 
28
  )
29
 
30
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ def get_key():
33
+ try:
34
+ google_api_key = st.secrets["GOOGLE_API_KEY"]
35
+ return google_api_key
36
+ except Exception as e:
37
+ google_api_key = os.environ.get("GOOGLE_API_KEY")
38
+ return google_api_key
39
+
40
+ google_api_key = get_key()
41
  #api_key2 = st.secrets["OPENAI_API_KEY"]
42
  os.environ["GOOGLE_API_KEY"] = google_api_key
43
 
 
182
 
183
  for msg in st.session_state.messages:
184
  st.chat_message(msg["role"]).write(msg["content"])
185
+ #if msg["role"] == "Assistant":
186
 
187
+ #st.chat_message(msg["role"]).audio(msg["audio_content"], format='audio/wav')
188
  #st.audio(audio_msg, format='audio/mp3').audio(audio_msg)
189
 
190
 
 
224
  compliment = ['thank you', 'thanks', 'thanks a lot', 'thanks a bunch', 'great', 'ok', 'ok thanks', 'okay', 'great', 'awesome', 'nice']
225
 
226
  prompt_template =dedent(r"""
227
+ You are a helpful assistant to help user find information from the olympics documents. which will be held in paris this summer.
228
  talk humbly. Answer the question from the provided context. Do not answer from your own training data.
229
  Use the following pieces of context to answer the question at the end.
230
  If you don't know the answer, just say that you don't know. Do not makeup any answer.
231
+
 
232
  Always use the context to find the answer.
233
 
234
  this is the context from study material:
 
288
 
289
  elif uploaded_file:
290
  with st.spinner('Bot is typing ...'):
291
+ docs = db.similarity_search(prompt, k=7, fetch_k=20)
292
  response = chain.run(input_documents=docs, question=prompt)
293
 
294
 
295
  lang = detect(response)
296
 
297
+ #audio_buffer = BytesIO()
298
+ #audio_file = gTTS(text=response, lang=lang, slow=False)
299
+ #audio_file.write_to_fp(audio_buffer)
300
+ #audio_buffer.seek(0)
301
  # st.audio(audio_buffer, format='audio/mp3')
302
  #st.session_state.audio.append({"role": "Assistant", "audio": audio_buffer})
303
+ #st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
304
 
305
  assistant_message = {"role": "assistant", "content": response}
306
  else:
 
310
  response = chain.invoke({"chat_history": memory, "question": prompt}).content
311
 
312
 
313
+ #lang = detect(response)
314
 
315
+ #audio_buffer = BytesIO()
316
+ #audio_file = gTTS(text=response, lang=lang, slow=False)
317
+ #audio_file.write_to_fp(audio_buffer)
318
+ #audio_buffer.seek(0)
319
  #st.audio(audio_buffer, format='audio/mp3')
320
  #st.session_state.audio.append({"role": "Assistant", "audio": audio_buffer})
321
+ #st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
322
 
323
  assistant_message = {"role": "assistant", "content": response}
324
 
325
  st.write(response)
326
+ #st.audio(audio_buffer, format='audio/wav')
327
 
328
 
329
  except Exception as e: