nihalaninihal commited on
Commit
34bfe42
·
verified ·
1 Parent(s): 3e4f04a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -109
app.py CHANGED
@@ -56,7 +56,7 @@ def convert_to_wav(audio_data: bytes, mime_type: str) -> bytes:
56
  return header + audio_data
57
 
58
 
59
- def parse_audio_mime_type(mime_type: str) -> dict[str, int | None]:
60
  """Parses bits per sample and rate from an audio MIME type string."""
61
  bits_per_sample = 16
62
  rate = 24000
@@ -79,10 +79,11 @@ def parse_audio_mime_type(mime_type: str) -> dict[str, int | None]:
79
  return {"bits_per_sample": bits_per_sample, "rate": rate}
80
 
81
 
82
- def fetch_web_content(url, progress=gr.Progress()):
83
  """Fetch and analyze web content using Gemini with tools."""
84
  try:
85
- progress(0.1, desc="Initializing Gemini client...")
 
86
  logger.info("Initializing Gemini client...")
87
 
88
  if not GEMINI_API_KEY:
@@ -90,10 +91,11 @@ def fetch_web_content(url, progress=gr.Progress()):
90
 
91
  client = genai.Client(api_key=GEMINI_API_KEY)
92
 
93
- progress(0.2, desc="Fetching web content...")
 
94
  logger.info(f"Fetching content from URL: {url}")
95
 
96
- model = "gemini-2.5-flash-preview-04-17"
97
  contents = [
98
  types.Content(
99
  role="user",
@@ -118,7 +120,8 @@ def fetch_web_content(url, progress=gr.Progress()):
118
  response_mime_type="text/plain",
119
  )
120
 
121
- progress(0.4, desc="Analyzing content with AI...")
 
122
  logger.info("Generating content with Gemini...")
123
 
124
  content_text = ""
@@ -130,7 +133,8 @@ def fetch_web_content(url, progress=gr.Progress()):
130
  if chunk.text:
131
  content_text += chunk.text
132
 
133
- progress(0.6, desc="Content analysis complete!")
 
134
  logger.info(f"Content generation complete. Length: {len(content_text)} characters")
135
  return content_text
136
 
@@ -139,10 +143,11 @@ def fetch_web_content(url, progress=gr.Progress()):
139
  raise e
140
 
141
 
142
- def generate_podcast_from_content(content_text, speaker1_name="Anna Chope", speaker2_name="Adam Chan", progress=gr.Progress()):
143
  """Generate audio podcast from text content."""
144
  try:
145
- progress(0.7, desc="Generating podcast audio...")
 
146
  logger.info("Starting audio generation...")
147
 
148
  if not GEMINI_API_KEY:
@@ -150,7 +155,7 @@ def generate_podcast_from_content(content_text, speaker1_name="Anna Chope", spea
150
 
151
  client = genai.Client(api_key=GEMINI_API_KEY)
152
 
153
- model = "gemini-2.5-flash-preview-tts"
154
 
155
  podcast_prompt = f"""Please read aloud the following content in a natural podcast interview style with two distinct speakers.
156
  Make it sound conversational and engaging:
@@ -197,7 +202,8 @@ def generate_podcast_from_content(content_text, speaker1_name="Anna Chope", spea
197
  ),
198
  )
199
 
200
- progress(0.8, desc="Converting to audio...")
 
201
  logger.info("Generating audio stream...")
202
 
203
  # Create temporary file
@@ -235,7 +241,8 @@ def generate_podcast_from_content(content_text, speaker1_name="Anna Chope", spea
235
  # For simplicity, just use the first chunk (you might want to concatenate them)
236
  final_audio = audio_chunks[0]
237
  save_binary_file(temp_file.name, final_audio)
238
- progress(1.0, desc="Podcast generated successfully!")
 
239
  logger.info(f"Audio file saved: {temp_file.name}")
240
  return temp_file.name
241
  else:
@@ -246,10 +253,11 @@ def generate_podcast_from_content(content_text, speaker1_name="Anna Chope", spea
246
  raise e
247
 
248
 
249
- def generate_web_podcast(url, speaker1_name, speaker2_name, progress=gr.Progress()):
250
  """Main function to fetch web content and generate podcast."""
251
  try:
252
- progress(0.0, desc="Starting podcast generation...")
 
253
  logger.info(f"Starting podcast generation for URL: {url}")
254
 
255
  # Validate inputs
@@ -285,106 +293,102 @@ def generate_web_podcast(url, speaker1_name, speaker2_name, progress=gr.Progress
285
 
286
  # Create Gradio interface
287
  def create_interface():
288
- try:
289
- with gr.Blocks(
290
- title="🎙️ Web-to-Podcast Generator",
291
- theme=gr.themes.Soft(),
292
- analytics_enabled=False
293
- ) as demo:
294
- gr.Markdown("""
295
- # 🎙️ Web-to-Podcast Generator
296
-
297
- Transform any website into an engaging podcast conversation between two AI hosts!
298
-
299
- Simply paste a URL and let AI create a natural dialogue discussing the content.
300
- """)
301
-
302
- with gr.Row():
303
- with gr.Column(scale=2):
304
- url_input = gr.Textbox(
305
- label="Website URL",
306
- placeholder="https://example.com",
307
- info="Enter the URL of the website you want to convert to a podcast",
 
 
 
 
 
 
 
 
308
  lines=1
309
  )
310
-
311
- with gr.Row():
312
- speaker1_input = gr.Textbox(
313
- label="Host 1 Name",
314
- value="Anna Chope",
315
- info="Name of the first podcast host",
316
- lines=1
317
- )
318
- speaker2_input = gr.Textbox(
319
- label="Host 2 Name",
320
- value="Adam Chan",
321
- info="Name of the second podcast host",
322
- lines=1
323
- )
324
-
325
- generate_btn = gr.Button("🎙️ Generate Podcast", variant="primary", size="lg")
326
-
327
- with gr.Column(scale=1):
328
- gr.Markdown("""
329
- ### Instructions:
330
- 1. Enter a website URL
331
- 2. Customize host names (optional)
332
- 3. Click "Generate Podcast"
333
- 4. Wait for the AI to analyze content and create audio
334
- 5. Download your podcast!
335
-
336
- ### Examples:
337
- - News articles
338
- - Blog posts
339
- - Product pages
340
- - Documentation
341
- - Research papers
342
- """)
343
-
344
- with gr.Row():
345
- status_output = gr.Textbox(label="Status", interactive=False, lines=2)
346
-
347
- with gr.Row():
348
- audio_output = gr.Audio(label="Generated Podcast", type="filepath")
349
-
350
- with gr.Accordion("📝 Generated Script Preview", open=False):
351
- script_output = gr.Textbox(
352
- label="Podcast Script",
353
- lines=10,
354
- interactive=False,
355
- info="Preview of the conversation script generated from the website content"
356
- )
357
-
358
- # Event handlers
359
- generate_btn.click(
360
- fn=generate_web_podcast,
361
- inputs=[url_input, speaker1_input, speaker2_input],
362
- outputs=[audio_output, status_output, script_output],
363
- show_progress=True
364
- )
365
-
366
- # Examples
367
- gr.Examples(
368
- examples=[
369
- ["https://github.com/weaviate/weaviate", "Anna", "Adam"],
370
- ["https://huggingface.co/blog", "Sarah", "Mike"],
371
- ["https://openai.com/blog", "Emma", "John"],
372
- ],
373
- inputs=[url_input, speaker1_input, speaker2_input],
374
  )
375
-
376
- gr.Markdown("""
377
- ---
378
- **Note:** API key is now directly embedded in the code for convenience.
379
-
380
- The generated podcast will feature two AI voices having a natural conversation about the website content.
381
- """)
382
 
383
- return demo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
384
 
385
- except Exception as e:
386
- logger.error(f"Error creating interface: {e}")
387
- raise e
388
 
389
 
390
  if __name__ == "__main__":
 
56
  return header + audio_data
57
 
58
 
59
+ def parse_audio_mime_type(mime_type: str) -> dict:
60
  """Parses bits per sample and rate from an audio MIME type string."""
61
  bits_per_sample = 16
62
  rate = 24000
 
79
  return {"bits_per_sample": bits_per_sample, "rate": rate}
80
 
81
 
82
+ def fetch_web_content(url, progress=None):
83
  """Fetch and analyze web content using Gemini with tools."""
84
  try:
85
+ if progress:
86
+ progress(0.1, desc="Initializing Gemini client...")
87
  logger.info("Initializing Gemini client...")
88
 
89
  if not GEMINI_API_KEY:
 
91
 
92
  client = genai.Client(api_key=GEMINI_API_KEY)
93
 
94
+ if progress:
95
+ progress(0.2, desc="Fetching web content...")
96
  logger.info(f"Fetching content from URL: {url}")
97
 
98
+ model = "gemini-2.0-flash-exp" # Updated model name
99
  contents = [
100
  types.Content(
101
  role="user",
 
120
  response_mime_type="text/plain",
121
  )
122
 
123
+ if progress:
124
+ progress(0.4, desc="Analyzing content with AI...")
125
  logger.info("Generating content with Gemini...")
126
 
127
  content_text = ""
 
133
  if chunk.text:
134
  content_text += chunk.text
135
 
136
+ if progress:
137
+ progress(0.6, desc="Content analysis complete!")
138
  logger.info(f"Content generation complete. Length: {len(content_text)} characters")
139
  return content_text
140
 
 
143
  raise e
144
 
145
 
146
+ def generate_podcast_from_content(content_text, speaker1_name="Anna Chope", speaker2_name="Adam Chan", progress=None):
147
  """Generate audio podcast from text content."""
148
  try:
149
+ if progress:
150
+ progress(0.7, desc="Generating podcast audio...")
151
  logger.info("Starting audio generation...")
152
 
153
  if not GEMINI_API_KEY:
 
155
 
156
  client = genai.Client(api_key=GEMINI_API_KEY)
157
 
158
+ model = "gemini-2.0-flash-exp" # Updated model name
159
 
160
  podcast_prompt = f"""Please read aloud the following content in a natural podcast interview style with two distinct speakers.
161
  Make it sound conversational and engaging:
 
202
  ),
203
  )
204
 
205
+ if progress:
206
+ progress(0.8, desc="Converting to audio...")
207
  logger.info("Generating audio stream...")
208
 
209
  # Create temporary file
 
241
  # For simplicity, just use the first chunk (you might want to concatenate them)
242
  final_audio = audio_chunks[0]
243
  save_binary_file(temp_file.name, final_audio)
244
+ if progress:
245
+ progress(1.0, desc="Podcast generated successfully!")
246
  logger.info(f"Audio file saved: {temp_file.name}")
247
  return temp_file.name
248
  else:
 
253
  raise e
254
 
255
 
256
+ def generate_web_podcast(url, speaker1_name, speaker2_name, progress=None):
257
  """Main function to fetch web content and generate podcast."""
258
  try:
259
+ if progress:
260
+ progress(0.0, desc="Starting podcast generation...")
261
  logger.info(f"Starting podcast generation for URL: {url}")
262
 
263
  # Validate inputs
 
293
 
294
  # Create Gradio interface
295
  def create_interface():
296
+ """Create and return the Gradio interface."""
297
+ with gr.Blocks(
298
+ title="🎙️ Web-to-Podcast Generator",
299
+ theme=gr.themes.Soft(),
300
+ analytics_enabled=False
301
+ ) as demo:
302
+ gr.Markdown("""
303
+ # 🎙️ Web-to-Podcast Generator
304
+
305
+ Transform any website into an engaging podcast conversation between two AI hosts!
306
+
307
+ Simply paste a URL and let AI create a natural dialogue discussing the content.
308
+ """)
309
+
310
+ with gr.Row():
311
+ with gr.Column(scale=2):
312
+ url_input = gr.Textbox(
313
+ label="Website URL",
314
+ placeholder="https://example.com",
315
+ info="Enter the URL of the website you want to convert to a podcast",
316
+ lines=1
317
+ )
318
+
319
+ with gr.Row():
320
+ speaker1_input = gr.Textbox(
321
+ label="Host 1 Name",
322
+ value="Anna Chope",
323
+ info="Name of the first podcast host",
324
  lines=1
325
  )
326
+ speaker2_input = gr.Textbox(
327
+ label="Host 2 Name",
328
+ value="Adam Chan",
329
+ info="Name of the second podcast host",
330
+ lines=1
331
+ )
332
+
333
+ generate_btn = gr.Button("🎙️ Generate Podcast", variant="primary", size="lg")
334
+
335
+ with gr.Column(scale=1):
336
+ gr.Markdown("""
337
+ ### Instructions:
338
+ 1. Enter a website URL
339
+ 2. Customize host names (optional)
340
+ 3. Click "Generate Podcast"
341
+ 4. Wait for the AI to analyze content and create audio
342
+ 5. Download your podcast!
343
+
344
+ ### Examples:
345
+ - News articles
346
+ - Blog posts
347
+ - Product pages
348
+ - Documentation
349
+ - Research papers
350
+ """)
351
+
352
+ with gr.Row():
353
+ status_output = gr.Textbox(label="Status", interactive=False, lines=2)
354
+
355
+ with gr.Row():
356
+ audio_output = gr.Audio(label="Generated Podcast", type="filepath")
357
+
358
+ with gr.Accordion("📝 Generated Script Preview", open=False):
359
+ script_output = gr.Textbox(
360
+ label="Podcast Script",
361
+ lines=10,
362
+ interactive=False,
363
+ info="Preview of the conversation script generated from the website content"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
364
  )
 
 
 
 
 
 
 
365
 
366
+ # Event handlers
367
+ generate_btn.click(
368
+ fn=generate_web_podcast,
369
+ inputs=[url_input, speaker1_input, speaker2_input],
370
+ outputs=[audio_output, status_output, script_output],
371
+ show_progress=True
372
+ )
373
+
374
+ # Examples
375
+ gr.Examples(
376
+ examples=[
377
+ ["https://github.com/weaviate/weaviate", "Anna", "Adam"],
378
+ ["https://huggingface.co/blog", "Sarah", "Mike"],
379
+ ["https://openai.com/blog", "Emma", "John"],
380
+ ],
381
+ inputs=[url_input, speaker1_input, speaker2_input],
382
+ )
383
+
384
+ gr.Markdown("""
385
+ ---
386
+ **Note:** API key is now directly embedded in the code for convenience.
387
+
388
+ The generated podcast will feature two AI voices having a natural conversation about the website content.
389
+ """)
390
 
391
+ return demo
 
 
392
 
393
 
394
  if __name__ == "__main__":