Merge pull request #1 from kavlab/mulyavin_aa
Browse files- .gitignore +148 -0
- README.md +8 -1
- mulyavin_aa/__init__.py +0 -0
- mulyavin_aa/langdetector.py +34 -0
- mulyavin_aa/translator.py +24 -0
- requirements.txt +2 -0
- run.py +50 -16
.gitignore
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### Example user template template
|
| 2 |
+
### Example user template
|
| 3 |
+
|
| 4 |
+
# IntelliJ project files
|
| 5 |
+
.idea
|
| 6 |
+
*.iml
|
| 7 |
+
out
|
| 8 |
+
gen
|
| 9 |
+
### Python template
|
| 10 |
+
# Byte-compiled / optimized / DLL files
|
| 11 |
+
__pycache__/
|
| 12 |
+
*.py[cod]
|
| 13 |
+
*$py.class
|
| 14 |
+
|
| 15 |
+
# C extensions
|
| 16 |
+
*.so
|
| 17 |
+
|
| 18 |
+
# Distribution / packaging
|
| 19 |
+
.Python
|
| 20 |
+
build/
|
| 21 |
+
develop-eggs/
|
| 22 |
+
dist/
|
| 23 |
+
downloads/
|
| 24 |
+
eggs/
|
| 25 |
+
.eggs/
|
| 26 |
+
lib/
|
| 27 |
+
lib64/
|
| 28 |
+
parts/
|
| 29 |
+
sdist/
|
| 30 |
+
var/
|
| 31 |
+
wheels/
|
| 32 |
+
share/python-wheels/
|
| 33 |
+
*.egg-info/
|
| 34 |
+
.installed.cfg
|
| 35 |
+
*.egg
|
| 36 |
+
MANIFEST
|
| 37 |
+
|
| 38 |
+
# PyInstaller
|
| 39 |
+
# Usually these files are written by a python script from a template
|
| 40 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 41 |
+
*.manifest
|
| 42 |
+
*.spec
|
| 43 |
+
|
| 44 |
+
# Installer logs
|
| 45 |
+
pip-log.txt
|
| 46 |
+
pip-delete-this-directory.txt
|
| 47 |
+
|
| 48 |
+
# Unit test / coverage reports
|
| 49 |
+
htmlcov/
|
| 50 |
+
.tox/
|
| 51 |
+
.nox/
|
| 52 |
+
.coverage
|
| 53 |
+
.coverage.*
|
| 54 |
+
.cache
|
| 55 |
+
nosetests.xml
|
| 56 |
+
coverage.xml
|
| 57 |
+
*.cover
|
| 58 |
+
*.py,cover
|
| 59 |
+
.hypothesis/
|
| 60 |
+
.pytest_cache/
|
| 61 |
+
cover/
|
| 62 |
+
|
| 63 |
+
# Translations
|
| 64 |
+
*.mo
|
| 65 |
+
*.pot
|
| 66 |
+
|
| 67 |
+
# Django stuff:
|
| 68 |
+
*.log
|
| 69 |
+
local_settings.py
|
| 70 |
+
db.sqlite3
|
| 71 |
+
db.sqlite3-journal
|
| 72 |
+
|
| 73 |
+
# Flask stuff:
|
| 74 |
+
instance/
|
| 75 |
+
.webassets-cache
|
| 76 |
+
|
| 77 |
+
# Scrapy stuff:
|
| 78 |
+
.scrapy
|
| 79 |
+
|
| 80 |
+
# Sphinx documentation
|
| 81 |
+
docs/_build/
|
| 82 |
+
|
| 83 |
+
# PyBuilder
|
| 84 |
+
.pybuilder/
|
| 85 |
+
target/
|
| 86 |
+
|
| 87 |
+
# Jupyter Notebook
|
| 88 |
+
.ipynb_checkpoints
|
| 89 |
+
|
| 90 |
+
# IPython
|
| 91 |
+
profile_default/
|
| 92 |
+
ipython_config.py
|
| 93 |
+
|
| 94 |
+
# pyenv
|
| 95 |
+
# For a library or package, you might want to ignore these files since the code is
|
| 96 |
+
# intended to run in multiple environments; otherwise, check them in:
|
| 97 |
+
# .python-version
|
| 98 |
+
|
| 99 |
+
# pipenv
|
| 100 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 101 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 102 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 103 |
+
# install all needed dependencies.
|
| 104 |
+
#Pipfile.lock
|
| 105 |
+
|
| 106 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
| 107 |
+
__pypackages__/
|
| 108 |
+
|
| 109 |
+
# Celery stuff
|
| 110 |
+
celerybeat-schedule
|
| 111 |
+
celerybeat.pid
|
| 112 |
+
|
| 113 |
+
# SageMath parsed files
|
| 114 |
+
*.sage.py
|
| 115 |
+
|
| 116 |
+
# Environments
|
| 117 |
+
.env
|
| 118 |
+
.venv
|
| 119 |
+
env/
|
| 120 |
+
venv/
|
| 121 |
+
ENV/
|
| 122 |
+
env.bak/
|
| 123 |
+
venv.bak/
|
| 124 |
+
|
| 125 |
+
# Spyder project settings
|
| 126 |
+
.spyderproject
|
| 127 |
+
.spyproject
|
| 128 |
+
|
| 129 |
+
# Rope project settings
|
| 130 |
+
.ropeproject
|
| 131 |
+
|
| 132 |
+
# mkdocs documentation
|
| 133 |
+
/site
|
| 134 |
+
|
| 135 |
+
# mypy
|
| 136 |
+
.mypy_cache/
|
| 137 |
+
.dmypy.json
|
| 138 |
+
dmypy.json
|
| 139 |
+
|
| 140 |
+
# Pyre type checker
|
| 141 |
+
.pyre/
|
| 142 |
+
|
| 143 |
+
# pytype static type analyzer
|
| 144 |
+
.pytype/
|
| 145 |
+
|
| 146 |
+
# Cython debug symbols
|
| 147 |
+
cython_debug/
|
| 148 |
+
|
README.md
CHANGED
|
@@ -1 +1,8 @@
|
|
| 1 |
-
# Программная инженерия. Практическое задание №2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Программная инженерия. Практическое задание №2
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
## Используемые модели
|
| 7 |
+
- Определение языка текста - [papluca/xlm-roberta-base-language-detection](https://huggingface.co/papluca/xlm-roberta-base-language-detection)
|
| 8 |
+
- Перевод текста с языка Ru на En - [Helsinki-NLP/opus-mt-ru-en](https://huggingface.co/Helsinki-NLP/opus-mt-ru-en)
|
mulyavin_aa/__init__.py
ADDED
|
File without changes
|
mulyavin_aa/langdetector.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Модуль определения языка
|
| 2 |
+
|
| 3 |
+
import transformers.pipelines.base
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def load_text_detection_model() -> transformers.pipelines.base.Pipeline:
|
| 8 |
+
"""
|
| 9 |
+
Подгрузка модели детектора языка
|
| 10 |
+
:return: Класс пайплайна для модели детектора языка
|
| 11 |
+
"""
|
| 12 |
+
return pipeline("text-classification", model="papluca/xlm-roberta-base-language-detection")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def lang_detect(text: str, langdetector: transformers.pipelines.base.Pipeline) -> str | None:
|
| 16 |
+
"""
|
| 17 |
+
Определение языка для введенного текста
|
| 18 |
+
:param text: Текст
|
| 19 |
+
:param langdetector: Пайплайн для модели детектора языка
|
| 20 |
+
:return: Код определенного языка (если определен)
|
| 21 |
+
"""
|
| 22 |
+
text_langs = list(langdetector(text, ))
|
| 23 |
+
|
| 24 |
+
if not text_langs:
|
| 25 |
+
return None
|
| 26 |
+
|
| 27 |
+
for i in range(3):
|
| 28 |
+
if i > len(text_langs) - 1:
|
| 29 |
+
break
|
| 30 |
+
print(text_langs[i])
|
| 31 |
+
if text_langs[i]['label'] in ['ru', 'en']:
|
| 32 |
+
return text_langs[i]['label']
|
| 33 |
+
|
| 34 |
+
return None
|
mulyavin_aa/translator.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Модуль перевода языка
|
| 2 |
+
|
| 3 |
+
import transformers.pipelines.base
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def load_text_translator_model() -> transformers.pipelines.base.Pipeline:
|
| 8 |
+
"""
|
| 9 |
+
Подгрузка модели переводчика языка
|
| 10 |
+
:return: Класс пайплайна для модели переводчика языка
|
| 11 |
+
"""
|
| 12 |
+
return pipeline("translation", model=f'Helsinki-NLP/opus-mt-ru-en')
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def translate_to_en(text: str, translator: transformers.pipelines.base.Pipeline) -> str:
|
| 16 |
+
"""
|
| 17 |
+
Перевод текста с русского на английский
|
| 18 |
+
:param text: Текст
|
| 19 |
+
:param translator: Пайплайна для модели переводчика языка
|
| 20 |
+
:return: Переведенный текст
|
| 21 |
+
"""
|
| 22 |
+
text = translator(text)[0]['translation_text']
|
| 23 |
+
print(text)
|
| 24 |
+
return text
|
requirements.txt
CHANGED
|
@@ -2,3 +2,5 @@ datasets==2.14.6
|
|
| 2 |
streamlit==1.28.1
|
| 3 |
torch==2.1.0
|
| 4 |
transformers==4.35.0
|
|
|
|
|
|
|
|
|
| 2 |
streamlit==1.28.1
|
| 3 |
torch==2.1.0
|
| 4 |
transformers==4.35.0
|
| 5 |
+
sentencepiece=0.1.99
|
| 6 |
+
sacremoses=0.1.1
|
run.py
CHANGED
|
@@ -1,27 +1,61 @@
|
|
| 1 |
-
from transformers import pipeline
|
| 2 |
-
from datasets import load_dataset
|
| 3 |
-
import torch
|
| 4 |
import streamlit as st
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
@st.cache_resource
|
| 7 |
-
def
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
+
from mulyavin_aa import langdetector
|
| 4 |
+
from mulyavin_aa import translator
|
| 5 |
+
|
| 6 |
+
LANG_DETECTOR = "LANG_DETECTOR"
|
| 7 |
+
TRANSLATOR = "TRANSLATOR"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
@st.cache_resource
|
| 11 |
+
def load_models() -> dict:
|
| 12 |
+
"""
|
| 13 |
+
Получение справочника моделей
|
| 14 |
+
:return: Справочник моделей
|
| 15 |
+
"""
|
| 16 |
+
models = dict()
|
| 17 |
+
models[LANG_DETECTOR] = langdetector.load_text_detection_model()
|
| 18 |
+
models[TRANSLATOR] = translator.load_text_translator_model()
|
| 19 |
+
|
| 20 |
+
return models
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def main_app():
|
| 24 |
+
"""
|
| 25 |
+
Основная программа
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
models = load_models()
|
| 29 |
+
|
| 30 |
+
st.title = 'Домашнее задание'
|
| 31 |
+
|
| 32 |
+
# Оформление заголовка
|
| 33 |
+
st.header('Домашнее задание', divider='gray')
|
| 34 |
|
| 35 |
+
input_text = st.text_area(
|
| 36 |
+
'Введите текст на русском или английском языке и нажмите кнопку генератора:')
|
| 37 |
|
| 38 |
+
if st.button('Генерировать!!!'):
|
| 39 |
+
# Определение языка
|
| 40 |
+
text_lang = langdetector.lang_detect(input_text, models[LANG_DETECTOR])
|
| 41 |
+
if text_lang not in ['ru', 'en']:
|
| 42 |
+
st.error('Язык текста не может быть определен')
|
| 43 |
+
return
|
| 44 |
|
| 45 |
+
# Перевод языка если не en
|
| 46 |
+
if text_lang in ['ru']:
|
| 47 |
+
input_text = translator.translate_to_en(input_text, models[TRANSLATOR])
|
| 48 |
|
| 49 |
+
tab1, tab2, tab3 = st.tabs(['Озвученный текст', 'Таб 2', 'Таб 3'])
|
| 50 |
+
with tab1:
|
| 51 |
+
st.header("Озвученный текст на английском языке")
|
| 52 |
+
# st.audio()
|
| 53 |
|
| 54 |
+
with tab2:
|
| 55 |
+
st.header("Таб 2")
|
| 56 |
|
| 57 |
+
with tab3:
|
| 58 |
+
st.header("Таб 3")
|
| 59 |
|
| 60 |
|
| 61 |
+
main_app()
|