Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,12 @@
|
|
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="google/flan-t5-
|
12 |
-
|
13 |
|
|
|
14 |
candidate_tasks = [
|
15 |
"change mobile plan", "top up balance", "report service outage",
|
16 |
"ask for billing support", "reactivate service", "cancel subscription",
|
@@ -46,28 +42,19 @@ def get_emotion_score(emotion):
|
|
46 |
else:
|
47 |
return 0.2
|
48 |
|
|
|
49 |
def generate_response(intent, human=True):
|
50 |
if human:
|
51 |
prompt = (
|
52 |
-
f"You are a
|
53 |
-
"
|
54 |
-
"1. Start with a short, polite greeting.\n"
|
55 |
-
"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"
|
56 |
-
"3. Ask if they'd like to proceed with the switch or need more details."
|
57 |
)
|
|
|
|
|
58 |
else:
|
59 |
-
|
60 |
-
f"The user intends to '{intent}' in a telecom support context. "
|
61 |
-
"Generate a complete structured reply with three parts:\n\n"
|
62 |
-
"1. Greet the user.\n"
|
63 |
-
"2. Provide a fictional current plan and recommend an upgrade.\n"
|
64 |
-
"3. End with a follow-up question to confirm.\n\n"
|
65 |
-
"Do not ask what plan the user has β assume and invent fictional details clearly."
|
66 |
-
)
|
67 |
-
result = text_generator(prompt, max_new_tokens=150, do_sample=False)
|
68 |
-
return result[0]['generated_text'].strip()
|
69 |
|
70 |
-
# Streamlit
|
71 |
st.set_page_config(page_title="Smart Customer Support Assistant", layout="wide")
|
72 |
st.sidebar.title("π Customer Selector")
|
73 |
|
@@ -87,14 +74,14 @@ if selected_customer not in st.session_state.chat_sessions:
|
|
87 |
session = st.session_state.chat_sessions[selected_customer]
|
88 |
st.title("Smart Customer Support Assistant (for Agents Only)")
|
89 |
|
90 |
-
#
|
91 |
st.markdown("### Conversation")
|
92 |
for msg in session["chat"]:
|
93 |
avatar = "π€" if msg['role'] == 'user' else ("π€" if msg.get("auto") else "π¨βπΌ")
|
94 |
with st.chat_message(msg['role'], avatar=avatar):
|
95 |
st.markdown(msg['content'])
|
96 |
|
97 |
-
#
|
98 |
col1, col2 = st.columns([6, 1])
|
99 |
with col1:
|
100 |
user_input = st.text_input("Enter customer message:", key="customer_input")
|
@@ -133,7 +120,7 @@ with col2:
|
|
133 |
session["agent_reply"] = ""
|
134 |
st.rerun()
|
135 |
|
136 |
-
#
|
137 |
if session["support_required"]:
|
138 |
st.markdown(f"### {session['support_required']}")
|
139 |
|
@@ -147,7 +134,7 @@ if st.button("Send Reply"):
|
|
147 |
session["support_required"] = ""
|
148 |
st.rerun()
|
149 |
|
150 |
-
#
|
151 |
if session["system_result"] is not None:
|
152 |
st.markdown("#### Customer Status")
|
153 |
st.markdown(f"- **Emotion:** {session['system_result']['emotion'].capitalize()}")
|
@@ -158,7 +145,6 @@ if session["system_result"] is not None:
|
|
158 |
st.markdown(f"**β’ {intent.capitalize()}**")
|
159 |
st.code(suggestion)
|
160 |
|
161 |
-
# End conversation
|
162 |
if st.button("End Conversation"):
|
163 |
session["chat"] = []
|
164 |
session["system_result"] = None
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Load models
|
5 |
+
emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
|
|
|
|
|
|
|
|
|
6 |
intent_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
7 |
+
text_generator = pipeline("text2text-generation", model="google/flan-t5-large")
|
|
|
8 |
|
9 |
+
# Intent categories
|
10 |
candidate_tasks = [
|
11 |
"change mobile plan", "top up balance", "report service outage",
|
12 |
"ask for billing support", "reactivate service", "cancel subscription",
|
|
|
42 |
else:
|
43 |
return 0.2
|
44 |
|
45 |
+
# β
Simplified fixed-format auto-reply
|
46 |
def generate_response(intent, human=True):
|
47 |
if human:
|
48 |
prompt = (
|
49 |
+
f"You are a telecom agent. The customer intends to '{intent}'. "
|
50 |
+
"Give a 3-part polite reply: 1) Greeting, 2) Mention current plan (fictional) and suggest better one, 3) Ask if want to proceed."
|
|
|
|
|
|
|
51 |
)
|
52 |
+
result = text_generator(prompt, max_new_tokens=150, do_sample=False)
|
53 |
+
return result[0]['generated_text'].strip()
|
54 |
else:
|
55 |
+
return f"[Below is a link to the service you needοΌ{intent} β https://support.example.com/{intent.replace(' ', '_')}]\\n[If your problem still can not be solved, welcome to continue to consult, we will continue to serve you!]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
# Streamlit App
|
58 |
st.set_page_config(page_title="Smart Customer Support Assistant", layout="wide")
|
59 |
st.sidebar.title("π Customer Selector")
|
60 |
|
|
|
74 |
session = st.session_state.chat_sessions[selected_customer]
|
75 |
st.title("Smart Customer Support Assistant (for Agents Only)")
|
76 |
|
77 |
+
# π¬ Conversation
|
78 |
st.markdown("### Conversation")
|
79 |
for msg in session["chat"]:
|
80 |
avatar = "π€" if msg['role'] == 'user' else ("π€" if msg.get("auto") else "π¨βπΌ")
|
81 |
with st.chat_message(msg['role'], avatar=avatar):
|
82 |
st.markdown(msg['content'])
|
83 |
|
84 |
+
# π Analyze input
|
85 |
col1, col2 = st.columns([6, 1])
|
86 |
with col1:
|
87 |
user_input = st.text_input("Enter customer message:", key="customer_input")
|
|
|
120 |
session["agent_reply"] = ""
|
121 |
st.rerun()
|
122 |
|
123 |
+
# π§ Agent reply
|
124 |
if session["support_required"]:
|
125 |
st.markdown(f"### {session['support_required']}")
|
126 |
|
|
|
134 |
session["support_required"] = ""
|
135 |
st.rerun()
|
136 |
|
137 |
+
# π If human required
|
138 |
if session["system_result"] is not None:
|
139 |
st.markdown("#### Customer Status")
|
140 |
st.markdown(f"- **Emotion:** {session['system_result']['emotion'].capitalize()}")
|
|
|
145 |
st.markdown(f"**β’ {intent.capitalize()}**")
|
146 |
st.code(suggestion)
|
147 |
|
|
|
148 |
if st.button("End Conversation"):
|
149 |
session["chat"] = []
|
150 |
session["system_result"] = None
|