DrishtiSharma commited on
Commit
752b448
·
verified ·
1 Parent(s): 8291b7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -46
app.py CHANGED
@@ -109,8 +109,8 @@ class DocumentRAG:
109
  except Exception as e:
110
  return f"Error processing documents: {str(e)}"
111
 
112
- def generate_summary(self, text):
113
- """Generate a summary of the provided text."""
114
  if not self.api_key:
115
  return "API Key not set. Please set it in the environment variables."
116
  try:
@@ -118,7 +118,7 @@ class DocumentRAG:
118
  response = client.chat.completions.create(
119
  model="gpt-4",
120
  messages=[
121
- {"role": "system", "content": "Summarize the document content concisely and provide 3-5 key points for discussion."},
122
  {"role": "user", "content": text[:4000]}
123
  ],
124
  temperature=0.3
@@ -127,8 +127,8 @@ class DocumentRAG:
127
  except Exception as e:
128
  return f"Error generating summary: {str(e)}"
129
 
130
- def create_podcast(self):
131
- """Generate a podcast script and audio based on the document summary."""
132
  if not self.document_summary:
133
  return "Please process documents before generating a podcast.", None
134
 
@@ -142,8 +142,8 @@ class DocumentRAG:
142
  script_response = client.chat.completions.create(
143
  model="gpt-4",
144
  messages=[
145
- {"role": "system", "content": "You are a professional podcast producer. Create a natural dialogue based on the provided document summary."},
146
- {"role": "user", "content": f"""Based on the following document summary, create a 1-2 minute podcast script:
147
  1. Clearly label the dialogue as 'Host 1:' and 'Host 2:'
148
  2. Keep the content engaging and insightful.
149
  3. Use conversational language suitable for a podcast.
@@ -201,32 +201,15 @@ class DocumentRAG:
201
  except Exception as e:
202
  return f"Error generating podcast: {str(e)}", None
203
 
204
- def generate_summary(self, text):
205
- """Generate a summary of the provided text."""
206
- if not self.api_key:
207
- return "API Key not set. Please set it in the environment variables."
208
- try:
209
- client = OpenAI(api_key=self.api_key)
210
- response = client.chat.completions.create(
211
- model="gpt-4",
212
- messages=[
213
- {"role": "system", "content": "Summarize the document content concisely and provide 3-5 key points for discussion."},
214
- {"role": "user", "content": text[:4000]}
215
- ],
216
- temperature=0.3
217
- )
218
- return response.choices[0].message.content
219
- except Exception as e:
220
- return f"Error generating summary: {str(e)}"
221
 
222
- def handle_query(self, question, history):
223
- """Handle user queries."""
224
  if not self.qa_chain:
225
  return history + [("System", "Please process the documents first.")]
226
  try:
227
- preface = """
228
- Instruction: Respond in English. Be professional and concise, keeping the response under 300 words.
229
- If you cannot provide an answer, say: "I am not sure about this question. Please try asking something else."
230
  """
231
  query = f"{preface}\nQuery: {question}"
232
 
@@ -308,7 +291,7 @@ summary_language = st.radio(
308
  key="summary_language"
309
  )
310
 
311
- # Confirm selected language
312
  st.write(f"Selected Summary Language: {summary_language}")
313
 
314
  if st.session_state.rag_system.document_summary:
@@ -316,10 +299,14 @@ if st.session_state.rag_system.document_summary:
316
  else:
317
  st.info("Please process documents first to generate summaries.")
318
  if st.button("Generate Summary"):
 
 
319
  st.session_state.rag_system.document_summary = st.session_state.rag_system.generate_summary(
320
- st.session_state.rag_system.document_summary,
321
  summary_language
322
  )
 
 
323
 
324
  # Step 3: Ask Questions
325
  st.subheader("Step 3: Ask Questions")
@@ -333,14 +320,25 @@ qa_language = st.radio(
333
  )
334
 
335
  if st.session_state.rag_system.qa_chain:
336
- history = []
 
 
 
337
  user_question = st.text_input("Ask a question:")
 
338
  if st.button("Submit Question"):
339
- # Handle the user query
340
- history = st.session_state.rag_system.handle_query(user_question, history, qa_language)
341
- for question, answer in history:
342
- st.chat_message("user").write(question)
343
- st.chat_message("assistant").write(answer)
 
 
 
 
 
 
 
344
  else:
345
  st.info("Please process documents first to enable Q&A.")
346
 
@@ -354,15 +352,26 @@ podcast_language = st.radio(
354
  horizontal=True,
355
  key="podcast_language"
356
  )
357
-
358
- if st.session_state.rag_system.document_summary:
359
  if st.button("Generate Podcast"):
360
- script, audio_path = st.session_state.rag_system.create_podcast(podcast_language)
361
- if audio_path:
362
- st.text_area("Generated Podcast Script", script, height=200)
363
- st.audio(audio_path, format="audio/mp3")
364
- st.success("Podcast generated successfully! You can listen to it above.")
 
 
 
 
 
 
 
 
 
 
 
365
  else:
366
- st.error(script)
367
  else:
368
- st.info("Please process documents and generate summaries before creating a podcast.")
 
109
  except Exception as e:
110
  return f"Error processing documents: {str(e)}"
111
 
112
+ def generate_summary(self, text, language="English"):
113
+ """Generate a summary of the provided text in the specified language."""
114
  if not self.api_key:
115
  return "API Key not set. Please set it in the environment variables."
116
  try:
 
118
  response = client.chat.completions.create(
119
  model="gpt-4",
120
  messages=[
121
+ {"role": "system", "content": f"Summarize the document content concisely in {language}. Provide 3-5 key points for discussion."},
122
  {"role": "user", "content": text[:4000]}
123
  ],
124
  temperature=0.3
 
127
  except Exception as e:
128
  return f"Error generating summary: {str(e)}"
129
 
130
+ def create_podcast(self, language="English"):
131
+ """Generate a podcast script and audio based on the document summary in the specified language."""
132
  if not self.document_summary:
133
  return "Please process documents before generating a podcast.", None
134
 
 
142
  script_response = client.chat.completions.create(
143
  model="gpt-4",
144
  messages=[
145
+ {"role": "system", "content": f"You are a professional podcast producer. Create a natural dialogue in {language} based on the provided document summary."},
146
+ {"role": "user", "content": f"""Based on the following document summary, create a 1-2 minute podcast script in {language}:
147
  1. Clearly label the dialogue as 'Host 1:' and 'Host 2:'
148
  2. Keep the content engaging and insightful.
149
  3. Use conversational language suitable for a podcast.
 
201
  except Exception as e:
202
  return f"Error generating podcast: {str(e)}", None
203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
 
205
+ def handle_query(self, question, history, language="English"):
206
+ """Handle user queries in the specified language."""
207
  if not self.qa_chain:
208
  return history + [("System", "Please process the documents first.")]
209
  try:
210
+ preface = f"""
211
+ Instruction: Respond in {language}. Be professional and concise, keeping the response under 300 words.
212
+ If you cannot provide an answer, say in the specified language: "I am not sure about this question. Please try asking something else."
213
  """
214
  query = f"{preface}\nQuery: {question}"
215
 
 
291
  key="summary_language"
292
  )
293
 
294
+ # Debugging: Confirm selected language
295
  st.write(f"Selected Summary Language: {summary_language}")
296
 
297
  if st.session_state.rag_system.document_summary:
 
299
  else:
300
  st.info("Please process documents first to generate summaries.")
301
  if st.button("Generate Summary"):
302
+ # Combine all document text
303
+ raw_text = " ".join([doc.page_content for doc in st.session_state.rag_system.document_store.documents])
304
  st.session_state.rag_system.document_summary = st.session_state.rag_system.generate_summary(
305
+ raw_text, # Pass the raw document content
306
  summary_language
307
  )
308
+ # Debugging: Confirm selected language
309
+ st.write(f"Selected Summary Language: {summary_language}")
310
 
311
  # Step 3: Ask Questions
312
  st.subheader("Step 3: Ask Questions")
 
320
  )
321
 
322
  if st.session_state.rag_system.qa_chain:
323
+ # Retain chat history in session state
324
+ if "chat_history" not in st.session_state:
325
+ st.session_state.chat_history = []
326
+
327
  user_question = st.text_input("Ask a question:")
328
+
329
  if st.button("Submit Question"):
330
+ if user_question.strip():
331
+ # Handle the user query in the selected language
332
+ st.session_state.chat_history = st.session_state.rag_system.handle_query(
333
+ user_question,
334
+ st.session_state.chat_history,
335
+ qa_language
336
+ )
337
+ for question, answer in st.session_state.chat_history:
338
+ st.chat_message("user").write(question)
339
+ st.chat_message("assistant").write(answer)
340
+ else:
341
+ st.warning("Please enter a valid question.")
342
  else:
343
  st.info("Please process documents first to enable Q&A.")
344
 
 
352
  horizontal=True,
353
  key="podcast_language"
354
  )
355
+ if st.session_state.rag_system.document_store:
356
+ podcast_language = st.session_state.get("podcast_language", "English") # Get the selected podcast language
357
  if st.button("Generate Podcast"):
358
+ # Fetch the raw document content (combine all chunks if needed)
359
+ raw_document_text = " ".join(
360
+ [doc.page_content for doc in st.session_state.rag_system.document_store.documents]
361
+ )
362
+
363
+ if raw_document_text:
364
+ # Generate the podcast script and audio
365
+ script, audio_path = st.session_state.rag_system.create_podcast(raw_document_text, language=podcast_language)
366
+
367
+ # Check if audio generation was successful
368
+ if audio_path:
369
+ st.text_area("Generated Podcast Script", script, height=200)
370
+ st.audio(audio_path, format="audio/mp3")
371
+ st.success("Podcast generated successfully! You can listen to it above.")
372
+ else:
373
+ st.error(f"Failed to generate podcast: {script}")
374
  else:
375
+ st.warning("No document content found. Please process documents before generating a podcast.")
376
  else:
377
+ st.info("Please process documents before attempting to generate a podcast.")