Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,36 +2,42 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# 1.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
hf_token = os.environ.get("HF_TOKEN")
|
| 7 |
model_id = "arnoldramo/garifuna-nllb"
|
| 8 |
|
| 9 |
-
|
| 10 |
-
translator = pipeline(
|
| 11 |
-
"translation",
|
| 12 |
-
model=model_id,
|
| 13 |
-
token=hf_token
|
| 14 |
-
)
|
| 15 |
-
print("Pipeline loaded successfully!")
|
| 16 |
|
| 17 |
def respond(message, history, source_lang):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
lang_map = {"English": "eng_Latn", "Spanish": "spa_Latn"}
|
| 19 |
src_code = lang_map.get(source_lang, "eng_Latn")
|
| 20 |
|
| 21 |
-
# Run translation
|
| 22 |
result = translator(message, src_lang=src_code, tgt_lang="cab_Latn")
|
| 23 |
return result[0]['translation_text']
|
| 24 |
|
| 25 |
-
# 2. Corrected Interface (Fixes the theme error)
|
| 26 |
demo = gr.ChatInterface(
|
| 27 |
fn=respond,
|
| 28 |
title="Garifuna Language Assistant",
|
| 29 |
-
description="Translate English or Spanish to Garifuna.",
|
| 30 |
additional_inputs=[
|
| 31 |
gr.Dropdown(label="Source Language", choices=["English", "Spanish"], value="English")
|
| 32 |
]
|
| 33 |
)
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
| 36 |
-
# We apply the theme here or leave it default to ensure it boots
|
| 37 |
demo.launch()
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# 1. YOUR CUSTOM DICTIONARY (Add more words here as you find them)
|
| 6 |
+
# Format: "English/Spanish Word": "Correct Garifuna"
|
| 7 |
+
CUSTOM_MAPPINGS = {
|
| 8 |
+
"morbid": "wiyeti samina",
|
| 9 |
+
"m贸rbido": "wiyeti samina",
|
| 10 |
+
"punching bag": "d眉s眉 saki", # Example fix
|
| 11 |
+
"hola": "h眉r眉ti",
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
hf_token = os.environ.get("HF_TOKEN")
|
| 15 |
model_id = "arnoldramo/garifuna-nllb"
|
| 16 |
|
| 17 |
+
translator = pipeline("translation", model=model_id, token=hf_token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def respond(message, history, source_lang):
|
| 20 |
+
# Normalize the input (lowercase and remove extra spaces)
|
| 21 |
+
clean_message = message.lower().strip()
|
| 22 |
+
|
| 23 |
+
# 2. CHECK CUSTOM DICTIONARY FIRST
|
| 24 |
+
if clean_message in CUSTOM_MAPPINGS:
|
| 25 |
+
return CUSTOM_MAPPINGS[clean_message]
|
| 26 |
+
|
| 27 |
+
# 3. OTHERWISE, ASK THE AI
|
| 28 |
lang_map = {"English": "eng_Latn", "Spanish": "spa_Latn"}
|
| 29 |
src_code = lang_map.get(source_lang, "eng_Latn")
|
| 30 |
|
|
|
|
| 31 |
result = translator(message, src_lang=src_code, tgt_lang="cab_Latn")
|
| 32 |
return result[0]['translation_text']
|
| 33 |
|
|
|
|
| 34 |
demo = gr.ChatInterface(
|
| 35 |
fn=respond,
|
| 36 |
title="Garifuna Language Assistant",
|
|
|
|
| 37 |
additional_inputs=[
|
| 38 |
gr.Dropdown(label="Source Language", choices=["English", "Spanish"], value="English")
|
| 39 |
]
|
| 40 |
)
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|
|
|
|
| 43 |
demo.launch()
|