mulyavinaa commited on
Commit
82d4d7d
1 Parent(s): 5a8691c

Создание API для моделей определения текста и перевода текста из Ru в En

Browse files
api.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import uvicorn
2
+ from fastapi import FastAPI
3
+
4
+ import mulyavin_aa.langdetector
5
+ import mulyavin_aa.translator
6
+ import mulyavin_aa.model.langdetector
7
+ import mulyavin_aa.model.translator
8
+
9
+ app = FastAPI()
10
+
11
+
12
+ # Представление проекта урлом по умолчанию
13
+ @app.get("/")
14
+ async def root():
15
+ """Получение базовой информации об API"""
16
+ return {"message": "Проект ДЗ: Модуль 3. Разработка API для приложений искусственного интеллекта (vo_HW)",
17
+ "git": "https://github.com/kavlab/urfu_iml_2023_1_3_hw2",
18
+ "apis": [
19
+ {
20
+ "descr": "API для определения языка текста ",
21
+ "base_url": "/langdetector"
22
+ },
23
+ {
24
+ "descr": "API для перевода текста из Ru в En",
25
+ "base_url": "/translator"}
26
+ ]}
27
+
28
+
29
+ @app.post("/langdetector/detect")
30
+ def lang_detect(request: mulyavin_aa.model.langdetector.Request) \
31
+ -> mulyavin_aa.model.langdetector.Response:
32
+ """Определение языка текста"""
33
+ pipe = mulyavin_aa.langdetector.load_text_detection_model()
34
+ langs = pipe(request.text, )
35
+ return mulyavin_aa.model.langdetector.Response(langs=langs)
36
+
37
+
38
+ @app.post("/translator/translate")
39
+ def lang_detect(request: mulyavin_aa.model.translator.TranslatorRequest) \
40
+ -> mulyavin_aa.model.translator.TranslatorResponse:
41
+ """Перевод текста из Ru в En"""
42
+ response = mulyavin_aa.model.translator.TranslatorResponse()
43
+ pipe = mulyavin_aa.translator.load_text_translator_model()
44
+ response.text = mulyavin_aa.translator.translate_to_en(request.text, pipe)
45
+
46
+ return response
47
+
48
+
49
+ # Запуск как приложения
50
+ if __name__ == '__main__':
51
+ uvicorn.run(app, port=8080, host='127.0.0.1')
mulyavin_aa/langdetector.py CHANGED
@@ -12,7 +12,9 @@ def load_text_detection_model() -> transformers.pipelines.base.Pipeline:
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: Текст
@@ -30,5 +32,7 @@ def lang_detect(text: str, langdetector: transformers.pipelines.base.Pipeline) -
30
  print(text_langs[i])
31
  if text_langs[i]['label'] in ['ru', 'en']:
32
  return text_langs[i]['label']
 
 
33
 
34
  return None
 
12
  return pipeline("text-classification", model="papluca/xlm-roberta-base-language-detection")
13
 
14
 
15
+ def lang_detect(text: str,
16
+ langdetector: transformers.pipelines.base.Pipeline,
17
+ is_any: bool = False) -> str:
18
  """
19
  Определение языка для введенного текста
20
  :param text: Текст
 
32
  print(text_langs[i])
33
  if text_langs[i]['label'] in ['ru', 'en']:
34
  return text_langs[i]['label']
35
+ if is_any:
36
+ return text_langs[i]['label']
37
 
38
  return None
mulyavin_aa/model/__init__.py ADDED
File without changes
mulyavin_aa/model/langdetector.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+
3
+
4
+ class Request(BaseModel):
5
+ """Структура запроса"""
6
+ text: str
7
+
8
+
9
+ class LangInfo(BaseModel):
10
+ """Информация об определении языка"""
11
+ label: str
12
+ score: float
13
+
14
+
15
+ class Response(BaseModel):
16
+ """Структура ответа"""
17
+ langs: list[LangInfo]
mulyavin_aa/model/translator.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+
3
+
4
+ class TranslatorRequest(BaseModel):
5
+ text: str
6
+
7
+
8
+ class TranslatorResponse(BaseModel):
9
+ text: str