Spaces:
Sleeping
Sleeping
Update app.py
#2
by ralphgaru10 - opened
app.py
CHANGED
|
@@ -11,7 +11,7 @@ device = "cpu"
|
|
| 11 |
print("Loading Whisper STT Model...")
|
| 12 |
whisper_model = whisper.load_model("small", device=device)
|
| 13 |
|
| 14 |
-
model_path = "Akariieieie/
|
| 15 |
print(f"Loading custom NLLB model from {model_path}...")
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 17 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_path).to(device)
|
|
@@ -54,12 +54,25 @@ def universal_translator(text, audio_path, source_lang_name, target_lang_name):
|
|
| 54 |
src_code = LANG_CODES.get(source_lang_name, "eng_Latn")
|
| 55 |
tgt_code = LANG_CODES.get(target_lang_name, "tgl_Latn")
|
| 56 |
|
|
|
|
|
|
|
| 57 |
# Set source language context
|
| 58 |
tokenizer.src_lang = src_code
|
| 59 |
inputs = tokenizer(working_text, return_tensors="pt").to(device)
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
with torch.no_grad():
|
| 65 |
output_tokens = model.generate(
|
|
@@ -71,12 +84,15 @@ def universal_translator(text, audio_path, source_lang_name, target_lang_name):
|
|
| 71 |
|
| 72 |
translation = tokenizer.batch_decode(output_tokens, skip_special_tokens=True)[0].strip()
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
return working_text, translation
|
| 76 |
|
| 77 |
# 3. Gradio Interface Layout
|
| 78 |
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 79 |
-
gr.Markdown("DialectGo Unified Translation System")
|
|
|
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
with gr.Column():
|
|
@@ -87,11 +103,9 @@ with gr.Blocks(theme=gr.themes.Base()) as demo:
|
|
| 87 |
submit_btn = gr.Button("Process Translation", variant="primary")
|
| 88 |
|
| 89 |
with gr.Column():
|
| 90 |
-
# MODIFICATION: Added text block interface container to capture the output transcript
|
| 91 |
transcription_out = gr.Textbox(label="Transcribed/Source Text", lines=3, interactive=False)
|
| 92 |
output_text = gr.Textbox(label="Translation Result", lines=4, interactive=False)
|
| 93 |
|
| 94 |
-
# MODIFICATION: Changed outputs array to map data cleanly to BOTH UI textboxes
|
| 95 |
submit_btn.click(
|
| 96 |
fn=universal_translator,
|
| 97 |
inputs=[text_in, audio_in, src_lang, tgt_lang],
|
|
@@ -99,4 +113,4 @@ with gr.Blocks(theme=gr.themes.Base()) as demo:
|
|
| 99 |
api_name="translate"
|
| 100 |
)
|
| 101 |
|
| 102 |
-
demo.launch()
|
|
|
|
| 11 |
print("Loading Whisper STT Model...")
|
| 12 |
whisper_model = whisper.load_model("small", device=device)
|
| 13 |
|
| 14 |
+
model_path = "Akariieieie/DialectGo-NLLB"
|
| 15 |
print(f"Loading custom NLLB model from {model_path}...")
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 17 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_path).to(device)
|
|
|
|
| 54 |
src_code = LANG_CODES.get(source_lang_name, "eng_Latn")
|
| 55 |
tgt_code = LANG_CODES.get(target_lang_name, "tgl_Latn")
|
| 56 |
|
| 57 |
+
print(f"Translating from {source_lang_name} ({src_code}) → {target_lang_name} ({tgt_code})")
|
| 58 |
+
|
| 59 |
# Set source language context
|
| 60 |
tokenizer.src_lang = src_code
|
| 61 |
inputs = tokenizer(working_text, return_tensors="pt").to(device)
|
| 62 |
|
| 63 |
+
# FIX: Use lang_code_to_id instead of convert_tokens_to_ids.
|
| 64 |
+
# convert_tokens_to_ids returns 0 (UNK) if the language code is not found as a
|
| 65 |
+
# regular vocab token, causing the model to hallucinate in its dominant language (Cebuano).
|
| 66 |
+
# lang_code_to_id correctly resolves NLLB language codes.
|
| 67 |
+
if hasattr(tokenizer, 'lang_code_to_id') and tgt_code in tokenizer.lang_code_to_id:
|
| 68 |
+
forced_bos_token_id = tokenizer.lang_code_to_id[tgt_code]
|
| 69 |
+
else:
|
| 70 |
+
# Fallback: try convert_tokens_to_ids and warn if it returns UNK (0)
|
| 71 |
+
forced_bos_token_id = tokenizer.convert_tokens_to_ids(tgt_code)
|
| 72 |
+
if forced_bos_token_id == tokenizer.unk_token_id or forced_bos_token_id == 0:
|
| 73 |
+
print(f"WARNING: Could not resolve token ID for '{tgt_code}'. Translation may be incorrect.")
|
| 74 |
+
|
| 75 |
+
print(f"Resolved forced_bos_token_id for {tgt_code}: {forced_bos_token_id}")
|
| 76 |
|
| 77 |
with torch.no_grad():
|
| 78 |
output_tokens = model.generate(
|
|
|
|
| 84 |
|
| 85 |
translation = tokenizer.batch_decode(output_tokens, skip_special_tokens=True)[0].strip()
|
| 86 |
|
| 87 |
+
print(f"Translation result: {translation}")
|
| 88 |
+
|
| 89 |
+
# Return both the transcription text and translation text
|
| 90 |
return working_text, translation
|
| 91 |
|
| 92 |
# 3. Gradio Interface Layout
|
| 93 |
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 94 |
+
gr.Markdown("# 🇵🇭 DialectGo Unified Translation System")
|
| 95 |
+
gr.Markdown("This space functions as both a web UI and an active API endpoint for multi-way voice/text translation.")
|
| 96 |
|
| 97 |
with gr.Row():
|
| 98 |
with gr.Column():
|
|
|
|
| 103 |
submit_btn = gr.Button("Process Translation", variant="primary")
|
| 104 |
|
| 105 |
with gr.Column():
|
|
|
|
| 106 |
transcription_out = gr.Textbox(label="Transcribed/Source Text", lines=3, interactive=False)
|
| 107 |
output_text = gr.Textbox(label="Translation Result", lines=4, interactive=False)
|
| 108 |
|
|
|
|
| 109 |
submit_btn.click(
|
| 110 |
fn=universal_translator,
|
| 111 |
inputs=[text_in, audio_in, src_lang, tgt_lang],
|
|
|
|
| 113 |
api_name="translate"
|
| 114 |
)
|
| 115 |
|
| 116 |
+
demo.launch()
|