JoshuaZywoo commited on
Commit
e16b3ce
·
verified ·
1 Parent(s): ef8cc0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -43
app.py CHANGED
@@ -97,55 +97,54 @@ if 'system_result' not in st.session_state:
97
  if 'agent_reply' not in st.session_state:
98
  st.session_state.agent_reply = ""
99
 
100
- # Display chat history
 
101
  for msg in st.session_state.chat:
102
  with st.chat_message(msg['role']):
103
  st.markdown(msg['content'])
104
 
105
- # Customer input + Analyze button
106
- col1, col2 = st.columns([5,1])
107
  with col1:
108
  user_input = st.text_input("Enter customer message:", key="user_input")
109
  with col2:
110
- if st.button("Analyze"):
111
- if user_input.strip():
112
- # Run analysis pipeline
113
- emotion_result = emotion_classifier(user_input)
114
- emotion_label = get_emotion_label(emotion_result, user_input)
115
- emotion_score = get_emotion_score(emotion_label)
116
-
117
- intent_result = intent_classifier(user_input, candidate_tasks)
118
- top_intents = [label for label, score in zip(intent_result['labels'], intent_result['scores']) if score > 0.15][:3]
119
-
120
- content_score = 0.0
121
- if any(x in user_input.lower() for x in ["out of service", "can't", "urgent", "immediately"]):
122
- content_score += 0.4
123
- if any(label in ["top up balance", "reactivate service"] for label in top_intents):
124
- content_score += 0.4
125
-
126
- final_score = 0.5 * emotion_score + 0.5 * content_score
127
-
128
- # Store user message
129
- st.session_state.chat.append({"role": "user", "content": user_input})
130
-
131
- # Auto response or escalate to agent
132
- if final_score < 0.5 and top_intents:
133
- intent = top_intents[0]
134
- response = f"Thank you for contacting us. I understand your concern. {intent_solutions[intent]} {intent_closings[intent]}"
135
- st.session_state.chat.append({"role": "assistant", "content": response})
136
- else:
137
- st.session_state.system_result = {
138
- "emotion": emotion_label,
139
- "tone": "Urgent" if emotion_score > 0.8 else "Concerned" if emotion_score > 0.5 else "Calm",
140
- "intents": top_intents
141
- }
142
-
143
- # If human support needed
144
  if st.session_state.system_result:
145
  st.markdown("---")
146
- st.subheader("Agent Response Panel")
147
 
148
- # Agent editable response
149
  st.session_state.agent_reply = st.text_area("Compose your reply:", value=st.session_state.agent_reply)
150
  if st.button("Send Reply"):
151
  if st.session_state.agent_reply.strip():
@@ -153,15 +152,13 @@ if st.session_state.system_result:
153
  st.session_state.agent_reply = ""
154
  st.session_state.system_result = None
155
 
156
- # Context info
157
  st.markdown("#### Customer Status")
158
  st.markdown(f"- **Emotion:** {st.session_state.system_result['emotion'].capitalize()}")
159
  st.markdown(f"- **Tone:** {st.session_state.system_result['tone']}")
160
 
161
- # Suggested replies
162
  st.markdown("#### Detected Customer Needs")
163
  for intent in st.session_state.system_result['intents']:
164
- st.markdown(f"**• {intent.capitalize()}**")
165
  suggestion = f"Thank you for contacting us. I understand your concern. {intent_solutions[intent]} {intent_closings[intent]}"
166
- if st.button(f"Use suggestion for '{intent}'", key=intent):
 
167
  st.session_state.agent_reply = suggestion
 
97
  if 'agent_reply' not in st.session_state:
98
  st.session_state.agent_reply = ""
99
 
100
+ # Always show conversation
101
+ st.markdown("### Conversation")
102
  for msg in st.session_state.chat:
103
  with st.chat_message(msg['role']):
104
  st.markdown(msg['content'])
105
 
106
+ # Input row with button aligned right
107
+ col1, col2 = st.columns([6,1])
108
  with col1:
109
  user_input = st.text_input("Enter customer message:", key="user_input")
110
  with col2:
111
+ analyze_clicked = st.button("Analyze")
112
+
113
+ if analyze_clicked and user_input.strip():
114
+ # Run analysis pipeline
115
+ emotion_result = emotion_classifier(user_input)
116
+ emotion_label = get_emotion_label(emotion_result, user_input)
117
+ emotion_score = get_emotion_score(emotion_label)
118
+
119
+ intent_result = intent_classifier(user_input, candidate_tasks)
120
+ top_intents = [label for label, score in zip(intent_result['labels'], intent_result['scores']) if score > 0.15][:3]
121
+
122
+ content_score = 0.0
123
+ if any(x in user_input.lower() for x in ["out of service", "can't", "urgent", "immediately"]):
124
+ content_score += 0.4
125
+ if any(label in ["top up balance", "reactivate service"] for label in top_intents):
126
+ content_score += 0.4
127
+
128
+ final_score = 0.5 * emotion_score + 0.5 * content_score
129
+
130
+ st.session_state.chat.append({"role": "user", "content": user_input})
131
+
132
+ if final_score < 0.5 and top_intents:
133
+ intent = top_intents[0]
134
+ response = f"Thank you for contacting us. I understand your concern. {intent_solutions[intent]} {intent_closings[intent]}"
135
+ st.session_state.chat.append({"role": "assistant", "content": response})
136
+ else:
137
+ st.session_state.system_result = {
138
+ "emotion": emotion_label,
139
+ "tone": "Urgent" if emotion_score > 0.8 else "Concerned" if emotion_score > 0.5 else "Calm",
140
+ "intents": top_intents
141
+ }
142
+
143
+ # Agent panel only if human support needed
 
144
  if st.session_state.system_result:
145
  st.markdown("---")
146
+ st.subheader("⚠️ Human Support Required")
147
 
 
148
  st.session_state.agent_reply = st.text_area("Compose your reply:", value=st.session_state.agent_reply)
149
  if st.button("Send Reply"):
150
  if st.session_state.agent_reply.strip():
 
152
  st.session_state.agent_reply = ""
153
  st.session_state.system_result = None
154
 
 
155
  st.markdown("#### Customer Status")
156
  st.markdown(f"- **Emotion:** {st.session_state.system_result['emotion'].capitalize()}")
157
  st.markdown(f"- **Tone:** {st.session_state.system_result['tone']}")
158
 
 
159
  st.markdown("#### Detected Customer Needs")
160
  for intent in st.session_state.system_result['intents']:
 
161
  suggestion = f"Thank you for contacting us. I understand your concern. {intent_solutions[intent]} {intent_closings[intent]}"
162
+ st.markdown(f"**• {intent.capitalize()}**")
163
+ if st.button(suggestion, key=f"btn_{intent}"):
164
  st.session_state.agent_reply = suggestion