File size: 1,520 Bytes
d3b9f74
be8b9f6
 
 
 
d3b9f74
be8b9f6
d3b9f74
 
be8b9f6
6d34151
be8b9f6
 
d3b9f74
 
 
 
 
 
 
 
 
 
 
be8b9f6
d3b9f74
be8b9f6
 
 
 
 
 
 
 
 
 
d3b9f74
be8b9f6
 
 
d3b9f74
be8b9f6
 
d3b9f74
be8b9f6
 
 
d3b9f74
 
be8b9f6
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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()