Spaces:
Running
Running
Update model.py
Browse files
model.py
CHANGED
|
@@ -15,16 +15,17 @@ import logging
|
|
| 15 |
import re
|
| 16 |
|
| 17 |
# === Pipelines ===
|
| 18 |
-
summarizer = pipeline("summarization", model="
|
| 19 |
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
| 20 |
-
|
| 21 |
|
| 22 |
# === Brief Summarization ===
|
| 23 |
-
def summarize_review(text, max_len=
|
| 24 |
try:
|
| 25 |
-
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
-
logging.warning(f"
|
| 28 |
return text
|
| 29 |
|
| 30 |
# === Smart Summarization with Clustering ===
|
|
@@ -54,18 +55,11 @@ def smart_summarize(text, n_clusters=1):
|
|
| 54 |
|
| 55 |
# === Emotion Detection (Fixed) ===
|
| 56 |
def detect_emotion(text):
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 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"
|
| 68 |
-
|
| 69 |
# === Follow-up Q&A ===
|
| 70 |
def answer_followup(text, question, verbosity="brief"):
|
| 71 |
try:
|
|
|
|
| 15 |
import re
|
| 16 |
|
| 17 |
# === Pipelines ===
|
| 18 |
+
summarizer = pipeline("summarization", model="google/pegasus-xsum")
|
| 19 |
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
| 20 |
+
emotion_model = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", top_k=1)
|
| 21 |
|
| 22 |
# === Brief Summarization ===
|
| 23 |
+
def summarize_review(text, max_len=60, min_len=15):
|
| 24 |
try:
|
| 25 |
+
result = summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)
|
| 26 |
+
return result[0]["summary_text"]
|
| 27 |
except Exception as e:
|
| 28 |
+
logging.warning(f"Fallback to raw text due to summarization error: {e}")
|
| 29 |
return text
|
| 30 |
|
| 31 |
# === Smart Summarization with Clustering ===
|
|
|
|
| 55 |
|
| 56 |
# === Emotion Detection (Fixed) ===
|
| 57 |
def detect_emotion(text):
|
| 58 |
+
result = emotion_model(text)
|
| 59 |
+
if isinstance(result, list) and len(result) > 0:
|
| 60 |
+
return result[0]["label"]
|
| 61 |
+
return "neutral"
|
| 62 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
# === Follow-up Q&A ===
|
| 64 |
def answer_followup(text, question, verbosity="brief"):
|
| 65 |
try:
|