Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -18,50 +18,78 @@ tokenizer = MarianTokenizer.from_pretrained(HF_MODEL)
|
|
18 |
|
19 |
# Função para transcrever áudio
|
20 |
def transcribe_audio(video_path: str) -> str:
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
23 |
|
24 |
# Função para traduzir texto
|
25 |
def translate_text(text: str, target_lang="pt") -> str:
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
29 |
|
30 |
# Função para gerar áudio em português (usando ElevenLabs)
|
31 |
def synthesize_speech(text: str, voice="Antônio") -> str:
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
# Função para substituir o áudio no vídeo
|
42 |
def replace_audio(video_path: str, new_audio_path: str) -> str:
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
|
51 |
-
|
|
|
|
|
52 |
|
53 |
# Pipeline completo
|
54 |
def process_video(video_file):
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
output_video_path = replace_audio(video_path, new_audio_path)
|
63 |
|
64 |
-
|
|
|
|
|
65 |
|
66 |
# Interface Gradio
|
67 |
iface = gr.Interface(
|
|
|
18 |
|
19 |
# Função para transcrever áudio
|
20 |
def transcribe_audio(video_path: str) -> str:
|
21 |
+
try:
|
22 |
+
result = whisper_model.transcribe(video_path)
|
23 |
+
return result["text"]
|
24 |
+
except Exception as e:
|
25 |
+
return f"Erro na transcrição: {str(e)}"
|
26 |
|
27 |
# Função para traduzir texto
|
28 |
def translate_text(text: str, target_lang="pt") -> str:
|
29 |
+
try:
|
30 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
31 |
+
translated_tokens = translator.generate(**inputs)
|
32 |
+
return tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
33 |
+
except Exception as e:
|
34 |
+
return f"Erro na tradução: {str(e)}"
|
35 |
|
36 |
# Função para gerar áudio em português (usando ElevenLabs)
|
37 |
def synthesize_speech(text: str, voice="Antônio") -> str:
|
38 |
+
try:
|
39 |
+
url = "https://api.elevenlabs.io/v1/text-to-speech"
|
40 |
+
headers = {"Authorization": f"Bearer {ELEVENLABS_API_KEY}"}
|
41 |
+
response = requests.post(url, json={"text": text, "voice": voice}, headers=headers)
|
42 |
|
43 |
+
if response.status_code != 200:
|
44 |
+
return f"Erro na geração de voz: {response.text}"
|
45 |
+
|
46 |
+
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
47 |
+
with open(temp_audio.name, "wb") as f:
|
48 |
+
f.write(response.content)
|
49 |
+
return temp_audio.name
|
50 |
+
except Exception as e:
|
51 |
+
return f"Erro na síntese de voz: {str(e)}"
|
52 |
|
53 |
# Função para substituir o áudio no vídeo
|
54 |
def replace_audio(video_path: str, new_audio_path: str) -> str:
|
55 |
+
try:
|
56 |
+
output_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
57 |
+
ffmpeg.input(video_path).output(
|
58 |
+
output_path,
|
59 |
+
audio=new_audio_path,
|
60 |
+
codec="copy"
|
61 |
+
).run(overwrite_output=True)
|
62 |
|
63 |
+
return output_path
|
64 |
+
except Exception as e:
|
65 |
+
return f"Erro na substituição do áudio: {str(e)}"
|
66 |
|
67 |
# Pipeline completo
|
68 |
def process_video(video_file):
|
69 |
+
try:
|
70 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
|
71 |
+
temp_video.write(video_file.read())
|
72 |
+
video_path = temp_video.name
|
73 |
+
|
74 |
+
transcript = transcribe_audio(video_path)
|
75 |
+
if "Erro" in transcript:
|
76 |
+
return transcript
|
77 |
+
|
78 |
+
translated_text = translate_text(transcript)
|
79 |
+
if "Erro" in translated_text:
|
80 |
+
return translated_text
|
81 |
+
|
82 |
+
new_audio_path = synthesize_speech(translated_text)
|
83 |
+
if "Erro" in new_audio_path:
|
84 |
+
return new_audio_path
|
85 |
|
86 |
+
output_video_path = replace_audio(video_path, new_audio_path)
|
87 |
+
if "Erro" in output_video_path:
|
88 |
+
return output_video_path
|
|
|
89 |
|
90 |
+
return output_video_path
|
91 |
+
except Exception as e:
|
92 |
+
return f"Erro inesperado: {str(e)}"
|
93 |
|
94 |
# Interface Gradio
|
95 |
iface = gr.Interface(
|