File size: 2,614 Bytes
1f28a77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb8577c
57047ce
 
1f28a77
 
 
 
 
 
 
05c5325
 
 
 
 
1f28a77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328f9ec
 
 
 
 
 
 
 
 
 
 
 
1f28a77
 
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
115
116

import os
import threading
import torch

from functools import lru_cache
from transformers import pipeline
from gliner import GLiNER
from sentence_transformers import SentenceTransformer


DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

DTYPE = (
    torch.float16
    if DEVICE == "cuda"
    else torch.float32
)

NER_MODEL = os.getenv(
    "NER_MODEL",
    "urchade/gliner_multi-v2.1"
)

SENTIMENT_MODEL = os.getenv(
    "SENTIMENT_MODEL",
    "nahiar/sentiment-analysis-v2"
)

SARCASM_MODEL = os.getenv(
    "SARCASM_MODEL",
    # "helinivan/multilingual-sarcasm-detector"
    # "jonathanjordan21/mdeberta-v3-base-indonesian-sarcasm"
    "jonathanjordan21/mdeberta-id-sarcasm-robust"
)

EMBEDDING_MODEL = os.getenv(
    "EMBEDDING_MODEL",
    "sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
)

ABSA_MODEL = os.getenv(
    "ABSA_MODEL",
    "damasukma/indobert-absa"
)


class ModelRegistry:
    """Lazy-loaded, process-local model registry."""

    def __init__(self):
        self._models = {}
        self._lock = threading.RLock()

    def get(self, name, loader):
        if name not in self._models:
            with self._lock:
                if name not in self._models:
                    self._models[name] = loader()

        return self._models[name]

    def ner(self):
        return self.get(
            "ner",
            lambda: GLiNER.from_pretrained(NER_MODEL)
        )

    def sentiment(self):
        return self.get(
            "sentiment",
            lambda: pipeline(
                "text-classification",
                model=SENTIMENT_MODEL,
                device=0 if DEVICE == "cuda" else -1,
                torch_dtype=DTYPE,
                truncation=True
            )
        )

    def sarcasm(self):
        return self.get(
            "sarcasm",
            lambda: pipeline(
                "text-classification",
                model=SARCASM_MODEL,
                device=0 if DEVICE == "cuda" else -1,
                torch_dtype=DTYPE,
                truncation=True
            )
        )

    def embedding(self):
        return self.get(
            "embedding",
            lambda: SentenceTransformer(
                EMBEDDING_MODEL,
                device=DEVICE
            )
        )

    def absa(self):
        return self.get(
            "absa",
            lambda : pipeline(
                "text-classification", 
                model=ABSA_MODEL, 
                device=0 if DEVICE == "cuda" else -1,
                torch_dtype=DTYPE,
                top_k=None
            )
        )


models = ModelRegistry()