Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +67 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import whisper
|
| 3 |
+
import torch
|
| 4 |
+
import os
|
| 5 |
+
import ffmpeg
|
| 6 |
+
from flask import send_file
|
| 7 |
+
import time
|
| 8 |
+
from deep_translator import GoogleTranslator
|
| 9 |
+
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
print("Usando dispositivo:", device)
|
| 12 |
+
|
| 13 |
+
model = whisper.load_model("medium").to(device)
|
| 14 |
+
|
| 15 |
+
def extract_audio(video_path, output_audio="temp_audio.wav"):
|
| 16 |
+
ffmpeg.input(video_path).output(output_audio, format="wav", acodec="pcm_s16le", ac=1, ar="16k").run(overwrite_output=True, quiet=True)
|
| 17 |
+
return output_audio
|
| 18 |
+
|
| 19 |
+
def translate_text(text, target_lang="es"):
|
| 20 |
+
return GoogleTranslator(source="auto", target=target_lang).translate(text)
|
| 21 |
+
|
| 22 |
+
def transcribe_audio(audio_path, target_language="es"):
|
| 23 |
+
start_time = time.time()
|
| 24 |
+
result = model.transcribe(audio_path, fp16=(device == "cuda"), task="translate", beam_size=5)
|
| 25 |
+
translated_text = translate_text(result["text"], target_language)
|
| 26 |
+
end_time = time.time()
|
| 27 |
+
print(f"Tiempo de transcripci贸n y traducci贸n: {end_time - start_time:.2f} segundos")
|
| 28 |
+
return translated_text
|
| 29 |
+
|
| 30 |
+
def save_transcription(text, format, output_name="translation"):
|
| 31 |
+
file_path = f"{output_name}.{format.lower()}"
|
| 32 |
+
with open(file_path, "w", encoding="utf-8") as f:
|
| 33 |
+
f.write(text)
|
| 34 |
+
return file_path
|
| 35 |
+
|
| 36 |
+
def process_media(file, format, target_language):
|
| 37 |
+
if not os.path.exists(file):
|
| 38 |
+
return "Error: No se encontr贸 el archivo."
|
| 39 |
+
extension = file.split(".")[-1].lower()
|
| 40 |
+
audio_extensions = ["mp3", "wav", "flac", "ogg", "m4a"]
|
| 41 |
+
video_extensions = ["mp4", "avi", "mov", "mkv"]
|
| 42 |
+
|
| 43 |
+
if extension in video_extensions:
|
| 44 |
+
audio_path = extract_audio(file)
|
| 45 |
+
elif extension in audio_extensions:
|
| 46 |
+
audio_path = file
|
| 47 |
+
else:
|
| 48 |
+
return "Error: Formato de archivo no compatible."
|
| 49 |
+
|
| 50 |
+
transcription = transcribe_audio(audio_path, target_language)
|
| 51 |
+
file_path = save_transcription(transcription, format)
|
| 52 |
+
|
| 53 |
+
return transcription, file_path
|
| 54 |
+
|
| 55 |
+
gui = gr.Interface(
|
| 56 |
+
fn=process_media,
|
| 57 |
+
inputs=[
|
| 58 |
+
gr.File(label="Sube un archivo de audio o v铆deo"),
|
| 59 |
+
gr.Radio(["TXT", "DOCX", "SRT", "JSON"], label="Formato de salida"),
|
| 60 |
+
gr.Dropdown(["es", "en", "fr", "de", "it"], label="Idioma de traducci贸n", value="es")
|
| 61 |
+
],
|
| 62 |
+
outputs=["text", "file"],
|
| 63 |
+
title="Transcriptor y Traductor de Audio y V铆deo",
|
| 64 |
+
description="Sube un archivo, transcribe y traduce a cualquier idioma con alta precisi贸n."
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
gui.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchaudio
|
| 3 |
+
torchvision
|
| 4 |
+
openai-whisper
|
| 5 |
+
deep-translator
|
| 6 |
+
ffmpeg-python
|
| 7 |
+
gradio
|
| 8 |
+
flask
|