Update app.py
Browse files
app.py
CHANGED
|
@@ -141,7 +141,7 @@ class DocumentRAG:
|
|
| 141 |
except Exception as e:
|
| 142 |
return f"Error generating summary: {str(e)}"
|
| 143 |
|
| 144 |
-
def create_podcast(self, language):
|
| 145 |
"""Generate a podcast script and audio based on doc summary in the specified language."""
|
| 146 |
if not self.document_summary:
|
| 147 |
return "Please process documents before generating a podcast.", None
|
|
@@ -164,14 +164,11 @@ class DocumentRAG:
|
|
| 164 |
3. Discuss Limitations of the research work.
|
| 165 |
4. Present the Conclusion
|
| 166 |
5. Mention Future Work
|
| 167 |
-
|
| 168 |
Clearly label the dialogue as 'Host 1:' and 'Host 2:'. Maintain a tone that is engaging, conversational,
|
| 169 |
-
and insightful
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
},
|
| 173 |
-
{"role": "user", "content": f"""
|
| 174 |
-
Document Summary: {self.document_summary}"""}
|
| 175 |
],
|
| 176 |
temperature=0.7
|
| 177 |
)
|
|
@@ -194,21 +191,43 @@ class DocumentRAG:
|
|
| 194 |
continue
|
| 195 |
|
| 196 |
try:
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
segment = AudioSegment.from_file(temp_audio_file.name)
|
| 208 |
final_audio += segment
|
| 209 |
final_audio += AudioSegment.silent(duration=300)
|
| 210 |
-
|
| 211 |
is_first_speaker = not is_first_speaker
|
|
|
|
| 212 |
except Exception as e:
|
| 213 |
print(f"Error generating audio for line: {text}")
|
| 214 |
print(f"Details: {e}")
|
|
@@ -224,6 +243,7 @@ class DocumentRAG:
|
|
| 224 |
except Exception as e:
|
| 225 |
return f"Error generating podcast: {str(e)}", None
|
| 226 |
|
|
|
|
| 227 |
def handle_query(self, question, history, language):
|
| 228 |
"""Handle user queries in the specified language."""
|
| 229 |
if not self.qa_chain:
|
|
@@ -349,15 +369,32 @@ podcast_language = st.radio(
|
|
| 349 |
key="podcast_language"
|
| 350 |
)
|
| 351 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
if st.session_state.rag_system.document_summary:
|
| 353 |
if st.button("Generate Podcast"):
|
| 354 |
with st.spinner("Generating podcast, please wait..."):
|
| 355 |
-
script, audio_path = st.session_state.rag_system.create_podcast(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
if audio_path:
|
| 357 |
st.text_area("Generated Podcast Script", script, height=200)
|
| 358 |
st.audio(audio_path, format="audio/mp3")
|
| 359 |
|
| 360 |
-
# Add this block to enable download
|
| 361 |
with open(audio_path, "rb") as audio_file:
|
| 362 |
st.download_button(
|
| 363 |
label="Download Podcast (.mp3)",
|
|
@@ -366,8 +403,6 @@ if st.session_state.rag_system.document_summary:
|
|
| 366 |
mime="audio/mpeg"
|
| 367 |
)
|
| 368 |
|
| 369 |
-
|
| 370 |
-
|
| 371 |
st.success("Podcast generated successfully! You can listen to it above.")
|
| 372 |
else:
|
| 373 |
st.error(script)
|
|
|
|
| 141 |
except Exception as e:
|
| 142 |
return f"Error generating summary: {str(e)}"
|
| 143 |
|
| 144 |
+
def create_podcast(self, language, tts_engine, elevenlabs_api_key=None):
|
| 145 |
"""Generate a podcast script and audio based on doc summary in the specified language."""
|
| 146 |
if not self.document_summary:
|
| 147 |
return "Please process documents before generating a podcast.", None
|
|
|
|
| 164 |
3. Discuss Limitations of the research work.
|
| 165 |
4. Present the Conclusion
|
| 166 |
5. Mention Future Work
|
| 167 |
+
|
| 168 |
Clearly label the dialogue as 'Host 1:' and 'Host 2:'. Maintain a tone that is engaging, conversational,
|
| 169 |
+
and insightful. Include a clear, thoughtful closing.
|
| 170 |
+
"""},
|
| 171 |
+
{"role": "user", "content": f"Document Summary: {self.document_summary}"}
|
|
|
|
|
|
|
|
|
|
| 172 |
],
|
| 173 |
temperature=0.7
|
| 174 |
)
|
|
|
|
| 191 |
continue
|
| 192 |
|
| 193 |
try:
|
| 194 |
+
if tts_engine == "OpenAI":
|
| 195 |
+
voice = "nova" if is_first_speaker else "onyx"
|
| 196 |
+
audio_response = client.audio.speech.create(
|
| 197 |
+
model="tts-1",
|
| 198 |
+
voice=voice,
|
| 199 |
+
input=text.strip()
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 203 |
+
audio_response.stream_to_file(temp_audio_file.name)
|
| 204 |
+
|
| 205 |
+
elif tts_engine == "ElevenLabs":
|
| 206 |
+
if not elevenlabs_api_key:
|
| 207 |
+
return "Please provide your ElevenLabs API key.", None
|
| 208 |
+
|
| 209 |
+
from elevenlabs import generate, set_api_key
|
| 210 |
+
set_api_key(elevenlabs_api_key)
|
| 211 |
+
|
| 212 |
+
voice_name = "Bella" if is_first_speaker else "Adam"
|
| 213 |
+
audio_bytes = generate(
|
| 214 |
+
text[:250],
|
| 215 |
+
voice=voice_name,
|
| 216 |
+
model="eleven_multilingual_v2"
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 220 |
+
temp_audio_file.write(audio_bytes)
|
| 221 |
+
temp_audio_file.flush()
|
| 222 |
+
|
| 223 |
+
else:
|
| 224 |
+
return "Invalid TTS engine selected.", None
|
| 225 |
|
| 226 |
segment = AudioSegment.from_file(temp_audio_file.name)
|
| 227 |
final_audio += segment
|
| 228 |
final_audio += AudioSegment.silent(duration=300)
|
|
|
|
| 229 |
is_first_speaker = not is_first_speaker
|
| 230 |
+
|
| 231 |
except Exception as e:
|
| 232 |
print(f"Error generating audio for line: {text}")
|
| 233 |
print(f"Details: {e}")
|
|
|
|
| 243 |
except Exception as e:
|
| 244 |
return f"Error generating podcast: {str(e)}", None
|
| 245 |
|
| 246 |
+
|
| 247 |
def handle_query(self, question, history, language):
|
| 248 |
"""Handle user queries in the specified language."""
|
| 249 |
if not self.qa_chain:
|
|
|
|
| 369 |
key="podcast_language"
|
| 370 |
)
|
| 371 |
|
| 372 |
+
# TTS Engine Selector
|
| 373 |
+
st.write("Select TTS Engine:")
|
| 374 |
+
tts_engine = st.radio(
|
| 375 |
+
"Choose voice generation engine:",
|
| 376 |
+
options=["OpenAI", "ElevenLabs"],
|
| 377 |
+
horizontal=True,
|
| 378 |
+
key="tts_engine"
|
| 379 |
+
)
|
| 380 |
+
|
| 381 |
+
# Optional ElevenLabs API Key Input
|
| 382 |
+
elevenlabs_api_key = None
|
| 383 |
+
if tts_engine == "ElevenLabs":
|
| 384 |
+
elevenlabs_api_key = st.text_input("Enter your ElevenLabs API Key:", type="password")
|
| 385 |
+
|
| 386 |
if st.session_state.rag_system.document_summary:
|
| 387 |
if st.button("Generate Podcast"):
|
| 388 |
with st.spinner("Generating podcast, please wait..."):
|
| 389 |
+
script, audio_path = st.session_state.rag_system.create_podcast(
|
| 390 |
+
podcast_language,
|
| 391 |
+
tts_engine,
|
| 392 |
+
elevenlabs_api_key=elevenlabs_api_key
|
| 393 |
+
)
|
| 394 |
if audio_path:
|
| 395 |
st.text_area("Generated Podcast Script", script, height=200)
|
| 396 |
st.audio(audio_path, format="audio/mp3")
|
| 397 |
|
|
|
|
| 398 |
with open(audio_path, "rb") as audio_file:
|
| 399 |
st.download_button(
|
| 400 |
label="Download Podcast (.mp3)",
|
|
|
|
| 403 |
mime="audio/mpeg"
|
| 404 |
)
|
| 405 |
|
|
|
|
|
|
|
| 406 |
st.success("Podcast generated successfully! You can listen to it above.")
|
| 407 |
else:
|
| 408 |
st.error(script)
|