Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,89 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
model_options = {
|
| 7 |
-
"Русский → Английский": "Helsinki-NLP/opus-mt-ru-en",
|
| 8 |
-
"Английский → Русский": "Helsinki-NLP/opus-mt-en-ru",
|
| 9 |
-
"Казахский → Русский": "Helsinki-NLP/opus-mt-kk-ru",
|
| 10 |
-
"Русский → Казахский": "Helsinki-NLP/opus-mt-ru-kk",
|
| 11 |
}
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def translate_interface(direction, text):
|
| 17 |
-
if not text.strip():
|
| 18 |
-
return "Введите текст для перевода!"
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
result =
|
| 24 |
-
return result
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
def bleu_interface(direction, original, reference):
|
| 30 |
-
if not original.strip() or not reference.strip():
|
| 31 |
-
return "Введите текст и эталон!"
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
return f"BLEU: {bleu:.4f}"
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", neutral_hue="slate", secondary_hue="pink")) as demo:
|
| 48 |
-
gr.Markdown("""
|
| 49 |
-
# 🌙 Мощный Машинный Перевод
|
| 50 |
-
Тёмная тема, стильный UI, BLEU, множество моделей.
|
| 51 |
-
""")
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
with gr.Row():
|
| 55 |
-
direction = gr.Dropdown(list(model_options.keys()), label="Направление перевода")
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
with gr.Row():
|
| 59 |
-
input_text = gr.Textbox(lines=4, label="Введите текст")
|
| 60 |
-
output_text = gr.Textbox(lines=4, label="Перевод")
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
translate_btn = gr.Button("🚀 Перевести")
|
| 64 |
-
translate_btn.click(fn=translate_interface, inputs=[direction, input_text], outputs=output_text)
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
gr.Markdown("---")
|
| 68 |
-
gr.Markdown("### 📊 BLEU Оценка качества")
|
| 69 |
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
with gr.Row():
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
bleu_out = gr.Textbox(label="BLEU Score")
|
| 75 |
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
|
|
|
| 79 |
|
|
|
|
|
|
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
|
|
|
|
|
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 3 |
+
from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
|
|
|
|
| 4 |
|
| 5 |
+
# =============================
|
| 6 |
+
# Словарь доступных моделей
|
| 7 |
+
# =============================
|
| 8 |
model_options = {
|
| 9 |
+
"Русский → Английский": "Helsinki-NLP/opus-mt-ru-en",
|
| 10 |
+
"Английский → Русский": "Helsinki-NLP/opus-mt-en-ru",
|
| 11 |
+
"Казахский → Русский": "Helsinki-NLP/opus-mt-kk-ru",
|
| 12 |
+
"Русский → Казахский": "Helsinki-NLP/opus-mt-ru-kk",
|
| 13 |
}
|
| 14 |
|
| 15 |
+
# =============================
|
| 16 |
+
# Кэш моделей для ускорения
|
| 17 |
+
# =============================
|
| 18 |
+
model_cache = {}
|
| 19 |
+
|
| 20 |
+
def load_translation_model(model_name):
|
| 21 |
+
if model_name in model_cache:
|
| 22 |
+
return model_cache[model_name]
|
| 23 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 24 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 25 |
+
model_cache[model_name] = (model, tokenizer)
|
| 26 |
+
return model, tokenizer
|
| 27 |
+
|
| 28 |
+
# =============================
|
| 29 |
+
# Функция перевода
|
| 30 |
+
# =============================
|
| 31 |
def translate_interface(direction, text):
|
| 32 |
+
if not text or len(text.strip()) == 0:
|
| 33 |
+
return "Введите текст для перевода!"
|
| 34 |
+
model_name = model_options[direction]
|
| 35 |
+
model, tokenizer = load_translation_model(model_name)
|
| 36 |
+
batch = tokenizer([text], return_tensors="pt", padding=True)
|
| 37 |
+
gen = model.generate(**batch)
|
| 38 |
+
result = tokenizer.batch_decode(gen, skip_special_tokens=True)[0]
|
| 39 |
+
return result
|
| 40 |
+
|
| 41 |
+
# =============================
|
| 42 |
+
# Функция BLEU
|
| 43 |
+
# =============================
|
| 44 |
def bleu_interface(direction, original, reference):
|
| 45 |
+
if not original.strip() or not reference.strip():
|
| 46 |
+
return "Введите текст и эталон!"
|
| 47 |
+
translation = translate_interface(direction, original)
|
| 48 |
+
smoothie = SmoothingFunction().method4
|
| 49 |
+
ref_tokens = [reference.split()]
|
| 50 |
+
hyp_tokens = translation.split()
|
| 51 |
+
bleu = sentence_bleu(ref_tokens, hyp_tokens, smoothing_function=smoothie)
|
| 52 |
+
return f"BLEU: {bleu:.4f}"
|
| 53 |
+
|
| 54 |
+
# =============================
|
| 55 |
+
# Gradio UI
|
| 56 |
+
# =============================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", neutral_hue="slate", secondary_hue="pink")) as demo:
|
| 58 |
+
gr.Markdown("""
|
| 59 |
+
# 🌙 Мощный Машинный Перевод
|
| 60 |
+
Тёмная тема, стильный UI, BLEU, множество моделей.
|
| 61 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
with gr.Row():
|
| 64 |
+
direction = gr.Dropdown(list(model_options.keys()), label="Направление перевода")
|
| 65 |
|
| 66 |
+
with gr.Row():
|
| 67 |
+
input_text = gr.Textbox(lines=4, label="Введите текст")
|
| 68 |
+
output_text = gr.Textbox(lines=4, label="Перевод")
|
|
|
|
| 69 |
|
| 70 |
+
translate_btn = gr.Button("🚀 Перевести")
|
| 71 |
+
translate_btn.click(fn=translate_interface, inputs=[direction, input_text], outputs=output_text)
|
| 72 |
|
| 73 |
+
# Кнопка копирования перевода
|
| 74 |
+
copy_btn = gr.Button("📋 Коп��ровать перевод")
|
| 75 |
+
copy_btn.click(lambda text: text, inputs=output_text, outputs=output_text)
|
| 76 |
|
| 77 |
+
gr.Markdown("---")
|
| 78 |
+
gr.Markdown("### 📊 BLEU Оценка качества")
|
| 79 |
|
| 80 |
+
with gr.Row():
|
| 81 |
+
original = gr.Textbox(lines=3, label="Исходный текст")
|
| 82 |
+
reference = gr.Textbox(lines=3, label="Эталонный перевод")
|
| 83 |
+
bleu_out = gr.Textbox(label="BLEU Score")
|
| 84 |
|
| 85 |
+
bleu_btn = gr.Button("📈 Посчитать BLEU")
|
| 86 |
+
bleu_btn.click(fn=bleu_interface, inputs=[direction, original, reference], outputs=bleu_out)
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
+
demo.launch()
|