Spaces:
Running
Running
Delete model.py
Browse files
model.py
DELETED
@@ -1,102 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf-cache"
|
3 |
-
os.environ["HF_HOME"] = "/tmp/hf-home"
|
4 |
-
|
5 |
-
import nltk
|
6 |
-
nltk.download("punkt", download_dir="/tmp/nltk_data")
|
7 |
-
|
8 |
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
9 |
-
from sklearn.cluster import KMeans
|
10 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
11 |
-
from nltk.tokenize import sent_tokenize
|
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=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
|
31 |
-
|
32 |
-
tfidf = TfidfVectorizer(stop_words="english")
|
33 |
-
tfidf_matrix = tfidf.fit_transform(sentences)
|
34 |
-
|
35 |
-
if len(sentences) <= n_clusters:
|
36 |
-
return " ".join(sentences)
|
37 |
-
|
38 |
-
kmeans = KMeans(n_clusters=n_clusters, random_state=42).fit(tfidf_matrix)
|
39 |
-
summary_sentences = []
|
40 |
-
|
41 |
-
for i in range(n_clusters):
|
42 |
-
idx = np.where(kmeans.labels_ == i)[0]
|
43 |
-
if not len(idx):
|
44 |
-
continue
|
45 |
-
avg_vector = np.asarray(tfidf_matrix[idx].mean(axis=0))
|
46 |
-
sim = cosine_similarity(avg_vector, tfidf_matrix[idx].toarray())
|
47 |
-
most_representative = sentences[idx[np.argmax(sim)]]
|
48 |
-
summary_sentences.append(most_representative)
|
49 |
-
|
50 |
-
return " ".join(sorted(summary_sentences, key=sentences.index))
|
51 |
-
|
52 |
-
# --- Rule-based Category Detectors ---
|
53 |
-
def detect_industry(text):
|
54 |
-
text = text.lower()
|
55 |
-
if any(k in text for k in ["doctor", "hospital", "health", "pill", "med"]):
|
56 |
-
return "Healthcare"
|
57 |
-
if any(k in text for k in ["flight", "hotel", "trip", "booking"]):
|
58 |
-
return "Travel"
|
59 |
-
if any(k in text for k in ["bank", "loan", "credit", "payment"]):
|
60 |
-
return "Banking"
|
61 |
-
if any(k in text for k in ["gym", "trainer", "fitness", "workout"]):
|
62 |
-
return "Fitness"
|
63 |
-
if any(k in text for k in ["movie", "series", "stream", "video"]):
|
64 |
-
return "Entertainment"
|
65 |
-
if any(k in text for k in ["game", "gaming", "console"]):
|
66 |
-
return "Gaming"
|
67 |
-
if any(k in text for k in ["food", "delivery", "restaurant", "order"]):
|
68 |
-
return "Food Delivery"
|
69 |
-
if any(k in text for k in ["school", "university", "teacher", "course"]):
|
70 |
-
return "Education"
|
71 |
-
if any(k in text for k in ["insurance", "policy", "claim"]):
|
72 |
-
return "Insurance"
|
73 |
-
if any(k in text for k in ["property", "rent", "apartment", "house"]):
|
74 |
-
return "Real Estate"
|
75 |
-
if any(k in text for k in ["shop", "buy", "product", "phone", "amazon", "flipkart"]):
|
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"]):
|
82 |
-
return "Mobile Devices"
|
83 |
-
if any(k in text for k in ["laptop", "macbook", "notebook", "chromebook"]):
|
84 |
-
return "Laptops"
|
85 |
-
if any(k in text for k in ["tv", "refrigerator", "microwave", "washer"]):
|
86 |
-
return "Home Appliances"
|
87 |
-
if any(k in text for k in ["watch", "band", "fitbit", "wearable"]):
|
88 |
-
return "Wearables"
|
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."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|