stazizov commited on
Commit
04bb5d6
·
verified ·
1 Parent(s): dfcd79c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSeq2SeqLM, NllbTokenizer
3
+ import torch
4
+ import spaces
5
+
6
+ device = "cuda"
7
+ model_id = "leks-forever/nllb-200-distilled-600M-v1"
8
+
9
+ @spaces.GPU
10
+ def translate(text, src_lang='lez_Cyrl', tgt_lang='rus_Cyrl', a=32, b=3, max_input_length=1024, num_beams=1, **kwargs):
11
+
12
+ tokenizer = NllbTokenizer.from_pretrained(model_id)
13
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
14
+ model = model.to(device)
15
+
16
+ if src_lang in language_codes:
17
+ src_lang = language_codes[src_lang]
18
+
19
+ if tgt_lang in language_codes:
20
+ tgt_lang = language_codes[tgt_lang]
21
+
22
+ tokenizer.src_lang = src_lang
23
+ tokenizer.tgt_lang = tgt_lang
24
+
25
+ inputs = tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=max_input_length)
26
+ result = model.generate(
27
+ **inputs.to(model.device),
28
+ forced_bos_token_id=tokenizer.convert_tokens_to_ids(tgt_lang),
29
+ max_new_tokens=int(a + b * inputs.input_ids.shape[1]),
30
+ num_beams=num_beams,
31
+ **kwargs
32
+ )
33
+ return tokenizer.batch_decode(result, skip_special_tokens=True)[0]
34
+
35
+ src_lang, tgt_lang = "Русский", "Лезги чlал"
36
+
37
+ interface = {
38
+ "Лезги чlал": {
39
+ "title": 'Лезги-урус чlалар',
40
+ 'placeholder': 'кхьих лезги чlалал',
41
+ 'translate_btn': 'Элкъурун',
42
+ 'lang_swap_btn': 'чlал Дегишрун',
43
+ },
44
+ "Русский": {
45
+ "title": 'русско-лезгинский переводчик',
46
+ 'placeholder': 'введите текст на русском для перевода',
47
+ 'translate_btn': 'перевести',
48
+ 'lang_swap_btn': 'сменить язык',
49
+ },
50
+ }
51
+
52
+ language_codes = {
53
+ "Русский": "rus_Cyrl",
54
+ "Лезги чlал": "lez_Cyrl",
55
+ }
56
+
57
+ def swap_languages(src_lang, tgt_lang, src_text):
58
+ new_src_lang = tgt_lang
59
+ new_tgt_lang = src_lang
60
+
61
+ return (
62
+ new_src_lang,
63
+ new_tgt_lang,
64
+ gr.Textbox(placeholder=interface[new_src_lang]['placeholder'], value=''),
65
+ gr.Markdown(f"# {interface[tgt_lang]['title']}"),
66
+ gr.Button(value=interface[new_src_lang]['translate_btn']),
67
+ gr.Button(value=interface[new_src_lang]['lang_swap_btn'])
68
+ )
69
+
70
+ with gr.Blocks() as demo:
71
+ title = gr.Markdown(f"# {interface[src_lang]['title']}")
72
+ info = gr.Markdown(info_text) # Add the info text here
73
+
74
+ with gr.Row():
75
+ with gr.Column():
76
+ src_text = gr.Textbox(label='', placeholder=interface[src_lang]['placeholder'])
77
+ with gr.Column():
78
+ tgt_text = gr.Textbox(label='', interactive=False)
79
+
80
+ src_lang_state = gr.State(value=src_lang)
81
+ tgt_lang_state = gr.State(value=tgt_lang)
82
+
83
+ translate_btn = gr.Button(interface[src_lang]['translate_btn'])
84
+ swap_button = gr.Button(interface[src_lang]['lang_swap_btn'])
85
+
86
+ translate_btn.click(
87
+ fn=translate,
88
+ inputs=[src_text, src_lang_state, tgt_lang_state],
89
+ outputs=tgt_text
90
+ )
91
+ swap_button.click(
92
+ fn=swap_languages,
93
+ inputs=[src_lang_state, tgt_lang_state, src_text],
94
+ outputs=[src_lang_state, tgt_lang_state, src_text, title, translate_btn, swap_button]
95
+ )
96
+
97
+ if __name__ == "__main__":
98
+ demo.launch()