Spaces:
Paused
Paused
Commit ·
8b4831d
1
Parent(s): c91fab0
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
+
from subprocess import run
|
| 4 |
+
from faster_whisper import WhisperModel
|
| 5 |
+
import soundfile as sf
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
# Inicialize o modelo NLLB
|
| 9 |
+
tokenizer = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M")
|
| 10 |
+
model = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M")
|
| 11 |
+
|
| 12 |
+
# Inicialize o modelo Whisper
|
| 13 |
+
model_size = "large-v2"
|
| 14 |
+
whisper_model = WhisperModel(model_size, device="cuda", compute_type="float16")
|
| 15 |
+
|
| 16 |
+
def process_video(radio, video, target_language, use_wav2lip):
|
| 17 |
+
# 1. Extraia o áudio do vídeo usando FFMPEG
|
| 18 |
+
run(["ffmpeg", "-i", video.name, "audio.wav"])
|
| 19 |
+
|
| 20 |
+
# 2. Transcrição usando Whisper
|
| 21 |
+
segments, _ = whisper_model.transcribe("audio.wav")
|
| 22 |
+
transcript = " ".join([segment.text for segment in segments])
|
| 23 |
+
|
| 24 |
+
# 3. Tradução usando NLLB
|
| 25 |
+
inputs = tokenizer(transcript, return_tensors="pt")
|
| 26 |
+
lang_code = {"English": "eng_Latn", "Spanish": "spa_Latn", "French": "fra_Latn"} # Adicione mais idiomas conforme necessário
|
| 27 |
+
translated_tokens = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id[lang_code[target_language]], max_length=100)
|
| 28 |
+
translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
| 29 |
+
|
| 30 |
+
# 4. Queimar a legenda traduzida no vídeo
|
| 31 |
+
with open("subtitle.srt", "w") as f:
|
| 32 |
+
f.write("1\n00:00:00,000 --> 00:00:10,000\n" + translated_text) # Este é um exemplo simples. Você pode dividir o texto em várias partes e ajustar os tempos.
|
| 33 |
+
|
| 34 |
+
run(["ffmpeg", "-i", video.name, "-vf", "subtitles=subtitle.srt", "output_video.mp4"])
|
| 35 |
+
|
| 36 |
+
return "output_video.mp4"
|
| 37 |
+
|
| 38 |
+
# Interface Gradio
|
| 39 |
+
video = gr.Video()
|
| 40 |
+
radio = gr.Radio(["Upload", "Record"], value="Upload", show_label=False)
|
| 41 |
+
iface = gr.Interface(
|
| 42 |
+
fn=process_video,
|
| 43 |
+
inputs=[
|
| 44 |
+
radio,
|
| 45 |
+
video,
|
| 46 |
+
gr.Dropdown(choices=["English", "Spanish", "French"], label="Target Language for Dubbing", value="Spanish"),
|
| 47 |
+
gr.Checkbox(label="Video has a close-up face. Use Wav2lip.", value=False)
|
| 48 |
+
],
|
| 49 |
+
outputs=gr.Video(),
|
| 50 |
+
live=False
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
iface.launch()
|