import gradio as gr import torch import whisper from transformers import AutoTokenizer, AutoModelForSeq2SeqLM # 1. Initialization and Model Loading (Optimized for Hugging Face Free CPU tier) print("Initializing Hugging Face Space environment...") device = "cpu" print("Loading Whisper STT Model...") whisper_model = whisper.load_model("small", device=device) model_path = "Akariieieie/DialectGo-NLLB" print(f"Loading custom NLLB model from {model_path}...") tokenizer = AutoTokenizer.from_pretrained(model_path) model = AutoModelForSeq2SeqLM.from_pretrained(model_path).to(device) LANG_CODES = { "English": "eng_Latn", "Tagalog": "tgl_Latn", "Cebuano": "ceb_Latn" } # Whisper language ISO mapping to enforce clean audio extraction accuracy WHISPER_LANG_MAPPING = { "English": "en", "Tagalog": "tl", "Cebuano": "tl" } # 2. Unified Processing Function def universal_translator(text, audio_path, source_lang_name, target_lang_name): working_text = text # If an audio file payload is sent (via UI or API), transcribe it first if audio_path is not None: print(f"Audio file detected. Transcribing via Whisper ({source_lang_name})...") whisper_lang = WHISPER_LANG_MAPPING.get(source_lang_name, None) # Guide Whisper with the selected language code for better accuracy if whisper_lang: whisper_result = whisper_model.transcribe(audio_path, language=whisper_lang) else: whisper_result = whisper_model.transcribe(audio_path) working_text = whisper_result["text"].strip() print(f"Transcription result: {working_text}") if not working_text or not working_text.strip(): return "No text or audio input provided.", "No text or audio input provided." # Map friendly names to NLLB language codes src_code = LANG_CODES.get(source_lang_name, "eng_Latn") tgt_code = LANG_CODES.get(target_lang_name, "tgl_Latn") # Set source language context tokenizer.src_lang = src_code inputs = tokenizer(working_text, return_tensors="pt").to(device) # Get target language token ID forced_bos_token_id = tokenizer.convert_tokens_to_ids(tgt_code) with torch.no_grad(): output_tokens = model.generate( **inputs, forced_bos_token_id=forced_bos_token_id, max_new_tokens=256, temperature=0.1 ) translation = tokenizer.batch_decode(output_tokens, skip_special_tokens=True)[0].strip() # MODIFICATION: Return both the transcription text and translation text return working_text, translation # 3. Gradio Interface Layout with gr.Blocks(theme=gr.themes.Base()) as demo: gr.Markdown("# 🇵🇭 DialectGo Unified Translation System") gr.Markdown("This space functions as both a web UI and an active API endpoint for multi-way voice/text translation.") with gr.Row(): with gr.Column(): audio_in = gr.Audio(label="Audio Input (File or Mic)", type="filepath") text_in = gr.Textbox(label="Text Input (Ignored if Audio is provided)", lines=3) src_lang = gr.Dropdown(choices=["English", "Tagalog", "Cebuano"], value="English", label="From") tgt_lang = gr.Dropdown(choices=["English", "Tagalog", "Cebuano"], value="Tagalog", label="To") submit_btn = gr.Button("Process Translation", variant="primary") with gr.Column(): # MODIFICATION: Added text block interface container to capture the output transcript transcription_out = gr.Textbox(label="Transcribed/Source Text", lines=3, interactive=False) output_text = gr.Textbox(label="Translation Result", lines=4, interactive=False) # MODIFICATION: Changed outputs array to map data cleanly to BOTH UI textboxes submit_btn.click( fn=universal_translator, inputs=[text_in, audio_in, src_lang, tgt_lang], outputs=[transcription_out, output_text], api_name="translate" ) demo.launch()