File size: 7,106 Bytes
63ee3e5
 
 
 
 
37652eb
63ee3e5
 
 
 
 
 
35bcda1
63ee3e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7f56a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63ee3e5
35bcda1
 
63ee3e5
35bcda1
c7f56a8
 
 
35bcda1
c7f56a8
35bcda1
63ee3e5
 
c7f56a8
63ee3e5
c7f56a8
63ee3e5
 
 
 
c7f56a8
 
 
63ee3e5
 
c7f56a8
63ee3e5
c7f56a8
 
 
63ee3e5
 
 
 
c7f56a8
 
 
63ee3e5
 
c7f56a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63ee3e5
 
 
 
 
c7f56a8
 
 
 
 
 
 
63ee3e5
 
 
 
c7f56a8
63ee3e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7f56a8
63ee3e5
c7f56a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63ee3e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7f56a8
63ee3e5
c7f56a8
 
 
63ee3e5
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import gradio as gr
import torch
import numpy as np
import librosa
from transformers import pipeline
from melo.api import TTS

# --------------------------------------------------
# ASR Pipeline (for English transcription)
# --------------------------------------------------
asr = pipeline(
    "automatic-speech-recognition",
    model="facebook/wav2vec2-base-960h"
)

# --------------------------------------------------
# Mapping for Target Languages and Models
# --------------------------------------------------
translation_models = {
    "Spanish": "Helsinki-NLP/opus-mt-en-es",
    "French": "Helsinki-NLP/opus-mt-en-fr",
    "German": "Helsinki-NLP/opus-mt-en-de",
    "Chinese": "Helsinki-NLP/opus-mt-en-zh",
    "Russian": "Helsinki-NLP/opus-mt-en-ru",
    "Arabic": "Helsinki-NLP/opus-mt-en-ar",
    "Portuguese": "Helsinki-NLP/opus-mt-en-pt",
    "Japanese": "Helsinki-NLP/opus-mt-en-ja",
    "Italian": "Helsinki-NLP/opus-mt-en-it",
    "Korean": "Helsinki-NLP/opus-mt-en-ko"
}

# Each language often requires a specific pipeline task name 
# (e.g., "translation_en_to_zh" rather than "translation_en_to_chinese")
translation_tasks = {
    "Spanish": "translation_en_to_es",
    "French": "translation_en_to_fr",
    "German": "translation_en_to_de",
    "Chinese": "translation_en_to_zh",
    "Russian": "translation_en_to_ru",
    "Arabic": "translation_en_to_ar",
    "Portuguese": "translation_en_to_pt",
    "Japanese": "translation_en_to_ja",
    "Italian": "translation_en_to_it",
    "Korean": "translation_en_to_ko"
}

# TTS models (some may not exist or may be unofficial)
tts_models = {
    "Spanish": "myshell-ai/MeloTTS-Spanish",
    "French": "myshell-ai/MeloTTS-French",
    "German": "tts_models/de/tacotron2",
    "Chinese": "myshell-ai/MeloTTS-English-v2",     # Verify if this actually exists on Hugging Face
    "Russian": "tts_models/ru/tacotron2",     # Same note
    "Arabic": "tts_models/ar/tacotron2",      # Same note
    "Portuguese": "tts_models/pt/tacotron2",  # Same note
    "Japanese": "myshell-ai/MeloTTS-Japanese",    # Same note
    "Italian": "tts_models/it/tacotron2",     # Same note
    "Korean": "myshell-ai/MeloTTS-Korean"       # Same note
}

# --------------------------------------------------
# Caches for translator and TTS pipelines
# --------------------------------------------------
translator_cache = {}
tts_cache = {}

def get_translator(target_language):
    """
    Retrieve or create a translation pipeline for the specified language.
    """
    if target_language in translator_cache:
        return translator_cache[target_language]
    
    model_name = translation_models[target_language]
    task_name = translation_tasks[target_language]
    
    translator = pipeline(task_name, model=model_name)
    translator_cache[target_language] = translator
    return translator

def get_tts(target_language):
    """
    Retrieve or create a TTS pipeline for the specified language, if available.
    """
    if target_language in tts_cache:
        return tts_cache[target_language]
    
    model_name = tts_models.get(target_language)
    if model_name is None:
        # If no TTS model is mapped, raise an error or handle gracefully
        raise ValueError(f"No TTS model available for {target_language}.")

    try:
        tts_pipeline = pipeline("text-to-speech", model=model_name)
    except Exception as e:
        raise ValueError(
            f"Failed to load TTS model for {target_language}. "
            f"Make sure '{model_name}' exists on Hugging Face.\nError: {e}"
        )

    tts_cache[target_language] = tts_pipeline
    return tts_pipeline

# --------------------------------------------------
# Prediction Function
# --------------------------------------------------
def predict(audio, text, target_language):
    """
    1. Obtain English text (from text input or ASR).
    2. Translate English -> target_language.
    3. Synthesize speech in target_language.
    """
    # 1. English text from text input (if provided), else from audio via ASR
    if text.strip():
        english_text = text.strip()
    elif audio is not None:
        sample_rate, audio_data = audio

        # Ensure the audio is float32 for librosa
        if audio_data.dtype not in [np.float32, np.float64]:
            audio_data = audio_data.astype(np.float32)

        # Convert stereo to mono if needed
        if len(audio_data.shape) > 1 and audio_data.shape[1] > 1:
            audio_data = np.mean(audio_data, axis=1)

        # Resample to 16 kHz if necessary
        if sample_rate != 16000:
            audio_data = librosa.resample(audio_data, orig_sr=sample_rate, target_sr=16000)

        input_audio = {"array": audio_data, "sampling_rate": 16000}
        asr_result = asr(input_audio)
        english_text = asr_result["text"]
    else:
        return "No input provided.", "", None

    # 2. Translation step
    translator = get_translator(target_language)
    try:
        translation_result = translator(english_text)
        translated_text = translation_result[0]["translation_text"]
    except Exception as e:
        # If there's an error in translation, return partial results
        return english_text, f"Translation error: {e}", None

    # 3. TTS step: synthesize speech from the translated text
    try:
        tts_pipeline = get_tts(target_language)
        tts_result = tts_pipeline(translated_text)
        # The TTS pipeline returns a dict with "wav" and "sample_rate"
        synthesized_audio = (tts_result["sample_rate"], tts_result["wav"])
    except Exception as e:
        # If TTS fails, return partial results
        return english_text, translated_text, f"TTS error: {e}"

    return english_text, translated_text, synthesized_audio

# --------------------------------------------------
# Gradio Interface Setup
# --------------------------------------------------
iface = gr.Interface(
    fn=predict,
    inputs=[
        gr.Audio(type="numpy", label="Record/Upload English Audio (optional)"),
        gr.Textbox(lines=4, placeholder="Or enter English text here", label="English Text Input (optional)"),
        gr.Dropdown(choices=list(translation_models.keys()), value="Spanish", label="Target Language")
    ],
    outputs=[
        gr.Textbox(label="English Transcription"),
        gr.Textbox(label="Translation (Target Language)"),
        gr.Audio(label="Synthesized Speech in Target Language")
    ],
    title="Multimodal Language Learning Aid",
    description=(
        "This app helps language learners by providing three outputs:\n"
        "1. English transcription (from ASR or text input),\n"
        "2. Translation to a target language (using Helsinki-NLP models), and\n"
        "3. Synthetic speech in the target language.\n\n"
        "Select one of the top 10 commonly used languages from the dropdown.\n"
        "Either record/upload an English audio sample or enter English text directly.\n\n"
        "Note: Some TTS models may not exist or be unstable for certain languages."
    ),
    allow_flagging="never"
)

if __name__ == "__main__":
    iface.launch()