Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,15 +16,19 @@ LANG_CODES = {
|
|
| 16 |
"Cebuano": "ceb_Latn"
|
| 17 |
}
|
| 18 |
|
| 19 |
-
def translate_text(text, target_lang_name):
|
| 20 |
if not text.strip():
|
| 21 |
return "Please enter text to translate."
|
| 22 |
|
|
|
|
|
|
|
| 23 |
target_code = LANG_CODES[target_lang_name]
|
| 24 |
|
| 25 |
-
#
|
|
|
|
| 26 |
inputs = tokenizer(text, return_tensors="pt")
|
| 27 |
|
|
|
|
| 28 |
translated_tokens = model.generate(
|
| 29 |
**inputs,
|
| 30 |
forced_bos_token_id=tokenizer.convert_tokens_to_ids(target_code),
|
|
@@ -36,8 +40,8 @@ def translate_text(text, target_lang_name):
|
|
| 36 |
|
| 37 |
# 3. Build the UI Layout
|
| 38 |
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 39 |
-
gr.Markdown("# 🇵🇭 DialectGo Translator")
|
| 40 |
-
gr.Markdown("Translate seamlessly between English, Tagalog, and Cebuano
|
| 41 |
|
| 42 |
with gr.Row():
|
| 43 |
with gr.Column():
|
|
@@ -46,11 +50,20 @@ with gr.Blocks(theme=gr.themes.Base()) as demo:
|
|
| 46 |
placeholder="Type your text here...",
|
| 47 |
lines=4
|
| 48 |
)
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
submit_btn = gr.Button("Translate", variant="primary")
|
| 55 |
|
| 56 |
with gr.Column():
|
|
@@ -60,9 +73,10 @@ with gr.Blocks(theme=gr.themes.Base()) as demo:
|
|
| 60 |
interactive=False
|
| 61 |
)
|
| 62 |
|
|
|
|
| 63 |
submit_btn.click(
|
| 64 |
fn=translate_text,
|
| 65 |
-
inputs=[input_text, target_lang],
|
| 66 |
outputs=output_text
|
| 67 |
)
|
| 68 |
|
|
|
|
| 16 |
"Cebuano": "ceb_Latn"
|
| 17 |
}
|
| 18 |
|
| 19 |
+
def translate_text(text, source_lang_name, target_lang_name):
|
| 20 |
if not text.strip():
|
| 21 |
return "Please enter text to translate."
|
| 22 |
|
| 23 |
+
# Get the correct NLLB language tokens
|
| 24 |
+
source_code = LANG_CODES[source_lang_name]
|
| 25 |
target_code = LANG_CODES[target_lang_name]
|
| 26 |
|
| 27 |
+
# Force the tokenizer to use the selected source language prefix
|
| 28 |
+
tokenizer.src_lang = source_code
|
| 29 |
inputs = tokenizer(text, return_tensors="pt")
|
| 30 |
|
| 31 |
+
# Generate translation targeting the destination language prefix
|
| 32 |
translated_tokens = model.generate(
|
| 33 |
**inputs,
|
| 34 |
forced_bos_token_id=tokenizer.convert_tokens_to_ids(target_code),
|
|
|
|
| 40 |
|
| 41 |
# 3. Build the UI Layout
|
| 42 |
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 43 |
+
gr.Markdown("# 🇵🇭 DialectGo Multi-Way Translator")
|
| 44 |
+
gr.Markdown("Translate seamlessly between English, Tagalog, and Cebuano in any direction.")
|
| 45 |
|
| 46 |
with gr.Row():
|
| 47 |
with gr.Column():
|
|
|
|
| 50 |
placeholder="Type your text here...",
|
| 51 |
lines=4
|
| 52 |
)
|
| 53 |
+
|
| 54 |
+
# Sub-layout to put selectors side-by-side
|
| 55 |
+
with gr.Row():
|
| 56 |
+
source_lang = gr.Dropdown(
|
| 57 |
+
choices=["English", "Tagalog", "Cebuano"],
|
| 58 |
+
value="English",
|
| 59 |
+
label="Translate From"
|
| 60 |
+
)
|
| 61 |
+
target_lang = gr.Dropdown(
|
| 62 |
+
choices=["English", "Tagalog", "Cebuano"],
|
| 63 |
+
value="Tagalog",
|
| 64 |
+
label="Translate To"
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
submit_btn = gr.Button("Translate", variant="primary")
|
| 68 |
|
| 69 |
with gr.Column():
|
|
|
|
| 73 |
interactive=False
|
| 74 |
)
|
| 75 |
|
| 76 |
+
# Include both language variables in inputs array
|
| 77 |
submit_btn.click(
|
| 78 |
fn=translate_text,
|
| 79 |
+
inputs=[input_text, source_lang, target_lang],
|
| 80 |
outputs=output_text
|
| 81 |
)
|
| 82 |
|