Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForSeq2SeqLM, NllbTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
6 |
+
tokenizer = NllbTokenizer.from_pretrained("leks-forever/nllb-200-distilled-600M")
|
7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("leks-forever/nllb-200-distilled-600M")
|
8 |
+
model = model.to(device)
|
9 |
+
|
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 |
+
global tokenizer
|
12 |
+
|
13 |
+
if src_lang in language_codes:
|
14 |
+
src_lang = language_codes[src_lang]
|
15 |
+
|
16 |
+
if tgt_lang in language_codes:
|
17 |
+
tgt_lang = language_codes[tgt_lang]
|
18 |
+
|
19 |
+
tokenizer.src_lang = src_lang
|
20 |
+
tokenizer.tgt_lang = tgt_lang
|
21 |
+
|
22 |
+
inputs = tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=max_input_length)
|
23 |
+
result = model.generate(
|
24 |
+
**inputs.to(model.device),
|
25 |
+
forced_bos_token_id=tokenizer.convert_tokens_to_ids(tgt_lang),
|
26 |
+
max_new_tokens=int(a + b * inputs.input_ids.shape[1]),
|
27 |
+
num_beams=num_beams,
|
28 |
+
**kwargs
|
29 |
+
)
|
30 |
+
return tokenizer.batch_decode(result, skip_special_tokens=True)[0]
|
31 |
+
|
32 |
+
import gradio as gr
|
33 |
+
|
34 |
+
src_lang, tgt_lang = "Русский", "Лезги чlал"
|
35 |
+
|
36 |
+
interface = {
|
37 |
+
"Лезги чlал": {
|
38 |
+
"title": 'Лезги-урус чlалар',
|
39 |
+
'placeholder': 'кхьих лезги чlалал',
|
40 |
+
'translate_btn': 'Элкъурун',
|
41 |
+
'lang_swap_btn': 'чlал Дегишрун',
|
42 |
+
},
|
43 |
+
"Русский": {
|
44 |
+
"title": 'русско-лезгинский переводчик',
|
45 |
+
'placeholder': 'введите текст на русском для перевода',
|
46 |
+
'translate_btn': 'перевести',
|
47 |
+
'lang_swap_btn': 'сменить язык',
|
48 |
+
},
|
49 |
+
}
|
50 |
+
|
51 |
+
language_codes = {
|
52 |
+
"Русский": "rus_Cyrl",
|
53 |
+
"Лезги чlал": "lez_Cyrl",
|
54 |
+
}
|
55 |
+
|
56 |
+
def swap_languages(src_lang, tgt_lang, src_text):
|
57 |
+
# Swap languages and update the relevant UI components (placeholder, button labels)
|
58 |
+
new_src_lang = tgt_lang
|
59 |
+
new_tgt_lang = src_lang
|
60 |
+
|
61 |
+
# Update components' properties dynamically
|
62 |
+
return (
|
63 |
+
new_src_lang,
|
64 |
+
new_tgt_lang,
|
65 |
+
gr.Textbox(placeholder=interface[new_src_lang]['placeholder'], value=''), # Clear and update placeholder
|
66 |
+
gr.Markdown(f"# {interface[tgt_lang]['title']}"),
|
67 |
+
gr.Button(value=interface[new_src_lang]['translate_btn']), # Update translate button label
|
68 |
+
gr.Button(value=interface[new_src_lang]['lang_swap_btn']) # Update swap button label
|
69 |
+
)
|
70 |
+
|
71 |
+
with gr.Blocks() as demo:
|
72 |
+
title = gr.Markdown(f"# {interface[src_lang]['title']}")
|
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] # Update states and components
|
95 |
+
)
|
96 |
+
|
97 |
+
if __name__ == "__main__":
|
98 |
+
demo.launch()
|