Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# languages and model
|
7 |
+
LANGS = ["eng_Latn", "fra_Latn", "spa_Latn"]
|
8 |
+
model_name = "facebook/nllb-200-distilled-600M"
|
9 |
+
|
10 |
+
# model and tokinizer
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
13 |
+
device = 0 if torch.cuda.is_available() else -1
|
14 |
+
|
15 |
+
# translate function
|
16 |
+
def translate(input, src_lang, tgt_lang):
|
17 |
+
translation_pipeline = pipeline("translation", model=model, tokenizer=tokenizer,
|
18 |
+
src_lang=src_lang, tgt_lang=tgt_lang,
|
19 |
+
max_length=400, device=device)
|
20 |
+
res = translation_pipeline(input)
|
21 |
+
return res[0]['translation_text']
|
22 |
+
##
|
23 |
+
##
|
24 |
+
with gr.Blocks() as demo:
|
25 |
+
with gr.Row():
|
26 |
+
gr.Markdown("""
|
27 |
+
> 👽 0xZee
|
28 |
+
# 📝 NLP : Translation Demo
|
29 |
+
> Demo #1 : **facebook/nllb-200-distilled-600M** Model (finetuned)
|
30 |
+
*Translation : English, French, Spanish*
|
31 |
+
""")
|
32 |
+
#
|
33 |
+
with gr.Row():
|
34 |
+
input = gr.Textbox(label="Text to translate", placeholder="Text here")
|
35 |
+
with gr.Row():
|
36 |
+
src = gr.Dropdown(label="Source Language", choices=LANGS)
|
37 |
+
tgt = gr.Dropdown(label="Target Language", choices=LANGS)
|
38 |
+
with gr.Row():
|
39 |
+
gr.Examples(examples=[["Building a better world, with sustainable energies", "eng_Latn", "spa_Latn"], ["Messi es el mejor del mundo, y de lejos", "spa_Latn", "fra_Latn"]], inputs=[input, src, tgt])
|
40 |
+
with gr.Row():
|
41 |
+
btn = gr.Button(value="Go Translate")
|
42 |
+
with gr.Row():
|
43 |
+
text_out = gr.Textbox(label="Translation")
|
44 |
+
btn.click(fn=translate, inputs=[input, src, tgt], outputs=text_out)
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
if __name__ == "__main__":
|
49 |
+
demo.launch(debug=True)
|
50 |
+
|