|
import gradio as gr |
|
import os |
|
import torch |
|
import torchaudio |
|
from transformers import ( |
|
WhisperProcessor, WhisperForConditionalGeneration, |
|
SpeechT5Processor, SpeechT5ForTextToSpeech, |
|
MarianMTModel, MarianTokenizer |
|
) |
|
import ffmpeg |
|
import soundfile as sf |
|
|
|
|
|
UPLOAD_FOLDER = "uploads" |
|
OUTPUT_FOLDER = "outputs" |
|
|
|
if not os.path.exists(UPLOAD_FOLDER): |
|
os.makedirs(UPLOAD_FOLDER) |
|
if not os.path.exists(OUTPUT_FOLDER): |
|
os.makedirs(OUTPUT_FOLDER) |
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
print(f"Usando dispositivo: {device}") |
|
|
|
|
|
whisper_processor = WhisperProcessor.from_pretrained("openai/whisper-small") |
|
whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small").to(device) |
|
|
|
tts_processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts") |
|
tts_model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts").to(device) |
|
|
|
translation_model_name = "Helsinki-NLP/opus-mt-en-pt" |
|
translation_tokenizer = MarianTokenizer.from_pretrained(translation_model_name) |
|
translation_model = MarianMTModel.from_pretrained(translation_model_name).to(device) |
|
|
|
|
|
def transcribe_audio(audio_path): |
|
waveform, sample_rate = torchaudio.load(audio_path) |
|
waveform = waveform.to(device) |
|
inputs = whisper_processor(waveform.squeeze().cpu().numpy(), sampling_rate=sample_rate, return_tensors="pt").to(device) |
|
with torch.no_grad(): |
|
predicted_ids = whisper_model.generate(inputs.input_features) |
|
transcription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True) |
|
return transcription[0] |
|
|
|
def synthesize_speech(text, output_path): |
|
inputs = tts_processor(text, return_tensors="pt").to(device) |
|
with torch.no_grad(): |
|
speech = tts_model.generate_speech(inputs["input_ids"], tts_model.speaker_embeddings) |
|
sf.write(output_path, speech.cpu().numpy(), samplerate=22050) |
|
|
|
def translate_text(text, target_language="pt"): |
|
inputs = translation_tokenizer(text, return_tensors="pt", truncation=True).to(device) |
|
with torch.no_grad(): |
|
translated_ids = translation_model.generate(**inputs) |
|
translated_text = translation_tokenizer.decode(translated_ids[0], skip_special_tokens=True) |
|
return translated_text |
|
|
|
def extract_audio(video_path, audio_path): |
|
ffmpeg.input(video_path).output(audio_path, ac=1, ar="16000").run(overwrite_output=True) |
|
|
|
def replace_audio_in_video(video_path, audio_path, output_path): |
|
video = ffmpeg.input(video_path) |
|
audio = ffmpeg.input(audio_path) |
|
ffmpeg.output(video.video, audio.audio, output_path, vcodec="copy", acodec="aac").run(overwrite_output=True) |
|
|
|
|
|
def translate_video(video): |
|
video_path = os.path.join(UPLOAD_FOLDER, "input_video.mp4") |
|
with open(video_path, "wb") as f: |
|
f.write(video) |
|
|
|
try: |
|
|
|
audio_path = os.path.join(UPLOAD_FOLDER, "audio.wav") |
|
extract_audio(video_path, audio_path) |
|
|
|
|
|
transcribed_text = transcribe_audio(audio_path) |
|
print("Texto transcrito:", transcribed_text) |
|
|
|
|
|
translated_text = translate_text(transcribed_text, target_language="pt") |
|
print("Texto traduzido:", translated_text) |
|
|
|
|
|
synthesized_audio_path = os.path.join(UPLOAD_FOLDER, "synthesized_audio.wav") |
|
synthesize_speech(translated_text, synthesized_audio_path) |
|
|
|
|
|
output_video_path = os.path.join(OUTPUT_FOLDER, "translated_video.mp4") |
|
replace_audio_in_video(video_path, synthesized_audio_path, output_video_path) |
|
|
|
return output_video_path |
|
|
|
except Exception as e: |
|
return f"Erro: {str(e)}" |
|
|
|
|
|
iface = gr.Interface( |
|
fn=translate_video, |
|
inputs=gr.Video(), |
|
outputs=gr.Video(), |
|
title="Tradutor de Vídeo", |
|
description="Carregue um vídeo em qualquer idioma e ele será traduzido para português." |
|
) |
|
|
|
iface.launch() |