Docfile commited on
Commit
0cc968c
·
verified ·
1 Parent(s): 6366f15

Upload 2 files

Browse files
Files changed (1) hide show
  1. app.py +49 -13
app.py CHANGED
@@ -65,6 +65,7 @@ async def generate_speech(text, selected_voice):
65
 
66
  # Configure the voice settings
67
  speech_config = genai.types.SpeechConfig(
 
68
  voice_config=genai.types.VoiceConfig(
69
  prebuilt_voice_config=genai.types.PrebuiltVoiceConfig(
70
  voice_name=selected_voice
@@ -170,12 +171,30 @@ async def generate_podcast_route():
170
  "message": "Démarrage de la génération..."
171
  }
172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  # Generate audio for each character
174
  characters = scenario.get('characters', [])
175
  total_characters = len(characters)
176
  update_progress(0, total_characters, f"Préparation du podcast avec {total_characters} personnages...")
177
 
178
  audio_segments = []
 
179
 
180
  for idx, character in enumerate(characters):
181
  character_name = character.get('name', 'Unknown')
@@ -189,8 +208,13 @@ async def generate_podcast_route():
189
  voice = 'Kore'
190
 
191
  # Generate speech for this character
192
- audio_file = await generate_speech(text, voice)
193
- audio_segments.append(audio_file)
 
 
 
 
 
194
 
195
  update_progress(total_characters, total_characters, "Assemblage des segments audio...")
196
 
@@ -205,21 +229,33 @@ async def generate_podcast_route():
205
 
206
  # Export the combined audio
207
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as output_file:
208
- output_filename = output_file.name
209
- combined.export(output_filename, format="wav")
210
 
211
- update_progress(total_characters + 1, total_characters + 1, "Podcast généré avec succès!")
212
 
213
- return jsonify({
214
- "status": "success",
215
- "message": "Podcast generated successfully",
216
- "audioUrl": f"/audio/{os.path.basename(output_filename)}"
217
- })
218
-
219
  except Exception as e:
220
- logger.error(f"Error in generate-podcast endpoint: {str(e)}")
221
  update_progress(0, 0, f"Erreur: {str(e)}")
222
- return jsonify({"error": str(e)}), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
 
224
  @app.route('/generation-progress')
225
  def get_generation_progress():
 
65
 
66
  # Configure the voice settings
67
  speech_config = genai.types.SpeechConfig(
68
+ language_code=language_code,
69
  voice_config=genai.types.VoiceConfig(
70
  prebuilt_voice_config=genai.types.PrebuiltVoiceConfig(
71
  voice_name=selected_voice
 
171
  "message": "Démarrage de la génération..."
172
  }
173
 
174
+ # Start the background task for podcast generation
175
+ # We'll return immediately and let the client poll for progress
176
+ asyncio.create_task(generate_podcast_background(scenario))
177
+
178
+ return jsonify({
179
+ "status": "started",
180
+ "message": "Génération du podcast commencée. Suivez la progression sur l'interface."
181
+ })
182
+
183
+ except Exception as e:
184
+ logger.error(f"Error in generate-podcast endpoint: {str(e)}")
185
+ update_progress(0, 0, f"Erreur: {str(e)}")
186
+ return jsonify({"error": str(e)}), 500
187
+
188
+ async def generate_podcast_background(scenario):
189
+ """Generate a podcast in the background."""
190
+ try:
191
  # Generate audio for each character
192
  characters = scenario.get('characters', [])
193
  total_characters = len(characters)
194
  update_progress(0, total_characters, f"Préparation du podcast avec {total_characters} personnages...")
195
 
196
  audio_segments = []
197
+ podcast_filename = None
198
 
199
  for idx, character in enumerate(characters):
200
  character_name = character.get('name', 'Unknown')
 
208
  voice = 'Kore'
209
 
210
  # Generate speech for this character
211
+ try:
212
+ audio_file = await generate_speech(text, voice)
213
+ audio_segments.append(audio_file)
214
+ except Exception as e:
215
+ logger.error(f"Error generating speech for {character_name}: {str(e)}")
216
+ update_progress(0, 0, f"Erreur lors de la génération pour {character_name}: {str(e)}")
217
+ return
218
 
219
  update_progress(total_characters, total_characters, "Assemblage des segments audio...")
220
 
 
229
 
230
  # Export the combined audio
231
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as output_file:
232
+ podcast_filename = output_file.name
233
+ combined.export(podcast_filename, format="wav")
234
 
235
+ update_progress(total_characters + 1, total_characters + 1, f"Podcast généré avec succès! audio:{os.path.basename(podcast_filename)}")
236
 
 
 
 
 
 
 
237
  except Exception as e:
238
+ logger.error(f"Error in podcast background task: {str(e)}")
239
  update_progress(0, 0, f"Erreur: {str(e)}")
240
+
241
+ @app.route('/podcast-status')
242
+ def podcast_status():
243
+ """Get the current status of the podcast generation."""
244
+ global generation_progress
245
+
246
+ # If status is complete and contains an audioUrl in the message, extract it
247
+ if generation_progress["status"] == "complete" and "audio:" in generation_progress["message"]:
248
+ message_parts = generation_progress["message"].split("audio:")
249
+ if len(message_parts) > 1:
250
+ audio_filename = message_parts[1].strip()
251
+ return jsonify({
252
+ "status": "complete",
253
+ "message": message_parts[0].strip(),
254
+ "audioUrl": f"/audio/{audio_filename}"
255
+ })
256
+
257
+ # Otherwise just return the current progress
258
+ return jsonify(generation_progress)
259
 
260
  @app.route('/generation-progress')
261
  def get_generation_progress():