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 |
+
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 |
+
import logging
|
| 15 |
+
|
| 16 |
+
# === Pipelines ===
|
| 17 |
+
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 18 |
+
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
| 19 |
+
emotion_pipeline = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion", top_k=1)
|
| 20 |
+
|
| 21 |
+
# === Brief Summarization ===
|
| 22 |
+
def summarize_review(text, max_len=80, min_len=20):
|
| 23 |
+
try:
|
| 24 |
+
return summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)[0]["summary_text"]
|
| 25 |
+
except Exception as e:
|
| 26 |
+
logging.warning(f"Summarization fallback used: {e}")
|
| 27 |
+
return text
|
| 28 |
+
|
| 29 |
+
# === Smart Summarization with Clustering ===
|
| 30 |
+
def smart_summarize(text, n_clusters=1):
|
| 31 |
+
try:
|
| 32 |
+
sentences = sent_tokenize(text)
|
| 33 |
+
if len(sentences) <= 1:
|
| 34 |
+
return text
|
| 35 |
+
tfidf = TfidfVectorizer(stop_words="english")
|
| 36 |
+
tfidf_matrix = tfidf.fit_transform(sentences)
|
| 37 |
+
if len(sentences) <= n_clusters:
|
| 38 |
+
return " ".join(sentences)
|
| 39 |
+
kmeans = KMeans(n_clusters=n_clusters, random_state=42).fit(tfidf_matrix)
|
| 40 |
+
summary_sentences = []
|
| 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 |
+
return " ".join(sorted(summary_sentences, key=sentences.index))
|
| 50 |
+
except Exception as e:
|
| 51 |
+
logging.error(f"Smart summarize error: {e}")
|
| 52 |
+
return text
|
| 53 |
+
|
| 54 |
+
# === Emotion Detection ===
|
| 55 |
+
def detect_emotion(text):
|
| 56 |
+
try:
|
| 57 |
+
result = emotion_pipeline(text)[0]
|
| 58 |
+
return result["label"]
|
| 59 |
+
except Exception as e:
|
| 60 |
+
logging.warning(f"Emotion detection failed: {e}")
|
| 61 |
+
return "neutral"
|
| 62 |
+
|
| 63 |
+
# === Follow-up Q&A (Flexible for list or str) ===
|
| 64 |
+
def answer_followup(text, question, verbosity="brief"):
|
| 65 |
+
try:
|
| 66 |
+
if isinstance(question, list):
|
| 67 |
+
answers = []
|
| 68 |
+
for q in question:
|
| 69 |
+
response = qa_pipeline({"question": q, "context": text})
|
| 70 |
+
ans = response.get("answer", "")
|
| 71 |
+
if verbosity.lower() == "detailed":
|
| 72 |
+
answers.append(f"**{q}** → {ans}")
|
| 73 |
+
else:
|
| 74 |
+
answers.append(ans)
|
| 75 |
+
return answers
|
| 76 |
+
else:
|
| 77 |
+
response = qa_pipeline({"question": question, "context": text})
|
| 78 |
+
ans = response.get("answer", "")
|
| 79 |
+
return f"**{question}** → {ans}" if verbosity.lower() == "detailed" else ans
|
| 80 |
+
except Exception as e:
|
| 81 |
+
logging.warning(f"Follow-up error: {e}")
|
| 82 |
+
return "Sorry, I couldn't generate a follow-up answer."
|
| 83 |
+
|
| 84 |
+
# === Fast follow-up (used for direct /followup route) ===
|
| 85 |
+
def answer_only(text, question):
|
| 86 |
+
try:
|
| 87 |
+
if not question:
|
| 88 |
+
return "No question provided."
|
| 89 |
+
return qa_pipeline({"question": question, "context": text}).get("answer", "No answer found.")
|
| 90 |
+
except Exception as e:
|
| 91 |
+
logging.warning(f"Answer-only failed: {e}")
|
| 92 |
+
return "Q&A failed."
|
| 93 |
+
|
| 94 |
+
# === Optional Explanation Generator ===
|
| 95 |
+
def generate_explanation(text):
|
| 96 |
+
try:
|
| 97 |
+
explanation = summarizer(text, max_length=60, min_length=20, do_sample=False)[0]["summary_text"]
|
| 98 |
+
return f"🧠 This review can be explained as: {explanation}"
|
| 99 |
+
except Exception as e:
|
| 100 |
+
logging.warning(f"Explanation failed: {e}")
|
| 101 |
+
return "⚠️ Explanation could not be generated."
|
| 102 |
+
|
| 103 |
+
# === Industry Detector ===
|
| 104 |
+
def detect_industry(text):
|
| 105 |
+
text = text.lower()
|
| 106 |
+
if any(k in text for k in ["doctor", "hospital", "health", "pill", "med"]):
|
| 107 |
+
return "Healthcare"
|
| 108 |
+
if any(k in text for k in ["flight", "hotel", "trip", "booking"]):
|
| 109 |
+
return "Travel"
|
| 110 |
+
if any(k in text for k in ["bank", "loan", "credit", "payment"]):
|
| 111 |
+
return "Banking"
|
| 112 |
+
if any(k in text for k in ["gym", "trainer", "fitness", "workout"]):
|
| 113 |
+
return "Fitness"
|
| 114 |
+
if any(k in text for k in ["movie", "series", "stream", "video"]):
|
| 115 |
+
return "Entertainment"
|
| 116 |
+
if any(k in text for k in ["game", "gaming", "console"]):
|
| 117 |
+
return "Gaming"
|
| 118 |
+
if any(k in text for k in ["food", "delivery", "restaurant", "order"]):
|
| 119 |
+
return "Food Delivery"
|
| 120 |
+
if any(k in text for k in ["school", "university", "teacher", "course"]):
|
| 121 |
+
return "Education"
|
| 122 |
+
if any(k in text for k in ["insurance", "policy", "claim"]):
|
| 123 |
+
return "Insurance"
|
| 124 |
+
if any(k in text for k in ["property", "rent", "apartment", "house"]):
|
| 125 |
+
return "Real Estate"
|
| 126 |
+
if any(k in text for k in ["shop", "buy", "product", "phone", "amazon", "flipkart"]):
|
| 127 |
+
return "E-commerce"
|
| 128 |
+
return "Generic"
|
| 129 |
+
|
| 130 |
+
# === Product Category Detector ===
|
| 131 |
+
def detect_product_category(text):
|
| 132 |
+
text = text.lower()
|
| 133 |
+
if any(k in text for k in ["mobile", "smartphone", "iphone", "samsung", "phone"]):
|
| 134 |
+
return "Mobile Devices"
|
| 135 |
+
if any(k in text for k in ["laptop", "macbook", "notebook", "chromebook"]):
|
| 136 |
+
return "Laptops"
|
| 137 |
+
if any(k in text for k in ["tv", "refrigerator", "microwave", "washer"]):
|
| 138 |
+
return "Home Appliances"
|
| 139 |
+
if any(k in text for k in ["watch", "band", "fitbit", "wearable"]):
|
| 140 |
+
return "Wearables"
|
| 141 |
+
if any(k in text for k in ["app", "portal", "site", "website"]):
|
| 142 |
+
return "Web App"
|
| 143 |
+
return "General"
|