Spaces:
Sleeping
Sleeping
jossy-gutierrez commited on
Commit ·
f43b21a
1
Parent(s): 0858e23
feat: cambio a pysentimiento para local sentiment analysis model
Browse files- app/analyzer.py +7 -15
- requirements-local.txt +1 -0
app/analyzer.py
CHANGED
|
@@ -35,17 +35,13 @@ class SentimentAnalyzer:
|
|
| 35 |
|
| 36 |
def _load_local_model(self):
|
| 37 |
try:
|
| 38 |
-
from
|
| 39 |
-
logger.info(f"Loading
|
| 40 |
-
self.pipeline =
|
| 41 |
-
|
| 42 |
-
model=self.model_name,
|
| 43 |
-
device=-1,
|
| 44 |
-
)
|
| 45 |
-
logger.info("Local model loaded successfully")
|
| 46 |
except ImportError:
|
| 47 |
raise RuntimeError(
|
| 48 |
-
"
|
| 49 |
"Run: pip install -r requirements-local.txt"
|
| 50 |
)
|
| 51 |
|
|
@@ -77,12 +73,8 @@ class SentimentAnalyzer:
|
|
| 77 |
def _analyze_local(self, text: str) -> SentimentResult:
|
| 78 |
if not self.pipeline:
|
| 79 |
raise RuntimeError("Local model not loaded")
|
| 80 |
-
result = self.pipeline(text)
|
| 81 |
-
|
| 82 |
-
confidence = result["score"]
|
| 83 |
-
scores = {"POS": 0.0, "NEG": 0.0, "NEU": 0.0}
|
| 84 |
-
scores[label] = confidence
|
| 85 |
-
items = [{"label": k, "score": v} for k, v in scores.items()]
|
| 86 |
return self._build_result(items)
|
| 87 |
|
| 88 |
def _build_result(self, items: list) -> SentimentResult:
|
|
|
|
| 35 |
|
| 36 |
def _load_local_model(self):
|
| 37 |
try:
|
| 38 |
+
from pysentimiento import create_analyzer
|
| 39 |
+
logger.info(f"Loading pysentimiento analyzer: {self.model_name}")
|
| 40 |
+
self.pipeline = create_analyzer(task="sentiment", lang="es")
|
| 41 |
+
logger.info("pysentimiento analyzer loaded successfully")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
except ImportError:
|
| 43 |
raise RuntimeError(
|
| 44 |
+
"pysentimiento not installed. "
|
| 45 |
"Run: pip install -r requirements-local.txt"
|
| 46 |
)
|
| 47 |
|
|
|
|
| 73 |
def _analyze_local(self, text: str) -> SentimentResult:
|
| 74 |
if not self.pipeline:
|
| 75 |
raise RuntimeError("Local model not loaded")
|
| 76 |
+
result = self.pipeline.predict(text)
|
| 77 |
+
items = [{"label": k, "score": v} for k, v in result.probas.items()]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
return self._build_result(items)
|
| 79 |
|
| 80 |
def _build_result(self, items: list) -> SentimentResult:
|
requirements-local.txt
CHANGED
|
@@ -6,3 +6,4 @@ numpy<2.0.0
|
|
| 6 |
transformers==4.37.0
|
| 7 |
torch==2.1.2
|
| 8 |
sentencepiece==0.1.99
|
|
|
|
|
|
| 6 |
transformers==4.37.0
|
| 7 |
torch==2.1.2
|
| 8 |
sentencepiece==0.1.99
|
| 9 |
+
pysentimiento
|