RXTIME commited on
Commit
11296df
·
verified ·
1 Parent(s): 24977fc

Create App.py

Browse files
Files changed (1) hide show
  1. App.py +75 -0
App.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import whisper
3
+ import torch
4
+ from transformers import MarianMTModel, MarianTokenizer
5
+ import ffmpeg
6
+ import requests
7
+ import os
8
+ import tempfile
9
+
10
+ # Configuração do Hugging Face e ElevenLabs
11
+ HF_MODEL = "Helsinki-NLP/opus-mt-mul-en"
12
+ ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY") # Defina essa variável no Hugging Face Spaces
13
+
14
+ # Carregar modelos
15
+ whisper_model = whisper.load_model("small")
16
+ translator = MarianMTModel.from_pretrained(HF_MODEL)
17
+ tokenizer = MarianTokenizer.from_pretrained(HF_MODEL)
18
+
19
+ # Função para transcrever áudio
20
+ def transcribe_audio(video_path: str) -> str:
21
+ result = whisper_model.transcribe(video_path)
22
+ return result["text"]
23
+
24
+ # Função para traduzir texto
25
+ def translate_text(text: str, target_lang="pt") -> str:
26
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
27
+ translated_tokens = translator.generate(**inputs)
28
+ return tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
29
+
30
+ # Função para gerar áudio em português (usando ElevenLabs)
31
+ def synthesize_speech(text: str, voice="Antônio") -> str:
32
+ url = "https://api.elevenlabs.io/v1/text-to-speech"
33
+ headers = {"Authorization": f"Bearer {ELEVENLABS_API_KEY}"}
34
+ response = requests.post(url, json={"text": text, "voice": voice}, headers=headers)
35
+
36
+ temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
37
+ with open(temp_audio.name, "wb") as f:
38
+ f.write(response.content)
39
+ return temp_audio.name
40
+
41
+ # Função para substituir o áudio no vídeo
42
+ def replace_audio(video_path: str, new_audio_path: str) -> str:
43
+ output_path = video_path.replace(".mp4", "_translated.mp4")
44
+
45
+ ffmpeg.input(video_path).output(
46
+ output_path,
47
+ audio=new_audio_path,
48
+ codec="copy"
49
+ ).run(overwrite_output=True)
50
+
51
+ return output_path
52
+
53
+ # Pipeline completo
54
+ def process_video(video_file):
55
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
56
+ temp_video.write(video_file.read())
57
+ video_path = temp_video.name
58
+
59
+ transcript = transcribe_audio(video_path)
60
+ translated_text = translate_text(transcript)
61
+ new_audio_path = synthesize_speech(translated_text)
62
+ output_video_path = replace_audio(video_path, new_audio_path)
63
+
64
+ return output_video_path
65
+
66
+ # Interface Gradio
67
+ iface = gr.Interface(
68
+ fn=process_video,
69
+ inputs=gr.File(label="Carregar Vídeo (MP4)"),
70
+ outputs=gr.File(label="Baixar Vídeo Traduzido"),
71
+ title="Tradutor de Vídeos para Português",
72
+ description="Faz a transcrição, tradução e substituição de áudio em vídeos automaticamente."
73
+ )
74
+
75
+ iface.launch()