Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,356 Bytes
9331fff 035d3fc 8bb46f0 035d3fc 7343e3f 8bb46f0 035d3fc 8bb46f0 035d3fc 8bb46f0 035d3fc 8bb46f0 7343e3f 035d3fc 8bb46f0 035d3fc 56749bd 8bb46f0 035d3fc 8bb46f0 035d3fc 8bb46f0 035d3fc 8bb46f0 035d3fc 8bb46f0 035d3fc 8bb46f0 035d3fc 8bb46f0 1b7efab 742237b 8bb46f0 035d3fc 8bb46f0 035d3fc 8bb46f0 |
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 44 45 46 47 48 49 50 51 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
import gradio as gr
from transformers import pipeline
import torch
import logging
import spaces
from typing import Union, List
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
if torch.cuda.is_available():
device = "cuda"
logger.info("Using CUDA for inference.")
elif torch.backends.mps.is_available():
device = "mps"
logger.info("Using MPS for inference.")
else:
device = "cpu"
logger.info("Using CPU for inference.")
class BambaraTranslator:
def __init__(self, model_name: str = "sudoping01/nllb-bambara-v2"):
self.translator = pipeline(
"translation",
model=model_name,
device=device,
max_length=512,
truncation=True
)
self.flores_codes = {
"French": "fra_Latn",
"English": "eng_Latn",
"Bambara": "bam_Latn"
}
logger.info("Translation pipeline initialized successfully.")
def translate(self, text: Union[str, List[str]], src_lang: str, tgt_lang: str) -> Union[str, List[str]]:
source_lang = self.flores_codes[src_lang]
target_lang = self.flores_codes[tgt_lang]
logger.info(f"Translating text from {source_lang} to {target_lang}.")
try:
if isinstance(text, str):
translation = self.translator(text, src_lang=source_lang, tgt_lang=target_lang, num_beams=2)
return str(translation[0]['translation_text'])
else:
translations = self.translator(text, src_lang=source_lang, tgt_lang=target_lang, num_beams=2)
return [str(t['translation_text']) for t in translations]
except Exception as e:
logger.error(f"Translation failed: {e}")
return "An error occurred during translation."
translator = BambaraTranslator()
examples = [
["Gafe kalan ka di Saratu ye. Saratu bɛ gafew kalan minnu siginidenw ye wow ye, a bɛ se ka minnu kalan ni a bolonkɔninw ye. O sɛbɛnni cogo in bɛ wele ko barayi. Saratu bɛ se ka gafe kalan i n'a fɔ denmisɛn min bɛ yeli kɛ.", "Bambara", "French"],
["Le Mali est un pays riche en culture mais confronté à de nombreux défis.", "French", "Bambara"],
["The sun rises every morning to bring light to the world.", "English", "Bambara"],
["Good morning", "English", "Bambara"],
]
@spaces.GPU()
def translate_text(text: str, src_lang: str, tgt_lang: str) -> str:
"""
Translate the input text from the source language to the target language.
"""
if not text.strip():
return "Please enter text to translate."
if src_lang == tgt_lang:
return "Source and target languages must be different."
try:
result = translator.translate(text, src_lang, tgt_lang)
logger.info("Translation successful.")
return result
except Exception as e:
logger.error(f"Translation failed: {e}")
return f"Error: {str(e)}"
def build_interface():
"""
Builds the Gradio interface for translating text between supported languages.
"""
with gr.Blocks(title="Bambara Translator") as demo:
gr.Markdown(
"""
# 🇲🇱 Bambara Translator
Translate between Bambara, French, and English instantly using NLLB model.
## How to Use
1. Select source and target languages from the dropdowns
2. Enter your text or choose from examples
3. Click "Translate" to see the result
"""
)
with gr.Row():
with gr.Column():
text_input = gr.Textbox(
lines=5,
label="Text to Translate",
placeholder="Enter text here..."
)
with gr.Row():
src_lang = gr.Dropdown(
choices=["Bambara", "French", "English"],
label="Source Language",
value="Bambara"
)
tgt_lang = gr.Dropdown(
choices=["Bambara", "French", "English"],
label="Target Language",
value="French"
)
translate_btn = gr.Button("Translate", variant="primary")
with gr.Column():
output = gr.Textbox(label="Translation", lines=5, interactive=False)
# Examples section
gr.Examples(
examples=examples,
inputs=[text_input, src_lang, tgt_lang],
outputs=output,
fn=translate_text,
cache_examples=False
)
gr.Markdown(
"""
**License:** CC BY-NC 4.0
**Based on:** Meta's NLLB (No Language Left Behind)
"""
)
translate_btn.click(
fn=translate_text,
inputs=[text_input, src_lang, tgt_lang],
outputs=output
)
return demo
if __name__ == "__main__":
logger.info("Starting the Gradio interface for the Bambara translator.")
interface = build_interface()
interface.launch()
logger.info("Gradio interface running.") |