Spaces:
Sleeping
Sleeping
update new sentiment_analysis_service.py
Browse files
src/expon/presentation/domain/services/sentiment_analysis_service.py
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
import os
|
2 |
-
# 👇 Redirigir el caché de Hugging Face y Transformers a /tmp
|
3 |
-
os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers"
|
4 |
-
os.environ["HF_HOME"] = "/tmp/huggingface"
|
5 |
-
|
6 |
from transformers import pipeline
|
7 |
from typing import Dict
|
8 |
from huggingface_hub import login
|
9 |
-
|
|
|
|
|
|
|
10 |
|
11 |
class SentimentAnalysisService:
|
12 |
def __init__(self):
|
@@ -24,8 +23,9 @@ class SentimentAnalysisService:
|
|
24 |
try:
|
25 |
print("[LOG] Cargando pipeline...")
|
26 |
self.pipeline = pipeline(
|
27 |
-
"
|
28 |
-
model="mrm8488/t5-base-
|
|
|
29 |
)
|
30 |
print("[LOG] Pipeline cargado correctamente.")
|
31 |
except Exception as e:
|
@@ -34,11 +34,11 @@ class SentimentAnalysisService:
|
|
34 |
|
35 |
def analyze(self, transcript: str) -> Dict:
|
36 |
print("[LOG] Análisis de transcripción recibido.")
|
37 |
-
prompt = f"emocion: {transcript}"
|
38 |
try:
|
39 |
-
|
40 |
-
print("[LOG] Resultado del modelo:",
|
41 |
-
|
|
|
42 |
except Exception as e:
|
43 |
print("[ERROR] Falló la predicción:", e)
|
44 |
return {
|
@@ -55,22 +55,15 @@ class SentimentAnalysisService:
|
|
55 |
"enfado": "frustrado",
|
56 |
"amor": "conectado",
|
57 |
"sorpresa": "sorprendido",
|
58 |
-
# etiquetas del modelo (en inglés)
|
59 |
-
"joy": "entusiasta",
|
60 |
-
"fear": "ansioso",
|
61 |
-
"anger": "frustrado",
|
62 |
-
"love": "conectado",
|
63 |
-
"surprise": "sorprendido",
|
64 |
-
"sadness": "desmotivado",
|
65 |
-
"trust": "motivado"
|
66 |
}
|
67 |
|
68 |
-
mapped_emotion = emotion_mapping.get(
|
69 |
|
70 |
return {
|
71 |
"dominant_emotion": mapped_emotion,
|
72 |
"emotion_probabilities": {
|
73 |
-
mapped_emotion: 1.0
|
74 |
},
|
75 |
-
"confidence": 1.0
|
76 |
}
|
|
|
|
1 |
import os
|
|
|
|
|
|
|
|
|
2 |
from transformers import pipeline
|
3 |
from typing import Dict
|
4 |
from huggingface_hub import login
|
5 |
+
|
6 |
+
# 👇 Redirigir el caché de Hugging Face a /tmp
|
7 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers"
|
8 |
+
os.environ["HF_HOME"] = "/tmp/huggingface"
|
9 |
|
10 |
class SentimentAnalysisService:
|
11 |
def __init__(self):
|
|
|
23 |
try:
|
24 |
print("[LOG] Cargando pipeline...")
|
25 |
self.pipeline = pipeline(
|
26 |
+
"text-classification",
|
27 |
+
model="mrm8488/t5-base-spanish-emotion",
|
28 |
+
top_k=1
|
29 |
)
|
30 |
print("[LOG] Pipeline cargado correctamente.")
|
31 |
except Exception as e:
|
|
|
34 |
|
35 |
def analyze(self, transcript: str) -> Dict:
|
36 |
print("[LOG] Análisis de transcripción recibido.")
|
|
|
37 |
try:
|
38 |
+
result = self.pipeline(transcript)[0]
|
39 |
+
print("[LOG] Resultado del modelo:", result)
|
40 |
+
label = result['label'].lower()
|
41 |
+
score = result['score']
|
42 |
except Exception as e:
|
43 |
print("[ERROR] Falló la predicción:", e)
|
44 |
return {
|
|
|
55 |
"enfado": "frustrado",
|
56 |
"amor": "conectado",
|
57 |
"sorpresa": "sorprendido",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
}
|
59 |
|
60 |
+
mapped_emotion = emotion_mapping.get(label, "desconocido")
|
61 |
|
62 |
return {
|
63 |
"dominant_emotion": mapped_emotion,
|
64 |
"emotion_probabilities": {
|
65 |
+
mapped_emotion: 1.0 # 👈 devolvemos 1.0 para que no tengas que tocar nada más
|
66 |
},
|
67 |
+
"confidence": 1.0 # 👈 mismo valor fijo como antes
|
68 |
}
|
69 |
+
|