JoshuaZywoo commited on
Commit
b3f2d3d
·
verified ·
1 Parent(s): e6fc2f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -27
app.py CHANGED
@@ -1,12 +1,12 @@
1
  # Smart Customer Support Assistant (Enhanced UI Version)
2
- # Note: Core analysis logic remains unchanged
3
 
4
  import streamlit as st
5
  from transformers import pipeline
6
  import re
7
 
8
  # ------------------------------
9
- # Load models (same as before)
10
  # ------------------------------
11
  emotion_classifier = pipeline(
12
  "text-classification",
@@ -14,9 +14,10 @@ emotion_classifier = pipeline(
14
  return_all_scores=True
15
  )
16
  intent_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
 
17
 
18
  # ------------------------------
19
- # Candidate tasks / templates
20
  # ------------------------------
21
  candidate_tasks = [
22
  "change mobile plan",
@@ -29,27 +30,10 @@ candidate_tasks = [
29
  "upgrade device"
30
  ]
31
 
32
- intent_solutions = {
33
- "top up balance": "Your balance is ¥12. Promo: recharge ¥100 get ¥5 bonus.",
34
- "reactivate service": "Service suspended due to unpaid ¥38. Recharge to restore in 30 mins.",
35
- "change mobile plan": "You're on Basic (¥68/5GB). Suggest Plus (¥98/20GB).",
36
- "check account status": "Data: 3.2GB/5GB. Balance: ¥12. Calls left: 22 mins.",
37
- "ask for billing support": "Last bill: ¥96 (Mar). Includes ¥16 overage.",
38
- "cancel subscription": "Contract ends: 2025-06-30. No penalty after this date.",
39
- "upgrade device": "Eligible for upgrade. New iPhone plan: ¥399/month.",
40
- "report service outage": "Signal issues detected (ZIP: XXX). Engineers notified."
41
- }
42
-
43
- intent_closings = {
44
- "top up balance": "Proceed with recharge now?",
45
- "reactivate service": "Shall I help restart your service?",
46
- "report service outage": "Shall I file a service report?",
47
- "change mobile plan": "Switch to a better plan?",
48
- "ask for billing support": "Show recent billing records?",
49
- "cancel subscription": "Guide you through cancellation?",
50
- "check account status": "Show usage and balance?",
51
- "upgrade device": "See available upgrades?"
52
- }
53
 
54
  urgent_emotions = {"anger", "frustration", "anxiety", "urgency", "afraid", "annoyed"}
55
  moderate_emotions = {"confused", "sad", "tired", "concerned", "sadness"}
@@ -133,7 +117,7 @@ if analyze_clicked and user_input.strip():
133
 
134
  if final_score < 0.5 and top_intents:
135
  intent = top_intents[0]
136
- response = f"Thank you for contacting us. I understand your concern. {intent_solutions[intent]} {intent_closings[intent]}"
137
  st.session_state.chat.append({"role": "assistant", "content": response})
138
  st.session_state.system_result = None
139
  st.session_state.support_required = "🟢 Automated response handled this request."
@@ -167,7 +151,7 @@ if st.session_state.system_result is not None:
167
 
168
  st.markdown("#### Detected Customer Needs")
169
  for intent in st.session_state.system_result['intents']:
170
- suggestion = f"Thank you for contacting us. I understand your concern. {intent_solutions[intent]} {intent_closings[intent]}"
171
  st.markdown(f"**• {intent.capitalize()}**")
172
  if st.button(suggestion, key=f"btn_{intent}"):
173
- st.session_state.agent_reply = suggestion
 
1
  # Smart Customer Support Assistant (Enhanced UI Version)
2
+ # Note: Core analysis logic remains unchanged, now with text generation
3
 
4
  import streamlit as st
5
  from transformers import pipeline
6
  import re
7
 
8
  # ------------------------------
9
+ # Load models (now includes 3rd: text generation)
10
  # ------------------------------
11
  emotion_classifier = pipeline(
12
  "text-classification",
 
14
  return_all_scores=True
15
  )
16
  intent_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
17
+ text_generator = pipeline("text2text-generation", model="declare-lab/flan-alpaca-base")
18
 
19
  # ------------------------------
20
+ # Candidate tasks / prompts
21
  # ------------------------------
22
  candidate_tasks = [
23
  "change mobile plan",
 
30
  "upgrade device"
31
  ]
32
 
33
+ def generate_response(intent):
34
+ prompt = f"Write a polite and helpful customer service response for this request: '{intent}'"
35
+ output = text_generator(prompt, max_new_tokens=80, do_sample=True)[0]['generated_text']
36
+ return output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  urgent_emotions = {"anger", "frustration", "anxiety", "urgency", "afraid", "annoyed"}
39
  moderate_emotions = {"confused", "sad", "tired", "concerned", "sadness"}
 
117
 
118
  if final_score < 0.5 and top_intents:
119
  intent = top_intents[0]
120
+ response = generate_response(intent)
121
  st.session_state.chat.append({"role": "assistant", "content": response})
122
  st.session_state.system_result = None
123
  st.session_state.support_required = "🟢 Automated response handled this request."
 
151
 
152
  st.markdown("#### Detected Customer Needs")
153
  for intent in st.session_state.system_result['intents']:
154
+ suggestion = generate_response(intent)
155
  st.markdown(f"**• {intent.capitalize()}**")
156
  if st.button(suggestion, key=f"btn_{intent}"):
157
+ st.session_state.agent_reply = suggestion