qqwjq1981 commited on
Commit
51a7dcd
·
verified ·
1 Parent(s): fc6897b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -1
app.py CHANGED
@@ -27,6 +27,9 @@ from textblob import TextBlob
27
  import whisper
28
  import time
29
 
 
 
 
30
  def silence(duration, fps=44100):
31
  """
32
  Returns a silent AudioClip of the specified duration.
@@ -324,7 +327,47 @@ def generate_voiceover(translated_json, language, output_audio_path):
324
  tts.save(output_audio_path)
325
  except Exception as e:
326
  raise ValueError(f"Error generating voiceover: {e}")
327
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
  def upload_and_manage(file, target_language, mode="transcription"):
329
  if file is None:
330
  logger.info("No file uploaded. Please upload a video/audio file.")
 
27
  import whisper
28
  import time
29
 
30
+ # Set your OpenAI API key
31
+ openai.api_key = "YOUR_OPENAI_API_KEY"
32
+
33
  def silence(duration, fps=44100):
34
  """
35
  Returns a silent AudioClip of the specified duration.
 
327
  tts.save(output_audio_path)
328
  except Exception as e:
329
  raise ValueError(f"Error generating voiceover: {e}")
330
+
331
+ def generate_voiceover_OpenAI(translated_json, language, output_audio_path):
332
+ """
333
+ Generate voiceover from translated text for a given language using OpenAI TTS API.
334
+ """
335
+ # Concatenate translated text into a single string
336
+ full_text = " ".join(entry["translated"] for entry in translated_json)
337
+
338
+ # Define the voice based on the language (for now, use 'alloy' as default)
339
+ voice = "alloy" # Adjust based on language if needed
340
+
341
+ # Define the model (use tts-1 for real-time applications)
342
+ model = "tts-1"
343
+
344
+ max_retries = 3
345
+ retry_count = 0
346
+
347
+ while retry_count < max_retries:
348
+ try:
349
+ # Create the speech using OpenAI TTS API
350
+ response = openai.Audio.speech.create(
351
+ model=model,
352
+ voice=voice,
353
+ input=full_text
354
+ )
355
+
356
+ # Save the audio to the specified path
357
+ with open(output_audio_path, "wb") as f:
358
+ f.write(response['audio'])
359
+
360
+ logging.info(f"Voiceover generated successfully for {output_audio_path}")
361
+ break
362
+
363
+ except Exception as e:
364
+ retry_count += 1
365
+ logging.error(f"Error generating voiceover (retry {retry_count}/{max_retries}): {e}")
366
+ time.sleep(5) # Wait 5 seconds before retrying
367
+
368
+ if retry_count == max_retries:
369
+ raise ValueError(f"Failed to generate voiceover after {max_retries} retries.")
370
+
371
  def upload_and_manage(file, target_language, mode="transcription"):
372
  if file is None:
373
  logger.info("No file uploaded. Please upload a video/audio file.")