jonathanjordan21 commited on
Commit
1f28a77
·
verified ·
1 Parent(s): 4700bdd

Create app/models.py

Browse files
Files changed (1) hide show
  1. app/models.py +96 -0
app/models.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import threading
4
+ import torch
5
+
6
+ from functools import lru_cache
7
+ from transformers import pipeline
8
+ from gliner import GLiNER
9
+ from sentence_transformers import SentenceTransformer
10
+
11
+
12
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
13
+
14
+ DTYPE = (
15
+ torch.float16
16
+ if DEVICE == "cuda"
17
+ else torch.float32
18
+ )
19
+
20
+ NER_MODEL = os.getenv(
21
+ "NER_MODEL",
22
+ "urchade/gliner_multi-v2.1"
23
+ )
24
+
25
+ SENTIMENT_MODEL = os.getenv(
26
+ "SENTIMENT_MODEL",
27
+ "nahiar/sentiment-analysis-v2"
28
+ )
29
+
30
+ SARCASM_MODEL = os.getenv(
31
+ "SARCASM_MODEL",
32
+ "helinivan/multilingual-sarcasm-detector"
33
+ )
34
+
35
+ EMBEDDING_MODEL = os.getenv(
36
+ "EMBEDDING_MODEL",
37
+ "sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
38
+ )
39
+
40
+
41
+ class ModelRegistry:
42
+ """Lazy-loaded, process-local model registry."""
43
+
44
+ def __init__(self):
45
+ self._models = {}
46
+ self._lock = threading.RLock()
47
+
48
+ def get(self, name, loader):
49
+ if name not in self._models:
50
+ with self._lock:
51
+ if name not in self._models:
52
+ self._models[name] = loader()
53
+
54
+ return self._models[name]
55
+
56
+ def ner(self):
57
+ return self.get(
58
+ "ner",
59
+ lambda: GLiNER.from_pretrained(NER_MODEL)
60
+ )
61
+
62
+ def sentiment(self):
63
+ return self.get(
64
+ "sentiment",
65
+ lambda: pipeline(
66
+ "text-classification",
67
+ model=SENTIMENT_MODEL,
68
+ device=0 if DEVICE == "cuda" else -1,
69
+ torch_dtype=DTYPE,
70
+ truncation=True
71
+ )
72
+ )
73
+
74
+ def sarcasm(self):
75
+ return self.get(
76
+ "sarcasm",
77
+ lambda: pipeline(
78
+ "text-classification",
79
+ model=SARCASM_MODEL,
80
+ device=0 if DEVICE == "cuda" else -1,
81
+ torch_dtype=DTYPE,
82
+ truncation=True
83
+ )
84
+ )
85
+
86
+ def embedding(self):
87
+ return self.get(
88
+ "embedding",
89
+ lambda: SentenceTransformer(
90
+ EMBEDDING_MODEL,
91
+ device=DEVICE
92
+ )
93
+ )
94
+
95
+
96
+ models = ModelRegistry()