Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,14 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
# Load
|
5 |
emotion_classifier = pipeline(
|
6 |
"text-classification",
|
7 |
model="j-hartmann/emotion-english-distilroberta-base",
|
8 |
return_all_scores=True
|
9 |
)
|
10 |
intent_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
11 |
-
text_generator = pipeline("text2text-generation", model="
|
12 |
|
13 |
candidate_tasks = [
|
14 |
"change mobile plan", "top up balance", "report service outage",
|
@@ -48,17 +48,22 @@ def get_emotion_score(emotion):
|
|
48 |
def generate_response(intent, human=True):
|
49 |
if human:
|
50 |
prompt = (
|
51 |
-
f"You are a telecom
|
52 |
-
"
|
53 |
-
"
|
54 |
-
"
|
|
|
55 |
)
|
56 |
else:
|
57 |
prompt = (
|
58 |
-
f"
|
59 |
-
"
|
|
|
|
|
|
|
|
|
60 |
)
|
61 |
-
result = text_generator(prompt, max_new_tokens=
|
62 |
return result[0]['generated_text'].strip()
|
63 |
|
64 |
# Streamlit UI
|
@@ -81,14 +86,14 @@ if selected_customer not in st.session_state.chat_sessions:
|
|
81 |
session = st.session_state.chat_sessions[selected_customer]
|
82 |
st.title("Smart Customer Support Assistant (for Agents Only)")
|
83 |
|
84 |
-
#
|
85 |
st.markdown("### Conversation")
|
86 |
for msg in session["chat"]:
|
87 |
avatar = "👤" if msg['role'] == 'user' else ("🤖" if msg.get("auto") else "👨💼")
|
88 |
with st.chat_message(msg['role'], avatar=avatar):
|
89 |
st.markdown(msg['content'])
|
90 |
|
91 |
-
#
|
92 |
col1, col2 = st.columns([6, 1])
|
93 |
with col1:
|
94 |
user_input = st.text_input("Enter customer message:", key="customer_input")
|
@@ -127,11 +132,10 @@ with col2:
|
|
127 |
session["agent_reply"] = ""
|
128 |
st.rerun()
|
129 |
|
130 |
-
#
|
131 |
if session["support_required"]:
|
132 |
st.markdown(f"### {session['support_required']}")
|
133 |
|
134 |
-
# Agent Reply
|
135 |
st.subheader("Agent Response Console")
|
136 |
session["agent_reply"] = st.text_area("Compose your reply:", value=session["agent_reply"], key="agent_reply_box")
|
137 |
if st.button("Send Reply"):
|
@@ -142,7 +146,7 @@ if st.button("Send Reply"):
|
|
142 |
session["support_required"] = ""
|
143 |
st.rerun()
|
144 |
|
145 |
-
#
|
146 |
if session["system_result"] is not None:
|
147 |
st.markdown("#### Customer Status")
|
148 |
st.markdown(f"- **Emotion:** {session['system_result']['emotion'].capitalize()}")
|
@@ -153,7 +157,7 @@ if session["system_result"] is not None:
|
|
153 |
st.markdown(f"**• {intent.capitalize()}**")
|
154 |
st.code(suggestion)
|
155 |
|
156 |
-
# End
|
157 |
if st.button("End Conversation"):
|
158 |
session["chat"] = []
|
159 |
session["system_result"] = None
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Load pipelines
|
5 |
emotion_classifier = pipeline(
|
6 |
"text-classification",
|
7 |
model="j-hartmann/emotion-english-distilroberta-base",
|
8 |
return_all_scores=True
|
9 |
)
|
10 |
intent_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
11 |
+
text_generator = pipeline("text2text-generation", model="google/flan-t5-large")
|
12 |
|
13 |
candidate_tasks = [
|
14 |
"change mobile plan", "top up balance", "report service outage",
|
|
|
48 |
def generate_response(intent, human=True):
|
49 |
if human:
|
50 |
prompt = (
|
51 |
+
f"You are a professional telecom support agent. The customer intends to '{intent}'. "
|
52 |
+
"Please generate a structured 3-part response:\n\n"
|
53 |
+
"1. Start with a short, polite greeting.\n"
|
54 |
+
"2. Mention customer is currently on a fictional plan (e.g., Plan X, ¥68/month), then recommend a better plan (e.g., Plan Y, ¥88/month, 20GB).\n"
|
55 |
+
"3. Ask if they'd like to proceed with the switch or need more details."
|
56 |
)
|
57 |
else:
|
58 |
prompt = (
|
59 |
+
f"The user intends to '{intent}' in a telecom support context. "
|
60 |
+
"Generate a complete structured reply with three parts:\n\n"
|
61 |
+
"1. Greet the user.\n"
|
62 |
+
"2. Provide a fictional current plan and recommend an upgrade.\n"
|
63 |
+
"3. End with a follow-up question to confirm.\n\n"
|
64 |
+
"Do not ask what plan the user has — assume and invent fictional details clearly."
|
65 |
)
|
66 |
+
result = text_generator(prompt, max_new_tokens=150, do_sample=False)
|
67 |
return result[0]['generated_text'].strip()
|
68 |
|
69 |
# Streamlit UI
|
|
|
86 |
session = st.session_state.chat_sessions[selected_customer]
|
87 |
st.title("Smart Customer Support Assistant (for Agents Only)")
|
88 |
|
89 |
+
# Show conversation
|
90 |
st.markdown("### Conversation")
|
91 |
for msg in session["chat"]:
|
92 |
avatar = "👤" if msg['role'] == 'user' else ("🤖" if msg.get("auto") else "👨💼")
|
93 |
with st.chat_message(msg['role'], avatar=avatar):
|
94 |
st.markdown(msg['content'])
|
95 |
|
96 |
+
# Input + Analyze
|
97 |
col1, col2 = st.columns([6, 1])
|
98 |
with col1:
|
99 |
user_input = st.text_input("Enter customer message:", key="customer_input")
|
|
|
132 |
session["agent_reply"] = ""
|
133 |
st.rerun()
|
134 |
|
135 |
+
# Priority & Agent reply
|
136 |
if session["support_required"]:
|
137 |
st.markdown(f"### {session['support_required']}")
|
138 |
|
|
|
139 |
st.subheader("Agent Response Console")
|
140 |
session["agent_reply"] = st.text_area("Compose your reply:", value=session["agent_reply"], key="agent_reply_box")
|
141 |
if st.button("Send Reply"):
|
|
|
146 |
session["support_required"] = ""
|
147 |
st.rerun()
|
148 |
|
149 |
+
# Detected needs
|
150 |
if session["system_result"] is not None:
|
151 |
st.markdown("#### Customer Status")
|
152 |
st.markdown(f"- **Emotion:** {session['system_result']['emotion'].capitalize()}")
|
|
|
157 |
st.markdown(f"**• {intent.capitalize()}**")
|
158 |
st.code(suggestion)
|
159 |
|
160 |
+
# End conversation
|
161 |
if st.button("End Conversation"):
|
162 |
session["chat"] = []
|
163 |
session["system_result"] = None
|