Rausda6 commited on
Commit
eaeaf35
·
verified ·
1 Parent(s): 7faf9f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +560 -142
app.py CHANGED
@@ -2,166 +2,584 @@ import gradio as gr
2
  import random
3
  import time
4
  import os
5
- from elevenlabs import generate, set_api_key, save
6
  from pathlib import Path
7
  from transformers import AutoTokenizer, AutoModelForCausalLM
8
  import torch
9
 
10
- # Load model and tokenizer
11
- tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-alpha")
 
 
 
 
 
 
 
12
  model = AutoModelForCausalLM.from_pretrained(
13
- "HuggingFaceH4/zephyr-7b-alpha",
14
- torch_dtype=torch.float16, # Use float16 for memory efficiency
15
- device_map="auto" # Automatically determine device placement
16
- )
17
-
18
- api_key = os.getenv("ELEVENLABS_API_KEY")
19
- set_api_key(api_key)
20
- podcasts_directory = "podcasts"
21
- os.makedirs(podcasts_directory, exist_ok=True)
22
-
23
- def progress_callback(progress):
24
- if progress:
25
- if isinstance(progress, int):
26
- return progress
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  else:
28
- try:
29
- return float(progress)
30
- except (ValueError, TypeError):
31
- return 0
32
- return 0
33
-
34
- def generate_podcast_intro(podcast_topic, structure, perspective, tone, existing_podcast_info):
35
- with open("prompt_engineered.txt", "r", encoding='utf-8') as file:
36
- prompt_template = file.read()
37
-
38
- prompt = prompt_template.format(
39
- podcast_topic=podcast_topic,
40
- structure=structure,
41
- perspective=perspective,
42
- tone=tone,
43
- existing_podcast_info=existing_podcast_info
44
- )
45
-
46
- return prompt
47
 
48
- # Function to generate content
49
- def generate_content(prompt):
50
- # Format prompt for the Zephyr model (which follows ChatML format)
51
- messages = [{"role": "user", "content": prompt}]
52
-
53
- # Convert to model inputs
54
- encoded_input = tokenizer.apply_chat_template(
55
- messages,
56
- return_tensors="pt"
57
- ).to(model.device)
58
-
59
- # Generate response
60
- with torch.no_grad():
61
- output = model.generate(
62
- encoded_input,
63
- max_new_tokens=1500, # Adjust based on desired output length
64
- do_sample=True,
65
- temperature=0.7, # Adjust for creativity vs determinism
66
- top_p=0.95
67
- )
68
-
69
- # Decode and return only the new tokens (response)
70
- response = tokenizer.decode(output[0][encoded_input.shape[1]:], skip_special_tokens=True)
71
- return response
72
 
73
- def generate_podcast_audio(podcast_script, voice, progress=gr.Progress()):
74
- if not api_key:
75
- return "Error: ElevenLabs API key not set. Please set the ELEVENLABS_API_KEY environment variable."
76
-
77
- try:
78
- audio = generate(
79
- text=podcast_script,
80
- voice=voice,
81
- model="eleven_turbo_v2"
82
- )
83
 
84
- random_id = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz0123456789', k=6))
85
- filename = os.path.join(podcasts_directory, f"podcast_{random_id}.mp3")
86
- save(audio, filename)
87
- return filename
88
- except Exception as e:
89
- return f"Error generating audio: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
- def create_podcast(podcast_topic, structure, perspective, tone, existing_podcast_info, voice_option, progress=gr.Progress()):
92
- progress(0, desc="Generating podcast content...")
93
-
94
- prompt = generate_podcast_intro(podcast_topic, structure, perspective, tone, existing_podcast_info)
95
-
96
- progress(20, desc="Processing with AI...")
97
- podcast_content = generate_content(prompt)
98
-
99
- progress(60, desc="Generating audio...")
100
- audio_file = generate_podcast_audio(podcast_content, voice_option, progress)
101
 
102
- progress(100, desc="Complete!")
103
- return podcast_content, audio_file
104
-
105
- available_voices = [
106
- "Adam", "Antoni", "Arnold", "Bella", "Callum", "Charlie", "Christina", "Clyde", "Daniel", "Dorothy",
107
- "Ella", "Elli", "Emily", "Fin", "Freya", "Gigi", "Giovanni", "Glinda", "Grace", "Harry",
108
- "James", "Jeremy", "Joseph", "Josh", "Knightley", "Liam", "Matilda", "Matthew", "Michael", "Nicole",
109
- "Patrick", "Rachel", "Richard", "Sam", "Sarah", "Serena", "Thomas", "Victor", "Wayne", "Charlotte"
110
- ]
111
-
112
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
113
- gr.Markdown("# 🎙️ AI Podcast Generator")
114
- gr.Markdown("Generate a complete podcast with AI, including audio narration.")
 
 
 
115
 
116
- with gr.Row():
117
- with gr.Column():
118
- podcast_topic = gr.Textbox(
119
- label="Podcast Topic",
120
- placeholder="Enter the main topic of your podcast",
121
- lines=2
122
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
- structure = gr.Radio(
125
- ["Interview Style", "Solo Monologue", "Panel Discussion", "Storytelling", "Educational"],
126
- label="Podcast Structure",
127
- value="Interview Style"
128
- )
 
 
 
 
 
129
 
130
- perspective = gr.Radio(
131
- ["Balanced and Objective", "Personal Opinion", "Expert Analysis", "Conversational", "Investigative"],
132
- label="Perspective",
133
- value="Balanced and Objective"
 
 
 
 
 
 
 
134
  )
 
 
 
 
 
 
 
 
135
 
136
- tone = gr.Radio(
137
- ["Professional", "Casual & Friendly", "Humorous", "Serious & Formal", "Inspirational"],
138
- label="Tone",
139
- value="Professional"
140
- )
 
 
 
 
 
 
 
 
 
 
 
141
 
142
- existing_podcast_info = gr.Textbox(
143
- label="Additional Context (Optional)",
144
- placeholder="Any additional information, context, or specific points you want to include",
145
- lines=3
146
- )
147
 
148
- voice_option = gr.Dropdown(
149
- choices=available_voices,
150
- label="Voice for Audio",
151
- value="Adam"
152
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
 
154
- generate_btn = gr.Button("Generate Podcast", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
- with gr.Column():
157
- podcast_output = gr.Textbox(label="Generated Podcast Script", lines=12)
158
- audio_output = gr.Audio(label="Podcast Audio")
 
 
 
 
 
159
 
160
- generate_btn.click(
161
- create_podcast,
162
- inputs=[podcast_topic, structure, perspective, tone, existing_podcast_info, voice_option],
163
- outputs=[podcast_output, audio_output]
164
- )
165
-
166
- if __name__ == "__main__":
167
- demo.launch()
 
2
  import random
3
  import time
4
  import os
5
+
6
  from pathlib import Path
7
  from transformers import AutoTokenizer, AutoModelForCausalLM
8
  import torch
9
 
10
+ # Define model name clearly
11
+ MODEL_NAME = "HuggingFaceH4/zephyr-7b-alpha"
12
+
13
+ # Device setup
14
+ device = "cuda" if torch.cuda.is_available() else "cpu"
15
+ print(f"Using device: {device}")
16
+
17
+ # Load model and tokenizer (explicit evaluation mode)
18
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
19
  model = AutoModelForCausalLM.from_pretrained(
20
+ MODEL_NAME,
21
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
22
+ device_map="auto"
23
+ ).eval()
24
+
25
+ # Constants
26
+ MAX_FILE_SIZE_MB = 20
27
+ MAX_FILE_SIZE_BYTES = MAX_FILE_SIZE_MB * 1024 * 1024 # Convert MB to bytes
28
+
29
+ class PodcastGenerator:
30
+ def __init__(self):
31
+ pass
32
+
33
+ async def generate_script(self, prompt: str, language: str, api_key: str, file_obj=None, progress=None) -> Dict:
34
+ example = """
35
+ {
36
+ "topic": "AGI",
37
+ "podcast": [
38
+ {
39
+ "speaker": 2,
40
+ "line": "So, AGI, huh? Seems like everyone's talking about it these days."
41
+ },
42
+ {
43
+ "speaker": 1,
44
+ "line": "Yeah, it's definitely having a moment, isn't it?"
45
+ },
46
+ {
47
+ "speaker": 2,
48
+ "line": "It is and for good reason, right? I mean, you've been digging into this stuff, listening to the podcasts and everything. What really stood out to you? What got you hooked?"
49
+ },
50
+ {
51
+ "speaker": 1,
52
+ "line": "Honestly, it's the sheer scale of what AGI could do. We're talking about potentially reshaping well everything."
53
+ },
54
+ {
55
+ "speaker": 2,
56
+ "line": "No kidding, but let's be real. Sometimes it feels like every other headline is either hyping AGI up as this technological utopia or painting it as our inevitable robot overlords."
57
+ },
58
+ {
59
+ "speaker": 1,
60
+ "line": "It's easy to get lost in the noise, for sure."
61
+ },
62
+ {
63
+ "speaker": 2,
64
+ "line": "Exactly. So how about we try to cut through some of that, shall we?"
65
+ },
66
+ {
67
+ "speaker": 1,
68
+ "line": "Sounds like a plan."
69
+ },
70
+ {
71
+ "speaker": 2,
72
+ "line": "Okay, so first things first, AGI, what is it really? And I don't just mean some dictionary definition, we're talking about something way bigger than just a super smart computer, right?"
73
+ },
74
+ {
75
+ "speaker": 1,
76
+ "line": "Right, it's not just about more processing power or better algorithms, it's about a fundamental shift in how we think about intelligence itself."
77
+ },
78
+ {
79
+ "speaker": 2,
80
+ "line": "So like, instead of programming a machine for a specific task, we're talking about creating something that can learn and adapt like we do."
81
+ },
82
+ {
83
+ "speaker": 1,
84
+ "line": "Exactly, think of it this way: Right now, we've got AI that can beat a grandmaster at chess but ask that same AI to, say, write a poem or compose a symphony. No chance."
85
+ },
86
+ {
87
+ "speaker": 2,
88
+ "line": "Okay, I see. So, AGI is about bridging that gap, creating something that can move between those different realms of knowledge seamlessly."
89
+ },
90
+ {
91
+ "speaker": 1,
92
+ "line": "Precisely. It's about replicating that uniquely human ability to learn something new and apply that knowledge in completely different contexts and that's a tall order, let me tell you."
93
+ },
94
+ {
95
+ "speaker": 2,
96
+ "line": "I bet. I mean, think about how much we still don't even understand about our own brains."
97
+ },
98
+ {
99
+ "speaker": 1,
100
+ "line": "That's exactly it. We're essentially trying to reverse-engineer something we don't fully comprehend."
101
+ },
102
+ {
103
+ "speaker": 2,
104
+ "line": "And how are researchers even approaching that? What are some of the big ideas out there?"
105
+ },
106
+ {
107
+ "speaker": 1,
108
+ "line": "Well, there are a few different schools of thought. One is this idea of neuromorphic computing where they're literally trying to build computer chips that mimic the structure and function of the human brain."
109
+ },
110
+ {
111
+ "speaker": 2,
112
+ "line": "Wow, so like actually replicating the physical architecture of the brain. That's wild."
113
+ },
114
+ {
115
+ "speaker": 1,
116
+ "line": "It's pretty mind-blowing stuff and then you've got folks working on something called whole brain emulation."
117
+ },
118
+ {
119
+ "speaker": 2,
120
+ "line": "Okay, and what's that all about?"
121
+ },
122
+ {
123
+ "speaker": 1,
124
+ "line": "The basic idea there is to create a complete digital copy of a human brain down to the last neuron and synapse and run it on a sufficiently powerful computer simulation."
125
+ },
126
+ {
127
+ "speaker": 2,
128
+ "line": "Hold on, a digital copy of an entire brain, that sounds like something straight out of science fiction."
129
+ },
130
+ {
131
+ "speaker": 1,
132
+ "line": "It does, doesn't it? But it gives you an idea of the kind of ambition we're talking about here and the truth is we're still a long way off from truly achieving AGI, no matter which approach you look at."
133
+ },
134
+ {
135
+ "speaker": 2,
136
+ "line": "That makes sense but it's still exciting to think about the possibilities, even if they're a ways off."
137
+ },
138
+ {
139
+ "speaker": 1,
140
+ "line": "Absolutely and those possibilities are what really get people fired up about AGI, right? Yeah."
141
+ },
142
+ {
143
+ "speaker": 2,
144
+ "line": "For sure. In fact, I remember you mentioning something in that podcast about AGI's potential to revolutionize scientific research. Something about supercharging breakthroughs."
145
+ },
146
+ {
147
+ "speaker": 1,
148
+ "line": "Oh, absolutely. Imagine an AI that doesn't just crunch numbers but actually understands scientific data the way a human researcher does. We're talking about potential breakthroughs in everything from medicine and healthcare to material science and climate change."
149
+ },
150
+ {
151
+ "speaker": 2,
152
+ "line": "It's like giving scientists this incredibly powerful new tool to tackle some of the biggest challenges we face."
153
+ },
154
+ {
155
+ "speaker": 1,
156
+ "line": "Exactly, it could be a total game changer."
157
+ },
158
+ {
159
+ "speaker": 2,
160
+ "line": "Okay, but let's be real, every coin has two sides. What about the potential downsides of AGI? Because it can't all be sunshine and roses, right?"
161
+ },
162
+ {
163
+ "speaker": 1,
164
+ "line": "Right, there are definitely valid concerns. Probably the biggest one is the impact on the job market. As AGI gets more sophisticated, there's a real chance it could automate a lot of jobs that are currently done by humans."
165
+ },
166
+ {
167
+ "speaker": 2,
168
+ "line": "So we're not just talking about robots taking over factories but potentially things like, what, legal work, analysis, even creative fields?"
169
+ },
170
+ {
171
+ "speaker": 1,
172
+ "line": "Potentially, yes. And that raises a whole host of questions about what happens to those workers, how we retrain them, how we ensure that the benefits of AGI are shared equitably."
173
+ },
174
+ {
175
+ "speaker": 2,
176
+ "line": "Right, because it's not just about the technology itself, but how we choose to integrate it into society."
177
+ },
178
+ {
179
+ "speaker": 1,
180
+ "line": "Absolutely. We need to be having these conversations now about ethics, about regulation, about how to make sure AGI is developed and deployed responsibly."
181
+ },
182
+ {
183
+ "speaker": 2,
184
+ "line": "So it's less about preventing some kind of sci-fi robot apocalypse and more about making sure we're steering this technology in the right direction from the get-go."
185
+ },
186
+ {
187
+ "speaker": 1,
188
+ "line": "Exactly, AGI has the potential to be incredibly beneficial, but it's not going to magically solve all our problems. It's on us to make sure we're using it for good."
189
+ },
190
+ {
191
+ "speaker": 2,
192
+ "line": "It's like you said earlier, it's about shaping the future of intelligence."
193
+ },
194
+ {
195
+ "speaker": 1,
196
+ "line": "I like that. It really is."
197
+ },
198
+ {
199
+ "speaker": 2,
200
+ "line": "And honestly, that's a responsibility that extends beyond just the researchers and the policymakers."
201
+ },
202
+ {
203
+ "speaker": 1,
204
+ "line": "100%"
205
+ },
206
+ {
207
+ "speaker": 2,
208
+ "line": "So to everyone listening out there I'll leave you with this. As AGI continues to develop, what role do you want to play in shaping its future?"
209
+ },
210
+ {
211
+ "speaker": 1,
212
+ "line": "That's a question worth pondering."
213
+ },
214
+ {
215
+ "speaker": 2,
216
+ "line": "It certainly is and on that note, we'll wrap up this deep dive. Thanks for listening, everyone."
217
+ },
218
+ {
219
+ "speaker": 1,
220
+ "line": "Peace."
221
+ }
222
+ ]
223
+ }
224
+ """
225
+
226
+ if language == "Auto Detect":
227
+ language_instruction = "- The podcast MUST be in the same language as the user input."
228
  else:
229
+ language_instruction = f"- The podcast MUST be in {language} language"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
 
231
+ system_prompt = f"""
232
+ You are a professional podcast generator. Your task is to generate a professional podcast script based on the user input.
233
+ {language_instruction}
234
+ - The podcast should have 2 speakers.
235
+ - The podcast should be long.
236
+ - Do not use names for the speakers.
237
+ - The podcast should be interesting, lively, and engaging, and hook the listener from the start.
238
+ - The input text might be disorganized or unformatted, originating from sources like PDFs or text files. Ignore any formatting inconsistencies or irrelevant details; your task is to distill the essential points, identify key definitions, and highlight intriguing facts that would be suitable for discussion in a podcast.
239
+ - The script must be in JSON format.
240
+ Follow this example structure:
241
+ {example}
242
+ """
243
+ user_prompt = ""
244
+ if prompt and file_obj:
245
+ user_prompt = f"Please generate a podcast script based on the uploaded file following user input:\n{prompt}"
246
+ elif prompt:
247
+ user_prompt = f"Please generate a podcast script based on the following user input:\n{prompt}"
248
+ else:
249
+ user_prompt = "Please generate a podcast script based on the uploaded file."
 
 
 
 
 
250
 
251
+ messages = []
 
 
 
 
 
 
 
 
 
252
 
253
+ # If file is provided, add it to the messages
254
+ if file_obj:
255
+ file_data = await self._read_file_bytes(file_obj)
256
+ mime_type = self._get_mime_type(file_obj.name)
257
+
258
+ messages.append(
259
+ types.Content(
260
+ role="user",
261
+ parts=[
262
+ types.Part.from_bytes(
263
+ data=file_data,
264
+ mime_type=mime_type,
265
+ )
266
+ ],
267
+ )
268
+ )
269
+
270
+ # Add text prompt
271
+ messages.append(
272
+ types.Content(
273
+ role="user",
274
+ parts=[
275
+ types.Part.from_text(text=user_prompt)
276
+ ],
277
+ )
278
+ )
279
+
280
+ try:
281
+ if progress:
282
+ progress(0.3, "Generating podcast script...")
283
+
284
+ # Compose the prompt from your messages
285
+ prompt_text = system_prompt + "\n" + "\n".join([msg["content"] for msg in messages])
286
+
287
+ def hf_generate(prompt_text):
288
+ inputs = tokenizer(prompt_text, return_tensors="pt").to(model.device)
289
+ outputs = model.generate(
290
+ **inputs,
291
+ max_new_tokens=1024,
292
+ do_sample=True,
293
+ temperature=1.0
294
+ )
295
+ text = tokenizer.decode(outputs[0], skip_special_tokens=True)
296
+ return text
297
+
298
+ generated_text = await asyncio.wait_for(
299
+ asyncio.to_thread(hf_generate, prompt_text),
300
+ timeout=60
301
+ )
302
+
303
+ except asyncio.TimeoutError:
304
+ raise Exception("The script generation request timed out. Please try again later.")
305
+ except Exception as e:
306
+ raise Exception(f"Failed to generate podcast script: {e}")
307
+
308
+ print(f"Generated podcast script:\n{generated_text}")
309
+
310
+ if progress:
311
+ progress(0.4, "Script generated successfully!")
312
+
313
+ # Ensure the return type matches the original code (as JSON)
314
+ return json.loads(generated_text)
315
 
 
 
 
 
 
 
 
 
 
 
316
 
317
+ async def _read_file_bytes(self, file_obj) -> bytes:
318
+ """Read file bytes from a file object"""
319
+ # Check file size before reading
320
+ if hasattr(file_obj, 'size'):
321
+ file_size = file_obj.size
322
+ else:
323
+ file_size = os.path.getsize(file_obj.name)
324
+
325
+ if file_size > MAX_FILE_SIZE_BYTES:
326
+ raise Exception(f"File size exceeds the {MAX_FILE_SIZE_MB}MB limit. Please upload a smaller file.")
327
+
328
+ if hasattr(file_obj, 'read'):
329
+ return file_obj.read()
330
+ else:
331
+ async with aiofiles.open(file_obj.name, 'rb') as f:
332
+ return await f.read()
333
 
334
+ def _get_mime_type(self, filename: str) -> str:
335
+ """Determine MIME type based on file extension"""
336
+ ext = os.path.splitext(filename)[1].lower()
337
+ if ext == '.pdf':
338
+ return "application/pdf"
339
+ elif ext == '.txt':
340
+ return "text/plain"
341
+ else:
342
+ # Fallback to the default mime type detector
343
+ mime_type, _ = mimetypes.guess_type(filename)
344
+ return mime_type or "application/octet-stream"
345
+
346
+ async def tts_generate(self, text: str, speaker: int, speaker1: str, speaker2: str) -> str:
347
+ voice = speaker1 if speaker == 1 else speaker2
348
+ speech = edge_tts.Communicate(text, voice)
349
+
350
+ temp_filename = f"temp_{uuid.uuid4()}.wav"
351
+ try:
352
+ # Add timeout to TTS generation
353
+ await asyncio.wait_for(speech.save(temp_filename), timeout=30) # 30 seconds timeout
354
+ return temp_filename
355
+ except asyncio.TimeoutError:
356
+ if os.path.exists(temp_filename):
357
+ os.remove(temp_filename)
358
+ raise Exception("Text-to-speech generation timed out. Please try with a shorter text.")
359
+ except Exception as e:
360
+ if os.path.exists(temp_filename):
361
+ os.remove(temp_filename)
362
+ raise e
363
+
364
+ async def combine_audio_files(self, audio_files: List[str], progress=None) -> str:
365
+ if progress:
366
+ progress(0.9, "Combining audio files...")
367
 
368
+ combined_audio = AudioSegment.empty()
369
+ for audio_file in audio_files:
370
+ combined_audio += AudioSegment.from_file(audio_file)
371
+ os.remove(audio_file) # Clean up temporary files
372
+
373
+ output_filename = f"output_{uuid.uuid4()}.wav"
374
+ combined_audio.export(output_filename, format="wav")
375
+
376
+ if progress:
377
+ progress(1.0, "Podcast generated successfully!")
378
 
379
+ return output_filename
380
+
381
+ async def generate_podcast(self, input_text: str, language: str, speaker1: str, speaker2: str, api_key: str, file_obj=None, progress=None) -> str:
382
+ try:
383
+ if progress:
384
+ progress(0.1, "Starting podcast generation...")
385
+
386
+ # Set overall timeout for the entire process
387
+ return await asyncio.wait_for(
388
+ self._generate_podcast_internal(input_text, language, speaker1, speaker2, api_key, file_obj, progress),
389
+ timeout=600 # 10 minutes total timeout
390
  )
391
+ except asyncio.TimeoutError:
392
+ raise Exception("The podcast generation process timed out. Please try with shorter text or try again later.")
393
+ except Exception as e:
394
+ raise Exception(f"Error generating podcast: {str(e)}")
395
+
396
+ async def _generate_podcast_internal(self, input_text: str, language: str, speaker1: str, speaker2: str, api_key: str, file_obj=None, progress=None) -> str:
397
+ if progress:
398
+ progress(0.2, "Generating podcast script...")
399
 
400
+ podcast_json = await self.generate_script(input_text, language, api_key, file_obj, progress)
401
+
402
+ if progress:
403
+ progress(0.5, "Converting text to speech...")
404
+
405
+ # Process TTS in batches for concurrent processing
406
+ audio_files = []
407
+ total_lines = len(podcast_json['podcast'])
408
+
409
+ # Define batch size to control concurrency
410
+ batch_size = 10 # Adjust based on system resources
411
+
412
+ # Process in batches
413
+ for batch_start in range(0, total_lines, batch_size):
414
+ batch_end = min(batch_start + batch_size, total_lines)
415
+ batch = podcast_json['podcast'][batch_start:batch_end]
416
 
417
+ # Create tasks for concurrent processing
418
+ tts_tasks = []
419
+ for item in batch:
420
+ tts_task = self.tts_generate(item['line'], item['speaker'], speaker1, speaker2)
421
+ tts_tasks.append(tts_task)
422
 
423
+ try:
424
+ # Process batch concurrently
425
+ batch_results = await asyncio.gather(*tts_tasks, return_exceptions=True)
426
+
427
+ # Check for exceptions and handle results
428
+ for i, result in enumerate(batch_results):
429
+ if isinstance(result, Exception):
430
+ # Clean up any files already created
431
+ for file in audio_files:
432
+ if os.path.exists(file):
433
+ os.remove(file)
434
+ raise Exception(f"Error generating speech: {str(result)}")
435
+ else:
436
+ audio_files.append(result)
437
+
438
+ # Update progress
439
+ if progress:
440
+ current_progress = 0.5 + (0.4 * (batch_end / total_lines))
441
+ progress(current_progress, f"Processed {batch_end}/{total_lines} speech segments...")
442
+
443
+ except Exception as e:
444
+ # Clean up any files already created
445
+ for file in audio_files:
446
+ if os.path.exists(file):
447
+ os.remove(file)
448
+ raise Exception(f"Error in batch TTS generation: {str(e)}")
449
+
450
+ combined_audio = await self.combine_audio_files(audio_files, progress)
451
+ return combined_audio
452
+
453
+ async def process_input(input_text: str, input_file, language: str, speaker1: str, speaker2: str, api_key: str = "", progress=None) -> str:
454
+ start_time = time.time()
455
+
456
+ voice_names = {
457
+ "Andrew - English (United States)": "en-US-AndrewMultilingualNeural",
458
+ "Ava - English (United States)": "en-US-AvaMultilingualNeural",
459
+ "Brian - English (United States)": "en-US-BrianMultilingualNeural",
460
+ "Emma - English (United States)": "en-US-EmmaMultilingualNeural",
461
+ "Florian - German (Germany)": "de-DE-FlorianMultilingualNeural",
462
+ "Seraphina - German (Germany)": "de-DE-SeraphinaMultilingualNeural",
463
+ "Remy - French (France)": "fr-FR-RemyMultilingualNeural",
464
+ "Vivienne - French (France)": "fr-FR-VivienneMultilingualNeural"
465
+ }
466
+
467
+ speaker1 = voice_names[speaker1]
468
+ speaker2 = voice_names[speaker2]
469
+
470
+ try:
471
+ if progress:
472
+ progress(0.05, "Processing input...")
473
+
474
+ if not api_key:
475
+ api_key = os.getenv("GENAI_API_KEY")
476
+ if not api_key:
477
+ raise Exception("No API key provided. Please provide a Gemini API key.")
478
+
479
+ podcast_generator = PodcastGenerator()
480
+ podcast = await podcast_generator.generate_podcast(input_text, language, speaker1, speaker2, api_key, input_file, progress)
481
+
482
+ end_time = time.time()
483
+ print(f"Total podcast generation time: {end_time - start_time:.2f} seconds")
484
+ return podcast
485
+
486
+ except Exception as e:
487
+ # Ensure we show a user-friendly error
488
+ error_msg = str(e)
489
+ if "rate limit" in error_msg.lower():
490
+ raise Exception("Rate limit exceeded. Please try again later or use your own API key.")
491
+ elif "timeout" in error_msg.lower():
492
+ raise Exception("The request timed out. This could be due to server load or the length of your input. Please try again with shorter text.")
493
+ else:
494
+ raise Exception(f"Error: {error_msg}")
495
+
496
+ # Gradio UI
497
+ def generate_podcast_gradio(input_text, input_file, language, speaker1, speaker2, api_key, progress=gr.Progress()):
498
+ # Handle the file if uploaded
499
+ file_obj = None
500
+ if input_file is not None:
501
+ file_obj = input_file
502
+
503
+ # Use the progress function from Gradio
504
+ def progress_callback(value, text):
505
+ progress(value, text)
506
+
507
+ # Run the async function in the event loop
508
+ result = asyncio.run(process_input(
509
+ input_text,
510
+ file_obj,
511
+ language,
512
+ speaker1,
513
+ speaker2,
514
+ api_key,
515
+ progress_callback
516
+ ))
517
+
518
+ return result
519
+
520
+ def main():
521
+ # Define language options
522
+ language_options = [
523
+ "Auto Detect",
524
+ "Afrikaans", "Albanian", "Amharic", "Arabic", "Armenian", "Azerbaijani",
525
+ "Bahasa Indonesian", "Bangla", "Basque", "Bengali", "Bosnian", "Bulgarian",
526
+ "Burmese", "Catalan", "Chinese Cantonese", "Chinese Mandarin",
527
+ "Chinese Taiwanese", "Croatian", "Czech", "Danish", "Dutch", "English",
528
+ "Estonian", "Filipino", "Finnish", "French", "Galician", "Georgian",
529
+ "German", "Greek", "Hebrew", "Hindi", "Hungarian", "Icelandic", "Irish",
530
+ "Italian", "Japanese", "Javanese", "Kannada", "Kazakh", "Khmer", "Korean",
531
+ "Lao", "Latvian", "Lithuanian", "Macedonian", "Malay", "Malayalam",
532
+ "Maltese", "Mongolian", "Nepali", "Norwegian Bokmål", "Pashto", "Persian",
533
+ "Polish", "Portuguese", "Romanian", "Russian", "Serbian", "Sinhala",
534
+ "Slovak", "Slovene", "Somali", "Spanish", "Sundanese", "Swahili",
535
+ "Swedish", "Tamil", "Telugu", "Thai", "Turkish", "Ukrainian", "Urdu",
536
+ "Uzbek", "Vietnamese", "Welsh", "Zulu"
537
+ ]
538
+
539
+ # Define voice options
540
+ voice_options = [
541
+ "Andrew - English (United States)",
542
+ "Ava - English (United States)",
543
+ "Brian - English (United States)",
544
+ "Emma - English (United States)",
545
+ "Florian - German (Germany)",
546
+ "Seraphina - German (Germany)",
547
+ "Remy - French (France)",
548
+ "Vivienne - French (France)"
549
+ ]
550
+
551
+ # Create Gradio interface
552
+ with gr.Blocks(title="PodcastGen 🎙️") as demo:
553
+ gr.Markdown("# PodcastGen 🎙️")
554
+ gr.Markdown("Generate a 2-speaker podcast from text input or documents!")
555
+
556
+ with gr.Row():
557
+ with gr.Column(scale=2):
558
+ input_text = gr.Textbox(label="Input Text", lines=10, placeholder="Enter text for podcast generation...")
559
 
560
+ with gr.Column(scale=1):
561
+ input_file = gr.File(label="Or Upload a PDF or TXT file", file_types=[".pdf", ".txt"])
562
+
563
+ with gr.Row():
564
+ with gr.Column():
565
+ api_key = gr.Textbox(label="Your Gemini API Key (Optional)", placeholder="Enter API key here if you're getting rate limited", type="password")
566
+ language = gr.Dropdown(label="Language", choices=language_options, value="Auto Detect")
567
+
568
+ with gr.Column():
569
+ speaker1 = gr.Dropdown(label="Speaker 1 Voice", choices=voice_options, value="Andrew - English (United States)")
570
+ speaker2 = gr.Dropdown(label="Speaker 2 Voice", choices=voice_options, value="Ava - English (United States)")
571
+
572
+ generate_btn = gr.Button("Generate Podcast", variant="primary")
573
 
574
+ with gr.Row():
575
+ output_audio = gr.Audio(label="Generated Podcast", type="filepath", format="wav")
576
+
577
+ generate_btn.click(
578
+ fn=generate_podcast_gradio,
579
+ inputs=[input_text, input_file, language, speaker1, speaker2, api_key],
580
+ outputs=[output_audio]
581
+ )
582
 
583
+ demo.launch()
584
+
585
+ if __name__ == "__main__":