Valeriy Sinyukov
commited on
Commit
·
9d5b1d9
1
Parent(s):
ef4294f
Support multiple languages
Browse filesSupport english and russian for now.
Change UI according to selected language.
app.py
CHANGED
|
@@ -2,6 +2,12 @@ import streamlit as st
|
|
| 2 |
|
| 3 |
from category_classification.models import models as class_models
|
| 4 |
from common import Input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
@st.cache_data
|
| 7 |
def load_class_model(name):
|
|
@@ -9,12 +15,12 @@ def load_class_model(name):
|
|
| 9 |
return model
|
| 10 |
|
| 11 |
|
|
|
|
|
|
|
| 12 |
model_name = st.selectbox(
|
| 13 |
-
|
| 14 |
-
options=class_models.get_all_model_names()
|
| 15 |
)
|
| 16 |
-
|
| 17 |
-
abstract = st.text_area("Paper abstract")
|
| 18 |
|
| 19 |
if title:
|
| 20 |
input = Input(title=title, abstract=abstract)
|
|
|
|
| 2 |
|
| 3 |
from category_classification.models import models as class_models
|
| 4 |
from common import Input
|
| 5 |
+
from languages import *
|
| 6 |
+
|
| 7 |
+
title = {en: "Papers classification", ru: "Классификация статей"}
|
| 8 |
+
model_label = {en: "Select model", ru: "Выберете модель"}
|
| 9 |
+
abstract_label = {en: "Abstract", ru: "Аннотация"}
|
| 10 |
+
|
| 11 |
|
| 12 |
@st.cache_data
|
| 13 |
def load_class_model(name):
|
|
|
|
| 15 |
return model
|
| 16 |
|
| 17 |
|
| 18 |
+
lang = st.pills(label=langs_str, options=langs, default=en)
|
| 19 |
+
st.title(title[lang])
|
| 20 |
model_name = st.selectbox(
|
| 21 |
+
model_label[lang], options=class_models.get_model_names_by_lang(lang)
|
|
|
|
| 22 |
)
|
| 23 |
+
abstract = st.text_area(abstract_label[lang])
|
|
|
|
| 24 |
|
| 25 |
if title:
|
| 26 |
input = Input(title=title, abstract=abstract)
|
category_classification/models/models.py
CHANGED
|
@@ -1,11 +1,15 @@
|
|
| 1 |
import importlib.util
|
| 2 |
import os
|
| 3 |
import sys
|
|
|
|
| 4 |
import warnings
|
| 5 |
from pathlib import Path
|
| 6 |
|
|
|
|
| 7 |
def import_model_module(file_path: os.PathLike):
|
| 8 |
-
module_name = str(Path(file_path).relative_to(os.getcwd())).replace(
|
|
|
|
|
|
|
| 9 |
spec = importlib.util.spec_from_file_location(module_name, file_path)
|
| 10 |
module = importlib.util.module_from_spec(spec)
|
| 11 |
sys.modules[module_name] = module
|
|
@@ -14,6 +18,7 @@ def import_model_module(file_path: os.PathLike):
|
|
| 14 |
|
| 15 |
|
| 16 |
models = {}
|
|
|
|
| 17 |
|
| 18 |
file_dir = Path(__file__).parents[0]
|
| 19 |
|
|
@@ -25,8 +30,10 @@ for path in file_dir.glob("*"):
|
|
| 25 |
module = import_model_module(model_file_path)
|
| 26 |
name_key = "name"
|
| 27 |
get_model_key = "get_model"
|
|
|
|
| 28 |
name = getattr(module, name_key, None)
|
| 29 |
get_model = getattr(module, get_model_key, None)
|
|
|
|
| 30 |
|
| 31 |
def check_attr_exists(attr_name, attr):
|
| 32 |
if attr is None:
|
|
@@ -54,12 +61,19 @@ for path in file_dir.glob("*"):
|
|
| 54 |
continue
|
| 55 |
if not check_attr_exists(get_model_key, get_model):
|
| 56 |
continue
|
|
|
|
|
|
|
| 57 |
if not check_attr_type(name_key, name, str):
|
| 58 |
continue
|
| 59 |
if not check_attr_callable(get_model_key, get_model):
|
| 60 |
continue
|
|
|
|
|
|
|
| 61 |
|
| 62 |
models[name] = get_model
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
def get_model(name: str):
|
|
@@ -70,3 +84,8 @@ def get_model(name: str):
|
|
| 70 |
|
| 71 |
def get_all_model_names():
|
| 72 |
return list(models.keys())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import importlib.util
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
+
import typing as tp
|
| 5 |
import warnings
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
+
|
| 9 |
def import_model_module(file_path: os.PathLike):
|
| 10 |
+
module_name = str(Path(file_path).relative_to(os.getcwd())).replace(
|
| 11 |
+
os.path.sep, "."
|
| 12 |
+
)
|
| 13 |
spec = importlib.util.spec_from_file_location(module_name, file_path)
|
| 14 |
module = importlib.util.module_from_spec(spec)
|
| 15 |
sys.modules[module_name] = module
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
models = {}
|
| 21 |
+
language_to_models = {}
|
| 22 |
|
| 23 |
file_dir = Path(__file__).parents[0]
|
| 24 |
|
|
|
|
| 30 |
module = import_model_module(model_file_path)
|
| 31 |
name_key = "name"
|
| 32 |
get_model_key = "get_model"
|
| 33 |
+
supported_langs_key = "supported_langs"
|
| 34 |
name = getattr(module, name_key, None)
|
| 35 |
get_model = getattr(module, get_model_key, None)
|
| 36 |
+
supported_langs = getattr(module, supported_langs_key, None)
|
| 37 |
|
| 38 |
def check_attr_exists(attr_name, attr):
|
| 39 |
if attr is None:
|
|
|
|
| 61 |
continue
|
| 62 |
if not check_attr_exists(get_model_key, get_model):
|
| 63 |
continue
|
| 64 |
+
if not check_attr_exists(supported_langs_key, supported_langs):
|
| 65 |
+
continue
|
| 66 |
if not check_attr_type(name_key, name, str):
|
| 67 |
continue
|
| 68 |
if not check_attr_callable(get_model_key, get_model):
|
| 69 |
continue
|
| 70 |
+
if not check_attr_type(supported_langs_key, supported_langs, tp.Iterable):
|
| 71 |
+
continue
|
| 72 |
|
| 73 |
models[name] = get_model
|
| 74 |
+
for lang in supported_langs:
|
| 75 |
+
language_to_models.setdefault(lang, {})
|
| 76 |
+
language_to_models[lang][name] = get_model
|
| 77 |
|
| 78 |
|
| 79 |
def get_model(name: str):
|
|
|
|
| 84 |
|
| 85 |
def get_all_model_names():
|
| 86 |
return list(models.keys())
|
| 87 |
+
|
| 88 |
+
def get_model_names_by_lang(lang):
|
| 89 |
+
if lang not in language_to_models:
|
| 90 |
+
return []
|
| 91 |
+
return language_to_models[lang]
|
category_classification/models/oracat__bert_paper_classifier/model.py
CHANGED
|
@@ -15,3 +15,5 @@ class BertPaperClassifierModel:
|
|
| 15 |
|
| 16 |
def get_model():
|
| 17 |
return BertPaperClassifierModel()
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def get_model():
|
| 17 |
return BertPaperClassifierModel()
|
| 18 |
+
|
| 19 |
+
supported_langs = ['en']
|
category_classification/models/oracat__bert_paper_classifier_arxiv/model.py
CHANGED
|
@@ -12,3 +12,5 @@ class BertPaperClassifierArxivModel:
|
|
| 12 |
|
| 13 |
def get_model():
|
| 14 |
return BertPaperClassifierArxivModel()
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def get_model():
|
| 14 |
return BertPaperClassifierArxivModel()
|
| 15 |
+
|
| 16 |
+
supported_langs = ['en']
|
languages.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
en = "en"
|
| 2 |
+
ru = "ru"
|
| 3 |
+
|
| 4 |
+
langs = [en, ru]
|
| 5 |
+
|
| 6 |
+
langs_str = "Language/Язык"
|