File size: 1,297 Bytes
099a699
 
 
 
c452ba3
 
 
 
 
 
 
 
 
78670f4
099a699
 
c452ba3
099a699
78670f4
c452ba3
 
 
 
 
 
 
 
78670f4
099a699
 
78670f4
 
099a699
 
78670f4
 
099a699
78670f4
 
099a699
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import gradio as gr
from transformers import pipeline
import os

# 1. YOUR CUSTOM DICTIONARY (Add more words here as you find them)
# Format: "English/Spanish Word": "Correct Garifuna"
CUSTOM_MAPPINGS = {
    "morbid": "wiyeti samina",
    "mórbido": "wiyeti samina",
    "punching bag": "düsü saki", # Example fix
    "hola": "hürüti",
}

hf_token = os.environ.get("HF_TOKEN")
model_id = "arnoldramo/garifuna-nllb"

translator = pipeline("translation", model=model_id, token=hf_token)

def respond(message, history, source_lang):
    # Normalize the input (lowercase and remove extra spaces)
    clean_message = message.lower().strip()
    
    # 2. CHECK CUSTOM DICTIONARY FIRST
    if clean_message in CUSTOM_MAPPINGS:
        return CUSTOM_MAPPINGS[clean_message]
    
    # 3. OTHERWISE, ASK THE AI
    lang_map = {"English": "eng_Latn", "Spanish": "spa_Latn"}
    src_code = lang_map.get(source_lang, "eng_Latn")
    
    result = translator(message, src_lang=src_code, tgt_lang="cab_Latn")
    return result[0]['translation_text']

demo = gr.ChatInterface(
    fn=respond,
    title="Garifuna Language Assistant",
    additional_inputs=[
        gr.Dropdown(label="Source Language", choices=["English", "Spanish"], value="English")
    ]
)

if __name__ == "__main__":
    demo.launch()