File size: 9,919 Bytes
2370590
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc7c005
2370590
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import gradio as gr
import torch
from kuidastaltsutadalaamat.trainllm import load_model, load_tokenizer
from kuidastaltsutadalaamat.inference import llm_generate
from kuidastaltsutadalaamat.data import LazyTokenizingInferenceDataset
from kuidastaltsutadalaamat.promptops import *

accel = None
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = load_model("mphi/smugri4-1808-ep3-tmptest", device, accelerator=accel, attention="eager") #eager for cpu
model.eval()
tokenizer = load_tokenizer("mphi/smugri4-1808-ep3-tmptest", accelerator=accel)
languages = ["English",
    "English, bible",
    "English, dictionary",
    "English, speech",
    "Erzya",
    "Erzya, bible",
    "Erzya, dictionary",
    "Estonian",
    "Estonian, Amb, dictionary",
    "Estonian, Ann, dictionary",
    "Estonian, Aud, dictionary",
    "Estonian, Hag, dictionary",
    "Estonian, Han, dictionary",
    "Estonian, Hii, emm, dictionary",
    "Estonian, Hii, käi, dictionary",
    "Estonian, Hii, phl, dictionary",
    "Estonian, Hii, rei, dictionary",
    "Estonian, Hiiu, dictionary",
    "Estonian, Hjn, dictionary",
    "Estonian, Hljr, dictionary",
    "Estonian, Hmd, dictionary",
    "Estonian, Hää, dictionary",
    "Estonian, Iisi, dictionary",
    "Estonian, Iisk, dictionary",
    "Estonian, Iisr, dictionary",
    "Estonian, Jjn, dictionary",
    "Estonian, Jmd, dictionary",
    "Estonian, Juu, dictionary",
    "Estonian, Jõer, dictionary",
    "Estonian, Jõh, dictionary",
    "Estonian, Jür, dictionary",
    "Estonian, Kad, dictionary",
    "Estonian, Kihnu, dictionary",
    "Estonian, Kjn, dictionary",
    "Estonian, Kodi, dictionary",
    "Estonian, Kos, dictionary",
    "Estonian, Kse, dictionary",
    "Estonian, Kuuk, dictionary",
    "Estonian, Kuur, dictionary",
    "Estonian, Lng, dictionary",
    "Estonian, Lüg, dictionary",
    "Estonian, Mar, dictionary",
    "Estonian, Mih, dictionary",
    "Estonian, Muhu, dictionary",
    "Estonian, Mul, hel, dictionary",
    "Estonian, Mul, hls, dictionary",
    "Estonian, Mul, krk, dictionary",
    "Estonian, Mul, trv, dictionary",
    "Estonian, Mulgi",
    "Estonian, Mulgi, dictionary",
    "Estonian, Mär, dictionary",
    "Estonian, Pjg, dictionary",
    "Estonian, Plt, dictionary",
    "Estonian, Ranna, dictionary",
    "Estonian, Rap, dictionary",
    "Estonian, Rid, dictionary",
    "Estonian, Saa, dictionary",
    "Estonian, Saa, jäm, dictionary",
    "Estonian, Saa, kaa, dictionary",
    "Estonian, Saa, khk, dictionary",
    "Estonian, Saa, kär, dictionary",
    "Estonian, Saa, mus, dictionary",
    "Estonian, Saa, pha, dictionary",
    "Estonian, Saa, pöi, dictionary",
    "Estonian, Seto",
    "Estonian, Seto, dictionary",
    "Estonian, Trm, dictionary",
    "Estonian, Trt, nõo, dictionary",
    "Estonian, Trt, ote, dictionary",
    "Estonian, Trt, puh, dictionary",
    "Estonian, Trt, ran, dictionary",
    "Estonian, Tõs, dictionary",
    "Estonian, Tür, dictionary",
    "Estonian, Vair, dictionary",
    "Estonian, Var, dictionary",
    "Estonian, Vig, dictionary",
    "Estonian, Vjg, dictionary",
    "Estonian, Vng, dictionary",
    "Estonian, Vän, dictionary",
    "Estonian, bible",
    "Estonian, dictionary",
    "Estonian, Äks, dictionary",
    "Finnish",
    "Finnish, bible",
    "Finnish, dictionary",
    "French",
    "German",
    "Hill Mari",
    "Hill Mari, bible",
    "Hungarian",
    "Hungarian, bible",
    "Inari Sami",
    "Inari Sami, dictionary",
    "Izhorian",
    "Izhorian, Alamaluuga, speech",
    "Izhorian, Mehmet",
    "Izhorian, Soikkola",
    "Izhorian, Soikkola, speech",
    "Izhorian, speech",
    "Kazym Khanty",
    "Kazym Khanty, 2000",
    "Kazym Khanty, 2013",
    "Kildin Sami",
    "Kildin Sami, Orth1",
    "Kildin Sami, Orth1, dictionary",
    "Kildin Sami, Orth2",
    "Kildin Sami, Orth2, dictionary",
    "Komi-Permyak",
    "Komi-Permyak, bible",
    "Komi-Zyrian",
    "Komi-Zyrian, bible",
    "Komi-Zyrian, dictionary",
    "Kven",
    "Kven, dictionary",
    "Latvian",
    "Latvian, bible",
    "Latvian, dictionary",
    "Livonian, Idaliivi, ft",
    "Livonian, Ira, ft",
    "Livonian, Lääneliivi, ft",
    "Livonian, Standard",
    "Livonian, Standard, dictionary",
    "Livvi",
    "Livvi, Impilahti",
    "Livvi, Kondushi",
    "Livvi, Kotkozero",
    "Livvi, Nekkula",
    "Livvi, Newwritten",
    "Livvi, Oldwritten",
    "Livvi, Rypushkalitsa",
    "Livvi, Salmi",
    "Livvi, Syamozero",
    "Livvi, Tulmozero",
    "Livvi, Vedlozero",
    "Livvi, Vidlitsa",
    "Livvi, bible",
    "Ludian",
    "Ludian, Central",
    "Ludian, Miikul",
    "Ludian, Miikul, dictionary",
    "Ludian, Mikhailovskoye",
    "Ludian, Newwritten",
    "Ludian, Northern",
    "Ludian, Southern",
    "Lule Sami",
    "Lule Sami, dictionary",
    "Mansi, Obs",
    "Mansi, Sosv",
    "Mansi, Unk",
    "Mansi, Unk, bible",
    "Mansi, Verh",
    "Meadow Mari",
    "Meadow Mari, bible",
    "Meadow Mari, dictionary",
    "Meänkieli",
    "Moksha",
    "Moksha, bible",
    "Northern Sami",
    "Northern Sami, dictionary",
    "Norwegian",
    "Norwegian, bible",
    "Norwegian, dictionary",
    "Pite Sami",
    "Priur Khanty",
    "Proper Karelian",
    "Proper Karelian, Dyorzha",
    "Proper Karelian, Ilomantsi",
    "Proper Karelian, Keret",
    "Proper Karelian, Kestenga",
    "Proper Karelian, Kontokki",
    "Proper Karelian, Korbiselga",
    "Proper Karelian, Myandyselga",
    "Proper Karelian, Newwritten",
    "Proper Karelian, Newwrittentver",
    "Proper Karelian, Oldwritten",
    "Proper Karelian, Oldwrittentver",
    "Proper Karelian, Oulanga",
    "Proper Karelian, Padany",
    "Proper Karelian, Panozero",
    "Proper Karelian, Poduzhemye",
    "Proper Karelian, Porosozero",
    "Proper Karelian, Reboly",
    "Proper Karelian, Rugozero",
    "Proper Karelian, Suistamo",
    "Proper Karelian, Suoyarvi",
    "Proper Karelian, Suvi",
    "Proper Karelian, Tikhtozero",
    "Proper Karelian, Tikhvin",
    "Proper Karelian, Tolmachi",
    "Proper Karelian, Tunguda",
    "Proper Karelian, Uhta",
    "Proper Karelian, Valdai",
    "Proper Karelian, Vesyegonsk",
    "Proper Karelian, Viena",
    "Proper Karelian, Voknavolok",
    "Proper Karelian, Vychetaibola",
    "Proper Karelian, Yushkozero",
    "Proper Karelian, bible",
    "Russian",
    "Russian, bible",
    "Russian, dictionary",
    "Russian, speech",
    "Shur Khanty",
    "Shur Khanty, 2013",
    "Shur Khanty, bible",
    "Skolt Sami",
    "Skolt Sami, dictionary",
    "Southern Sami",
    "Southern Sami, dictionary",
    "Sred Khanty",
    "Surgut Khanty",
    "Surgut Khanty, 2000",
    "Surgut Khanty, 2013",
    "Swedish",
    "Udmurt",
    "Udmurt, bible",
    "Udmurt, dictionary",
    "Ume Sami",
    "Unk Khanty",
    "Vakh Khanty",
    "Vakh Khanty, 2013",
    "Veps",
    "Veps, Centraleastern",
    "Veps, Centralwestern",
    "Veps, Newwritten",
    "Veps, Northern",
    "Veps, Southern",
    "Veps, bible",
    "Votic, I",
    "Votic, Idavadja, ft",
    "Votic, J",
    "Votic, Ja",
    "Votic, K",
    "Votic, Ke",
    "Votic, Kõ",
    "Votic, L",
    "Votic, Li",
    "Votic, Lu",
    "Votic, Läänevadja, ft",
    "Votic, M",
    "Votic, P",
    "Votic, Po",
    "Votic, R",
    "Votic, Ra",
    "Votic, S",
    "Votic, Standard",
    "Votic, U",
    "Votic, Unk",
    "Votic, Unk, dictionary",
    "Votic, Unk, speech",
    "Votic, V",
    "Votic, dictionary",
    "Võro, Har, dictionary",
    "Võro, Kan, dictionary",
    "Võro, Krl, dictionary",
    "Võro, Lei, dictionary",
    "Võro, Lut, dictionary",
    "Võro, Plv, dictionary",
    "Võro, Räp, dictionary",
    "Võro, Rõu, dictionary",
    "Võro, Setom",
    "Võro, Sõnaq",
    "Võro, Sõnaq, dictionary",
    "Võro, Uma",
    "Võro, Urv, dictionary",
    "Võro, Vas, dictionary",
    "Võro, X"
]

def run_inference(text, from_lang, to_lang, mode):
    entry = {"src_segm": text, "task": mode}
    if mode == "translate":
        entry.update({"src_lang": from_lang, "tgt_lang": to_lang})
        prompt_format = PF_SMUGRI_MT
    else:
        prompt_format = PF_SMUGRI_LID

    ds = LazyTokenizingInferenceDataset([entry], tokenizer, prompt_format)
    tok = ds[0]
    output = llm_generate(model, tokenizer, tok, debug=False, max_len=512)
    return output[0]

with gr.Blocks() as demo:
    text_input = gr.Textbox(label="Text", lines=6, placeholder="Enter text...")
    identify_btn = gr.Button("Identify language", interactive=False)
    with gr.Row():
        from_dropdown = gr.Dropdown(choices=languages, label="From", value=None)
        to_dropdown = gr.Dropdown(choices=languages, label="To", value=None)
    translate_btn = gr.Button("Translate", interactive=False)
    output = gr.Textbox(label="Output", lines=6)

    def toggle_identify(text):
        return gr.update(interactive=bool(text.strip()))
    text_input.change(toggle_identify, [text_input], [identify_btn])

    def toggle_translate(text, f, t):
        return gr.update(interactive=bool(text.strip() and f and t))
    text_input.change(toggle_translate, [text_input, from_dropdown, to_dropdown], [translate_btn])
    from_dropdown.change(toggle_translate, [text_input, from_dropdown, to_dropdown], [translate_btn])
    to_dropdown.change(toggle_translate, [text_input, from_dropdown, to_dropdown], [translate_btn])

    identify_btn.click(
        fn=lambda text: run_inference(text, None, None, mode="identify"),
        inputs=[text_input],
        outputs=[from_dropdown],
    ).then(
        lambda *args: gr.update(interactive=bool(text_input.value.strip() and from_dropdown.value and to_dropdown.value)),
        [], [translate_btn]
    )

    translate_btn.click(
        fn=lambda text, f, t: run_inference(text, f, t, mode="translate"),
        inputs=[text_input, from_dropdown, to_dropdown],
        outputs=[output]
    )

if __name__ == "__main__":
    demo.launch()