Spaces:
Sleeping
Sleeping
File size: 5,029 Bytes
a2b29a7 11e68b1 4ec5914 11e68b1 8064c8d 11e68b1 8064c8d e723e47 8064c8d e723e47 8064c8d 11e68b1 8064c8d 11e68b1 4152c07 8064c8d 4152c07 8064c8d 4152c07 8064c8d 4152c07 8064c8d 4152c07 |
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
from pathlib import Path
import gradio as gr
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
lang_to_code = {
"Akrikaans": "af",
"Albanian": "sq",
"Amharic": "am",
"Arabic": "ar",
"Armenian": "hy",
"Assamese": "as",
"Asturian": "ast",
"Aymara": "ay",
"Azerbaijani": "az",
"Bashkir": "ba",
"Belarusian": "be",
"Bengali": "bn",
"Bosnian": "bs",
"Breton": "br",
"Bulgarian": "bg",
"Burmese": "my",
"Catalan": "ca",
"Cebuano": "ceb",
"Central Khmer": "km",
"Chinese": "zh",
"Chokwe": "cjk",
"Croatian": "hr",
"Czech": "cs",
"Danish": "da",
"Dutch": "nl",
"Dyula": "dyu",
"English": "en",
"Estonian": "et",
"Finnish": "fi",
"French": "fr",
"Fulah": "ff",
"Galician": "gl",
"Ganda": "lg",
"Georgian": "ka",
"German": "de",
"Greek": "el",
"Gujarati": "gu",
"Haitian Creole": "ht",
"Hausa": "ha",
"Hebrew": "he",
"Hindi": "hi",
"Hungarian": "hu",
"Icelandic": "is",
"Igbo": "ig",
"Iloko": "ilo",
"Indonesian": "id",
"Irish": "ga",
"Italian": "it",
"Japanese": "ja",
"Javanese": "jv",
"Kabuverdianu": "kea",
"Kachin": "kac",
"Kamba": "kam",
"Kannada": "kn",
"Kazakh": "kk",
"Kimbundu": "kmb",
"Kongo": "kg",
"Korean": "ko",
"Kurdish": "ku",
"Kyrgyz": "ky",
"Lao": "lo",
"Latvian": "lv",
"Lingala": "ln",
"Lithuanian": "lt",
"Luo": "luo",
"Luxembourgish": "lb",
"Macedonian": "mk",
"Malagasy": "mg",
"Malay": "ms",
"Malayalam": "ml",
"Maltese": "mt",
"Maori": "mi",
"Marathi": "mr",
"Mongolian": "mn",
"Nepali": "ne",
"Northern Kurdish": "kmr",
"Northern Sotho": "ns",
"Norwegian": "no",
"Nyanja": "ny",
"Occitan": "oc",
"Oriya": "or",
"Oromo": "om",
"Pashto": "ps",
"Persian": "fa",
"Polish": "pl",
"Portuguese": "pt",
"Punjabi": "pa",
"Quechua": "qu",
"Romanian": "ro",
"Russian": "ru",
"Scottish Gaelic": "gd",
"Serbian": "sr",
"Shan": "shn",
"Shona": "sn",
"Sindhi": "sd",
"Sinhala": "si",
"Slovak": "sk",
"Slovenian": "sl",
"Somali": "so",
"Spanish": "es",
"Sundanese": "su",
"Swahili": "sw",
"Swati": "ss",
"Swedish": "sv",
"Tagalog": "tl",
"Tajik": "tg",
"Tamil": "ta",
"Telugu": "te",
"Thai": "th",
"Tigrinya": "ti",
"Tswana": "tn",
"Turkish": "tr",
"Ukrainian": "uk",
"Umbundu": "umb",
"Urdu": "ur",
"Uzbek": "uz",
"Vietnamese": "vi",
"Welsh": "cy",
"Western Frisian": "fy",
"Wolof": "wo",
"Xhosa": "xh",
"Yiddish": "yi",
"Yoruba": "yo",
"Zulu": "zu",
}
lang_names = list(lang_to_code.keys())
# load model
model_path = Path("./model_files").resolve()
print(f"model_path: {model_path}")
tokenizer: M2M100Tokenizer = M2M100Tokenizer.from_pretrained(
pretrained_model_name_or_path=str(model_path), local_files_only=True
)
model = M2M100ForConditionalGeneration.from_pretrained(
pretrained_model_name_or_path=str(model_path), local_files_only=True
)
# fix tokenizer
tokenizer.lang_token_to_id = {
t: i
for t, i in zip(tokenizer.all_special_tokens, tokenizer.all_special_ids)
if i > 5
}
tokenizer.lang_code_to_token = {s.strip("_"): s for s in tokenizer.lang_token_to_id}
tokenizer.lang_code_to_id = {
s.strip("_"): i for s, i in tokenizer.lang_token_to_id.items()
}
tokenizer.id_to_lang_token = {i: s for s, i in tokenizer.lang_token_to_id.items()}
def translate(src_text: str, source_lang: str, target_lang: str) -> str:
# get lang code
src_lang = lang_to_code[source_lang]
tgt_lang = lang_to_code[target_lang]
# encode
tokenizer.src_lang = src_lang
encoded_input = tokenizer(src_text, return_tensors="pt")
# inference
generated_tokens = model.generate(
**encoded_input,
forced_bos_token_id=tokenizer.get_lang_id(tgt_lang),
max_length=1024,
)
# decode
pred_texts = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
pred_text = pred_texts[0]
assert isinstance(pred_text, str)
return pred_text
inputs = [
gr.Textbox(lines=4, value="Hello world!", label="Input Text"),
gr.Dropdown(lang_names, value="English", label="Source Language"),
gr.Dropdown(lang_names, value="Korean", label="Target Language"),
]
outputs = gr.Textbox(lines=4, label="Output Text")
demo = gr.Interface(
fn=translate,
inputs=inputs,
outputs=outputs,
title="Flores101: Large-Scale Multilingual Machine Translation",
description="[`seyoungsong/flores101_mm100_175M`](https://huggingface.co/seyoungsong/flores101_mm100_175M)",
)
if __name__ == "__main__":
# https://huggingface.co/seyoungsong/flores101_mm100_175M
# https://huggingface.co/spaces/seyoungsong/flores101_mm100_175M
# gradio src/pretrained/gradio/app.py
# http://127.0.0.1:7860
demo.launch()
|