whisperSpanish / app.py
Dani
region
51ce539
raw
history blame
1.83 kB
import boto3
import gradio as gr
from transformers import pipeline
import tempfile
import os
# from huggingface_hub import login
#
# with open("../token.txt", "r") as file:
# token = file.readline().strip()
#
#
# login(token=token, add_to_git_credential=True)
pipe = pipeline(model="dacavi/whisper-small-es")
def translate_text(text, target_language_code):
translate = boto3.client('translate', region_name='us-east-1')
response = translate.translate_text(
Text=text,
SourceLanguageCode='es', # Auto-detect source language
TargetLanguageCode=target_language_code
)
translated_text = response['TranslatedText']
return translated_text
def translate_video(video_url,language):
if video_url and language:
# Download video and extract audio
with tempfile.NamedTemporaryFile(suffix=".wav", delete=True) as temp_audio:
# os.system(f"yt-dlp -o {temp_audio.name} -x --audio-format wav {video_url}")
os.system(f"yt-dlp -o audioSample.wav -x --audio-format wav {video_url}")
print("Downloaded audio:", temp_audio.name)
# Transcribe audio
text = pipe("audioSample.wav")["text"]
translatedText = translate_text(text,language)
# Clean up temporary files
os.remove("audioSample.wav")
else:
translatedText = "Please fill in both parameters."
return translatedText
iface = gr.Interface(
fn=translate_video,
inputs=[gr.Textbox(label="Video URL"),
gr.Dropdown(choices=["en", "fr", "de", "it"], label="Select Target Language")
],
outputs="text",
live=True,
title="SPANISH VIDEO TRANSLATION",
description="Paste the URL of a Spanish video to translate the text to any of the available languages.",
)
iface.launch()