DrishtiSharma commited on
Commit
f177b28
·
verified ·
1 Parent(s): 493df63

Update interim.py

Browse files
Files changed (1) hide show
  1. interim.py +108 -1
interim.py CHANGED
@@ -12,6 +12,7 @@ from langchain_community.document_loaders import (
12
  CSVLoader
13
  )
14
  from datetime import datetime
 
15
  import pytz
16
 
17
  from langchain.chains import ConversationalRetrievalChain
@@ -126,6 +127,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:
@@ -188,4 +281,18 @@ if st.session_state.rag_system.qa_chain:
188
  st.chat_message("user").write(question)
189
  st.chat_message("assistant").write(answer)
190
  else:
191
- st.info("Please process documents before asking questions.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  CSVLoader
13
  )
14
  from datetime import datetime
15
+ from pydub import AudioSegment
16
  import pytz
17
 
18
  from langchain.chains import ConversationalRetrievalChain
 
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
+
135
+ if not self.api_key:
136
+ return "Please set the OpenAI API key in the environment variables.", None
137
+
138
+ try:
139
+ client = OpenAI(api_key=self.api_key)
140
+
141
+ # Generate podcast script
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.
150
+ 4. Ensure the script has a clear opening and closing.
151
+ Document Summary: {self.document_summary}"""}
152
+ ],
153
+ temperature=0.7
154
+ )
155
+
156
+ script = script_response.choices[0].message.content
157
+ if not script:
158
+ return "Error: Failed to generate podcast script.", None
159
+
160
+ # Convert script to audio
161
+ final_audio = AudioSegment.empty()
162
+ is_first_speaker = True
163
+
164
+ lines = [line.strip() for line in script.split("\n") if line.strip()]
165
+ for line in lines:
166
+ if ":" not in line:
167
+ continue
168
+
169
+ speaker, text = line.split(":", 1)
170
+ if not text.strip():
171
+ continue
172
+
173
+ try:
174
+ voice = "nova" if is_first_speaker else "onyx"
175
+ audio_response = client.audio.speech.create(
176
+ model="tts-1",
177
+ voice=voice,
178
+ input=text.strip()
179
+ )
180
+
181
+ temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
182
+ audio_response.stream_to_file(temp_audio_file.name)
183
+
184
+ segment = AudioSegment.from_file(temp_audio_file.name)
185
+ final_audio += segment
186
+ final_audio += AudioSegment.silent(duration=300)
187
+
188
+ is_first_speaker = not is_first_speaker
189
+ except Exception as e:
190
+ print(f"Error generating audio for line: {text}")
191
+ print(f"Details: {e}")
192
+ continue
193
+
194
+ if len(final_audio) == 0:
195
+ return "Error: No audio could be generated.", None
196
+
197
+ output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3").name
198
+ final_audio.export(output_file, format="mp3")
199
+ return script, output_file
200
+
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:
 
281
  st.chat_message("user").write(question)
282
  st.chat_message("assistant").write(answer)
283
  else:
284
+ st.info("Please process documents before asking questions.")
285
+
286
+ # Podcast Generation
287
+ st.subheader("Step 3: Generate Podcast")
288
+ if st.session_state.rag_system.document_summary:
289
+ if st.button("Generate Podcast"):
290
+ script, audio_path = st.session_state.rag_system.create_podcast()
291
+ if audio_path:
292
+ st.text_area("Generated Podcast Script", script, height=200)
293
+ st.audio(audio_path, format="audio/mp3")
294
+ st.success("Podcast generated successfully! You can listen to it above.")
295
+ else:
296
+ st.error(script)
297
+ else:
298
+ st.info("Please process documents to generate a podcast.")