File size: 1,913 Bytes
e41b97b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4344bf1
 
 
e41b97b
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
# 🗣️ Audio Translator | CPU-only HF Space

import tempfile
from deep_translator import GoogleTranslator
from gtts import gTTS
import gradio as gr
from transformers import pipeline

# 1. ASR pipeline (Whisper Tiny)
asr = pipeline(
    "automatic-speech-recognition",
    model="openai/whisper-tiny",
    device=-1,
    chunk_length_s=30
)

def audio_translate(audio_path: str, target_lang: str):
    if not audio_path:
        return "", "", None

    # 2. Transcribe
    result = asr(audio_path)
    transcript = result["text"].strip()

    # 3. Translate
    dest_code = "es" if target_lang == "Spanish" else "en"
    translated = GoogleTranslator(source="auto", target=dest_code).translate(transcript)

    # 4. Text-to-speech
    tts = gTTS(translated, lang=dest_code)
    tmp = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
    tts.write_to_fp(tmp)
    tmp.flush()

    return transcript, translated, tmp.name

# 5. Gradio UI
with gr.Blocks(title="🗣️ Audio Translator") as demo:
    gr.Markdown(
        "# 🗣️ Audio Translator\n"
        "Upload an audio clip (any language), choose **English** or **Spanish**,\n"
        "and get an immediate translated transcript and spoken audio."
    )

    # <-- here: remove `source="upload"` entirely -->
    audio_in   = gr.Audio(label="Upload Audio", type="filepath")
    lang_sel   = gr.Radio(["Spanish", "English"], value="Spanish", label="Translate to")
    translate_btn = gr.Button("Translate 🔄", variant="primary")

    orig_txt  = gr.Textbox(label="Transcription", interactive=False)
    trans_txt = gr.Textbox(label="Translation", interactive=False)
    audio_out = gr.Audio(label="Translated Speech", type="filepath")

    translate_btn.click(
        audio_translate,
        inputs=[audio_in, lang_sel],
        outputs=[orig_txt, trans_txt, audio_out]
    )

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0")