File size: 3,016 Bytes
21db53c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
from enum import Enum

from loguru import logger
from pydantic import BaseModel
from pydantic_settings import BaseSettings, SettingsConfigDict

DOCKER_SECRETS_DIR = '/run/secrets'


class QdrantMode(str, Enum):
    SERVER = 'server'
    LOCAL = 'local'
    MEMORY = 'memory'


class QdrantSettings(BaseModel):
    mode: QdrantMode = QdrantMode.SERVER

    host: str = 'localhost'
    port: int = 6333
    grpc_port: int = 6334
    coll: str = 'NekoImg'
    prefer_grpc: bool = True
    api_key: str | None = None

    local_path: str = './images_metadata'


class ModelsSettings(BaseModel):
    clip: str = 'openai/clip-vit-large-patch14'
    bert: str = 'bert-base-chinese'
    easypaddleocr: str | None = None


class OCRSearchSettings(BaseModel):
    enable: bool = True
    ocr_module: str = 'easypaddleocr'
    ocr_language: list[str] = ['ch_sim', 'en']
    ocr_min_confidence: float = 1e-2


class S3StorageSettings(BaseModel):
    path: str = "./static"
    bucket: str | None = None
    region: str | None = None
    endpoint_url: str | None = None
    access_key_id: str | None = None
    secret_access_key: str | None = None
    session_token: str | None = None


class LocalStorageSettings(BaseModel):
    path: str = './static'


class StorageMode(str, Enum):
    LOCAL = 'local'
    S3 = 's3'
    DISABLED = 'disabled'

    @property
    def enabled(self):
        return self != StorageMode.DISABLED


class StorageSettings(BaseModel):
    method: StorageMode = StorageMode.LOCAL
    s3: S3StorageSettings = S3StorageSettings()
    local: LocalStorageSettings = LocalStorageSettings()


# [Deprecated]
class StaticFileSettings(BaseModel):
    path: str = '[DEPRECATED]'
    enable: bool = True  # Deprecated


class Config(BaseSettings):
    qdrant: QdrantSettings = QdrantSettings()
    model: ModelsSettings = ModelsSettings()
    ocr_search: OCRSearchSettings = OCRSearchSettings()
    static_file: StaticFileSettings = StaticFileSettings()  # [Deprecated]
    storage: StorageSettings = StorageSettings()

    device: str = 'auto'
    cors_origins: set[str] = {'*'}
    admin_api_enable: bool = False
    admin_token: str = ''
    admin_index_queue_max_length: int = 200

    access_protected: bool = False
    access_token: str = ''

    model_config = SettingsConfigDict(env_prefix="app_", env_nested_delimiter='__',
                                      env_file=('config/default.env', 'config/local.env'),
                                      env_file_encoding='utf-8',
                                      secrets_dir=DOCKER_SECRETS_DIR if os.path.exists(
                                          DOCKER_SECRETS_DIR) else None)  # for docker secret


class Environment(BaseModel):
    local_indexing: bool = False


def _check_deprecated_settings(_config):
    if _config.static_file.path != '[DEPRECATED]':
        logger.warning("Config StaticFileSettings is deprecated and should not be set.")


config = Config()
environment = Environment()
_check_deprecated_settings(config)