TGPro1 commited on
Commit ยท
c4f4c46
1
Parent(s): 83a7864
Deploy NLLB-200 Translation API with Gradio
Browse files- README.md +67 -5
- app.py +234 -4
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -1,12 +1,74 @@
|
|
| 1 |
---
|
| 2 |
title: NLLB200
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: NLLB200
|
| 3 |
+
emoji: ๐
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# NLLB-200 Translation API
|
| 14 |
+
|
| 15 |
+
High-quality translation API powered by Meta's **No Language Left Behind (NLLB-200)** model.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
|
| 19 |
+
- โ
**200 Languages Supported** - Direct translation between any language pair
|
| 20 |
+
- โ
**44% Better Quality** - Compared to previous translation models
|
| 21 |
+
- โ
**+70% for Complex Languages** - Especially Arabic, Hindi, and other low-resource languages
|
| 22 |
+
- โ
**No Pivot Translation** - Direct translation without going through English
|
| 23 |
+
- โ
**Cached Results** - Faster repeated translations
|
| 24 |
+
- โ
**API Access** - Use via Gradio Client or HTTP
|
| 25 |
+
|
| 26 |
+
## Supported Languages (Sample)
|
| 27 |
+
|
| 28 |
+
Arabic, English, French, Spanish, German, Italian, Portuguese, Russian, Japanese, Korean, Chinese, Hindi, Turkish, Dutch, Polish, Swedish, Indonesian, Vietnamese, Thai, Ukrainian, Romanian, Greek, Hebrew, and more!
|
| 29 |
+
|
| 30 |
+
## Usage
|
| 31 |
+
|
| 32 |
+
### Web Interface
|
| 33 |
+
|
| 34 |
+
Visit the Space and use the interactive interface to translate text between any supported languages.
|
| 35 |
+
|
| 36 |
+
### API
|
| 37 |
+
|
| 38 |
+
```python
|
| 39 |
+
from gradio_client import Client
|
| 40 |
+
|
| 41 |
+
client = Client("TGPro1/NLLB200")
|
| 42 |
+
result = client.predict(
|
| 43 |
+
"Hello, world!", # text
|
| 44 |
+
"English", # source language
|
| 45 |
+
"Arabic", # target language
|
| 46 |
+
api_name="/predict"
|
| 47 |
+
)
|
| 48 |
+
print(result) # ู
ุฑุญุจุง ุจุงูุนุงูู
!
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
## Model
|
| 52 |
+
|
| 53 |
+
- **Model**: `facebook/nllb-200-distilled-600M`
|
| 54 |
+
- **Parameters**: 600M
|
| 55 |
+
- **Size**: ~2.4GB
|
| 56 |
+
- **Languages**: 200
|
| 57 |
+
|
| 58 |
+
## Performance
|
| 59 |
+
|
| 60 |
+
- **Quality**: State-of-the-art for low-resource languages
|
| 61 |
+
- **Speed**: Fast inference with distilled model
|
| 62 |
+
- **Cache**: LRU cache for frequently translated phrases
|
| 63 |
+
|
| 64 |
+
## Credits
|
| 65 |
+
|
| 66 |
+
- **Model**: Meta AI Research - [NLLB Project](https://ai.meta.com/research/no-language-left-behind/)
|
| 67 |
+
- **Benchmark**: FLORES-200
|
| 68 |
+
- **License**: Apache 2.0
|
| 69 |
+
|
| 70 |
+
## Links
|
| 71 |
+
|
| 72 |
+
- [Model Card](https://huggingface.co/facebook/nllb-200-distilled-600M)
|
| 73 |
+
- [Research Paper](https://arxiv.org/abs/2207.04672)
|
| 74 |
+
- [Full Language List](https://github.com/facebookresearch/flores/blob/main/flores200/README.md)
|
app.py
CHANGED
|
@@ -1,7 +1,237 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
import torch
|
| 4 |
+
from functools import lru_cache
|
| 5 |
|
| 6 |
+
# Language mappings for NLLB-200
|
| 7 |
+
LANGUAGE_CODES = {
|
| 8 |
+
"Arabic": "arb_Arab",
|
| 9 |
+
"English": "eng_Latn",
|
| 10 |
+
"French": "fra_Latn",
|
| 11 |
+
"Spanish": "spa_Latn",
|
| 12 |
+
"German": "deu_Latn",
|
| 13 |
+
"Italian": "ita_Latn",
|
| 14 |
+
"Portuguese": "por_Latn",
|
| 15 |
+
"Russian": "rus_Cyrl",
|
| 16 |
+
"Japanese": "jpn_Jpan",
|
| 17 |
+
"Korean": "kor_Hang",
|
| 18 |
+
"Chinese (Simplified)": "zho_Hans",
|
| 19 |
+
"Hindi": "hin_Deva",
|
| 20 |
+
"Turkish": "tur_Latn",
|
| 21 |
+
"Dutch": "nld_Latn",
|
| 22 |
+
"Polish": "pol_Latn",
|
| 23 |
+
"Swedish": "swe_Latn",
|
| 24 |
+
"Arabic (Egyptian)": "arz_Arab",
|
| 25 |
+
"Arabic (Moroccan)": "ary_Arab",
|
| 26 |
+
"Indonesian": "ind_Latn",
|
| 27 |
+
"Vietnamese": "vie_Latn",
|
| 28 |
+
"Thai": "tha_Thai",
|
| 29 |
+
"Ukrainian": "ukr_Cyrl",
|
| 30 |
+
"Romanian": "ron_Latn",
|
| 31 |
+
"Greek": "ell_Grek",
|
| 32 |
+
"Hebrew": "heb_Hebr",
|
| 33 |
+
}
|
| 34 |
|
| 35 |
+
# Load model
|
| 36 |
+
print("Loading NLLB-200 model...")
|
| 37 |
+
model_name = "facebook/nllb-200-distilled-600M"
|
| 38 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 39 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 40 |
+
|
| 41 |
+
# Use GPU if available
|
| 42 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 43 |
+
model = model.to(device)
|
| 44 |
+
print(f"Model loaded on {device}")
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
@lru_cache(maxsize=1000)
|
| 48 |
+
def translate_cached(text, src_lang_code, tgt_lang_code):
|
| 49 |
+
"""Cached translation to avoid repeating same translations"""
|
| 50 |
+
return translate(text, src_lang_code, tgt_lang_code)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def translate(text, src_lang, tgt_lang):
|
| 54 |
+
"""
|
| 55 |
+
Translate text using NLLB-200
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
text: Text to translate
|
| 59 |
+
src_lang: Source language name
|
| 60 |
+
tgt_lang: Target language name
|
| 61 |
+
|
| 62 |
+
Returns:
|
| 63 |
+
Translated text
|
| 64 |
+
"""
|
| 65 |
+
if not text or not text.strip():
|
| 66 |
+
return ""
|
| 67 |
+
|
| 68 |
+
# Get language codes
|
| 69 |
+
src_lang_code = LANGUAGE_CODES.get(src_lang, "eng_Latn")
|
| 70 |
+
tgt_lang_code = LANGUAGE_CODES.get(tgt_lang, "arb_Arab")
|
| 71 |
+
|
| 72 |
+
# Use cached version
|
| 73 |
+
try:
|
| 74 |
+
return translate_cached(text.strip(), src_lang_code, tgt_lang_code)
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return f"Translation error: {str(e)}"
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def translate_actual(text, src_lang_code, tgt_lang_code):
|
| 80 |
+
"""Actual translation logic"""
|
| 81 |
+
tokenizer.src_lang = src_lang_code
|
| 82 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, max_length=512, truncation=True)
|
| 83 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 84 |
+
|
| 85 |
+
with torch.no_grad():
|
| 86 |
+
translated_tokens = model.generate(
|
| 87 |
+
**inputs,
|
| 88 |
+
forced_bos_token_id=tokenizer.lang_code_to_id[tgt_lang_code],
|
| 89 |
+
max_length=512,
|
| 90 |
+
num_beams=5,
|
| 91 |
+
early_stopping=True
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
translation = tokenizer.batch_decode(
|
| 95 |
+
translated_tokens,
|
| 96 |
+
skip_special_tokens=True
|
| 97 |
+
)[0]
|
| 98 |
+
|
| 99 |
+
return translation
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
# Patch the cached function to use actual translation
|
| 103 |
+
translate_cached.__wrapped__ = translate_actual
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def gradio_translate(text, src_lang, tgt_lang):
|
| 107 |
+
"""Gradio interface function"""
|
| 108 |
+
if src_lang == tgt_lang:
|
| 109 |
+
return text
|
| 110 |
+
|
| 111 |
+
result = translate(text, src_lang, tgt_lang)
|
| 112 |
+
return result
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
# Available languages (sorted alphabetically)
|
| 116 |
+
LANGUAGES = sorted(LANGUAGE_CODES.keys())
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# Create Gradio Interface
|
| 120 |
+
with gr.Blocks(title="NLLB-200 Translation API", theme=gr.themes.Soft()) as demo:
|
| 121 |
+
gr.Markdown(
|
| 122 |
+
"""
|
| 123 |
+
# ๐ NLLB-200 Translation API
|
| 124 |
+
|
| 125 |
+
**Meta's No Language Left Behind** - 200 Languages Translation
|
| 126 |
+
|
| 127 |
+
- โ
High-quality translation for 200+ languages
|
| 128 |
+
- โ
44% better than previous models
|
| 129 |
+
- โ
+70% improvement for complex languages (Arabic, Hindi, etc.)
|
| 130 |
+
- โ
Direct translation (no pivot through English)
|
| 131 |
+
- โ
Cached for faster repeated translations
|
| 132 |
+
|
| 133 |
+
**Powered by**: `facebook/nllb-200-distilled-600M`
|
| 134 |
+
"""
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
with gr.Row():
|
| 138 |
+
with gr.Column():
|
| 139 |
+
src_lang = gr.Dropdown(
|
| 140 |
+
choices=LANGUAGES,
|
| 141 |
+
value="English",
|
| 142 |
+
label="Source Language",
|
| 143 |
+
interactive=True
|
| 144 |
+
)
|
| 145 |
+
input_text = gr.Textbox(
|
| 146 |
+
label="Text to Translate",
|
| 147 |
+
placeholder="Enter text here...",
|
| 148 |
+
lines=5,
|
| 149 |
+
max_lines=10
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
with gr.Column():
|
| 153 |
+
tgt_lang = gr.Dropdown(
|
| 154 |
+
choices=LANGUAGES,
|
| 155 |
+
value="Arabic",
|
| 156 |
+
label="Target Language",
|
| 157 |
+
interactive=True
|
| 158 |
+
)
|
| 159 |
+
output_text = gr.Textbox(
|
| 160 |
+
label="Translation",
|
| 161 |
+
lines=5,
|
| 162 |
+
max_lines=10,
|
| 163 |
+
interactive=False
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
with gr.Row():
|
| 167 |
+
translate_btn = gr.Button("Translate ๐", variant="primary", size="lg")
|
| 168 |
+
clear_btn = gr.Button("Clear", variant="secondary")
|
| 169 |
+
|
| 170 |
+
# Examples
|
| 171 |
+
gr.Examples(
|
| 172 |
+
examples=[
|
| 173 |
+
["Hello, how are you?", "English", "Arabic"],
|
| 174 |
+
["ู
ุฑุญุจุงุ ููู ุญุงููุ", "Arabic", "French"],
|
| 175 |
+
["Bonjour, comment allez-vous?", "French", "English"],
|
| 176 |
+
["This is a test of NLLB-200 translation model.", "English", "Spanish"],
|
| 177 |
+
],
|
| 178 |
+
inputs=[input_text, src_lang, tgt_lang],
|
| 179 |
+
outputs=output_text,
|
| 180 |
+
fn=gradio_translate,
|
| 181 |
+
cache_examples=False
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
# Event handlers
|
| 185 |
+
translate_btn.click(
|
| 186 |
+
fn=gradio_translate,
|
| 187 |
+
inputs=[input_text, src_lang, tgt_lang],
|
| 188 |
+
outputs=output_text
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
clear_btn.click(
|
| 192 |
+
fn=lambda: ("", ""),
|
| 193 |
+
inputs=None,
|
| 194 |
+
outputs=[input_text, output_text]
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
# Also translate on Enter key
|
| 198 |
+
input_text.submit(
|
| 199 |
+
fn=gradio_translate,
|
| 200 |
+
inputs=[input_text, src_lang, tgt_lang],
|
| 201 |
+
outputs=output_text
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
gr.Markdown(
|
| 205 |
+
"""
|
| 206 |
+
---
|
| 207 |
+
### API Usage
|
| 208 |
+
|
| 209 |
+
You can use this Space programmatically via the Gradio API:
|
| 210 |
+
|
| 211 |
+
```python
|
| 212 |
+
from gradio_client import Client
|
| 213 |
+
|
| 214 |
+
client = Client("TGPro1/NLLB200")
|
| 215 |
+
result = client.predict(
|
| 216 |
+
"Hello, world!", # text
|
| 217 |
+
"English", # source language
|
| 218 |
+
"Arabic", # target language
|
| 219 |
+
api_name="/predict"
|
| 220 |
+
)
|
| 221 |
+
print(result)
|
| 222 |
+
```
|
| 223 |
+
|
| 224 |
+
**Supported Languages**: 25+ major languages (see dropdown)
|
| 225 |
+
|
| 226 |
+
For full list of 200 languages, check the [NLLB-200 documentation](https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200)
|
| 227 |
+
"""
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
if __name__ == "__main__":
|
| 232 |
+
demo.queue(max_size=10)
|
| 233 |
+
demo.launch(
|
| 234 |
+
server_name="0.0.0.0",
|
| 235 |
+
server_port=7860,
|
| 236 |
+
share=False
|
| 237 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.44.0
|
| 2 |
+
transformers==4.36.2
|
| 3 |
+
torch==2.1.2
|
| 4 |
+
sentencepiece==0.1.99
|
| 5 |
+
sacremoses==0.1.1
|
| 6 |
+
accelerate==0.25.0
|