bluenevus commited on
Commit
38f82cf
·
verified ·
1 Parent(s): a727789

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -3,6 +3,7 @@ import google.generativeai as genai
3
  import numpy as np
4
  import edge_tts
5
  import asyncio
 
6
 
7
  # Set up logging
8
  import logging
@@ -17,19 +18,14 @@ def generate_podcast_script(api_key, content, duration):
17
  model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
18
 
19
  prompt = f"""
20
- Create a podcast script for two people (Host 1 and Host 2) discussing the following content:
21
  {content}
22
 
23
  The podcast should last approximately {duration}. Include natural speech patterns,
24
  humor, and occasional off-topic chit-chat. Use speech fillers like "um", "ah",
25
  "yes", "I see", "Ok now". Vary the emotional tone.
26
 
27
- Format the script as follows, with each line representing a single speaker's dialogue:
28
- Host 1: Dialog
29
- Host 2: Dialog
30
- Host 1: Dialog
31
- Host 2: Dialog
32
-
33
  Do not include any other text, markdown, or formatting. Only include the alternating dialogue lines.
34
  Ensure the conversation flows naturally and stays relevant to the topic.
35
  """
@@ -38,19 +34,21 @@ def generate_podcast_script(api_key, content, duration):
38
 
39
  async def text_to_speech(text, voice):
40
  communicate = edge_tts.Communicate(text, voice)
41
- audio_data = await communicate.to_wav()
42
- return audio_data
 
 
 
 
43
 
44
  async def render_podcast(api_key, script, voice1, voice2):
45
  lines = script.split('\n')
46
  audio_segments = []
47
 
48
- for line in lines:
49
- if line.startswith("Host 1:"):
50
- audio = await text_to_speech(line[7:], voice1)
51
- audio_segments.append(audio)
52
- elif line.startswith("Host 2:"):
53
- audio = await text_to_speech(line[7:], voice2)
54
  audio_segments.append(audio)
55
 
56
  if not audio_segments:
 
3
  import numpy as np
4
  import edge_tts
5
  import asyncio
6
+ import io
7
 
8
  # Set up logging
9
  import logging
 
18
  model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
19
 
20
  prompt = f"""
21
+ Create a podcast script for two people discussing the following content:
22
  {content}
23
 
24
  The podcast should last approximately {duration}. Include natural speech patterns,
25
  humor, and occasional off-topic chit-chat. Use speech fillers like "um", "ah",
26
  "yes", "I see", "Ok now". Vary the emotional tone.
27
 
28
+ Format the script as alternating lines of dialogue without speaker labels.
 
 
 
 
 
29
  Do not include any other text, markdown, or formatting. Only include the alternating dialogue lines.
30
  Ensure the conversation flows naturally and stays relevant to the topic.
31
  """
 
34
 
35
  async def text_to_speech(text, voice):
36
  communicate = edge_tts.Communicate(text, voice)
37
+ audio = io.BytesIO()
38
+ async for chunk in communicate.stream():
39
+ if chunk["type"] == "audio":
40
+ audio.write(chunk["data"])
41
+ audio.seek(0)
42
+ return audio.read()
43
 
44
  async def render_podcast(api_key, script, voice1, voice2):
45
  lines = script.split('\n')
46
  audio_segments = []
47
 
48
+ for i, line in enumerate(lines):
49
+ if line.strip(): # Skip empty lines
50
+ voice = voice1 if i % 2 == 0 else voice2
51
+ audio = await text_to_speech(line, voice)
 
 
52
  audio_segments.append(audio)
53
 
54
  if not audio_segments: