Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
-
|
| 4 |
-
import base64
|
| 5 |
|
|
|
|
| 6 |
MODEL_TE_TO_EN = "Helsinki-NLP/opus-mt-te-en"
|
| 7 |
MODEL_EN_TO_TE = "Helsinki-NLP/opus-mt-en-te"
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
|
|
|
|
| 16 |
if direction == "te->en":
|
| 17 |
-
|
| 18 |
else:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Build Gradio UI
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
demo.launch()
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# app.py
|
| 3 |
+
import os
|
| 4 |
+
import tempfile
|
| 5 |
+
import traceback
|
| 6 |
+
from typing import Tuple, Optional
|
| 7 |
+
|
| 8 |
import gradio as gr
|
| 9 |
+
from transformers import pipeline, Pipeline
|
| 10 |
+
import soundfile as sf
|
|
|
|
| 11 |
|
| 12 |
+
# --- Models (change if you prefer other models) ---
|
| 13 |
MODEL_TE_TO_EN = "Helsinki-NLP/opus-mt-te-en"
|
| 14 |
MODEL_EN_TO_TE = "Helsinki-NLP/opus-mt-en-te"
|
| 15 |
+
# Try a TTS model that transformers supports via pipeline("text-to-speech")
|
| 16 |
+
TTS_MODEL_TELUGU = "facebook/mms-tts-tel" # may require more resources
|
| 17 |
+
TTS_MODEL_ENGLISH = "facebook/mms-tts-en" # optional; replace with a different English TTS if preferred
|
| 18 |
+
|
| 19 |
+
# --- Globals to hold pipelines (created lazily) ---
|
| 20 |
+
translator_te_en: Optional[Pipeline] = None
|
| 21 |
+
translator_en_te: Optional[Pipeline] = None
|
| 22 |
+
tts_telugu: Optional[Pipeline] = None
|
| 23 |
+
tts_english: Optional[Pipeline] = None
|
| 24 |
+
|
| 25 |
+
def make_translation_pipelines(device: int = -1):
|
| 26 |
+
global translator_te_en, translator_en_te
|
| 27 |
+
if translator_te_en is None:
|
| 28 |
+
translator_te_en = pipeline("translation", model=MODEL_TE_TO_EN, device=device)
|
| 29 |
+
if translator_en_te is None:
|
| 30 |
+
translator_en_te = pipeline("translation", model=MODEL_EN_TO_TE, device=device)
|
| 31 |
+
|
| 32 |
+
def make_tts_pipelines(device: int = -1):
|
| 33 |
+
global tts_telugu, tts_english
|
| 34 |
+
if tts_telugu is None:
|
| 35 |
+
# pipeline("text-to-speech") support depends on transformers version & model type
|
| 36 |
+
tts_telugu = pipeline("text-to-speech", model=TTS_MODEL_TELUGU, device=device)
|
| 37 |
+
if tts_english is None:
|
| 38 |
+
tts_english = pipeline("text-to-speech", model=TTS_MODEL_ENGLISH, device=device)
|
| 39 |
|
| 40 |
+
def safe_init_pipelines():
|
| 41 |
+
"""
|
| 42 |
+
Try to initialize pipelines. If any model cannot be downloaded due to
|
| 43 |
+
permissions/401, the exception will be captured and returned so the UI
|
| 44 |
+
can show a helpful message.
|
| 45 |
+
"""
|
| 46 |
+
try:
|
| 47 |
+
# device=-1 -> CPU. If you have GPU, set device=0 (or auto logic).
|
| 48 |
+
device = -1
|
| 49 |
+
make_translation_pipelines(device=device)
|
| 50 |
+
make_tts_pipelines(device=device)
|
| 51 |
+
return None
|
| 52 |
+
except Exception as e:
|
| 53 |
+
tb = traceback.format_exc()
|
| 54 |
+
return str(e) + "\n\n" + tb
|
| 55 |
|
| 56 |
+
def translate_text(text: str, direction: str) -> str:
|
| 57 |
+
if not text or text.strip() == "":
|
| 58 |
+
return ""
|
| 59 |
if direction == "te->en":
|
| 60 |
+
out = translator_te_en(text, max_length=512)
|
| 61 |
else:
|
| 62 |
+
out = translator_en_te(text, max_length=512)
|
| 63 |
+
# pipelines for Marian-like models return list of dicts with 'translation_text'
|
| 64 |
+
if isinstance(out, list) and len(out) > 0 and isinstance(out[0], dict):
|
| 65 |
+
for k in ("translation_text", "generated_text", "text"):
|
| 66 |
+
if k in out[0]:
|
| 67 |
+
return out[0][k]
|
| 68 |
+
# fallback
|
| 69 |
+
return list(out[0].values())[0]
|
| 70 |
+
# fallback conversion
|
| 71 |
+
return str(out)
|
| 72 |
+
|
| 73 |
+
def tts_to_wav_bytes(tts_pipeline: Pipeline, text: str) -> bytes:
|
| 74 |
+
"""
|
| 75 |
+
Use a text-to-speech pipeline to get audio bytes. Different pipeline implementations
|
| 76 |
+
return different shapes:
|
| 77 |
+
- a dict with {"audio": (sample_rate, np_array)} OR
|
| 78 |
+
- an object with "audio" bytes OR
|
| 79 |
+
- bytes directly.
|
| 80 |
+
We'll try to handle common return types.
|
| 81 |
+
"""
|
| 82 |
+
res = tts_pipeline(text)
|
| 83 |
+
# Many tts pipelines from transformers return {"wav": sample_rate, "audio": np_array} or {"audio": (sr, np_array)}
|
| 84 |
+
# Some return a dict with 'audio' as bytes or base64 — but local pipelines often return arrays.
|
| 85 |
+
# We'll attempt a few patterns.
|
| 86 |
+
# 1) If it's bytes, return directly
|
| 87 |
+
if isinstance(res, (bytes, bytearray)):
|
| 88 |
+
return bytes(res)
|
| 89 |
+
|
| 90 |
+
# 2) If list, take first element
|
| 91 |
+
if isinstance(res, list) and len(res) > 0:
|
| 92 |
+
res = res[0]
|
| 93 |
+
|
| 94 |
+
if isinstance(res, dict):
|
| 95 |
+
# a) {"audio": np_array, "sample_rate": sr} or {"audio": (sr, np_array)}
|
| 96 |
+
if "audio" in res:
|
| 97 |
+
audio = res["audio"]
|
| 98 |
+
# audio might be tuple (sr, np_array)
|
| 99 |
+
if isinstance(audio, tuple) and len(audio) == 2:
|
| 100 |
+
sr, arr = audio
|
| 101 |
+
else:
|
| 102 |
+
arr = audio
|
| 103 |
+
sr = res.get("sample_rate", 22050)
|
| 104 |
+
# save arr as wav bytes
|
| 105 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 106 |
+
sf.write(tmp.name, arr, samplerate=int(sr))
|
| 107 |
+
tmp.close()
|
| 108 |
+
with open(tmp.name, "rb") as f:
|
| 109 |
+
b = f.read()
|
| 110 |
+
return b
|
| 111 |
+
# b) {"wav": b} or {"wav": base64str} - handle bytes
|
| 112 |
+
if "wav" in res and isinstance(res["wav"], (bytes, bytearray)):
|
| 113 |
+
return bytes(res["wav"])
|
| 114 |
+
# c) {"wav": "<base64>"} is unlikely for local pipeline, but we won't decode here
|
| 115 |
+
# If we reach here, try to coerce to string and raise
|
| 116 |
+
raise RuntimeError(f"Unexpected TTS pipeline output format: {type(res)}")
|
| 117 |
+
|
| 118 |
+
def translate_and_speak(text: str, direction: str):
|
| 119 |
+
"""
|
| 120 |
+
Main function bound to Gradio. Returns (translated_text, wav_file_path_or_None, error_message_or_None)
|
| 121 |
+
Gradio audio output accepts a file path or bytes.
|
| 122 |
+
"""
|
| 123 |
+
if not text or text.strip() == "":
|
| 124 |
+
return "Input is empty", None, None
|
| 125 |
+
|
| 126 |
+
# Translate
|
| 127 |
+
try:
|
| 128 |
+
translated = translate_text(text, direction)
|
| 129 |
+
except Exception as e:
|
| 130 |
+
return "", None, f"Translation error: {e}"
|
| 131 |
+
|
| 132 |
+
# Choose TTS pipeline
|
| 133 |
+
target_lang = "en" if direction == "te->en" else "te"
|
| 134 |
+
try:
|
| 135 |
+
if target_lang == "te":
|
| 136 |
+
audio_bytes = tts_to_wav_bytes(tts_telugu, translated)
|
| 137 |
+
else:
|
| 138 |
+
audio_bytes = tts_to_wav_bytes(tts_english, translated)
|
| 139 |
+
except Exception as e:
|
| 140 |
+
return translated, None, f"TTS error: {e}"
|
| 141 |
+
|
| 142 |
+
# Save to temp wav file to return path (Gradio accepts path or bytes)
|
| 143 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 144 |
+
tmp.write(audio_bytes)
|
| 145 |
+
tmp.flush()
|
| 146 |
+
tmp.close()
|
| 147 |
+
return translated, tmp.name, None
|
| 148 |
+
|
| 149 |
+
# --- Initialize at start (catch errors so we can instruct the user) ---
|
| 150 |
+
init_error = safe_init_pipelines()
|
| 151 |
|
| 152 |
# Build Gradio UI
|
| 153 |
+
with gr.Blocks() as demo:
|
| 154 |
+
gr.Markdown("# Telugu ↔ English — Local Translation + TTS (Gradio)")
|
| 155 |
+
gr.Markdown(
|
| 156 |
+
"This Space loads models locally with `transformers`. "
|
| 157 |
+
"If the models are gated or require authentication, you'll see an error below with instructions."
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
input_text = gr.Textbox(label="Enter text (Telugu or English)", lines=3, placeholder="Type here...")
|
| 161 |
+
direction = gr.Radio(["te->en", "en->te"], value="te->en", label="Direction")
|
| 162 |
+
translate_btn = gr.Button("Translate & Speak")
|
| 163 |
+
|
| 164 |
+
with gr.Row():
|
| 165 |
+
translated_out = gr.Textbox(label="Translated text")
|
| 166 |
+
audio_out = gr.Audio(label="Speech (wav)")
|
| 167 |
+
|
| 168 |
+
error_box = gr.Textbox(label="Initialization / runtime messages", lines=6, interactive=False)
|
| 169 |
+
|
| 170 |
+
def on_click_translate(text, dir):
|
| 171 |
+
if init_error:
|
| 172 |
+
msg = (
|
| 173 |
+
"Model initialization failed when starting pipelines.\n\n"
|
| 174 |
+
"Common causes:\n"
|
| 175 |
+
" • The model repo is gated/private and requires an HF_TOKEN.\n"
|
| 176 |
+
" • The model name is incorrect or no longer available.\n\n"
|
| 177 |
+
"What to do:\n"
|
| 178 |
+
" 1) Go to your Space -> Settings -> Secrets and add HF_TOKEN with a Hugging Face token that has access.\n"
|
| 179 |
+
" 2) If the model is removed/renamed, change the model constants in app.py to a public model.\n\n"
|
| 180 |
+
f"Raw error:\n{init_error}"
|
| 181 |
+
)
|
| 182 |
+
return "", None, msg
|
| 183 |
+
|
| 184 |
+
translated, wav_path, runtime_err = translate_and_speak(text, dir)
|
| 185 |
+
if runtime_err:
|
| 186 |
+
return translated, None, runtime_err
|
| 187 |
+
return translated, wav_path, "OK"
|
| 188 |
+
|
| 189 |
+
translate_btn.click(on_click_translate, inputs=[input_text, direction], outputs=[translated_out, audio_out, error_box])
|
| 190 |
|
| 191 |
if __name__ == "__main__":
|
| 192 |
demo.launch()
|
| 193 |
+
|