Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -164,6 +164,14 @@ st.sidebar.markdown("[Website](https://tenlancer.com/)")
|
|
164 |
|
165 |
|
166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
def main():
|
168 |
try:
|
169 |
if (use_openai and openai_api_key) or use_google:
|
@@ -174,9 +182,9 @@ def main():
|
|
174 |
|
175 |
for msg in st.session_state.messages:
|
176 |
st.chat_message(msg["role"]).write(msg["content"])
|
177 |
-
|
178 |
|
179 |
-
|
180 |
#st.audio(audio_msg, format='audio/mp3').audio(audio_msg)
|
181 |
|
182 |
|
@@ -216,11 +224,12 @@ def main():
|
|
216 |
compliment = ['thank you', 'thanks', 'thanks a lot', 'thanks a bunch', 'great', 'ok', 'ok thanks', 'okay', 'great', 'awesome', 'nice']
|
217 |
|
218 |
prompt_template =dedent(r"""
|
219 |
-
You are a helpful assistant to help user find information from
|
220 |
talk humbly. Answer the question from the provided context. Do not answer from your own training data.
|
221 |
Use the following pieces of context to answer the question at the end.
|
222 |
If you don't know the answer, just say that you don't know. Do not makeup any answer.
|
223 |
-
|
|
|
224 |
Always use the context to find the answer.
|
225 |
|
226 |
this is the context from study material:
|
@@ -260,10 +269,11 @@ def main():
|
|
260 |
response = 'Hi, how are you? I am here to help you get information from your file. How can I assist you?'
|
261 |
|
262 |
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
|
|
267 |
#st.audio(audio_buffer, format='audio/mp3')
|
268 |
st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
|
269 |
|
@@ -271,28 +281,28 @@ def main():
|
|
271 |
response = 'My pleasure! If you have any more questions, feel free to ask.'
|
272 |
|
273 |
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
|
|
278 |
#st.audio(audio_buffer, format='audio/mp3')
|
279 |
st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
|
280 |
|
281 |
elif uploaded_file:
|
282 |
with st.spinner('Bot is typing ...'):
|
283 |
-
docs = db.similarity_search(prompt, k=
|
284 |
response = chain.run(input_documents=docs, question=prompt)
|
285 |
|
286 |
|
287 |
lang = detect(response)
|
288 |
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
#audio_buffer.seek(0)
|
293 |
# st.audio(audio_buffer, format='audio/mp3')
|
294 |
#st.session_state.audio.append({"role": "Assistant", "audio": audio_buffer})
|
295 |
-
|
296 |
|
297 |
assistant_message = {"role": "assistant", "content": response}
|
298 |
else:
|
@@ -302,20 +312,19 @@ def main():
|
|
302 |
response = chain.invoke({"chat_history": memory, "question": prompt}).content
|
303 |
|
304 |
|
305 |
-
|
306 |
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
#audio_buffer.seek(0)
|
311 |
#st.audio(audio_buffer, format='audio/mp3')
|
312 |
#st.session_state.audio.append({"role": "Assistant", "audio": audio_buffer})
|
313 |
-
|
314 |
|
315 |
assistant_message = {"role": "assistant", "content": response}
|
316 |
|
317 |
st.write(response)
|
318 |
-
|
319 |
|
320 |
|
321 |
except Exception as e:
|
@@ -326,6 +335,8 @@ def main():
|
|
326 |
elif use_openai:
|
327 |
"Please check your OpenAI API key"
|
328 |
|
|
|
|
|
329 |
|
330 |
|
331 |
hide_streamlit_style = """
|
|
|
164 |
|
165 |
|
166 |
|
167 |
+
def text_to_audio(response, lang):
|
168 |
+
audio_buffer = BytesIO()
|
169 |
+
audio_file = gTTS(text=response, lang=lang, slow=False)
|
170 |
+
audio_file.write_to_fp(audio_buffer)
|
171 |
+
audio_buffer.seek(0)
|
172 |
+
return audio_buffer
|
173 |
+
|
174 |
+
|
175 |
def main():
|
176 |
try:
|
177 |
if (use_openai and openai_api_key) or use_google:
|
|
|
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 his documents.
|
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 |
+
Do not answer hypothetically. Do not answer in more than 100 words.
|
232 |
+
Please Do Not say: "Based on the provided context"
|
233 |
Always use the context to find the answer.
|
234 |
|
235 |
this is the context from study material:
|
|
|
269 |
response = 'Hi, how are you? I am here to help you get information from your file. How can I assist you?'
|
270 |
|
271 |
|
272 |
+
lang = "en"
|
273 |
+
|
274 |
+
|
275 |
+
|
276 |
+
audio_buffer = text_to_audio(response, lang)
|
277 |
#st.audio(audio_buffer, format='audio/mp3')
|
278 |
st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
|
279 |
|
|
|
281 |
response = 'My pleasure! If you have any more questions, feel free to ask.'
|
282 |
|
283 |
|
284 |
+
lang = "en"
|
285 |
+
|
286 |
+
|
287 |
+
|
288 |
+
audio_buffer = text_to_audio(response, lang)
|
289 |
#st.audio(audio_buffer, format='audio/mp3')
|
290 |
st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
|
291 |
|
292 |
elif uploaded_file:
|
293 |
with st.spinner('Bot is typing ...'):
|
294 |
+
docs = db.similarity_search(prompt, k=5, fetch_k=10)
|
295 |
response = chain.run(input_documents=docs, question=prompt)
|
296 |
|
297 |
|
298 |
lang = detect(response)
|
299 |
|
300 |
+
|
301 |
+
|
302 |
+
audio_buffer = text_to_audio(response, lang)
|
|
|
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 |
response = chain.invoke({"chat_history": memory, "question": prompt}).content
|
313 |
|
314 |
|
315 |
+
lang = detect(response)
|
316 |
|
317 |
+
|
318 |
+
|
319 |
+
audio_buffer = text_to_audio(response, lang)
|
|
|
320 |
#st.audio(audio_buffer, format='audio/mp3')
|
321 |
#st.session_state.audio.append({"role": "Assistant", "audio": audio_buffer})
|
322 |
+
st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
|
323 |
|
324 |
assistant_message = {"role": "assistant", "content": response}
|
325 |
|
326 |
st.write(response)
|
327 |
+
st.audio(audio_buffer, format='audio/wav')
|
328 |
|
329 |
|
330 |
except Exception as e:
|
|
|
335 |
elif use_openai:
|
336 |
"Please check your OpenAI API key"
|
337 |
|
338 |
+
|
339 |
+
|
340 |
|
341 |
|
342 |
hide_streamlit_style = """
|