Spaces:
Running
Running
Update model.py
Browse files
model.py
CHANGED
@@ -52,12 +52,16 @@ def smart_summarize(text, n_clusters=1):
|
|
52 |
logging.error(f"Smart summarize error: {e}")
|
53 |
return text
|
54 |
|
55 |
-
# === Emotion Detection ===
|
56 |
def detect_emotion(text):
|
57 |
try:
|
58 |
result = emotion_pipeline(text)
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
61 |
except Exception as e:
|
62 |
logging.warning(f"Emotion detection failed: {e}")
|
63 |
return "neutral"
|
@@ -65,9 +69,13 @@ def detect_emotion(text):
|
|
65 |
# === Follow-up Q&A ===
|
66 |
def answer_followup(text, question, verbosity="brief"):
|
67 |
try:
|
|
|
|
|
68 |
if isinstance(question, list):
|
69 |
answers = []
|
70 |
for q in question:
|
|
|
|
|
71 |
response = qa_pipeline({"question": q, "context": text})
|
72 |
ans = response.get("answer", "")
|
73 |
answers.append(f"**{q}** → {ans}" if verbosity.lower() == "detailed" else ans)
|
@@ -80,6 +88,7 @@ def answer_followup(text, question, verbosity="brief"):
|
|
80 |
logging.warning(f"Follow-up error: {e}")
|
81 |
return "Sorry, I couldn't generate a follow-up answer."
|
82 |
|
|
|
83 |
def answer_only(text, question):
|
84 |
try:
|
85 |
if not question:
|
@@ -106,10 +115,6 @@ def assess_churn_risk(sentiment_label, emotion_label):
|
|
106 |
|
107 |
# === Pain Point Extractor ===
|
108 |
def extract_pain_points(text):
|
109 |
-
"""
|
110 |
-
Returns a list of keyword-based user pain points.
|
111 |
-
Later you can extend with transformer-based aspect mining (KeyBERT, LLMs).
|
112 |
-
"""
|
113 |
common_issues = [
|
114 |
"slow", "crash", "lag", "expensive", "confusing", "noisy", "poor", "rude",
|
115 |
"unhelpful", "bug", "broken", "unresponsive", "not working", "error", "delay", "disconnect"
|
|
|
52 |
logging.error(f"Smart summarize error: {e}")
|
53 |
return text
|
54 |
|
55 |
+
# === Emotion Detection (Fixed) ===
|
56 |
def detect_emotion(text):
|
57 |
try:
|
58 |
result = emotion_pipeline(text)
|
59 |
+
if isinstance(result, list) and len(result) > 0:
|
60 |
+
item = result[0]
|
61 |
+
if isinstance(item, list): # Nested list case
|
62 |
+
return item[0]["label"]
|
63 |
+
return item["label"]
|
64 |
+
return "neutral"
|
65 |
except Exception as e:
|
66 |
logging.warning(f"Emotion detection failed: {e}")
|
67 |
return "neutral"
|
|
|
69 |
# === Follow-up Q&A ===
|
70 |
def answer_followup(text, question, verbosity="brief"):
|
71 |
try:
|
72 |
+
if not question:
|
73 |
+
return "No question provided."
|
74 |
if isinstance(question, list):
|
75 |
answers = []
|
76 |
for q in question:
|
77 |
+
if not q.strip():
|
78 |
+
continue
|
79 |
response = qa_pipeline({"question": q, "context": text})
|
80 |
ans = response.get("answer", "")
|
81 |
answers.append(f"**{q}** → {ans}" if verbosity.lower() == "detailed" else ans)
|
|
|
88 |
logging.warning(f"Follow-up error: {e}")
|
89 |
return "Sorry, I couldn't generate a follow-up answer."
|
90 |
|
91 |
+
# === Direct follow-up route handler ===
|
92 |
def answer_only(text, question):
|
93 |
try:
|
94 |
if not question:
|
|
|
115 |
|
116 |
# === Pain Point Extractor ===
|
117 |
def extract_pain_points(text):
|
|
|
|
|
|
|
|
|
118 |
common_issues = [
|
119 |
"slow", "crash", "lag", "expensive", "confusing", "noisy", "poor", "rude",
|
120 |
"unhelpful", "bug", "broken", "unresponsive", "not working", "error", "delay", "disconnect"
|