Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from typing import Union, List
|
4 |
+
|
5 |
+
class BambaraTranslator:
|
6 |
+
def __init__(self, model_name: str = "sudoping01/nllb-bambara-v2"):
|
7 |
+
self.translator = pipeline(
|
8 |
+
"translation",
|
9 |
+
model=model_name,
|
10 |
+
max_length=512,
|
11 |
+
truncation=True
|
12 |
+
device=-1 # Force CPU
|
13 |
+
)
|
14 |
+
self.flores_codes = {
|
15 |
+
"French": "fra_Latn",
|
16 |
+
"English": "eng_Latn",
|
17 |
+
"Bambara": "bam_Latn"
|
18 |
+
}
|
19 |
+
|
20 |
+
def translate(self, text: Union[str, List[str]], src_lang: str, tgt_lang: str) -> Union[str, List[str]]:
|
21 |
+
source_lang = self.flores_codes[src_lang]
|
22 |
+
target_lang = self.flores_codes[tgt_lang]
|
23 |
+
if isinstance(text, str):
|
24 |
+
translation = self.translator(text, src_lang=source_lang, tgt_lang=target_lang)
|
25 |
+
return str(translation[0]['translation_text'])
|
26 |
+
translations = self.translator(text, src_lang=source_lang, tgt_lang=target_lang)
|
27 |
+
return [str(t['translation_text']) for t in translations]
|
28 |
+
|
29 |
+
# Initialize translator
|
30 |
+
translator = BambaraTranslator()
|
31 |
+
|
32 |
+
# Three examples (Bambara, French, English)
|
33 |
+
examples = [
|
34 |
+
"An filɛ ni ye yɔrɔ minna ni an ye an sigi ka a layɛ yala an bɛ ka baara min kɛ ɛsike a kɛlen don ka Ɲɛ wa ? Bɛɛ ka kan ka i jɔyɔrɔ fa walasa an ka se ka taa Ɲɛ",
|
35 |
+
"Le Mali est un pays riche en culture mais confronté à de nombreux défis.",
|
36 |
+
"The sun rises every morning to bring light to the world."
|
37 |
+
]
|
38 |
+
|
39 |
+
def translate_text(src_lang, tgt_lang, text):
|
40 |
+
if not text.strip():
|
41 |
+
return "Please enter text to translate."
|
42 |
+
if src_lang == tgt_lang:
|
43 |
+
return "Source and target languages must be different."
|
44 |
+
try:
|
45 |
+
result = translator.translate(text, src_lang, tgt_lang)
|
46 |
+
return result
|
47 |
+
except Exception as e:
|
48 |
+
return f"Error: {str(e)}"
|
49 |
+
|
50 |
+
with gr.Blocks(title="Bambara Translator") as demo:
|
51 |
+
gr.Markdown(
|
52 |
+
"""
|
53 |
+
# 🇲🇱 Bambara Translator
|
54 |
+
|
55 |
+
Translate between Bambara, French, and English instantly.
|
56 |
+
|
57 |
+
|
58 |
+
## How to Use
|
59 |
+
1. Pick source and target languages
|
60 |
+
2. Enter text or select an example
|
61 |
+
3. Click "Translate" to see the result
|
62 |
+
"""
|
63 |
+
)
|
64 |
+
|
65 |
+
with gr.Row():
|
66 |
+
src_lang = gr.Dropdown(choices=["Bambara", "French", "English"], label="Source Language", value="Bambara")
|
67 |
+
tgt_lang = gr.Dropdown(choices=["Bambara", "French", "English"], label="Target Language", value="French")
|
68 |
+
with gr.Column():
|
69 |
+
text_input = gr.Textbox(label="Input Text", lines=5, placeholder="Type your text here...")
|
70 |
+
example_dropdown = gr.Dropdown(
|
71 |
+
choices=examples,
|
72 |
+
label="Examples",
|
73 |
+
value=examples[0]
|
74 |
+
)
|
75 |
+
|
76 |
+
translate_btn = gr.Button("Translate", variant="primary")
|
77 |
+
output = gr.Textbox(label="Translation", lines=3)
|
78 |
+
|
79 |
+
gr.Markdown(
|
80 |
+
"""
|
81 |
+
By [sudoping01](https://huggingface.co/sudoping01), from [sudoping01/nllb-bambara-v2](https://huggingface.co/sudoping01/nllb-bambara-v2). Fine-tuned on Meta’s NLLB, CC BY-NC 4.0, non-commercial.
|
82 |
+
"""
|
83 |
+
)
|
84 |
+
|
85 |
+
# Connect functions
|
86 |
+
translate_btn.click(
|
87 |
+
fn=translate_text,
|
88 |
+
inputs=[src_lang, tgt_lang, text_input],
|
89 |
+
outputs=output
|
90 |
+
)
|
91 |
+
example_dropdown.change(
|
92 |
+
fn=lambda x: x,
|
93 |
+
inputs=example_dropdown,
|
94 |
+
outputs=text_input
|
95 |
+
)
|
96 |
+
|
97 |
+
demo.launch()
|