Update app.py
Browse files
app.py
CHANGED
@@ -26,6 +26,13 @@ import logging
|
|
26 |
from textblob import TextBlob
|
27 |
import whisper
|
28 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
def silence(duration, fps=44100):
|
31 |
"""
|
@@ -240,7 +247,7 @@ def process_entry(entry, i, video_width, video_height, add_voiceover, target_lan
|
|
240 |
audio_segment = None
|
241 |
if add_voiceover:
|
242 |
segment_audio_path = f"segment_{i}_voiceover.wav"
|
243 |
-
|
244 |
audio_clip = AudioFileClip(segment_audio_path)
|
245 |
# Get and log all methods in AudioFileClip
|
246 |
logger.info("Methods in AudioFileClip:")
|
@@ -324,7 +331,46 @@ 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.")
|
|
|
26 |
from textblob import TextBlob
|
27 |
import whisper
|
28 |
import time
|
29 |
+
import os
|
30 |
+
import openai
|
31 |
+
from openai import OpenAI
|
32 |
+
|
33 |
+
client = OpenAI(
|
34 |
+
api_key= os.environ.get("openAI_api_key"), # This is the default and can be omitted
|
35 |
+
)
|
36 |
|
37 |
def silence(duration, fps=44100):
|
38 |
"""
|
|
|
247 |
audio_segment = None
|
248 |
if add_voiceover:
|
249 |
segment_audio_path = f"segment_{i}_voiceover.wav"
|
250 |
+
generate_voiceover_OpenAI([entry], target_language, segment_audio_path)
|
251 |
audio_clip = AudioFileClip(segment_audio_path)
|
252 |
# Get and log all methods in AudioFileClip
|
253 |
logger.info("Methods in AudioFileClip:")
|
|
|
331 |
tts.save(output_audio_path)
|
332 |
except Exception as e:
|
333 |
raise ValueError(f"Error generating voiceover: {e}")
|
334 |
+
|
335 |
+
def generate_voiceover_OpenAI(translated_json, language, output_audio_path):
|
336 |
+
"""
|
337 |
+
Generate voiceover from translated text for a given language using OpenAI TTS API.
|
338 |
+
"""
|
339 |
+
# Concatenate translated text into a single string
|
340 |
+
full_text = " ".join(entry["translated"] for entry in translated_json)
|
341 |
+
|
342 |
+
# Define the voice based on the language (for now, use 'alloy' as default)
|
343 |
+
voice = "alloy" # Adjust based on language if needed
|
344 |
+
|
345 |
+
# Define the model (use tts-1 for real-time applications)
|
346 |
+
model = "tts-1"
|
347 |
+
|
348 |
+
max_retries = 3
|
349 |
+
retry_count = 0
|
350 |
+
|
351 |
+
while retry_count < max_retries:
|
352 |
+
try:
|
353 |
+
# Create the speech using OpenAI TTS API
|
354 |
+
response = client.audio.speech.create(
|
355 |
+
model=model,
|
356 |
+
voice=voice,
|
357 |
+
input=full_text
|
358 |
+
)
|
359 |
+
# Save the audio to the specified path
|
360 |
+
with open(output_audio_path, 'wb') as f:
|
361 |
+
for chunk in response.iter_bytes():
|
362 |
+
f.write(chunk)
|
363 |
+
logging.info(f"Voiceover generated successfully for {output_audio_path}")
|
364 |
+
break
|
365 |
+
|
366 |
+
except Exception as e:
|
367 |
+
retry_count += 1
|
368 |
+
logging.error(f"Error generating voiceover (retry {retry_count}/{max_retries}): {e}")
|
369 |
+
time.sleep(5) # Wait 5 seconds before retrying
|
370 |
+
|
371 |
+
if retry_count == max_retries:
|
372 |
+
raise ValueError(f"Failed to generate voiceover after {max_retries} retries.")
|
373 |
+
|
374 |
def upload_and_manage(file, target_language, mode="transcription"):
|
375 |
if file is None:
|
376 |
logger.info("No file uploaded. Please upload a video/audio file.")
|