techasad commited on
Commit
2b48034
1 Parent(s): b89b0a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -42
app.py CHANGED
@@ -3,7 +3,7 @@ from PyPDF2 import PdfReader
3
  import langchain
4
  from textwrap import dedent
5
  import pandas as pd
6
-
7
  from langchain_community.callbacks import StreamlitCallbackHandler
8
  from langchain_openai import ChatOpenAI
9
  from langchain_community.chat_models import ChatGooglePalm
@@ -30,15 +30,57 @@ from langchain.prompts import (
30
 
31
 
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  #api_key2 = st.secrets["OPENAI_API_KEY"]
34
- os.environ["GOOGLE_API_KEY"] = "AIzaSyD29fEos3V6S2L-AGSQgNu03GqZEIgJads"
35
 
36
- st.set_page_config(page_title='Personal Chatbot', page_icon='books')
37
- st.header('Knowledge Query Assistant')
38
- st.write("Upload your file to begin a chat, or ask any general questions you have")
39
- st.sidebar.title('Options')
40
 
41
 
 
42
  st.sidebar.subheader("Please Choose the AI Engine")
43
  use_google = st.sidebar.checkbox("Use Free AI", value =True)
44
  use_openai = st.sidebar.checkbox("Use OpenAI with your API Key")
@@ -163,7 +205,6 @@ st.sidebar.markdown("[Website](https://tenlancer.com/)")
163
  ########--Save PDF--########
164
 
165
 
166
-
167
  def text_to_audio(response, lang):
168
  audio_buffer = BytesIO()
169
  audio_file = gTTS(text=response, lang=lang, slow=False)
@@ -173,7 +214,7 @@ def text_to_audio(response, lang):
173
 
174
 
175
  def main():
176
- try:
177
  if (use_openai and openai_api_key) or use_google:
178
  if uploaded_file:
179
  db = processing_csv_pdf_docx(uploaded_file)
@@ -181,17 +222,20 @@ def main():
181
  st.success(f'Your File: {file.name} is Embedded', icon="✅")
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
 
191
  if prompt := st.chat_input(placeholder="Type your question!"):
192
  st.session_state.messages.append({"role": "user", "content": prompt})
193
- st.chat_message("user").write(prompt)
194
- memory = ConversationBufferMemory(memory_key="chat_history", input_key="question", human_prefix= "", ai_prefix= "")
195
  user_message = {"role": "user", "content": prompt}
196
 
197
 
@@ -200,15 +244,15 @@ def main():
200
  user_prompt = st.session_state.messages[i]
201
  ai_res = st.session_state.messages[i + 1]
202
 
203
- current_role = user_prompt["role"]
204
  current_content = user_prompt["content"]
205
 
206
- next_role = ai_res["role"]
207
  next_content = ai_res["content"]
208
 
209
  # Concatenate role and content for context and output
210
- user = f"{current_role}: {current_content}"
211
- ai = f"{next_role}: {next_content}"
212
 
213
  memory.save_context({"question": user}, {"output": ai})
214
 
@@ -224,12 +268,15 @@ def main():
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 an informative chatbot designed to assist users with queries specifically related to context.
228
- Your purpose is to provide accurate information sourced from a document, ensuring relevance and reliability in your responses.
229
- Respond with humility and avoid speculative or hypothetical answers. Use the provided context to formulate concise responses, staying within 100 words, and refrain from adding unnecessary information.
230
- Ensure that your answers are directly related to the user's query and do not deviate from the provided context.
 
 
 
231
 
232
- this is the context from study material:
233
  ---------
234
  {context}
235
  ---------
@@ -260,7 +307,7 @@ def main():
260
 
261
  #chain = load_qa_chain(ChatOpenAI(temperature=0.9, model="gpt-3.5-turbo-0613", streaming=True) , verbose= True, prompt = PROMPT, memory=memory,chain_type="stuff")
262
 
263
- with st.chat_message("Assistant"):
264
  st_cb = StreamlitCallbackHandler(st.container())
265
  if prompt.lower() in greetings:
266
  response = 'Hi, how are you? I am here to help you get information from your file. How can I assist you?'
@@ -272,8 +319,7 @@ def main():
272
 
273
  audio_buffer = text_to_audio(response, lang)
274
  #st.audio(audio_buffer, format='audio/mp3')
275
- st.session_state.messages.append({"role": "Assistant", "content": response})
276
- #st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
277
 
278
  elif prompt.lower() in compliment:
279
  response = 'My pleasure! If you have any more questions, feel free to ask.'
@@ -285,12 +331,11 @@ def main():
285
 
286
  audio_buffer = text_to_audio(response, lang)
287
  #st.audio(audio_buffer, format='audio/mp3')
288
- st.session_state.messages.append({"role": "Assistant", "content": response})
289
- #st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
290
 
291
  elif uploaded_file:
292
  with st.spinner('Bot is typing ...'):
293
- docs = db.similarity_search(prompt, k=10, fetch_k=30)
294
  response = chain.run(input_documents=docs, question=prompt)
295
 
296
 
@@ -301,8 +346,8 @@ def main():
301
  audio_buffer = text_to_audio(response, lang)
302
  # st.audio(audio_buffer, format='audio/mp3')
303
  #st.session_state.audio.append({"role": "Assistant", "audio": audio_buffer})
304
- #st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
305
- st.session_state.messages.append({"role": "Assistant", "content": response})
306
  assistant_message = {"role": "assistant", "content": response}
307
  else:
308
  with st.spinner('Bot is typing ...'):
@@ -315,27 +360,25 @@ def main():
315
 
316
 
317
 
318
- #audio_buffer = text_to_audio(response, lang)
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
- st.session_state.messages.append({"role": "Assistant", "content": response})
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:
330
 
331
- "Sorry, there was a problem. A corrupted file or;"
332
- if use_google:
333
- "Google PaLM AI only take English Data and Questions. Or the AI could not find the answer in your provided document."
334
- elif use_openai:
335
- "Please check your OpenAI API key"
336
 
337
-
338
-
339
 
340
 
341
  hide_streamlit_style = """
 
3
  import langchain
4
  from textwrap import dedent
5
  import pandas as pd
6
+ from langchain_google_genai import ChatGoogleGenerativeAI
7
  from langchain_community.callbacks import StreamlitCallbackHandler
8
  from langchain_openai import ChatOpenAI
9
  from langchain_community.chat_models import ChatGooglePalm
 
30
 
31
 
32
 
33
+ st.set_page_config(page_title='Personal Chatbot', page_icon='books')
34
+
35
+
36
+
37
+
38
+ st.markdown(
39
+ """
40
+ <style>
41
+ [data-testid=stImage]{
42
+ text-align: center;
43
+ display: block;
44
+ margin-left: 10%;
45
+ margin-right:10%;
46
+ width: 100%;
47
+ }
48
+ img {
49
+ border-radius: 50%;
50
+ align: center;
51
+ }
52
+ </style>
53
+ """, unsafe_allow_html=True
54
+ )
55
+
56
+
57
+
58
+ st.image("tenlancer.png", width=80)
59
+
60
+ st.markdown("<h3 style='text-align: center; color: white;'> Knowledge Query Assistant </h3>", unsafe_allow_html=True)
61
+
62
+
63
+
64
+
65
+ st.markdown(
66
+ """
67
+ <style>
68
+ [data-testid="stChatMessageContent"] p{
69
+ font-size: 1.2rem;
70
+ color: #404040
71
+ }
72
+ </style>
73
+ """, unsafe_allow_html=True
74
+ )
75
+
76
+
77
+ google_api_key = st.secrets["GOOGLE_API_KEY"]
78
  #api_key2 = st.secrets["OPENAI_API_KEY"]
79
+ os.environ["GOOGLE_API_KEY"] = google_api_key
80
 
 
 
 
 
81
 
82
 
83
+ st.sidebar.header("options")
84
  st.sidebar.subheader("Please Choose the AI Engine")
85
  use_google = st.sidebar.checkbox("Use Free AI", value =True)
86
  use_openai = st.sidebar.checkbox("Use OpenAI with your API Key")
 
205
  ########--Save PDF--########
206
 
207
 
 
208
  def text_to_audio(response, lang):
209
  audio_buffer = BytesIO()
210
  audio_file = gTTS(text=response, lang=lang, slow=False)
 
214
 
215
 
216
  def main():
217
+ # try:
218
  if (use_openai and openai_api_key) or use_google:
219
  if uploaded_file:
220
  db = processing_csv_pdf_docx(uploaded_file)
 
222
  st.success(f'Your File: {file.name} is Embedded', icon="✅")
223
 
224
  for msg in st.session_state.messages:
225
+ if msg["role"] == "user":
226
+ st.chat_message("user", avatar="user.png").write(msg["content"])
227
+
228
+ if msg["role"] == "Assistant":
229
+ st.chat_message("Assistant", avatar="logo.png").write(msg["content"])
230
 
231
+ st.audio(msg["audio_content"], format='audio/wav')
232
  #st.audio(audio_msg, format='audio/mp3').audio(audio_msg)
233
 
234
 
235
  if prompt := st.chat_input(placeholder="Type your question!"):
236
  st.session_state.messages.append({"role": "user", "content": prompt})
237
+ st.chat_message("user", avatar="user.png").write(prompt)
238
+ memory = ConversationBufferMemory(memory_key="chat_history", input_key="question", human_prefix= "User", ai_prefix= "Assistant")
239
  user_message = {"role": "user", "content": prompt}
240
 
241
 
 
244
  user_prompt = st.session_state.messages[i]
245
  ai_res = st.session_state.messages[i + 1]
246
 
247
+
248
  current_content = user_prompt["content"]
249
 
250
+
251
  next_content = ai_res["content"]
252
 
253
  # Concatenate role and content for context and output
254
+ user = f" {current_content}"
255
+ ai = f" {next_content}"
256
 
257
  memory.save_context({"question": user}, {"output": ai})
258
 
 
268
  compliment = ['thank you', 'thanks', 'thanks a lot', 'thanks a bunch', 'great', 'ok', 'ok thanks', 'okay', 'great', 'awesome', 'nice']
269
 
270
  prompt_template =dedent(r"""
271
+ You are a helpful assistant.
272
+ talk humbly. Answer the question from the provided context. Do not answer from your own training data.
273
+ Use the following pieces of context to answer the question at the end.
274
+ If you don't know the answer, just say that you don't know. Do not makeup any answer.
275
+ Do not answer hypothetically. Do not answer in more than 100 words.
276
+ Please Do Not say: "Based on the provided context"
277
+
278
 
279
+ this is the context:
280
  ---------
281
  {context}
282
  ---------
 
307
 
308
  #chain = load_qa_chain(ChatOpenAI(temperature=0.9, model="gpt-3.5-turbo-0613", streaming=True) , verbose= True, prompt = PROMPT, memory=memory,chain_type="stuff")
309
 
310
+ with st.chat_message("Assistant", avatar="logo.png"):
311
  st_cb = StreamlitCallbackHandler(st.container())
312
  if prompt.lower() in greetings:
313
  response = 'Hi, how are you? I am here to help you get information from your file. How can I assist you?'
 
319
 
320
  audio_buffer = text_to_audio(response, lang)
321
  #st.audio(audio_buffer, format='audio/mp3')
322
+ st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
 
323
 
324
  elif prompt.lower() in compliment:
325
  response = 'My pleasure! If you have any more questions, feel free to ask.'
 
331
 
332
  audio_buffer = text_to_audio(response, lang)
333
  #st.audio(audio_buffer, format='audio/mp3')
334
+ st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
 
335
 
336
  elif uploaded_file:
337
  with st.spinner('Bot is typing ...'):
338
+ docs = db.similarity_search(prompt, k=5, fetch_k=10)
339
  response = chain.run(input_documents=docs, question=prompt)
340
 
341
 
 
346
  audio_buffer = text_to_audio(response, lang)
347
  # st.audio(audio_buffer, format='audio/mp3')
348
  #st.session_state.audio.append({"role": "Assistant", "audio": audio_buffer})
349
+ st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
350
+
351
  assistant_message = {"role": "assistant", "content": response}
352
  else:
353
  with st.spinner('Bot is typing ...'):
 
360
 
361
 
362
 
363
+ audio_buffer = text_to_audio(response, lang)
364
  #st.audio(audio_buffer, format='audio/mp3')
365
  #st.session_state.audio.append({"role": "Assistant", "audio": audio_buffer})
366
+ st.session_state.messages.append({"role": "Assistant", "content": response, "audio_content": audio_buffer})
367
+
368
  assistant_message = {"role": "assistant", "content": response}
369
 
370
  st.write(response)
371
+ st.audio(audio_buffer, format='audio/wav')
372
 
373
 
374
+ #except Exception as e:
375
 
376
+ # "Sorry, there was a problem. A corrupted file or;"
377
+ # if use_google:
378
+ # "Google PaLM AI only take English Data and Questions. Or the AI could not find the answer in your provided document."
379
+ #elif use_openai:
380
+ # "Please check your OpenAI API key"
381
 
 
 
382
 
383
 
384
  hide_streamlit_style = """