73 lines
2.0 KiB
Python
73 lines
2.0 KiB
Python
from functools import lru_cache
|
||
from pathlib import Path
|
||
|
||
from app.ml.image.clip_classifier import ClipClassifier
|
||
from app.ml.image.image_classifier import ImageClassifier
|
||
from app.ml.image.weapon_detector import WeaponDetector
|
||
from app.ml.text_classifier import TextClassifier
|
||
from app.moderation.image.validator import ImageValidator
|
||
from app.moderation.profanity.detector import ProfanityDetector
|
||
from app.moderation.profanity.dictionary import ProfanityDictionary
|
||
from app.moderation.profanity.lemmatizer import Lemmatizer
|
||
from app.services.image_moderation_service import ImageModerationService
|
||
from app.services.text_moderation_service import TextModerationService
|
||
|
||
# Путь от пакета, а не от текущей директории: иначе запуск не из
|
||
# ai-moderation/ (тесты, IDE) падает с FileNotFoundError
|
||
PROFANITY_WORDS_PATH = (
|
||
Path(__file__).resolve().parent.parent / "resources" / "profanity_words.txt"
|
||
)
|
||
|
||
|
||
@lru_cache
|
||
def get_clip_classifier() -> ClipClassifier:
|
||
return ClipClassifier()
|
||
|
||
|
||
@lru_cache
|
||
def get_text_classifier() -> TextClassifier:
|
||
return TextClassifier()
|
||
|
||
|
||
@lru_cache
|
||
def get_profanity_detector() -> ProfanityDetector:
|
||
return ProfanityDetector(
|
||
dictionary=ProfanityDictionary(
|
||
file_path=PROFANITY_WORDS_PATH
|
||
),
|
||
lemmatizer=Lemmatizer()
|
||
)
|
||
|
||
|
||
@lru_cache
|
||
def get_text_moderation_service() -> TextModerationService:
|
||
return TextModerationService(
|
||
classifier=get_text_classifier(),
|
||
profanity_detector=get_profanity_detector()
|
||
)
|
||
|
||
|
||
@lru_cache
|
||
def get_image_validator() -> ImageValidator:
|
||
return ImageValidator()
|
||
|
||
|
||
@lru_cache
|
||
def get_image_classifier() -> ImageClassifier:
|
||
return ImageClassifier()
|
||
|
||
|
||
@lru_cache
|
||
def get_weapon_detector() -> WeaponDetector:
|
||
return WeaponDetector()
|
||
|
||
|
||
@lru_cache
|
||
def get_image_moderation_service() -> ImageModerationService:
|
||
return ImageModerationService(
|
||
classifier=get_image_classifier(),
|
||
validator=get_image_validator(),
|
||
clip_classifier=get_clip_classifier(),
|
||
weapon_detector=get_weapon_detector()
|
||
)
|