Spaces:
Running
Running
Update model.py
Browse files
model.py
CHANGED
@@ -28,11 +28,16 @@ emotion_model = pipeline("text-classification", model="j-hartmann/emotion-englis
|
|
28 |
def summarize_review(text, max_len=100, min_len=30):
|
29 |
try:
|
30 |
result = summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)
|
31 |
-
|
|
|
|
|
|
|
|
|
32 |
except Exception as e:
|
33 |
logging.warning(f"Fallback to raw text due to summarization error: {e}")
|
34 |
return text
|
35 |
|
|
|
36 |
# === Smart Summarization with Clustering ===
|
37 |
def smart_summarize(text, n_clusters=1):
|
38 |
try:
|
@@ -60,12 +65,18 @@ def smart_summarize(text, n_clusters=1):
|
|
60 |
|
61 |
# === Emotion Detection (Fixed) ===
|
62 |
def detect_emotion(text):
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
return "neutral"
|
70 |
|
71 |
# === Follow-up Q&A ===
|
|
|
28 |
def summarize_review(text, max_len=100, min_len=30):
|
29 |
try:
|
30 |
result = summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)
|
31 |
+
if result and isinstance(result, list) and "summary_text" in result[0]:
|
32 |
+
return result[0]["summary_text"]
|
33 |
+
else:
|
34 |
+
logging.warning("Summarizer output malformed, falling back.")
|
35 |
+
return text
|
36 |
except Exception as e:
|
37 |
logging.warning(f"Fallback to raw text due to summarization error: {e}")
|
38 |
return text
|
39 |
|
40 |
+
|
41 |
# === Smart Summarization with Clustering ===
|
42 |
def smart_summarize(text, n_clusters=1):
|
43 |
try:
|
|
|
65 |
|
66 |
# === Emotion Detection (Fixed) ===
|
67 |
def detect_emotion(text):
|
68 |
+
if not text.strip():
|
69 |
+
return "neutral"
|
70 |
+
try:
|
71 |
+
result = emotion_model(text, top_k=1)
|
72 |
+
if isinstance(result, list) and isinstance(result[0], dict):
|
73 |
+
return result[0]["label"]
|
74 |
+
elif isinstance(result, dict) and "label" in result:
|
75 |
+
return result["label"]
|
76 |
+
else:
|
77 |
+
return "neutral"
|
78 |
+
except Exception as e:
|
79 |
+
logging.warning(f"Emotion detection failed: {e}")
|
80 |
return "neutral"
|
81 |
|
82 |
# === Follow-up Q&A ===
|