Syu kadirnar commited on
Commit
f399f41
0 Parent(s):

Duplicate from kadirnar/Multilingual-Translation

Browse files

Co-authored-by: Kadir Nar <[email protected]>

Files changed (5) hide show
  1. .gitattributes +34 -0
  2. README.md +14 -0
  3. app.py +61 -0
  4. requirements.txt +4 -0
  5. utils.py +128 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Multilingual Translation
3
+ emoji: 🌍
4
+ colorFrom: yellow
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 3.17.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ duplicated_from: kadirnar/Multilingual-Translation
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from multilingual_translation import text_to_text_generation
2
+ from utils import lang_ids, data_scraping
3
+ import gradio as gr
4
+
5
+ lang_list = list(lang_ids.keys())
6
+ model_list = data_scraping()
7
+
8
+ def multilingual_translate(
9
+ prompt: str,
10
+ model_id: str,
11
+ target_lang: str
12
+ ):
13
+
14
+ return text_to_text_generation(
15
+ prompt=prompt,
16
+ model_id=model_id,
17
+ device='cpu',
18
+ target_lang=lang_ids[target_lang]
19
+ )
20
+
21
+
22
+ inputs = [
23
+ gr.Textbox(lines=4, value="Hello world!", label="Input Text"),
24
+ gr.Dropdown(model_list, value="facebook/m2m100_418M", label="Model"),
25
+ gr.Dropdown(lang_list, value="Turkish", label="Target Language"),
26
+ ]
27
+
28
+ output = gr.outputs.Textbox(label="Output Text")
29
+
30
+ examples = [
31
+ [
32
+ "Hello world!",
33
+ "facebook/m2m100_418M",
34
+ "Turkish",
35
+ ],
36
+ [
37
+ "Omar ve Merve çok iyi arkadaşlar.",
38
+ "facebook/m2m100_418M",
39
+ "Spanish",
40
+ ],
41
+ [
42
+ "Hugging Face is a great company.",
43
+ "facebook/m2m100_418M",
44
+ "French",
45
+ ]
46
+ ]
47
+
48
+ title = "Beyond English-Centric Multilingual Machine Translation"
49
+
50
+ description = "M2M100 is a multilingual encoder-decoder (seq-to-seq) model trained for Many-to-Many multilingual translation. It was introduced in this [paper](https://arxiv.org/abs/2010.11125) and first released in [this](https://github.com/pytorch/fairseq/tree/master/examples/m2m_100) repository."
51
+
52
+ app = gr.Interface(
53
+ fn=multilingual_translate,
54
+ inputs=inputs,
55
+ outputs=output,
56
+ examples=examples,
57
+ title=title,
58
+ description=description,
59
+ cache_examples=True
60
+ )
61
+ app.launch(debug=True, enable_queue=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch
2
+ beautifulsoup4==4.11.2
3
+ multilingual_translation==0.0.5
4
+ requests==2.28.1
utils.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from bs4 import BeautifulSoup
2
+ import requests
3
+
4
+
5
+ lang_ids = {
6
+ "Afrikaans": "af",
7
+ "Amharic": "am",
8
+ "Arabic": "ar",
9
+ "Asturian": "ast",
10
+ "Azerbaijani": "az",
11
+ "Bashkir": "ba",
12
+ "Belarusian": "be",
13
+ "Bulgarian": "bg",
14
+ "Bengali": "bn",
15
+ "Breton": "br",
16
+ "Bosnian": "bs",
17
+ "Catalan": "ca",
18
+ "Cebuano": "ceb",
19
+ "Czech": "cs",
20
+ "Welsh": "cy",
21
+ "Danish": "da",
22
+ "German": "de",
23
+ "Greeek": "el",
24
+ "English": "en",
25
+ "Spanish": "es",
26
+ "Estonian": "et",
27
+ "Persian": "fa",
28
+ "Fulah": "ff",
29
+ "Finnish": "fi",
30
+ "French": "fr",
31
+ "Western Frisian": "fy",
32
+ "Irish": "ga",
33
+ "Gaelic": "gd",
34
+ "Galician": "gl",
35
+ "Gujarati": "gu",
36
+ "Hausa": "ha",
37
+ "Hebrew": "he",
38
+ "Hindi": "hi",
39
+ "Croatian": "hr",
40
+ "Haitian": "ht",
41
+ "Hungarian": "hu",
42
+ "Armenian": "hy",
43
+ "Indonesian": "id",
44
+ "Igbo": "ig",
45
+ "Iloko": "ilo",
46
+ "Icelandic": "is",
47
+ "Italian": "it",
48
+ "Japanese": "ja",
49
+ "Javanese": "jv",
50
+ "Georgian": "ka",
51
+ "Kazakh": "kk",
52
+ "Central Khmer": "km",
53
+ "Kannada": "kn",
54
+ "Korean": "ko",
55
+ "Luxembourgish": "lb",
56
+ "Ganda": "lg",
57
+ "Lingala": "ln",
58
+ "Lao": "lo",
59
+ "Lithuanian": "lt",
60
+ "Latvian": "lv",
61
+ "Malagasy": "mg",
62
+ "Macedonian": "mk",
63
+ "Malayalam": "ml",
64
+ "Mongolian": "mn",
65
+ "Marathi": "mr",
66
+ "Malay": "ms",
67
+ "Burmese": "my",
68
+ "Nepali": "ne",
69
+ "Dutch": "nl",
70
+ "Norwegian": "no",
71
+ "Northern Sotho": "ns",
72
+ "Occitan": "oc",
73
+ "Oriya": "or",
74
+ "Panjabi": "pa",
75
+ "Polish": "pl",
76
+ "Pushto": "ps",
77
+ "Portuguese": "pt",
78
+ "Romanian": "ro",
79
+ "Russian": "ru",
80
+ "Sindhi": "sd",
81
+ "Sinhala": "si",
82
+ "Slovak": "sk",
83
+ "Slovenian": "sl",
84
+ "Somali": "so",
85
+ "Albanian": "sq",
86
+ "Serbian": "sr",
87
+ "Swati": "ss",
88
+ "Sundanese": "su",
89
+ "Swedish": "sv",
90
+ "Swahili": "sw",
91
+ "Tamil": "ta",
92
+ "Thai": "th",
93
+ "Tagalog": "tl",
94
+ "Tswana": "tn",
95
+ "Turkish": "tr",
96
+ "Ukrainian": "uk",
97
+ "Urdu": "ur",
98
+ "Uzbek": "uz",
99
+ "Vietnamese": "vi",
100
+ "Wolof": "wo",
101
+ "Xhosa": "xh",
102
+ "Yiddish": "yi",
103
+ "Yoruba": "yo",
104
+ "Chinese": "zh",
105
+ "Zulu": "zu",
106
+ }
107
+
108
+ def model_url_list():
109
+ url_list = []
110
+ for i in range(0, 5):
111
+ url_list.append(f"https://huggingface.co/models?other=m2m_100&p={i}&sort=downloads")
112
+ return url_list
113
+
114
+ def data_scraping():
115
+ url_list = model_url_list()
116
+ model_list = []
117
+ for url in url_list:
118
+ response = requests.get(url)
119
+ soup = BeautifulSoup(response.text, "html.parser")
120
+ div_class = 'grid grid-cols-1 gap-5 2xl:grid-cols-2'
121
+ div = soup.find('div', {'class': div_class})
122
+ for a in div.find_all('a', href=True):
123
+ model_list.append(a['href'])
124
+
125
+ for i in range(len(model_list)):
126
+ model_list[i] = model_list[i][1:]
127
+
128
+ return model_list