Spaces:
Paused
Paused
Commit
·
a0e508d
1
Parent(s):
98d9c60
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from subprocess import run
|
|
| 4 |
from faster_whisper import WhisperModel
|
| 5 |
import json
|
| 6 |
import tempfile
|
|
|
|
| 7 |
|
| 8 |
# Carregar mapeamento de idiomas
|
| 9 |
with open('language_codes.json', 'r') as f:
|
|
@@ -14,31 +15,56 @@ tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M")
|
|
| 14 |
model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M")
|
| 15 |
whisper_model = WhisperModel("large-v2", device="cuda", compute_type="float16")
|
| 16 |
|
| 17 |
-
def process_video(radio, video, target_language
|
| 18 |
# 1. Extrair áudio
|
| 19 |
audio_file = tempfile.NamedTemporaryFile(suffix=".wav").name
|
| 20 |
run(["ffmpeg", "-i", video.name, audio_file])
|
| 21 |
|
| 22 |
# 2. Transcrição
|
| 23 |
-
segments, _ = whisper_model.transcribe(audio_file)
|
| 24 |
-
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Interface Gradio
|
| 44 |
iface = gr.Interface(
|
|
@@ -52,4 +78,4 @@ iface = gr.Interface(
|
|
| 52 |
title="AI Video Dubbing"
|
| 53 |
)
|
| 54 |
|
| 55 |
-
iface.launch()
|
|
|
|
| 4 |
from faster_whisper import WhisperModel
|
| 5 |
import json
|
| 6 |
import tempfile
|
| 7 |
+
import os # Importando o módulo os
|
| 8 |
|
| 9 |
# Carregar mapeamento de idiomas
|
| 10 |
with open('language_codes.json', 'r') as f:
|
|
|
|
| 15 |
model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M")
|
| 16 |
whisper_model = WhisperModel("large-v2", device="cuda", compute_type="float16")
|
| 17 |
|
| 18 |
+
def process_video(radio, video, target_language):
|
| 19 |
# 1. Extrair áudio
|
| 20 |
audio_file = tempfile.NamedTemporaryFile(suffix=".wav").name
|
| 21 |
run(["ffmpeg", "-i", video.name, audio_file])
|
| 22 |
|
| 23 |
# 2. Transcrição
|
| 24 |
+
segments, _ = whisper_model.transcribe(audio_file, beam_size=5) # Usando audio_file
|
| 25 |
+
segments = list(segments)
|
| 26 |
|
| 27 |
+
# Criar o arquivo .srt com carimbos de tempo
|
| 28 |
+
temp_transcript_file = tempfile.NamedTemporaryFile(delete=False, suffix=".srt")
|
| 29 |
+
with open(temp_transcript_file.name, "w", encoding="utf-8") as f:
|
| 30 |
+
counter = 1
|
| 31 |
+
for segment in segments:
|
| 32 |
+
start_minutes = int(segment.start // 60)
|
| 33 |
+
start_seconds = int(segment.start % 60)
|
| 34 |
+
start_milliseconds = int((segment.start - int(segment.start)) * 1000)
|
| 35 |
+
end_minutes = int(segment.end // 60)
|
| 36 |
+
end_seconds = int(segment.end % 60)
|
| 37 |
+
end_milliseconds = int((segment.end - int(segment.end)) * 1000)
|
| 38 |
+
formatted_start = f"{start_minutes:02d}:{start_seconds:02d},{start_milliseconds:03d}"
|
| 39 |
+
formatted_end = f"{end_minutes:02d}:{end_seconds:02d},{end_milliseconds:03d}"
|
| 40 |
|
| 41 |
+
f.write(f"{counter}\n")
|
| 42 |
+
f.write(f"{formatted_start} --> {formatted_end}\n")
|
| 43 |
+
f.write(f"{segment.text}\n\n")
|
| 44 |
+
counter += 1
|
| 45 |
|
| 46 |
+
# 3. Tradução
|
| 47 |
+
flores_code = lang_codes.get(target_language, "eng_Latn") # Definindo flores_code
|
| 48 |
+
temp_translated_file = tempfile.NamedTemporaryFile(delete=False, suffix=".srt")
|
| 49 |
+
with open(temp_transcript_file.name, "r", encoding="utf-8") as infile, open(temp_translated_file.name, "w", encoding="utf-8") as outfile:
|
| 50 |
+
for line in infile:
|
| 51 |
+
if line.strip().isnumeric() or "-->" in line:
|
| 52 |
+
outfile.write(line)
|
| 53 |
+
elif line.strip() != "":
|
| 54 |
+
inputs = tokenizer(line.strip(), return_tensors="pt")
|
| 55 |
+
translated_tokens = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id[flores_code], max_length=100)
|
| 56 |
+
translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
| 57 |
+
outfile.write(translated_text + "\n")
|
| 58 |
+
else:
|
| 59 |
+
outfile.write("\n")
|
| 60 |
|
| 61 |
+
# 5. Incorporar legenda
|
| 62 |
+
output_video = "output_video.mp4" # Definindo output_video
|
| 63 |
+
run(["ffmpeg", "-i", video.name, "-vf", f"subtitles={temp_translated_file.name}", output_video])
|
| 64 |
+
os.unlink(temp_transcript_file.name)
|
| 65 |
+
os.unlink(temp_translated_file.name)
|
| 66 |
+
|
| 67 |
+
return output_video # Retornando output_video
|
| 68 |
|
| 69 |
# Interface Gradio
|
| 70 |
iface = gr.Interface(
|
|
|
|
| 78 |
title="AI Video Dubbing"
|
| 79 |
)
|
| 80 |
|
| 81 |
+
iface.launch()
|