Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import gradio as grfrom transformers import pipelinefrom gtts import gTTSimport os# 1. LIGHTWEIGHT MODELS (Fast & Reliable)# These load instantly and don't require a GPUMODELS = {"Hindi": "Helsinki-NLP/opus-mt-en-hi","Tamil": "Helsinki-NLP/opus-mt-en-dra","Telugu": "Helsinki-NLP/opus-mt-en-dra","Gujarati": "Helsinki-NLP/opus-mt-en-inc","Marathi": "Helsinki-NLP/opus-mt-en-inc","Assamese": "Helsinki-NLP/opus-mt-en-inc"}# Specific codes for the voice and the neural 'switch'CONFIG = {"Hindi": {"v_code": "hi", "prefix": ""},"Tamil": {"v_code": "ta", "prefix": ">>tam<< "},"Telugu": {"v_code": "te", "prefix": ">>tel<< "},"Gujarati": {"v_code": "gu", "prefix": ">>guj<< "},"Marathi": {"v_code": "mr", "prefix": ">>mar<< "},"Assamese": {"v_code": "as", "prefix": ">>asm<< "}}# Cache for the translators to keep it fasttranslators = {}def fast_translate(text, lang):if not text.strip():return "Please enter text.", None# Load model only if neededif lang not in translators:translators[lang] = pipeline("translation", model=MODELS[lang])# Add prefix for multilingual modelsinput_text = CONFIG[lang]["prefix"] + text# Translateresult = translators[lang](input_text)[0]['translation_text']# Voice (using gTTS because it's the fastest and never fails)tts = gTTS(result, lang=CONFIG[lang]["v_code"])tts.save("voice.mp3")return result, "voice.mp3"# 2. CLEAN & SIMPLE UIwith gr.Blocks() as demo:gr.Markdown("# 🚀 Fast Language Translator")gr.Markdown("English to Indian Languages (Text-to-Speech)")with gr.Row():with gr.Column():input_text = gr.Textbox(label="English Text", placeholder="Type something...", lines=3)lang_choice = gr.Dropdown(choices=list(MODELS.keys()), value="Hindi", label="Target Language")btn = gr.Button("Translate", variant="primary")with gr.Column():output_text = gr.Textbox(label="Translation", interactive=False)output_audio = gr.Audio(label="Listen", autoplay=True)btn.click(fast_translate, inputs=[input_text, lang_choice], outputs=[output_text, output_audio])demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132import gradio as grfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLMfrom gtts import gTTSimport torchimport whisperimport os# 1. BRAIN CONFIGURATION: Bi-directional Model Mapping# 'to_indic' for English -> Language# 'to_english' for Language -> EnglishCONFIG = {"Hindi": {"to_indic": "Helsinki-NLP/opus-mt-en-hi","to_english": "Helsinki-NLP/opus-mt-hi-en","v_code": "hi", "prefix": ""},"Tamil": {"to_indic": "Helsinki-NLP/opus-mt-en-dra","to_english": "Helsinki-NLP/opus-mt-dra-en","v_code": "ta", "prefix": ">>tam<< "},"Telugu": {"to_indic": "Helsinki-NLP/opus-mt-en-dra","to_english": "Helsinki-NLP/opus-mt-dra-en","v_code": "te", "prefix": ">>tel<< "},"Gujarati": {"to_indic": "Helsinki-NLP/opus-mt-en-inc","to_english": "Helsinki-NLP/opus-mt-inc-en","v_code": "gu", "prefix": ">>guj<< "},"Marathi": {"to_indic": "Helsinki-NLP/opus-mt-en-inc","to_english": "Helsinki-NLP/opus-mt-inc-en","v_code": "mr", "prefix": ">>mar<< "},"Assamese": {"to_indic": "Helsinki-NLP/opus-mt-en-inc","to_english": "Helsinki-NLP/opus-mt-inc-en","v_code": "as", "prefix": ">>asm<< "}}# Cache for modelsloaded_models = {}# Load Whisper for Speech-to-Text (The "Ears")stt_model = whisper.load_model("base")def get_model(model_name):if model_name not in loaded_models:device = "cuda" if torch.cuda.is_available() else "cpu"tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device)loaded_models[model_name] = (tokenizer, model, device)return loaded_models[model_name]# --- MODE 1: YOU TO THEM (Text -> Translation -> Voice) ---def you_to_them(text, language):if not text.strip(): return "Please enter text...", Nonecfg = CONFIG[language]tokenizer, model, device = get_model(cfg["to_indic"])input_text = cfg["prefix"] + textinputs = tokenizer(input_text, return_tensors="pt", padding=True).to(device)with torch.no_grad():tokens = model.generate(**inputs, max_length=200)translation = tokenizer.decode(tokens[0], skip_special_tokens=True)tts = gTTS(translation, lang=cfg["v_code"])tts.save("outgoing.mp3")return translation, "outgoing.mp3"# --- MODE 2: THEM TO YOU (Microphone -> Transcription -> English) ---def them_to_you(audio_path, language):if audio_path is None: return "Please record audio first."# 1. Speech to Text (Whisper)result = stt_model.transcribe(audio_path)spoken_text = result['text']# 2. Text to English Translationcfg = CONFIG[language]tokenizer, model, device = get_model(cfg["to_english"])inputs = tokenizer(spoken_text, return_tensors="pt", padding=True).to(device)with torch.no_grad():tokens = model.generate(**inputs, max_length=200)english_text = tokenizer.decode(tokens[0], skip_special_tokens=True)return f"They Spoke ({language}): {spoken_text}\n\nEnglish Meaning: {english_text}"# --- ARTISTIC INTERFACE ---artistic_theme = gr.themes.Soft(primary_hue="purple", neutral_hue="slate").set(body_background_fill='*neutral_950',block_background_fill='*neutral_900',block_radius='20px',button_primary_background_fill='linear-gradient(90deg, #9333ea 0%, #3b82f6 100%)',button_primary_text_color='white',block_shadow='0 0 25px rgba(147, 51, 234, 0.2)')with gr.Blocks(theme=artistic_theme, title="Polyglot Prophet v3") as demo:gr.Markdown("# <center>🌌 The Polyglot Prophet: Conversation Suite</center>")gr.Markdown("<center>A professional bidirectional translation engine for the Indian subcontinent.</center>")with gr.Tabs():# TAB 1: YOU TO THEMwith gr.TabItem("1. You ➔ Them (Voice Out)"):with gr.Row():with gr.Column():in_text = gr.Textbox(label="Type your English message", lines=4)target_lang = gr.Dropdown(choices=list(CONFIG.keys()), value="Hindi", label="Their Language")btn_send = gr.Button("Awaken Translation", variant="primary")with gr.Column():out_text = gr.Textbox(label="Translated Text", interactive=False)out_audio = gr.Audio(label="Spoken Revelation", autoplay=True)btn_send.click(you_to_them, inputs=[in_text, target_lang], outputs=[out_text, out_audio])# TAB 2: THEM TO YOUwith gr.TabItem("2. Them ➔ You (Listen & Decode)"):with gr.Row():with gr.Column():gr.Markdown("### Ask the other person to speak.")mic_input = gr.Audio(sources=["microphone"], type="filepath", label="Record their voice")source_lang = gr.Dropdown(choices=list(CONFIG.keys()), value="Hindi", label="Language they are speaking")btn_listen = gr.Button("Decode to English", variant="primary")with gr.Column():final_transcript = gr.Textbox(label="Prophet's Interpretation", lines=6, interactive=False)btn_listen.click(them_to_you, inputs=[mic_input, source_lang], outputs=final_transcript)if __name__ == "__main__":demo.launch()
app.py+115-50