Spaces:
Sleeping
Sleeping
Download app/models.py from jonathanjordan21/Test-ticketing: direct link, hf CLI and curl.
- Browser
- Download file 2.61 kB
-
https://huggingface.co/spaces/jonathanjordan21/Test-ticketing/resolve/main/app/models.py
- Command line
-
hf download hf://spaces/jonathanjordan21/Test-ticketing/app/models.py
-
curl -L -o models.py https://huggingface.co/spaces/jonathanjordan21/Test-ticketing/resolve/main/app/models.py
2.61 kB
| 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() | |