File size: 28,888 Bytes
56f497c
70ebea3
a103fac
2bbff53
 
cae0132
4a7fa9d
7748528
56f497c
2bbff53
cae0132
7748528
4045d37
d99f102
b4c4fbe
8b76c73
4894c8f
37ea4b9
 
4045d37
83386a6
 
 
53f9025
3e539e5
ec5a10c
a50fb3c
 
ae49959
 
ed9f776
9f0b22d
f434999
a50fb3c
309f943
 
9b5378d
37ea4b9
e13fba3
310e819
 
 
 
 
 
 
 
0b61062
f434999
92c9491
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f434999
e389a2d
 
 
 
 
87d4bd8
e389a2d
 
 
 
 
fe11d17
 
 
f434999
 
 
 
 
 
fe11d17
f434999
 
 
 
fe11d17
f434999
fe11d17
 
f434999
 
 
 
 
 
 
d99f102
 
 
87d4bd8
35be497
0448b0f
d99f102
0448b0f
d99f102
0448b0f
f434999
 
 
 
87d4bd8
f434999
aead9b1
f434999
 
 
87d4bd8
f434999
aead9b1
f434999
87d4bd8
 
f434999
 
ed9f776
 
 
 
 
 
 
 
 
 
 
 
 
 
52193d6
 
 
 
 
 
 
 
 
 
 
 
 
 
21077c7
52193d6
 
21077c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92c9491
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec5a10c
92c9491
 
 
03dd083
1f3055b
92c9491
 
 
 
 
 
 
 
03dd083
1f3055b
 
92c9491
 
 
7d0b5b1
92c9491
 
 
 
f15fe8d
 
92c9491
 
 
f434999
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a64284b
 
e5ffc74
f434999
 
 
 
 
 
 
 
 
 
 
 
3b73549
f434999
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4dceed6
f434999
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a50fb3c
d63ee61
 
 
 
 
 
 
 
 
 
 
 
 
 
0d30e3d
70ebea3
f434999
135432f
481d87b
135432f
481d87b
135432f
f434999
 
481d87b
135432f
 
481d87b
 
4dfd4b1
481d87b
 
 
 
135432f
481d87b
4045d37
 
f434999
f3d3fd0
148dd30
d99f102
 
 
87d4bd8
f434999
148dd30
 
f434999
5fc3c5d
148dd30
52193d6
309f943
8be3c99
92c9491
148dd30
21077c7
f434999
21077c7
148dd30
f434999
ed9f776
 
e0efacd
148dd30
 
f434999
148dd30
 
f434999
148dd30
 
f434999
92c9491
9b5378d
148dd30
f434999
310e819
148dd30
f434999
3e539e5
148dd30
f434999
148dd30
92c9491
f434999
92c9491
 
f434999
92c9491
 
f434999
92c9491
148dd30
f434999
e389a2d
 
f434999
 
 
 
0d30e3d
 
d63ee61
e389a2d
148dd30
 
53f9025
e389a2d
53f9025
56f497c
8be3c99
2169b22
 
 
0b2c6b5
9b5a331
 
5b50e0a
83386a6
9b5a331
5b50e0a
9b5a331
 
c63efca
 
 
6eefb6d
bc01b39
 
56f497c
 
9f0b22d
c63efca
2169b22
5b50e0a
e44493c
 
5b50e0a
2bbff53
e44493c
0b2c6b5
c63efca
56f497c
8b76c73
e06dabc
f434999
9f0b22d
0b2c6b5
0294f68
 
2bbff53
e06dabc
f434999
b98b60d
56f497c
 
 
 
 
28cef2e
16105d3
 
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import gradio as gr
import spaces
import torch
from transformers import T5Tokenizer, T5ForConditionalGeneration, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM, AutoModel, pipeline
from transformers import logging as hflogging
import languagecodes
import requests, os
import polars as pl 

hflogging.set_verbosity_error()
favourite_langs = {"German": "de", "Romanian": "ro", "English": "en", "-----": "-----"}
df = pl.read_parquet("isolanguages.parquet")
non_empty_isos = df.slice(1).filter(pl.col("ISO639-1") != "").rows()
# all_langs = languagecodes.iso_languages_byname
all_langs = {iso[0]: (iso[1], iso[2], iso[3]) for iso in non_empty_isos} # {'Romanian': ('ro', 'rum', 'ron')}

# Language options as list, add favourite languages first
langs = list(favourite_langs.keys())
langs.extend(list(all_langs.keys()))

models = ["Helsinki-NLP",
          "Helsinki-NLP/opus-mt-tc-bible-big-mul-mul", "Helsinki-NLP/opus-mt-tc-bible-big-mul-deu_eng_nld",
          "Helsinki-NLP/opus-mt-tc-bible-big-mul-deu_eng_fra_por_spa", "Helsinki-NLP/opus-mt-tc-bible-big-deu_eng_fra_por_spa-mul",
          "facebook/nllb-200-distilled-600M", "facebook/nllb-200-distilled-1.3B", "facebook/nllb-200-1.3B", "facebook/nllb-200-3.3B",
          "facebook/mbart-large-50-many-to-many-mmt", "facebook/mbart-large-50-one-to-many-mmt", "facebook/mbart-large-50-many-to-one-mmt",
          "facebook/m2m100_418M", "facebook/m2m100_1.2B",
          "bigscience/mt0-small", "bigscience/mt0-base", "bigscience/mt0-large", "bigscience/mt0-xl",
          "bigscience/bloomz-560m", "bigscience/bloomz-1b1", "bigscience/bloomz-1b7", "bigscience/bloomz-3b",
          "t5-small", "t5-base", "t5-large",
          "google/flan-t5-small", "google/flan-t5-base", "google/flan-t5-large", "google/flan-t5-xl",
          "google/madlad400-3b-mt", "jbochi/madlad400-3b-mt", 
          "Argos", "Google",
          "HuggingFaceTB/SmolLM3-3B", "winninghealth/WiNGPT-Babel-2",
          "utter-project/EuroLLM-1.7B", "utter-project/EuroLLM-1.7B-Instruct",
          "Unbabel/Tower-Plus-2B", "Unbabel/TowerInstruct-7B-v0.2", "Unbabel/TowerInstruct-Mistral-7B-v0.2",
          "openGPT-X/Teuken-7B-instruct-commercial-v0.4", "openGPT-X/Teuken-7B-instruct-v0.6"
          ]
DEFAULTS = [langs[0], langs[1], models[0]]

def model_to_cuda(model):
    # Move the model to GPU if available
    if torch.cuda.is_available():
        model = model.to('cuda')
        print("CUDA is available! Using GPU.")
    else:
        print("CUDA not available! Using CPU.")
    return model

def HelsinkiNLPAutoTokenizer(sl, tl, input_text): # deprecated
    if model_name == "Helsinki-NLP":
        message_text = f'Translated from {sl} to {tl} with {model_name}.'
        try:
            model_name = f"Helsinki-NLP/opus-mt-{sl}-{tl}"
            tokenizer = AutoTokenizer.from_pretrained(model_name)
            model = model_to_cuda(AutoModelForSeq2SeqLM.from_pretrained(model_name))
        except EnvironmentError:
            try:   
                model_name = f"Helsinki-NLP/opus-tatoeba-{sl}-{tl}"
                tokenizer = AutoTokenizer.from_pretrained(model_name)
                model = model_to_cuda(AutoModelForSeq2SeqLM.from_pretrained(model_name))
                input_ids = tokenizer.encode(prompt, return_tensors="pt")
                output_ids = model.generate(input_ids, max_length=512)
                translated_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
                return translated_text, message_text
            except EnvironmentError as error:
                return f"Error finding model: {model_name}! Try other available language combination.", error
                      
class Translators:
    def __init__(self, model_name: str, sl: str, tl: str, input_text: str):
        self.model_name = model_name
        self.sl, self.tl = sl, tl
        self.input_text = input_text
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        
    def google(self):  
        url = os.environ['GCLIENT'] + f'sl={self.sl}&tl={self.tl}&q={self.input_text}'
        response = requests.get(url)
        return response.json()[0][0][0]
    
    @classmethod
    def download_argos_model(cls, from_code, to_code):
        import argostranslate.package
        print('Downloading model', from_code, to_code) 
        # Download and install Argos Translate package
        argostranslate.package.update_package_index()
        available_packages = argostranslate.package.get_available_packages()
        package_to_install = next(
            filter(lambda x: x.from_code == from_code and x.to_code == to_code, available_packages)
        )
        argostranslate.package.install_from_path(package_to_install.download())
    
    def argos(self):
        import argostranslate.translate, argostranslate.package
        try:
            Translators.download_argos_model(self.sl, self.tl) # Download model
            translated_text = argostranslate.translate.translate(self.input_text, self.sl, self.tl) # Translate
        except StopIteration:
            # packages_info = ', '.join(f"{pkg.get_description()}->{str(pkg.links)} {str(pkg.source_languages)}" for pkg in argostranslate.package.get_available_packages())
            packages_info = ', '.join(f"{pkg.from_name} ({pkg.from_code}) -> {pkg.to_name} ({pkg.to_code})" for pkg in argostranslate.package.get_available_packages())
            translated_text = f"No Argos model for {self.sl} to {self.tl}. Try other model or languages combination from the available Argos models: {packages_info}."
        except Exception as error:
            translated_text = error
        return translated_text

    def HelsinkiNLP_mulmul(self):
        try:
            pipe = pipeline("translation", model=self.model_name, device=self.device)                
            iso_dict = {iso[1]: iso[3] for iso in non_empty_isos}
            iso3tl = iso_dict.get(self.tl) # 'deu', 'ron', 'eng', 'fra'
            translation = pipe(f'>>{iso3tl}<< {self.input_text}')
            return translation[0]['translation_text'], f'Translated from {self.sl} to {self.tl} with {self.model_name}.'
        except Exception as error:
            return f"Error translating with model: {self.model_name}! Try other available language combination.", error
    
    def HelsinkiNLP(self):
        try: # Standard bilingual model
            model_name = f"Helsinki-NLP/opus-mt-{self.sl}-{self.tl}"
            pipe = pipeline("translation", model=model_name, device=self.device)
            translation = pipe(self.input_text)
            return translation[0]['translation_text'], f'Translated from {self.sl} to {self.tl} with {model_name}.'
        except EnvironmentError:
            try: # Tatoeba models
                model_name = f"Helsinki-NLP/opus-tatoeba-{self.sl}-{self.tl}"
                pipe = pipeline("translation", model=model_name, device=self.device)
                translation = pipe(self.input_text)
                return translation[0]['translation_text'], f'Translated from {self.sl} to {self.tl} with {model_name}.'
            except EnvironmentError as error:
                self.model_name = "Helsinki-NLP/opus-mt-tc-bible-big-mul-mul" # Last resort: try multi to multi
                return self.HelsinkiNLP_mulmul()
        except KeyError as error:
            return f"Error: Translation direction {self.sl} to {self.tl} is not supported by Helsinki Translation Models", error

    def madlad(self):
        model = T5ForConditionalGeneration.from_pretrained(self.model_name, device_map="auto")
        tokenizer = T5Tokenizer.from_pretrained(self.model_name)
        text = f"<2{self.tl}> {self.input_text}"
        # input_ids = tokenizer(text, return_tensors="pt").input_ids.to(model.device)
        # outputs = model.generate(input_ids=input_ids)    
        # return tokenizer.decode(outputs[0], skip_special_tokens=True)
        # Use a pipeline as a high-level helper
        translator = pipeline('translation', model=model, tokenizer=tokenizer, src_lang=self.sl, tgt_lang=self.tl)
        translated_text = translator(text, max_length=512)
        return translated_text[0]['translation_text']
        # pipe = pipeline("translation", model=)
    
    def smollm(self):
        tokenizer = AutoTokenizer.from_pretrained(self.model_name)
        model = AutoModelForCausalLM.from_pretrained(self.model_name)
        prompt = f"""Translate the following {self.sl} text to {self.tl}, generating only the translated text and maintaining the original meaning and tone:
        {self.input_text}
        Translation:"""
        inputs = tokenizer(prompt, return_tensors="pt")
        outputs = model.generate(
            inputs.input_ids,
            max_length=len(inputs.input_ids[0]) + 150,
            temperature=0.3,
            do_sample=True
        ) 
        response = tokenizer.decode(outputs[0], skip_special_tokens=True)
        print(response)
        return response.split("Translation:")[-1].strip()

    def flan(self):
        tokenizer = T5Tokenizer.from_pretrained(self.model_name, legacy=False)
        model = T5ForConditionalGeneration.from_pretrained(self.model_name)
        prompt = f"translate {self.sl} to {self.tl}: {self.input_text}"
        input_ids = tokenizer(prompt, return_tensors="pt").input_ids
        outputs = model.generate(input_ids)
        return tokenizer.decode(outputs[0], skip_special_tokens=True).strip()

    def tfive(self):
        tokenizer = T5Tokenizer.from_pretrained(self.model_name)
        model = T5ForConditionalGeneration.from_pretrained(self.model_name, device_map="auto")
        prompt = f"translate {self.sl} to {self.tl}: {self.input_text}"
        input_ids = tokenizer.encode(prompt, return_tensors="pt")
        output_ids = model.generate(input_ids, max_length=512)
        translated_text = tokenizer.decode(output_ids[0], skip_special_tokens=True).strip()
        return translated_text
    
    def mbart_many_to_many(self):
        from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
        model = MBartForConditionalGeneration.from_pretrained(self.model_name)
        tokenizer = MBart50TokenizerFast.from_pretrained(self.model_name)
        # translate source to target
        tokenizer.src_lang = languagecodes.mbart_large_languages[self.sl]
        encoded = tokenizer(self.input_text, return_tensors="pt")
        generated_tokens = model.generate(
            **encoded,
            forced_bos_token_id=tokenizer.lang_code_to_id[languagecodes.mbart_large_languages[self.tl]]
        )
        return tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
    
    def mbart_one_to_many(self):
        # translate from English
        from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
        model = MBartForConditionalGeneration.from_pretrained(self.model_name)
        tokenizer = MBart50TokenizerFast.from_pretrained(self.model_name, src_lang="en_XX")
        model_inputs = tokenizer(self.input_text, return_tensors="pt")
        langid = languagecodes.mbart_large_languages[self.tl]
        generated_tokens = model.generate(
            **model_inputs,
            forced_bos_token_id=tokenizer.lang_code_to_id[langid]
        )
        return tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
    
    def mbart_many_to_one(self):
        # translate to English
        from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
        model = MBartForConditionalGeneration.from_pretrained(self.model_name)
        tokenizer = MBart50TokenizerFast.from_pretrained(self.model_name)
        tokenizer.src_lang = languagecodes.mbart_large_languages[self.sl]
        encoded = tokenizer(self.input_text, return_tensors="pt")
        generated_tokens = model.generate(**encoded)
        return tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
        
    def mtom(self):
        from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
        model = M2M100ForConditionalGeneration.from_pretrained(self.model_name)
        tokenizer = M2M100Tokenizer.from_pretrained(self.model_name)
        tokenizer.src_lang = self.sl
        encoded = tokenizer(self.input_text, return_tensors="pt")
        generated_tokens = model.generate(**encoded, forced_bos_token_id=tokenizer.get_lang_id(self.tl))
        return tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]

    def bigscience(self):  
        tokenizer = AutoTokenizer.from_pretrained(self.model_name)
        model = AutoModelForSeq2SeqLM.from_pretrained(self.model_name)
        self.input_text = self.input_text if self.input_text.endswith('.') else f'{self.input_text}.'
        inputs = tokenizer.encode(f"Translate to {self.tl}: {self.input_text}", return_tensors="pt")
        outputs = model.generate(inputs)
        translation = tokenizer.decode(outputs[0])
        translation = translation.replace('<pad> ', '').replace('</s>', '')
        return translation
    
    def bloomz(self):  
        tokenizer = AutoTokenizer.from_pretrained(self.model_name)
        model = AutoModelForCausalLM.from_pretrained(self.model_name)
        self.input_text = self.input_text if self.input_text.endswith('.') else f'{self.input_text}.'
        # inputs = tokenizer.encode(f"Translate from {self.sl} to {self.tl}: {self.input_text} Translation:", return_tensors="pt")
        inputs = tokenizer.encode(f"Translate to {self.tl}: {self.input_text}", return_tensors="pt")
        outputs = model.generate(inputs)
        translation = tokenizer.decode(outputs[0])
        translation = translation.replace('<pad> ', '').replace('</s>', '')
        translation = translation.split('Translation:')[-1].strip() if 'Translation:' in translation else translation.strip()
        return translation
    
    def nllb(self):
        tokenizer = AutoTokenizer.from_pretrained(self.model_name, src_lang=self.sl)
        # model = AutoModelForSeq2SeqLM.from_pretrained(self.model_name, device_map="auto", torch_dtype=torch.bfloat16)
        model = AutoModelForSeq2SeqLM.from_pretrained(self.model_name)
        translator = pipeline('translation', model=model, tokenizer=tokenizer, src_lang=self.sl, tgt_lang=self.tl)
        translated_text = translator(self.input_text, max_length=512)
        return translated_text[0]['translation_text']
    
    def wingpt(self):
        model = AutoModelForCausalLM.from_pretrained(
           self.model_name,
           torch_dtype="auto",
           device_map="auto"
        )
        tokenizer = AutoTokenizer.from_pretrained(self.model_name)
        # input_json = '{"input_text": self.input_text}'
        messages = [
           {"role": "system", "content": f"Translate this to {self.tl} language"}, 
           {"role": "user", "content": self.input_text}
        ]
        
        text = tokenizer.apply_chat_template(
           messages,
           tokenize=False,
           add_generation_prompt=True
        )
        model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
        
        generated_ids = model.generate(
           **model_inputs,
           max_new_tokens=512,
           temperature=0.1
        )
        
        generated_ids = [
           output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
        ]
        print(tokenizer.batch_decode(generated_ids, skip_special_tokens=True))
        output = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
        result = output.split('\n')[-1].strip() if '\n' in output else output.strip()
        return result
    
    def eurollm(self):
        tokenizer = AutoTokenizer.from_pretrained(self.model_name)
        model = AutoModelForCausalLM.from_pretrained(self.model_name)  
        prompt = f"{self.sl}: {self.input_text} {self.tl}:"
        inputs = tokenizer(prompt, return_tensors="pt")
        outputs = model.generate(**inputs, max_new_tokens=512)
        output = tokenizer.decode(outputs[0], skip_special_tokens=True)
        print(output)
        # result = output.rsplit(f'{self.tl}:')[-1].strip() if f'{self.tl}:' in output else output.strip()
        result = output.rsplit(f'{self.tl}:')[-1].strip() if '\n' in output or f'{self.tl}:' in output else output.strip()
        return result
    
    def eurollm_instruct(self):
        tokenizer = AutoTokenizer.from_pretrained(self.model_name)
        model = AutoModelForCausalLM.from_pretrained(self.model_name)
        text = f'<|im_start|>system\n<|im_end|>\n<|im_start|>user\nTranslate the following {self.sl} source text to {self.tl}:\n{self.sl}: {self.input_text} \n{self.tl}: <|im_end|>\n<|im_start|>assistant\n'
        inputs = tokenizer(text, return_tensors="pt")
        outputs = model.generate(**inputs, max_new_tokens=512)
        output = tokenizer.decode(outputs[0], skip_special_tokens=True)
        if f'{self.tl}:' in output:
            output = output.rsplit(f'{self.tl}:')[-1].strip().replace('assistant\n', '').strip()
        return output

    def teuken(self):
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        model = AutoModelForCausalLM.from_pretrained(
            self.model_name,
            trust_remote_code=True,
            torch_dtype=torch.bfloat16,
        )
        model = model.to(device).eval()
        tokenizer = AutoTokenizer.from_pretrained(
            self.model_name,
            use_fast=False,
            trust_remote_code=True,
        )
        translation_prompt = f"Translate the following text from {self.sl} into {self.tl}: {self.input_text}"
        messages = [{"role": "User", "content": translation_prompt}]
        prompt_ids = tokenizer.apply_chat_template(messages, chat_template="EN", tokenize=True, add_generation_prompt=False, return_tensors="pt")
        prediction = model.generate(
            prompt_ids.to(model.device),
            max_length=512,
            do_sample=True,
            top_k=50,
            top_p=0.95,
            temperature=0.7,
            num_return_sequences=1,
        )
        translation = tokenizer.decode(prediction[0].tolist())
        return translation

    def unbabel(self):
        pipe = pipeline("text-generation", model=self.model_name, torch_dtype=torch.bfloat16, device_map="auto")
        messages = [{"role": "user",
                     "content": f"Translate the following text from {self.sl} into {self.tl}.\n{self.sl}: {self.input_text}.\n{self.tl}:"}]
        prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False)
        tokenized_input = pipe.tokenizer(self.input_text, return_tensors="pt")
        num_input_tokens = len(tokenized_input["input_ids"][0])
        max_new_tokens = round(num_input_tokens + 0.25 * num_input_tokens)
        outputs = pipe(prompt, max_new_tokens=max_new_tokens, do_sample=False)
        translated_text = outputs[0]["generated_text"]
        print(f"Input chars: {len(input_text)}", f"Input tokens: {num_input_tokens}", f"max_new_tokens: {max_new_tokens}",
              "Chars to tokens ratio:", round(len(input_text) / num_input_tokens, 2), f"Raw translation: {translated_text}")
        markers = ["<end_of_turn>", "<|im_end|>", "<|im_start|>assistant"] # , "\n" 
        for marker in markers:
            if marker in translated_text:
                translated_text = translated_text.split(marker)[1].strip()
        translated_text = translated_text.replace('Answer:', '', 1).strip() if translated_text.startswith('Answer:') else translated_text
        translated_text = translated_text.split("Translated text:")[0].strip() if "Translated text:" in translated_text else translated_text
        split_translated_text = translated_text.split('\n', translated_text.count('\n'))
        translated_text = '\n'.join(split_translated_text[:input_text.count('\n')+1])
        return translated_text

    def bergamot(model_name: str = 'deen', sl: str = 'de', tl: str = 'en', input_text: str = 'Hallo, mein Freund'):
        try:
            import bergamot
            # input_text = [input_text] if isinstance(input_text, str) else input_text           
            config = bergamot.ServiceConfig(numWorkers=4)
            service = bergamot.Service(config)
            model = service.modelFromConfigPath(f"./{model_name}/bergamot.config.yml")
            options = bergamot.ResponseOptions(alignment=False, qualityScores=False, HTML=False)
            rawresponse = service.translate(model, bergamot.VectorString(input_text), options)
            translated_text: str = next(iter(rawresponse)).target.text
            message_text = f"Translated from {sl} to {tl} with Bergamot {model_name}."
        except Exception as error:
            response = error
        return translated_text, message_text

@spaces.GPU
def translate_text(input_text: str, s_language: str, t_language: str, model_name: str) -> tuple[str, str]:
    """
    Translates the input text from the source language to the target language  using a specified model.

    Parameters:
        input_text (str): The source text to be translated
        s_language (str): The source language of the input text
        t_language (str): The target language in which the input text is translated
        model_name (str): The selected translation model name

    Returns:
        tuple: 
            translated_text(str): The input text translated to the selected target language
            message_text(str):  A descriptive message summarizing the translation process. Example: "Translated from English to German with Helsinki-NLP."
    
    Example:
        >>> translate_text("Hello world", "English", "German", "Helsinki-NLP")
        ("Hallo Welt", "Translated from English to German with Helsinki-NLP.")
    """
    
    sl = all_langs[s_language][0]
    tl = all_langs[t_language][0]
    message_text = f'Translated from {s_language} to {t_language} with {model_name}'
    translated_text = None
    try:    
        if model_name == "Helsinki-NLP/opus-mt-tc-bible-big-mul-mul":
            translated_text, message_text = Translators(model_name, sl, tl, input_text).HelsinkiNLP_mulmul()
        
        elif model_name == "Helsinki-NLP":
            translated_text, message_text = Translators(model_name, sl, tl, input_text).HelsinkiNLP()
        
        elif model_name == 'Argos':
            translated_text = Translators(model_name, sl, tl, input_text).argos()
    
        elif model_name == 'Google':
            translated_text = Translators(model_name, sl, tl, input_text).google()
    
        elif "m2m" in model_name.lower():
            translated_text = Translators(model_name, sl, tl, input_text).mtom()
        
        elif model_name.startswith('t5'):
            translated_text = Translators(model_name, s_language, t_language, input_text).tfive()
            
        elif 'flan' in model_name.lower():
            translated_text = Translators(model_name, s_language, t_language, input_text).flan()

        elif 'madlad' in model_name.lower():
            translated_text = Translators(model_name, sl, tl, input_text).madlad()
            
        elif 'mt0' in model_name.lower():
            translated_text = Translators(model_name, s_language, t_language, input_text).bigscience()
    
        elif 'bloomz' in model_name.lower():
            translated_text = Translators(model_name, s_language, t_language, input_text).bloomz()
            
        elif 'nllb' in model_name.lower():
            nnlbsl, nnlbtl = languagecodes.nllb_language_codes[s_language], languagecodes.nllb_language_codes[t_language]
            translated_text = Translators(model_name, nnlbsl, nnlbtl, input_text).nllb()
        
        elif model_name == "facebook/mbart-large-50-many-to-many-mmt":
            translated_text = Translators(model_name, s_language, t_language, input_text).mbart_many_to_many()
    
        elif model_name == "facebook/mbart-large-50-one-to-many-mmt":
            translated_text = Translators(model_name, s_language, t_language, input_text).mbart_one_to_many()
    
        elif model_name == "facebook/mbart-large-50-many-to-one-mmt":
            translated_text = Translators(model_name, s_language, t_language, input_text).mbart_many_to_one()
        
        elif 'teuken' in model_name.lower():
            translated_text = Translators(model_name, s_language, t_language, input_text).teuken()
    
        elif model_name == "utter-project/EuroLLM-1.7B-Instruct":
            translated_text = Translators(model_name, s_language, t_language, input_text).eurollm_instruct()
        
        elif model_name == "utter-project/EuroLLM-1.7B":
            translated_text = Translators(model_name, s_language, t_language, input_text).eurollm()
                
        elif 'Unbabel' in model_name:   
            translated_text = Translators(model_name, s_language, t_language, input_text).unbabel()
            
        elif model_name == "HuggingFaceTB/SmolLM3-3B":
            translated_text = Translators(model_name, s_language, t_language, input_text).smollm()

        elif model_name == "winninghealth/WiNGPT-Babel-2":      
            translated_text = Translators(model_name, s_language, t_language, input_text).wingpt()

        elif model_name == "Bergamot":
            translated_text, message_text = Translators(model_name, s_language, t_language, input_text).bergamot()
            
    except Exception as error:
        translated_text = error
    finally:
        print(input_text, translated_text, message_text)
        return translated_text, message_text

# Function to swap dropdown values
def swap_languages(src_lang, tgt_lang):
    return tgt_lang, src_lang 

def get_info(model_name: str, sl: str = None, tl: str = None):
    helsinki = '### [Helsinki-NLP](https://huggingface.co/Helsinki-NLP "Helsinki-NLP")'
    if model_name == "Helsinki-NLP" and sl is not None and tl is not None:
        response = requests.get(f'https://huggingface.co/{model_name}/opus-mt-{sl}-{tl}/raw/main/README.md').text
        if 'Repository not found' in response or 'Invalid username or password' in response.text:
            return helsinki 
        return response
    else:
        return helsinki
    if model_name == "Argos":
        return requests.get(f'https://huggingface.co/TiberiuCristianLeon/Argostranslate/raw/main/README.md').text
    if model_name == "Google":
        return "Google Translate"
    return requests.get(f'https://huggingface.co/{model_name}/raw/main/README.md').text

def create_interface():
    with gr.Blocks() as interface:
        gr.Markdown("### Machine Text Translation with Gradio API and MCP Server")
        input_text = gr.Textbox(label="Enter text to translate:", placeholder="Type your text here, maximum 512 tokens")
        
        with gr.Row(variant="compact"):
            s_language = gr.Dropdown(choices=langs, value = DEFAULTS[0], label="Source language", interactive=True, scale=2)
            t_language = gr.Dropdown(choices=langs, value = DEFAULTS[1], label="Target language", interactive=True, scale=2)
            swap_btn = gr.Button("Swap Languages", size="md", scale=1)
            swap_btn.click(fn=swap_languages, inputs=[s_language, t_language], outputs=[s_language, t_language], api_name=False, show_api=False)
        # with gr.Row(equal_height=True):
            model_name = gr.Dropdown(choices=models, label=f"Select a model. Default is {DEFAULTS[2]}.", value=DEFAULTS[2], interactive=True, scale=2)
            translate_btn = gr.Button("Translate", scale=1)

        translated_text = gr.Textbox(label="Translated text:", placeholder="Display field for translation", interactive=False, show_copy_button=True)
        message_text = gr.Textbox(label="Messages:", placeholder="Display field for status and error messages", interactive=False,
                                  value=f'Default translation settings: from {s_language.value} to {t_language.value} with {model_name.value}.')
        allmodels = gr.HTML(label="Model links:", value=', '.join([f'<a href="https://huggingface.co/{model}">{model}</a>' for model in models]))
        model_info = gr.Markdown(label="Model info:", value=DEFAULTS[2], show_copy_button=True)
        model_name.change(fn=get_info, inputs=[model_name, s_language, t_language], outputs=model_info, api_name=False, show_api=False)

        translate_btn.click(
            fn=translate_text, 
            inputs=[input_text, s_language, t_language, model_name], 
            outputs=[translated_text, message_text]
        )

    return interface

interface = create_interface()
if __name__ == "__main__":
    interface.launch(mcp_server=True)
    # interface.queue().launch(server_name="0.0.0.0", show_error=True, server_port=7860, mcp_server=True)