Spaces:
Running
Running
Update model.py
Browse files
model.py
CHANGED
@@ -12,19 +12,18 @@ from nltk.tokenize import sent_tokenize
|
|
12 |
from transformers import pipeline
|
13 |
import numpy as np
|
14 |
|
15 |
-
# Load
|
16 |
-
summarizer = pipeline("summarization", model="
|
17 |
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
|
|
18 |
|
19 |
-
#
|
20 |
def summarize_review(text, max_len=60, min_len=10):
|
21 |
-
"""
|
22 |
return summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)[0]["summary_text"]
|
|
|
23 |
def smart_summarize(text, n_clusters=1):
|
24 |
-
"""
|
25 |
-
Clustering + cosine similarity-based summarization
|
26 |
-
Selects most representative sentence(s) from each cluster
|
27 |
-
"""
|
28 |
sentences = sent_tokenize(text)
|
29 |
if len(sentences) <= 1:
|
30 |
return text
|
@@ -49,7 +48,45 @@ def smart_summarize(text, n_clusters=1):
|
|
49 |
|
50 |
return " ".join(sorted(summary_sentences, key=sentences.index))
|
51 |
|
52 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
def detect_industry(text):
|
54 |
text = text.lower()
|
55 |
if any(k in text for k in ["doctor", "hospital", "health", "pill", "med"]):
|
@@ -76,6 +113,7 @@ def detect_industry(text):
|
|
76 |
return "E-commerce"
|
77 |
return "Generic"
|
78 |
|
|
|
79 |
def detect_product_category(text):
|
80 |
text = text.lower()
|
81 |
if any(k in text for k in ["mobile", "smartphone", "iphone", "samsung", "phone"]):
|
@@ -89,14 +127,3 @@ def detect_product_category(text):
|
|
89 |
if any(k in text for k in ["app", "portal", "site", "website"]):
|
90 |
return "Web App"
|
91 |
return "General"
|
92 |
-
|
93 |
-
# --- Follow-up Q&A ---
|
94 |
-
def answer_followup(text, question, verbosity="brief"):
|
95 |
-
try:
|
96 |
-
response = qa_pipeline({"question": question, "context": text})
|
97 |
-
answer = response.get("answer", "")
|
98 |
-
if verbosity.lower() == "detailed":
|
99 |
-
return f"Based on the review, the answer is: **{answer}**"
|
100 |
-
return answer
|
101 |
-
except Exception:
|
102 |
-
return "Sorry, I couldn't generate a follow-up answer."
|
|
|
12 |
from transformers import pipeline
|
13 |
import numpy as np
|
14 |
|
15 |
+
# === Load Hugging Face Pipelines ===
|
16 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
17 |
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
18 |
+
emotion_pipeline = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", top_k=1)
|
19 |
|
20 |
+
# === Summarization Functions ===
|
21 |
def summarize_review(text, max_len=60, min_len=10):
|
22 |
+
"""Transformer-based summarization (brief)"""
|
23 |
return summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)[0]["summary_text"]
|
24 |
+
|
25 |
def smart_summarize(text, n_clusters=1):
|
26 |
+
"""TF-IDF Clustering based summarization"""
|
|
|
|
|
|
|
27 |
sentences = sent_tokenize(text)
|
28 |
if len(sentences) <= 1:
|
29 |
return text
|
|
|
48 |
|
49 |
return " ".join(sorted(summary_sentences, key=sentences.index))
|
50 |
|
51 |
+
# === Emotion Detection ===
|
52 |
+
def detect_emotion(text):
|
53 |
+
try:
|
54 |
+
result = emotion_pipeline(text)[0]
|
55 |
+
return result["label"]
|
56 |
+
except Exception:
|
57 |
+
return "unknown"
|
58 |
+
|
59 |
+
# === Follow-up Q&A (single or multi) ===
|
60 |
+
def answer_followup(text, question, verbosity="brief"):
|
61 |
+
try:
|
62 |
+
if isinstance(question, list):
|
63 |
+
answers = []
|
64 |
+
for q in question:
|
65 |
+
response = qa_pipeline({"question": q, "context": text})
|
66 |
+
answer = response.get("answer", "")
|
67 |
+
if verbosity.lower() == "detailed":
|
68 |
+
answers.append(f"**{q}** → {answer}")
|
69 |
+
else:
|
70 |
+
answers.append(answer)
|
71 |
+
return answers
|
72 |
+
else:
|
73 |
+
response = qa_pipeline({"question": question, "context": text})
|
74 |
+
answer = response.get("answer", "")
|
75 |
+
if verbosity.lower() == "detailed":
|
76 |
+
return f"Based on the review, the answer is: **{answer}**"
|
77 |
+
return answer
|
78 |
+
except Exception:
|
79 |
+
return "Sorry, I couldn't generate a follow-up answer."
|
80 |
+
|
81 |
+
# === Optional Explanation Generator ===
|
82 |
+
def generate_explanation(text):
|
83 |
+
try:
|
84 |
+
explanation = summarizer(text, max_length=60, min_length=20, do_sample=False)[0]["summary_text"]
|
85 |
+
return f"🧠 This review can be explained as: {explanation}"
|
86 |
+
except Exception:
|
87 |
+
return "⚠️ Explanation could not be generated."
|
88 |
+
|
89 |
+
# === Industry Detector ===
|
90 |
def detect_industry(text):
|
91 |
text = text.lower()
|
92 |
if any(k in text for k in ["doctor", "hospital", "health", "pill", "med"]):
|
|
|
113 |
return "E-commerce"
|
114 |
return "Generic"
|
115 |
|
116 |
+
# === Product Category Detector ===
|
117 |
def detect_product_category(text):
|
118 |
text = text.lower()
|
119 |
if any(k in text for k in ["mobile", "smartphone", "iphone", "samsung", "phone"]):
|
|
|
127 |
if any(k in text for k in ["app", "portal", "site", "website"]):
|
128 |
return "Web App"
|
129 |
return "General"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|