Hasitha16 commited on
Commit
e394afd
·
verified ·
1 Parent(s): c31e1c4

Update model.py

Browse files
Files changed (1) hide show
  1. model.py +35 -39
model.py CHANGED
@@ -12,6 +12,7 @@ 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")
@@ -60,7 +61,7 @@ def detect_emotion(text):
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):
@@ -68,10 +69,7 @@ def answer_followup(text, question, verbosity="brief"):
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})
@@ -81,7 +79,6 @@ def answer_followup(text, question, verbosity="brief"):
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:
@@ -91,7 +88,7 @@ def answer_only(text, question):
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"]
@@ -100,44 +97,43 @@ def generate_explanation(text):
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"
 
12
  from transformers import pipeline
13
  import numpy as np
14
  import logging
15
+ import re
16
 
17
  # === Pipelines ===
18
  summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
 
61
  logging.warning(f"Emotion detection failed: {e}")
62
  return "neutral"
63
 
64
+ # === Follow-up Q&A ===
65
  def answer_followup(text, question, verbosity="brief"):
66
  try:
67
  if isinstance(question, list):
 
69
  for q in question:
70
  response = qa_pipeline({"question": q, "context": text})
71
  ans = response.get("answer", "")
72
+ answers.append(f"**{q}** → {ans}" if verbosity.lower() == "detailed" else ans)
 
 
 
73
  return answers
74
  else:
75
  response = qa_pipeline({"question": question, "context": text})
 
79
  logging.warning(f"Follow-up error: {e}")
80
  return "Sorry, I couldn't generate a follow-up answer."
81
 
 
82
  def answer_only(text, question):
83
  try:
84
  if not question:
 
88
  logging.warning(f"Answer-only failed: {e}")
89
  return "Q&A failed."
90
 
91
+ # === Explanation Generator ===
92
  def generate_explanation(text):
93
  try:
94
  explanation = summarizer(text, max_length=60, min_length=20, do_sample=False)[0]["summary_text"]
 
97
  logging.warning(f"Explanation failed: {e}")
98
  return "⚠️ Explanation could not be generated."
99
 
100
+ # === Churn Risk Estimator ===
101
+ def assess_churn_risk(sentiment_label, emotion_label):
102
+ if sentiment_label.lower() == "negative" and emotion_label.lower() in ["anger", "fear", "sadness", "frustrated"]:
103
+ return "High Risk"
104
+ return "Low Risk"
105
+
106
+ # === Pain Point Extractor ===
107
+ def extract_pain_points(text):
108
+ common_issues = [
109
+ "slow", "crash", "lag", "expensive", "confusing", "noisy", "poor", "rude",
110
+ "unhelpful", "bug", "broken", "unresponsive", "not working", "error", "delay", "disconnect"
111
+ ]
112
+ matched = [kw for kw in common_issues if re.search(rf"\b{kw}\b", text.lower())]
113
+ return list(set(matched))[:5]
114
+
115
  # === Industry Detector ===
116
  def detect_industry(text):
117
  text = text.lower()
118
+ if any(k in text for k in ["doctor", "hospital", "health", "pill", "med"]): return "Healthcare"
119
+ if any(k in text for k in ["flight", "hotel", "trip", "booking"]): return "Travel"
120
+ if any(k in text for k in ["bank", "loan", "credit", "payment"]): return "Banking"
121
+ if any(k in text for k in ["gym", "trainer", "fitness", "workout"]): return "Fitness"
122
+ if any(k in text for k in ["movie", "series", "stream", "video"]): return "Entertainment"
123
+ if any(k in text for k in ["game", "gaming", "console"]): return "Gaming"
124
+ if any(k in text for k in ["food", "delivery", "restaurant", "order"]): return "Food Delivery"
125
+ if any(k in text for k in ["school", "university", "teacher", "course"]): return "Education"
126
+ if any(k in text for k in ["insurance", "policy", "claim"]): return "Insurance"
127
+ if any(k in text for k in ["property", "rent", "apartment", "house"]): return "Real Estate"
128
+ if any(k in text for k in ["shop", "buy", "product", "phone", "amazon", "flipkart"]): return "E-commerce"
 
 
 
 
 
 
 
 
 
 
 
129
  return "Generic"
130
 
131
  # === Product Category Detector ===
132
  def detect_product_category(text):
133
  text = text.lower()
134
+ if any(k in text for k in ["mobile", "smartphone", "iphone", "samsung", "phone"]): return "Mobile Devices"
135
+ if any(k in text for k in ["laptop", "macbook", "notebook", "chromebook"]): return "Laptops"
136
+ if any(k in text for k in ["tv", "refrigerator", "microwave", "washer"]): return "Home Appliances"
137
+ if any(k in text for k in ["watch", "band", "fitbit", "wearable"]): return "Wearables"
138
+ if any(k in text for k in ["app", "portal", "site", "website"]): return "Web App"
 
 
 
 
 
139
  return "General"