Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,97 +1,155 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import spaces
|
| 3 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
| 4 |
from typing import Union, List
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
class BambaraTranslator:
|
| 7 |
def __init__(self, model_name: str = "sudoping01/nllb-bambara-v2"):
|
| 8 |
self.translator = pipeline(
|
| 9 |
"translation",
|
| 10 |
model=model_name,
|
|
|
|
| 11 |
max_length=512,
|
| 12 |
-
truncation=True
|
| 13 |
-
device=0 if spaces.config.Hardware.current == "cuda" else -1
|
| 14 |
)
|
| 15 |
self.flores_codes = {
|
| 16 |
"French": "fra_Latn",
|
| 17 |
"English": "eng_Latn",
|
| 18 |
"Bambara": "bam_Latn"
|
| 19 |
}
|
|
|
|
| 20 |
|
| 21 |
-
@spaces.GPU
|
| 22 |
def translate(self, text: Union[str, List[str]], src_lang: str, tgt_lang: str) -> Union[str, List[str]]:
|
| 23 |
source_lang = self.flores_codes[src_lang]
|
| 24 |
target_lang = self.flores_codes[tgt_lang]
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
# Initialize translator
|
| 32 |
translator = BambaraTranslator()
|
| 33 |
|
| 34 |
-
# Three examples (Bambara, French, English)
|
| 35 |
examples = [
|
| 36 |
-
"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 Ɲɛ",
|
| 37 |
-
"Le Mali est un pays riche en culture mais confronté à de nombreux défis.",
|
| 38 |
-
"The sun rises every morning to bring light to the world."
|
|
|
|
| 39 |
]
|
| 40 |
|
| 41 |
-
@spaces.GPU
|
| 42 |
-
def translate_text(src_lang, tgt_lang
|
|
|
|
|
|
|
|
|
|
| 43 |
if not text.strip():
|
| 44 |
return "Please enter text to translate."
|
|
|
|
| 45 |
if src_lang == tgt_lang:
|
| 46 |
return "Source and target languages must be different."
|
|
|
|
| 47 |
try:
|
| 48 |
result = translator.translate(text, src_lang, tgt_lang)
|
|
|
|
| 49 |
return result
|
| 50 |
except Exception as e:
|
|
|
|
| 51 |
return f"Error: {str(e)}"
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
+
import logging
|
| 5 |
+
import spaces
|
| 6 |
from typing import Union, List
|
| 7 |
|
| 8 |
+
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
if torch.cuda.is_available():
|
| 13 |
+
device = "cuda"
|
| 14 |
+
logger.info("Using CUDA for inference.")
|
| 15 |
+
elif torch.backends.mps.is_available():
|
| 16 |
+
device = "mps"
|
| 17 |
+
logger.info("Using MPS for inference.")
|
| 18 |
+
else:
|
| 19 |
+
device = "cpu"
|
| 20 |
+
logger.info("Using CPU for inference.")
|
| 21 |
+
|
| 22 |
class BambaraTranslator:
|
| 23 |
def __init__(self, model_name: str = "sudoping01/nllb-bambara-v2"):
|
| 24 |
self.translator = pipeline(
|
| 25 |
"translation",
|
| 26 |
model=model_name,
|
| 27 |
+
device=device,
|
| 28 |
max_length=512,
|
| 29 |
+
truncation=True
|
|
|
|
| 30 |
)
|
| 31 |
self.flores_codes = {
|
| 32 |
"French": "fra_Latn",
|
| 33 |
"English": "eng_Latn",
|
| 34 |
"Bambara": "bam_Latn"
|
| 35 |
}
|
| 36 |
+
logger.info("Translation pipeline initialized successfully.")
|
| 37 |
|
|
|
|
| 38 |
def translate(self, text: Union[str, List[str]], src_lang: str, tgt_lang: str) -> Union[str, List[str]]:
|
| 39 |
source_lang = self.flores_codes[src_lang]
|
| 40 |
target_lang = self.flores_codes[tgt_lang]
|
| 41 |
+
|
| 42 |
+
logger.info(f"Translating text from {source_lang} to {target_lang}.")
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
if isinstance(text, str):
|
| 46 |
+
translation = self.translator(text, src_lang=source_lang, tgt_lang=target_lang, num_beams=2)
|
| 47 |
+
return str(translation[0]['translation_text'])
|
| 48 |
+
else:
|
| 49 |
+
translations = self.translator(text, src_lang=source_lang, tgt_lang=target_lang, num_beams=2)
|
| 50 |
+
return [str(t['translation_text']) for t in translations]
|
| 51 |
+
except Exception as e:
|
| 52 |
+
logger.error(f"Translation failed: {e}")
|
| 53 |
+
return "An error occurred during translation."
|
| 54 |
+
|
| 55 |
|
|
|
|
| 56 |
translator = BambaraTranslator()
|
| 57 |
|
|
|
|
| 58 |
examples = [
|
| 59 |
+
["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 Ɲɛ", "Bambara", "French"],
|
| 60 |
+
["Le Mali est un pays riche en culture mais confronté à de nombreux défis.", "French", "Bambara"],
|
| 61 |
+
["The sun rises every morning to bring light to the world.", "English", "Bambara"],
|
| 62 |
+
["Good morning", "English", "Bambara"],
|
| 63 |
]
|
| 64 |
|
| 65 |
+
@spaces.GPU()
|
| 66 |
+
def translate_text(text: str, src_lang: str, tgt_lang: str) -> str:
|
| 67 |
+
"""
|
| 68 |
+
Translate the input text from the source language to the target language.
|
| 69 |
+
"""
|
| 70 |
if not text.strip():
|
| 71 |
return "Please enter text to translate."
|
| 72 |
+
|
| 73 |
if src_lang == tgt_lang:
|
| 74 |
return "Source and target languages must be different."
|
| 75 |
+
|
| 76 |
try:
|
| 77 |
result = translator.translate(text, src_lang, tgt_lang)
|
| 78 |
+
logger.info("Translation successful.")
|
| 79 |
return result
|
| 80 |
except Exception as e:
|
| 81 |
+
logger.error(f"Translation failed: {e}")
|
| 82 |
return f"Error: {str(e)}"
|
| 83 |
|
| 84 |
+
def build_interface():
|
| 85 |
+
"""
|
| 86 |
+
Builds the Gradio interface for translating text between supported languages.
|
| 87 |
+
"""
|
| 88 |
+
with gr.Blocks(title="Bambara Translator") as demo:
|
| 89 |
+
gr.Markdown(
|
| 90 |
+
"""
|
| 91 |
+
# 🇲🇱 Bambara Translator
|
| 92 |
+
Translate between Bambara, French, and English instantly using NLLB model.
|
| 93 |
+
|
| 94 |
+
## How to Use
|
| 95 |
+
1. Select source and target languages from the dropdowns
|
| 96 |
+
2. Enter your text or choose from examples
|
| 97 |
+
3. Click "Translate" to see the result
|
| 98 |
+
"""
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
with gr.Row():
|
| 102 |
+
with gr.Column():
|
| 103 |
+
text_input = gr.Textbox(
|
| 104 |
+
lines=5,
|
| 105 |
+
label="Text to Translate",
|
| 106 |
+
placeholder="Enter text here..."
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
with gr.Row():
|
| 110 |
+
src_lang = gr.Dropdown(
|
| 111 |
+
choices=["Bambara", "French", "English"],
|
| 112 |
+
label="Source Language",
|
| 113 |
+
value="Bambara"
|
| 114 |
+
)
|
| 115 |
+
tgt_lang = gr.Dropdown(
|
| 116 |
+
choices=["Bambara", "French", "English"],
|
| 117 |
+
label="Target Language",
|
| 118 |
+
value="French"
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
translate_btn = gr.Button("Translate", variant="primary")
|
| 122 |
+
|
| 123 |
+
with gr.Column():
|
| 124 |
+
output = gr.Textbox(label="Translation", lines=5, interactive=False)
|
| 125 |
+
|
| 126 |
+
# Examples section
|
| 127 |
+
gr.Examples(
|
| 128 |
+
examples=examples,
|
| 129 |
+
inputs=[text_input, src_lang, tgt_lang],
|
| 130 |
+
outputs=output,
|
| 131 |
+
fn=translate_text,
|
| 132 |
+
cache_examples=False
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
gr.Markdown(
|
| 136 |
+
"""
|
| 137 |
+
**Model:** [sudoping01/nllb-bambara-v2](https://huggingface.co/sudoping01/nllb-bambara-v2)
|
| 138 |
+
**License:** CC BY-NC 4.0 (Non-commercial use)
|
| 139 |
+
**Based on:** Meta's NLLB (No Language Left Behind)
|
| 140 |
+
"""
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
translate_btn.click(
|
| 144 |
+
fn=translate_text,
|
| 145 |
+
inputs=[text_input, src_lang, tgt_lang],
|
| 146 |
+
outputs=output
|
| 147 |
+
)
|
| 148 |
|
| 149 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
if __name__ == "__main__":
|
| 152 |
+
logger.info("Starting the Gradio interface for the Bambara translator.")
|
| 153 |
+
interface = build_interface()
|
| 154 |
+
interface.launch()
|
| 155 |
+
logger.info("Gradio interface running.")
|