Spaces:
Sleeping
Sleeping
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') | |
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): | |
# 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,"en") | |
# Clean up temporary files | |
os.remove("audioSample.wav") | |
return translatedText | |
iface = gr.Interface( | |
fn=translate_video, | |
inputs="text", | |
outputs="text", | |
live=True, | |
title="Spanish to English video translation", | |
description="Paste the URL of a Spanish video to translate the text to English spoken content.", | |
) | |
iface.launch() | |