Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -126,6 +126,98 @@ class DocumentRAG:
|
|
126 |
except Exception as e:
|
127 |
return f"Error generating summary: {str(e)}"
|
128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
def handle_query(self, question, history):
|
130 |
"""Handle user queries."""
|
131 |
if not self.qa_chain:
|
@@ -189,3 +281,17 @@ if st.session_state.rag_system.qa_chain:
|
|
189 |
st.chat_message("assistant").write(answer)
|
190 |
else:
|
191 |
st.info("Please process documents before asking questions.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
except Exception as e:
|
127 |
return f"Error generating summary: {str(e)}"
|
128 |
|
129 |
+
def create_podcast(self):
|
130 |
+
"""Generate a podcast script and audio based on the document summary."""
|
131 |
+
if not self.document_summary:
|
132 |
+
return "Please process documents before generating a podcast.", None
|
133 |
+
|
134 |
+
if not self.api_key:
|
135 |
+
return "Please set the OpenAI API key in the environment variables.", None
|
136 |
+
|
137 |
+
try:
|
138 |
+
client = OpenAI(api_key=self.api_key)
|
139 |
+
|
140 |
+
# Generate podcast script
|
141 |
+
script_response = client.chat.completions.create(
|
142 |
+
model="gpt-4",
|
143 |
+
messages=[
|
144 |
+
{"role": "system", "content": "You are a professional podcast producer. Create a natural dialogue based on the provided document summary."},
|
145 |
+
{"role": "user", "content": f"""Based on the following document summary, create a 1-2 minute podcast script:
|
146 |
+
1. Clearly label the dialogue as 'Host 1:' and 'Host 2:'
|
147 |
+
2. Keep the content engaging and insightful.
|
148 |
+
3. Use conversational language suitable for a podcast.
|
149 |
+
4. Ensure the script has a clear opening and closing.
|
150 |
+
Document Summary: {self.document_summary}"""}
|
151 |
+
],
|
152 |
+
temperature=0.7
|
153 |
+
)
|
154 |
+
|
155 |
+
script = script_response.choices[0].message.content
|
156 |
+
if not script:
|
157 |
+
return "Error: Failed to generate podcast script.", None
|
158 |
+
|
159 |
+
# Convert script to audio
|
160 |
+
final_audio = AudioSegment.empty()
|
161 |
+
is_first_speaker = True
|
162 |
+
|
163 |
+
lines = [line.strip() for line in script.split("\n") if line.strip()]
|
164 |
+
for line in lines:
|
165 |
+
if ":" not in line:
|
166 |
+
continue
|
167 |
+
|
168 |
+
speaker, text = line.split(":", 1)
|
169 |
+
if not text.strip():
|
170 |
+
continue
|
171 |
+
|
172 |
+
try:
|
173 |
+
voice = "nova" if is_first_speaker else "onyx"
|
174 |
+
audio_response = client.audio.speech.create(
|
175 |
+
model="tts-1",
|
176 |
+
voice=voice,
|
177 |
+
input=text.strip()
|
178 |
+
)
|
179 |
+
|
180 |
+
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
181 |
+
audio_response.stream_to_file(temp_audio_file.name)
|
182 |
+
|
183 |
+
segment = AudioSegment.from_file(temp_audio_file.name)
|
184 |
+
final_audio += segment
|
185 |
+
final_audio += AudioSegment.silent(duration=300)
|
186 |
+
|
187 |
+
is_first_speaker = not is_first_speaker
|
188 |
+
except Exception as e:
|
189 |
+
print(f"Error generating audio for line: {text}")
|
190 |
+
print(f"Details: {e}")
|
191 |
+
continue
|
192 |
+
|
193 |
+
if len(final_audio) == 0:
|
194 |
+
return "Error: No audio could be generated.", None
|
195 |
+
|
196 |
+
output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3").name
|
197 |
+
final_audio.export(output_file, format="mp3")
|
198 |
+
return script, output_file
|
199 |
+
|
200 |
+
except Exception as e:
|
201 |
+
return f"Error generating podcast: {str(e)}", None
|
202 |
+
|
203 |
+
def generate_summary(self, text):
|
204 |
+
"""Generate a summary of the provided text."""
|
205 |
+
if not self.api_key:
|
206 |
+
return "API Key not set. Please set it in the environment variables."
|
207 |
+
try:
|
208 |
+
client = OpenAI(api_key=self.api_key)
|
209 |
+
response = client.chat.completions.create(
|
210 |
+
model="gpt-4",
|
211 |
+
messages=[
|
212 |
+
{"role": "system", "content": "Summarize the document content concisely and provide 3-5 key points for discussion."},
|
213 |
+
{"role": "user", "content": text[:4000]}
|
214 |
+
],
|
215 |
+
temperature=0.3
|
216 |
+
)
|
217 |
+
return response.choices[0].message.content
|
218 |
+
except Exception as e:
|
219 |
+
return f"Error generating summary: {str(e)}"
|
220 |
+
|
221 |
def handle_query(self, question, history):
|
222 |
"""Handle user queries."""
|
223 |
if not self.qa_chain:
|
|
|
281 |
st.chat_message("assistant").write(answer)
|
282 |
else:
|
283 |
st.info("Please process documents before asking questions.")
|
284 |
+
|
285 |
+
# Podcast Generation
|
286 |
+
st.subheader("Step 3: Generate Podcast")
|
287 |
+
if st.session_state.rag_system.document_summary:
|
288 |
+
if st.button("Generate Podcast"):
|
289 |
+
script, audio_path = st.session_state.rag_system.create_podcast()
|
290 |
+
if audio_path:
|
291 |
+
st.text_area("Generated Podcast Script", script, height=200)
|
292 |
+
st.audio(audio_path, format="audio/mp3")
|
293 |
+
st.success("Podcast generated successfully! You can listen to it above.")
|
294 |
+
else:
|
295 |
+
st.error(script)
|
296 |
+
else:
|
297 |
+
st.info("Please process documents to generate a podcast.")
|