Spaces:
Running
Running
Update app.py
Browse files
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":
|
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":
|
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,15 +201,32 @@ class DocumentRAG:
|
|
201 |
except Exception as e:
|
202 |
return f"Error generating podcast: {str(e)}", None
|
203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
|
205 |
-
def handle_query(self, question, history
|
206 |
-
"""Handle user queries
|
207 |
if not self.qa_chain:
|
208 |
return history + [("System", "Please process the documents first.")]
|
209 |
try:
|
210 |
-
preface =
|
211 |
-
Instruction: Respond in
|
212 |
-
If you cannot provide an answer, say
|
213 |
"""
|
214 |
query = f"{preface}\nQuery: {question}"
|
215 |
|
@@ -291,22 +308,15 @@ summary_language = st.radio(
|
|
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:
|
298 |
st.text_area("Document Summary", st.session_state.rag_system.document_summary, height=200)
|
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 |
-
|
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,25 +330,14 @@ qa_language = st.radio(
|
|
320 |
)
|
321 |
|
322 |
if st.session_state.rag_system.qa_chain:
|
323 |
-
|
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 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
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,26 +351,15 @@ podcast_language = st.radio(
|
|
352 |
horizontal=True,
|
353 |
key="podcast_language"
|
354 |
)
|
355 |
-
|
356 |
-
|
357 |
if st.button("Generate Podcast"):
|
358 |
-
|
359 |
-
|
360 |
-
|
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.
|
376 |
else:
|
377 |
-
st.info("Please process documents
|
|
|
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 |
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 |
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 |
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 |
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 |
key="summary_language"
|
309 |
)
|
310 |
|
|
|
|
|
|
|
311 |
if st.session_state.rag_system.document_summary:
|
312 |
st.text_area("Document Summary", st.session_state.rag_system.document_summary, height=200)
|
313 |
else:
|
314 |
st.info("Please process documents first to generate summaries.")
|
315 |
if st.button("Generate Summary"):
|
|
|
|
|
316 |
st.session_state.rag_system.document_summary = st.session_state.rag_system.generate_summary(
|
317 |
+
st.session_state.rag_system.document_summary,
|
318 |
summary_language
|
319 |
)
|
|
|
|
|
320 |
|
321 |
# Step 3: Ask Questions
|
322 |
st.subheader("Step 3: Ask Questions")
|
|
|
330 |
)
|
331 |
|
332 |
if st.session_state.rag_system.qa_chain:
|
333 |
+
history = []
|
|
|
|
|
|
|
334 |
user_question = st.text_input("Ask a question:")
|
|
|
335 |
if st.button("Submit Question"):
|
336 |
+
# Handle the user query
|
337 |
+
history = st.session_state.rag_system.handle_query(user_question, history, qa_language)
|
338 |
+
for question, answer in history:
|
339 |
+
st.chat_message("user").write(question)
|
340 |
+
st.chat_message("assistant").write(answer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
341 |
else:
|
342 |
st.info("Please process documents first to enable Q&A.")
|
343 |
|
|
|
351 |
horizontal=True,
|
352 |
key="podcast_language"
|
353 |
)
|
354 |
+
|
355 |
+
if st.session_state.rag_system.document_summary:
|
356 |
if st.button("Generate Podcast"):
|
357 |
+
script, audio_path = st.session_state.rag_system.create_podcast(podcast_language)
|
358 |
+
if audio_path:
|
359 |
+
st.text_area("Generated Podcast Script", script, height=200)
|
360 |
+
st.audio(audio_path, format="audio/mp3")
|
361 |
+
st.success("Podcast generated successfully! You can listen to it above.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
362 |
else:
|
363 |
+
st.error(script)
|
364 |
else:
|
365 |
+
st.info("Please process documents and generate summaries before creating a podcast.")
|