Spaces:
Running
Running
Update model.py
Browse files
model.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
import os
|
3 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf-cache"
|
4 |
os.environ["HF_HOME"] = "/tmp/hf-home"
|
@@ -13,9 +12,11 @@ from nltk.tokenize import sent_tokenize
|
|
13 |
from transformers import pipeline
|
14 |
import numpy as np
|
15 |
|
16 |
-
# Load summarizer
|
17 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
|
|
18 |
|
|
|
19 |
def summarize_review(text):
|
20 |
"""Standard transformer-based summarization"""
|
21 |
return summarizer(text, max_length=60, min_length=10, do_sample=False)[0]["summary_text"]
|
@@ -48,3 +49,55 @@ def smart_summarize(text, n_clusters=1):
|
|
48 |
summary_sentences.append(most_representative)
|
49 |
|
50 |
return " ".join(sorted(summary_sentences, key=sentences.index))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf-cache"
|
3 |
os.environ["HF_HOME"] = "/tmp/hf-home"
|
|
|
12 |
from transformers import pipeline
|
13 |
import numpy as np
|
14 |
|
15 |
+
# Load summarizer and Q&A pipeline
|
16 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
17 |
+
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
18 |
|
19 |
+
# --- Summarization Functions ---
|
20 |
def summarize_review(text):
|
21 |
"""Standard transformer-based summarization"""
|
22 |
return summarizer(text, max_length=60, min_length=10, do_sample=False)[0]["summary_text"]
|
|
|
49 |
summary_sentences.append(most_representative)
|
50 |
|
51 |
return " ".join(sorted(summary_sentences, key=sentences.index))
|
52 |
+
|
53 |
+
# --- Rule-based Category Detectors ---
|
54 |
+
def detect_industry(text):
|
55 |
+
text = text.lower()
|
56 |
+
if any(k in text for k in ["doctor", "hospital", "health", "pill", "med"]):
|
57 |
+
return "Healthcare"
|
58 |
+
if any(k in text for k in ["flight", "hotel", "trip", "booking"]):
|
59 |
+
return "Travel"
|
60 |
+
if any(k in text for k in ["bank", "loan", "credit", "payment"]):
|
61 |
+
return "Banking"
|
62 |
+
if any(k in text for k in ["gym", "trainer", "fitness", "workout"]):
|
63 |
+
return "Fitness"
|
64 |
+
if any(k in text for k in ["movie", "series", "stream", "video"]):
|
65 |
+
return "Entertainment"
|
66 |
+
if any(k in text for k in ["game", "gaming", "console"]):
|
67 |
+
return "Gaming"
|
68 |
+
if any(k in text for k in ["food", "delivery", "restaurant", "order"]):
|
69 |
+
return "Food Delivery"
|
70 |
+
if any(k in text for k in ["school", "university", "teacher", "course"]):
|
71 |
+
return "Education"
|
72 |
+
if any(k in text for k in ["insurance", "policy", "claim"]):
|
73 |
+
return "Insurance"
|
74 |
+
if any(k in text for k in ["property", "rent", "apartment", "house"]):
|
75 |
+
return "Real Estate"
|
76 |
+
if any(k in text for k in ["shop", "buy", "product", "phone", "amazon", "flipkart"]):
|
77 |
+
return "E-commerce"
|
78 |
+
return "Generic"
|
79 |
+
|
80 |
+
def detect_product_category(text):
|
81 |
+
text = text.lower()
|
82 |
+
if any(k in text for k in ["mobile", "smartphone", "iphone", "samsung", "phone"]):
|
83 |
+
return "Mobile Devices"
|
84 |
+
if any(k in text for k in ["laptop", "macbook", "notebook", "chromebook"]):
|
85 |
+
return "Laptops"
|
86 |
+
if any(k in text for k in ["tv", "refrigerator", "microwave", "washer"]):
|
87 |
+
return "Home Appliances"
|
88 |
+
if any(k in text for k in ["watch", "band", "fitbit", "wearable"]):
|
89 |
+
return "Wearables"
|
90 |
+
if any(k in text for k in ["app", "portal", "site", "website"]):
|
91 |
+
return "Web App"
|
92 |
+
return "General"
|
93 |
+
|
94 |
+
# --- Follow-up Q&A ---
|
95 |
+
def answer_followup(text, question, verbosity="brief"):
|
96 |
+
try:
|
97 |
+
response = qa_pipeline({"question": question, "context": text})
|
98 |
+
answer = response.get("answer", "")
|
99 |
+
if verbosity.lower() == "detailed":
|
100 |
+
return f"Based on the review, the answer is: **{answer}**"
|
101 |
+
return answer
|
102 |
+
except Exception:
|
103 |
+
return "Sorry, I couldn't generate a follow-up answer."
|