Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,57 @@
|
|
1 |
-
from transformers import pipeline
|
2 |
-
import torch
|
3 |
-
import gradio as gr
|
4 |
-
|
5 |
-
# Translation pipeline
|
6 |
-
translator = pipeline(task="translation",
|
7 |
-
model="facebook/nllb-200-1.3B",
|
8 |
-
torch_dtype=torch.bfloat16)
|
9 |
-
|
10 |
-
# list of EU languages and their FLoRes-200 code
|
11 |
-
eu_languages = {
|
12 |
-
'Bulgarian':'bul_Cyrl',
|
13 |
-
'Croatian':'hrv_Latn',
|
14 |
-
'Czech':'ces_Latn',
|
15 |
-
'Danish':'dan_Latn',
|
16 |
-
'Dutch':'nld_Latn',
|
17 |
-
'English':'eng_Latn',
|
18 |
-
'Estonian':'est_Latn',
|
19 |
-
'Finnish':'fin_Latn',
|
20 |
-
'French':'fra_Latn',
|
21 |
-
'German':'deu_Latn',
|
22 |
-
'Greek':'ell_Grek',
|
23 |
-
'Hungarian':'hun_Latn',
|
24 |
-
'Irish':'gle_Latn',
|
25 |
-
'Italian':'ita_Latn',
|
26 |
-
'Latvian':'lvs_Latn',
|
27 |
-
'Lithuanian':'lit_Latn',
|
28 |
-
'Maltese':'mlt_Latn',
|
29 |
-
'Polish':'pol_Latn',
|
30 |
-
'Portuguese':'por_Latn',
|
31 |
-
'Romanian':'ron_Latn',
|
32 |
-
'Slovak':'slk_Latn',
|
33 |
-
'Slovenian':'slv_Latn',
|
34 |
-
'Spanish':'spa_Latn',
|
35 |
-
'Swedish':'swe_Latn'
|
36 |
-
}
|
37 |
-
|
38 |
-
# Translate function
|
39 |
-
def translate(input, src, tgt):
|
40 |
-
src_lang = eu_languages[src]
|
41 |
-
tgt_lang = eu_languages[tgt]
|
42 |
-
output = translator(input, src_lang=src_lang, tgt_lang=tgt_lang)
|
43 |
-
return output[0]['translation_text']
|
44 |
-
|
45 |
-
|
46 |
-
# Gradio Interface
|
47 |
-
gr.close_all()
|
48 |
-
demo = gr.Interface(fn=translate,
|
49 |
-
inputs=[gr.Textbox(label="Text to translate", lines=6),
|
50 |
-
gr.Dropdown(eu_languages.keys(), label="Source Language"),
|
51 |
-
gr.Dropdown(eu_languages.keys(), label="Target Language")],
|
52 |
-
outputs=[gr.Textbox(label="Result", lines=10)],
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
56 |
demo.launch()
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Translation pipeline
|
6 |
+
translator = pipeline(task="translation",
|
7 |
+
model="facebook/nllb-200-1.3B",
|
8 |
+
torch_dtype=torch.bfloat16)
|
9 |
+
|
10 |
+
# list of EU languages and their FLoRes-200 code
|
11 |
+
eu_languages = {
|
12 |
+
'Bulgarian':'bul_Cyrl',
|
13 |
+
'Croatian':'hrv_Latn',
|
14 |
+
'Czech':'ces_Latn',
|
15 |
+
'Danish':'dan_Latn',
|
16 |
+
'Dutch':'nld_Latn',
|
17 |
+
'English':'eng_Latn',
|
18 |
+
'Estonian':'est_Latn',
|
19 |
+
'Finnish':'fin_Latn',
|
20 |
+
'French':'fra_Latn',
|
21 |
+
'German':'deu_Latn',
|
22 |
+
'Greek':'ell_Grek',
|
23 |
+
'Hungarian':'hun_Latn',
|
24 |
+
'Irish':'gle_Latn',
|
25 |
+
'Italian':'ita_Latn',
|
26 |
+
'Latvian':'lvs_Latn',
|
27 |
+
'Lithuanian':'lit_Latn',
|
28 |
+
'Maltese':'mlt_Latn',
|
29 |
+
'Polish':'pol_Latn',
|
30 |
+
'Portuguese':'por_Latn',
|
31 |
+
'Romanian':'ron_Latn',
|
32 |
+
'Slovak':'slk_Latn',
|
33 |
+
'Slovenian':'slv_Latn',
|
34 |
+
'Spanish':'spa_Latn',
|
35 |
+
'Swedish':'swe_Latn'
|
36 |
+
}
|
37 |
+
|
38 |
+
# Translate function
|
39 |
+
def translate(input, src, tgt):
|
40 |
+
src_lang = eu_languages[src]
|
41 |
+
tgt_lang = eu_languages[tgt]
|
42 |
+
output = translator(input, src_lang=src_lang, tgt_lang=tgt_lang)
|
43 |
+
return output[0]['translation_text']
|
44 |
+
|
45 |
+
|
46 |
+
# Gradio Interface
|
47 |
+
gr.close_all()
|
48 |
+
demo = gr.Interface(fn=translate,
|
49 |
+
inputs=[gr.Textbox(label="Text to translate", lines=6),
|
50 |
+
gr.Dropdown(eu_languages.keys(), label="Source Language"),
|
51 |
+
gr.Dropdown(eu_languages.keys(), label="Target Language")],
|
52 |
+
outputs=[gr.Textbox(label="Result", lines=10)],
|
53 |
+
examples=[["Jokainen on oman onnensa seppä.", "Finnish","English"]],
|
54 |
+
title="NLLB Translator between EU Languages",
|
55 |
+
description="Translate texts in EU languages using the `facebook/nllb-200-1.3B` model!"
|
56 |
+
)
|
57 |
demo.launch()
|