alexander1010 commited on
Commit
9723a64
verified
1 Parent(s): 2d9aa1b

Update new emociones

Browse files
src/expon/presentation/domain/services/sentiment_analysis_service.py CHANGED
@@ -2,6 +2,7 @@ from transformers import pipeline
2
  import os
3
  from typing import Dict
4
 
 
5
  os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers"
6
  os.environ["HF_HOME"] = "/tmp/huggingface"
7
 
@@ -12,7 +13,7 @@ class SentimentAnalysisService:
12
  self.pipeline = pipeline(
13
  "sentiment-analysis",
14
  model="finiteautomata/beto-sentiment-analysis",
15
- top_k=1
16
  )
17
  print("[LOG] Pipeline cargado correctamente.")
18
  except Exception as e:
@@ -22,10 +23,27 @@ class SentimentAnalysisService:
22
  def analyze(self, transcript: str) -> Dict:
23
  print("[LOG] An谩lisis de transcripci贸n recibido.")
24
  try:
25
- result = self.pipeline(transcript)[0]
26
- print("[LOG] Resultado del modelo:", result)
27
- label = result['label'].upper()
28
- score = result['score']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  except Exception as e:
30
  print("[ERROR] Fall贸 la predicci贸n:", e)
31
  return {
@@ -34,18 +52,8 @@ class SentimentAnalysisService:
34
  "confidence": 0.0
35
  }
36
 
37
- emotion_mapping = {
38
- "POS": "entusiasta",
39
- "NEU": "neutro",
40
- "NEG": "frustrado"
41
- }
42
-
43
- mapped_emotion = emotion_mapping.get(label, "desconocido")
44
-
45
  return {
46
- "dominant_emotion": mapped_emotion,
47
- "emotion_probabilities": {
48
- mapped_emotion: 1.0
49
- },
50
- "confidence": round(score, 2)
51
  }
 
2
  import os
3
  from typing import Dict
4
 
5
+ # Configuraci贸n de entorno para Hugging Face Spaces
6
  os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers"
7
  os.environ["HF_HOME"] = "/tmp/huggingface"
8
 
 
13
  self.pipeline = pipeline(
14
  "sentiment-analysis",
15
  model="finiteautomata/beto-sentiment-analysis",
16
+ top_k=3 # 猬咃笍 Mostrar las 3 emociones m谩s probables
17
  )
18
  print("[LOG] Pipeline cargado correctamente.")
19
  except Exception as e:
 
23
  def analyze(self, transcript: str) -> Dict:
24
  print("[LOG] An谩lisis de transcripci贸n recibido.")
25
  try:
26
+ results = self.pipeline(transcript)
27
+ print("[LOG] Resultado del modelo:", results)
28
+
29
+ # Emoci贸n dominante = la primera (mayor score)
30
+ dominant = results[0]
31
+
32
+ emotion_mapping = {
33
+ "POS": "entusiasta",
34
+ "NEU": "neutro",
35
+ "NEG": "frustrado"
36
+ }
37
+
38
+ dominant_emotion = emotion_mapping.get(dominant['label'], "desconocido")
39
+ confidence = round(dominant['score'], 2)
40
+
41
+ # Crear diccionario de probabilidades mapeadas
42
+ emotion_probabilities = {
43
+ emotion_mapping.get(r['label'], r['label']): round(r['score'], 2)
44
+ for r in results
45
+ }
46
+
47
  except Exception as e:
48
  print("[ERROR] Fall贸 la predicci贸n:", e)
49
  return {
 
52
  "confidence": 0.0
53
  }
54
 
 
 
 
 
 
 
 
 
55
  return {
56
+ "dominant_emotion": dominant_emotion,
57
+ "emotion_probabilities": emotion_probabilities,
58
+ "confidence": confidence
 
 
59
  }