JoshuaZywoo commited on
Commit
29a8952
Β·
verified Β·
1 Parent(s): b3f2d3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -44
app.py CHANGED
@@ -1,5 +1,5 @@
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
@@ -31,8 +31,8 @@ candidate_tasks = [
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"}
@@ -68,28 +68,37 @@ def get_emotion_score(emotion):
68
  return 0.2
69
 
70
  # ------------------------------
71
- # Streamlit UI Logic
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  # ------------------------------
73
- st.set_page_config(page_title="Smart Customer Support Assistant", layout="centered")
74
  st.title("Smart Customer Support Assistant (for Agents Only)")
75
 
76
- # Session state to store chat
77
- if 'chat' not in st.session_state:
78
- st.session_state.chat = []
79
- if 'system_result' not in st.session_state:
80
- st.session_state.system_result = None
81
- if 'agent_reply' not in st.session_state:
82
- st.session_state.agent_reply = ""
83
- if 'support_required' not in st.session_state:
84
- st.session_state.support_required = ""
85
-
86
- # Always show conversation
87
  st.markdown("### Conversation")
88
- for msg in st.session_state.chat:
89
  with st.chat_message(msg['role']):
90
  st.markdown(msg['content'])
91
 
92
- # Input row with button aligned right
93
  col1, col2 = st.columns([6,1])
94
  with col1:
95
  user_input = st.text_input("Enter customer message:", key="user_input")
@@ -97,7 +106,6 @@ with col2:
97
  analyze_clicked = st.button("Analyze")
98
 
99
  if analyze_clicked and user_input.strip():
100
- # Run analysis pipeline
101
  emotion_result = emotion_classifier(user_input)
102
  emotion_label = get_emotion_label(emotion_result, user_input)
103
  emotion_score = get_emotion_score(emotion_label)
@@ -112,46 +120,42 @@ if analyze_clicked and user_input.strip():
112
  content_score += 0.4
113
 
114
  final_score = 0.5 * emotion_score + 0.5 * content_score
115
-
116
- st.session_state.chat.append({"role": "user", "content": user_input})
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."
124
  else:
125
- st.session_state.system_result = {
126
  "emotion": emotion_label,
127
  "tone": "Urgent" if emotion_score > 0.8 else "Concerned" if emotion_score > 0.5 else "Calm",
128
  "intents": top_intents
129
  }
130
- st.session_state.support_required = "πŸ”΄ Human support required."
131
 
132
- # Show support need status
133
- if st.session_state.support_required:
134
- st.markdown(f"### {st.session_state.support_required}")
135
 
136
- # Always show agent input box
137
  st.subheader("Agent Response Console")
138
- st.session_state.agent_reply = st.text_area("Compose your reply:", value=st.session_state.agent_reply)
139
  if st.button("Send Reply"):
140
- if st.session_state.agent_reply.strip():
141
- st.session_state.chat.append({"role": "assistant", "content": st.session_state.agent_reply})
142
- st.session_state.agent_reply = ""
143
- st.session_state.system_result = None
144
- st.session_state.support_required = ""
145
-
146
- # If human support needed, show status and suggestions
147
- if st.session_state.system_result is not None:
148
  st.markdown("#### Customer Status")
149
- st.markdown(f"- **Emotion:** {st.session_state.system_result['emotion'].capitalize()}")
150
- st.markdown(f"- **Tone:** {st.session_state.system_result['tone']}")
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
 
1
  # Smart Customer Support Assistant (Enhanced UI Version)
2
+ # Note: Core analysis logic remains unchanged, now with text generation and customer selection
3
 
4
  import streamlit as st
5
  from transformers import pipeline
 
31
  ]
32
 
33
  def generate_response(intent):
34
+ prompt = f"Generate a polite and helpful customer service response for the request '{intent}'. Include a greeting, summary of current status like plan or balance using anonymized placeholders (e.g. Plan X, Β₯X), a suitable recommendation, and end with a question offering assistance."
35
+ output = text_generator(prompt, max_new_tokens=100, do_sample=True)[0]['generated_text']
36
  return output
37
 
38
  urgent_emotions = {"anger", "frustration", "anxiety", "urgency", "afraid", "annoyed"}
 
68
  return 0.2
69
 
70
  # ------------------------------
71
+ # UI: Sidebar for customer selection
72
+ # ------------------------------
73
+ st.set_page_config(page_title="Smart Customer Support Assistant", layout="wide")
74
+ st.sidebar.title("πŸ“ Customer Selector")
75
+ if "customers" not in st.session_state:
76
+ st.session_state.customers = {"Customer A": [], "Customer B": [], "Customer C": []}
77
+ customer_names = list(st.session_state.customers.keys())
78
+ selected_customer = st.sidebar.selectbox("Choose a customer:", customer_names)
79
+
80
+ # Load or init selected customer's session
81
+ if "chat_sessions" not in st.session_state:
82
+ st.session_state.chat_sessions = {}
83
+ if selected_customer not in st.session_state.chat_sessions:
84
+ st.session_state.chat_sessions[selected_customer] = {
85
+ "chat": [],
86
+ "system_result": None,
87
+ "agent_reply": "",
88
+ "support_required": ""
89
+ }
90
+ session = st.session_state.chat_sessions[selected_customer]
91
+
92
+ # ------------------------------
93
+ # Main Interface
94
  # ------------------------------
 
95
  st.title("Smart Customer Support Assistant (for Agents Only)")
96
 
 
 
 
 
 
 
 
 
 
 
 
97
  st.markdown("### Conversation")
98
+ for msg in session["chat"]:
99
  with st.chat_message(msg['role']):
100
  st.markdown(msg['content'])
101
 
 
102
  col1, col2 = st.columns([6,1])
103
  with col1:
104
  user_input = st.text_input("Enter customer message:", key="user_input")
 
106
  analyze_clicked = st.button("Analyze")
107
 
108
  if analyze_clicked and user_input.strip():
 
109
  emotion_result = emotion_classifier(user_input)
110
  emotion_label = get_emotion_label(emotion_result, user_input)
111
  emotion_score = get_emotion_score(emotion_label)
 
120
  content_score += 0.4
121
 
122
  final_score = 0.5 * emotion_score + 0.5 * content_score
123
+ session["chat"].append({"role": "user", "content": user_input})
 
124
 
125
  if final_score < 0.5 and top_intents:
126
  intent = top_intents[0]
127
  response = generate_response(intent)
128
+ session["chat"].append({"role": "assistant", "content": response})
129
+ session["system_result"] = None
130
+ session["support_required"] = "🟒 Automated response handled this request."
131
  else:
132
+ session["system_result"] = {
133
  "emotion": emotion_label,
134
  "tone": "Urgent" if emotion_score > 0.8 else "Concerned" if emotion_score > 0.5 else "Calm",
135
  "intents": top_intents
136
  }
137
+ session["support_required"] = "πŸ”΄ Human support required."
138
 
139
+ if session["support_required"]:
140
+ st.markdown(f"### {session['support_required']}")
 
141
 
 
142
  st.subheader("Agent Response Console")
143
+ session["agent_reply"] = st.text_area("Compose your reply:", value=session["agent_reply"])
144
  if st.button("Send Reply"):
145
+ if session["agent_reply"].strip():
146
+ session["chat"].append({"role": "assistant", "content": session["agent_reply"]})
147
+ session["agent_reply"] = ""
148
+ session["system_result"] = None
149
+ session["support_required"] = ""
150
+
151
+ if session["system_result"] is not None:
 
152
  st.markdown("#### Customer Status")
153
+ st.markdown(f"- **Emotion:** {session['system_result']['emotion'].capitalize()}")
154
+ st.markdown(f"- **Tone:** {session['system_result']['tone']}")
155
 
156
  st.markdown("#### Detected Customer Needs")
157
+ for intent in session['system_result']['intents']:
158
  suggestion = generate_response(intent)
159
  st.markdown(f"**β€’ {intent.capitalize()}**")
160
+ if st.button(f"Use suggestion: {suggestion}", key=f"btn_{selected_customer}_{intent}"):
161
+ session["agent_reply"] = suggestion