Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,99 +1,51 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
from huggingface_hub import InferenceApi
|
| 5 |
-
import soundfile as sf
|
| 6 |
import base64
|
| 7 |
|
| 8 |
-
# --- CONFIG: change these if you want different models ---
|
| 9 |
MODEL_TE_TO_EN = "Helsinki-NLP/opus-mt-te-en"
|
| 10 |
MODEL_EN_TO_TE = "Helsinki-NLP/opus-mt-en-te"
|
| 11 |
-
TTS_MODEL_TELUGU = "facebook/mms-tts-tel"
|
| 12 |
-
# Optionally pick an English TTS (Indic or other) if needed:
|
| 13 |
-
# TTS_MODEL_ENGLISH = "ai4bharat/indic-parler-tts" # supports English + Indian languages (example)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
return pipeline("translation", model=model_name, tokenizer=model_name)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
if direction == "te->en":
|
| 23 |
-
|
| 24 |
else:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
# --- text-to-speech using Hugging Face Inference API ---
|
| 31 |
-
def tts_hf(model_id, text, hf_token=None, output_wav="output.wav", sample_rate=24000):
|
| 32 |
-
# Create an InferenceApi object for the text-to-speech model
|
| 33 |
-
# The "task" param isn't strictly required in InferenceApi constructor but we will call the model directly.
|
| 34 |
-
if hf_token:
|
| 35 |
-
client = InferenceApi(repo_id=model_id, token=hf_token)
|
| 36 |
-
else:
|
| 37 |
-
client = InferenceApi(repo_id=model_id)
|
| 38 |
-
|
| 39 |
-
# Many TTS models on HF return audio bytes (raw wav or base64). InferenceApi will return binary audio for TTS models.
|
| 40 |
-
print(f"[tts_hf] Calling TTS model: {model_id}")
|
| 41 |
-
|
| 42 |
-
# The MMS TTS expects a dict input with "text" key, sometimes sampling params are accepted.
|
| 43 |
-
payload = {"inputs": text}
|
| 44 |
-
result = client(inputs=payload)
|
| 45 |
-
|
| 46 |
-
# result may be bytes (raw waveform) or a dict with "audio" base64 string
|
| 47 |
audio_bytes = None
|
| 48 |
-
|
| 49 |
if isinstance(result, bytes):
|
| 50 |
audio_bytes = result
|
| 51 |
elif isinstance(result, dict):
|
| 52 |
-
|
| 53 |
-
if "audio" in result and isinstance(result["audio"], str):
|
| 54 |
audio_bytes = base64.b64decode(result["audio"])
|
| 55 |
-
elif "wav" in result
|
| 56 |
audio_bytes = base64.b64decode(result["wav"])
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
print(f"[tts_hf] Saved audio to {output_wav}")
|
| 67 |
-
return output_wav
|
| 68 |
-
|
| 69 |
-
# --- high level function: translate + tts (direction = "te->en" or "en->te") ---
|
| 70 |
-
def translate_and_tts(text, direction="te->en", hf_token=None, out_wav="out.wav"):
|
| 71 |
-
print("Translating...")
|
| 72 |
-
translated = translate(text, direction=direction)
|
| 73 |
-
print(f"Translated text: {translated}")
|
| 74 |
-
|
| 75 |
-
# Choose TTS model depending on target language
|
| 76 |
-
target_lang = "en" if direction == "te->en" else "te"
|
| 77 |
-
if target_lang == "te":
|
| 78 |
-
tts_model = TTS_MODEL_TELUGU
|
| 79 |
-
else:
|
| 80 |
-
# for English you can use MMS or an English TTS model. We'll use MMS for demonstration if available.
|
| 81 |
-
tts_model = TTS_MODEL_TELUGU # you may replace with an English TTS model of your choice
|
| 82 |
-
|
| 83 |
-
wav_path = tts_hf(tts_model, translated, hf_token=hf_token, output_wav=out_wav)
|
| 84 |
-
return translated, wav_path
|
| 85 |
-
|
| 86 |
-
# --- Example usage ---
|
| 87 |
if __name__ == "__main__":
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
parser = argparse.ArgumentParser()
|
| 91 |
-
parser.add_argument("--text", type=str, required=True, help="Input text (Telugu or English to translate)")
|
| 92 |
-
parser.add_argument("--direction", type=str, choices=["te->en", "en->te"], default="te->en")
|
| 93 |
-
parser.add_argument("--out", type=str, default="tts_output.wav")
|
| 94 |
-
parser.add_argument("--token", type=str, default=os.environ.get("HF_TOKEN"))
|
| 95 |
-
args = parser.parse_args()
|
| 96 |
-
|
| 97 |
-
translated_text, wav = translate_and_tts(args.text, direction=args.direction, hf_token=args.token, out_wav=args.out)
|
| 98 |
-
print("Done. Translated text:", translated_text)
|
| 99 |
-
print("Saved speech file:", wav)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
|
|
|
| 3 |
from huggingface_hub import InferenceApi
|
|
|
|
| 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 |
+
TTS_MODEL_TELUGU = "facebook/mms-tts-tel"
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
te_en_pipe = pipeline("translation", model=MODEL_TE_TO_EN)
|
| 11 |
+
en_te_pipe = pipeline("translation", model=MODEL_EN_TO_TE)
|
| 12 |
+
tts_client = InferenceApi(repo_id=TTS_MODEL_TELUGU)
|
|
|
|
| 13 |
|
| 14 |
+
def translate_and_tts(text, direction):
|
| 15 |
+
# Translation
|
| 16 |
if direction == "te->en":
|
| 17 |
+
translated = te_en_pipe(text)[0]['translation_text']
|
| 18 |
else:
|
| 19 |
+
translated = en_te_pipe(text)[0]['translation_text']
|
| 20 |
+
|
| 21 |
+
# Text-to-Speech
|
| 22 |
+
result = tts_client(inputs={"text": translated})
|
| 23 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
audio_bytes = None
|
|
|
|
| 25 |
if isinstance(result, bytes):
|
| 26 |
audio_bytes = result
|
| 27 |
elif isinstance(result, dict):
|
| 28 |
+
if "audio" in result:
|
|
|
|
| 29 |
audio_bytes = base64.b64decode(result["audio"])
|
| 30 |
+
elif "wav" in result:
|
| 31 |
audio_bytes = base64.b64decode(result["wav"])
|
| 32 |
+
|
| 33 |
+
return translated, (22050, audio_bytes)
|
| 34 |
+
|
| 35 |
+
# Build Gradio UI
|
| 36 |
+
demo = gr.Interface(
|
| 37 |
+
fn=translate_and_tts,
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.Textbox(label="Enter Text (Telugu or English)", lines=2, placeholder="Type your text here..."),
|
| 40 |
+
gr.Radio(["te->en", "en->te"], label="Translation Direction", value="te->en")
|
| 41 |
+
],
|
| 42 |
+
outputs=[
|
| 43 |
+
gr.Textbox(label="Translated Text"),
|
| 44 |
+
gr.Audio(label="Speech Output")
|
| 45 |
+
],
|
| 46 |
+
title="Telugu ↔ English Translator + Text-to-Speech",
|
| 47 |
+
description="Translate between Telugu and English with speech output using Hugging Face models."
|
| 48 |
+
)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
if __name__ == "__main__":
|
| 51 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|