Spaces:
Sleeping
Sleeping
Commit ·
7c6cdf1
1
Parent(s): 13ad3f7
Initial version: English-Russian translator app
Browse files- README.md +38 -1
- app.py +36 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -10,4 +10,41 @@ pinned: false
|
|
| 10 |
license: apache-2.0
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
license: apache-2.0
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# English-to-Russian Translator
|
| 14 |
+
|
| 15 |
+
A simple web application for translating text from English to Russian using a pre-trained neural model.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
- Web interface built with [Gradio](https://gradio.app/)
|
| 19 |
+
- Uses the Hugging Face model [`Helsinki-NLP/opus-mt-en-ru`](https://huggingface.co/Helsinki-NLP/opus-mt-en-ru) for translation
|
| 20 |
+
- Input field for English text, translation button, and output field for Russian translation
|
| 21 |
+
- Handles empty input and long text errors gracefully
|
| 22 |
+
|
| 23 |
+
## How to Run (Locally)
|
| 24 |
+
1. Установите зависимости:
|
| 25 |
+
```bash
|
| 26 |
+
pip install -r requirements.txt
|
| 27 |
+
```
|
| 28 |
+
2. Запустите приложение:
|
| 29 |
+
```bash
|
| 30 |
+
python app.py
|
| 31 |
+
```
|
| 32 |
+
3. Откройте браузер и перейдите по адресу http://127.0.0.1:7860
|
| 33 |
+
|
| 34 |
+
## How to Deploy on Hugging Face Spaces
|
| 35 |
+
1. Загрузите в репозиторий следующие файлы:
|
| 36 |
+
- `app.py`
|
| 37 |
+
- `requirements.txt`
|
| 38 |
+
- `README.md`
|
| 39 |
+
2. Создайте новый Space на [Hugging Face Spaces](https://huggingface.co/spaces) с типом Gradio.
|
| 40 |
+
3. После загрузки файлов приложение автоматически соберётся и будет доступно онлайн.
|
| 41 |
+
|
| 42 |
+
---
|
| 43 |
+
|
| 44 |
+
## About
|
| 45 |
+
- This project was created in the **Windsurf** environment with the help of GPT-4.1.
|
| 46 |
+
- Author: [Trashchenkov Sergei](https://github.com/trashchenkov)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
**Made with ❤️ using Gradio, Hugging Face Transformers, and GPT-4.1 in Windsurf.**
|
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Загрузка модели и токенизатора
|
| 6 |
+
MODEL_NAME = "Helsinki-NLP/opus-mt-en-ru"
|
| 7 |
+
tokenizer = MarianTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
+
model = MarianMTModel.from_pretrained(MODEL_NAME)
|
| 9 |
+
|
| 10 |
+
def translate(text):
|
| 11 |
+
if not text.strip():
|
| 12 |
+
return "Пожалуйста, введите текст для перевода."
|
| 13 |
+
if len(text) > 500:
|
| 14 |
+
return "Слишком длинный текст. Пожалуйста, введите до 500 символов."
|
| 15 |
+
try:
|
| 16 |
+
batch = tokenizer([text], return_tensors="pt", padding=True)
|
| 17 |
+
with torch.no_grad():
|
| 18 |
+
gen = model.generate(**batch)
|
| 19 |
+
translated = tokenizer.batch_decode(gen, skip_special_tokens=True)[0]
|
| 20 |
+
return translated
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return f"Ошибка при переводе: {str(e)}"
|
| 23 |
+
|
| 24 |
+
with gr.Blocks() as demo:
|
| 25 |
+
gr.Markdown("# Переводчик с английского на русский")
|
| 26 |
+
with gr.Row():
|
| 27 |
+
input_text = gr.Textbox(label="Текст на английском", placeholder="Введите текст на английском языке")
|
| 28 |
+
with gr.Row():
|
| 29 |
+
translate_btn = gr.Button("Перевести")
|
| 30 |
+
with gr.Row():
|
| 31 |
+
output_text = gr.Textbox(label="Перевод на русский", interactive=False)
|
| 32 |
+
|
| 33 |
+
translate_btn.click(translate, inputs=input_text, outputs=output_text)
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=3.0.0
|
| 2 |
+
transformers>=4.0.0
|
| 3 |
+
torch
|
| 4 |
+
sentencepiece
|