Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,14 +5,10 @@ from huggingface_hub import login
|
|
| 5 |
|
| 6 |
login(os.environ["HF_TOKEN"])
|
| 7 |
|
| 8 |
-
|
| 9 |
-
# Load models
|
| 10 |
emotion_classifier = pipeline("text-classification", model="shengqizhao0124/emotion_trainer", return_all_scores=True)
|
| 11 |
intent_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 12 |
text_generator = pipeline("text2text-generation", model="declare-lab/flan-alpaca-base")
|
| 13 |
|
| 14 |
-
|
| 15 |
-
# Intent categories
|
| 16 |
candidate_tasks = [
|
| 17 |
"change mobile plan", "top up balance", "report service outage",
|
| 18 |
"ask for billing support", "reactivate service", "cancel subscription",
|
|
@@ -48,7 +44,6 @@ def get_emotion_score(emotion):
|
|
| 48 |
else:
|
| 49 |
return 0.2
|
| 50 |
|
| 51 |
-
# β
Simplified fixed-format auto-reply
|
| 52 |
def generate_response(intent, human=True):
|
| 53 |
if human:
|
| 54 |
prompt = (
|
|
@@ -60,7 +55,6 @@ def generate_response(intent, human=True):
|
|
| 60 |
else:
|
| 61 |
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!]"
|
| 62 |
|
| 63 |
-
# Streamlit App
|
| 64 |
st.set_page_config(page_title="Smart Customer Support Assistant", layout="wide")
|
| 65 |
st.sidebar.title("π Customer Selector")
|
| 66 |
|
|
@@ -80,25 +74,28 @@ if selected_customer not in st.session_state.chat_sessions:
|
|
| 80 |
session = st.session_state.chat_sessions[selected_customer]
|
| 81 |
st.title("Smart Customer Support Assistant (for Agents Only)")
|
| 82 |
|
| 83 |
-
#
|
| 84 |
st.markdown("### Conversation")
|
| 85 |
for msg in session["chat"]:
|
| 86 |
avatar = "π€" if msg['role'] == 'user' else ("π€" if msg.get("auto") else "π¨βπΌ")
|
| 87 |
with st.chat_message(msg['role'], avatar=avatar):
|
|
|
|
|
|
|
| 88 |
st.markdown(msg['content'])
|
| 89 |
|
| 90 |
-
#
|
| 91 |
col1, col2 = st.columns([6, 1])
|
| 92 |
with col1:
|
| 93 |
user_input = st.text_input("Enter customer message:", key="customer_input")
|
| 94 |
with col2:
|
| 95 |
if st.button("Analyze"):
|
| 96 |
if user_input.strip():
|
| 97 |
-
session["chat"].append({"role": "user", "content": user_input})
|
| 98 |
emotion_result = emotion_classifier(user_input)
|
| 99 |
emotion_label = get_emotion_label(emotion_result, user_input)
|
| 100 |
emotion_score = get_emotion_score(emotion_label)
|
| 101 |
|
|
|
|
|
|
|
| 102 |
intent_result = intent_classifier(user_input, candidate_tasks)
|
| 103 |
top_intents = [label for label, score in zip(intent_result['labels'], intent_result['scores']) if score > 0.15][:3]
|
| 104 |
|
|
@@ -126,7 +123,7 @@ with col2:
|
|
| 126 |
session["agent_reply"] = ""
|
| 127 |
st.rerun()
|
| 128 |
|
| 129 |
-
#
|
| 130 |
if session["support_required"]:
|
| 131 |
st.markdown(f"### {session['support_required']}")
|
| 132 |
|
|
@@ -140,7 +137,7 @@ if st.button("Send Reply"):
|
|
| 140 |
session["support_required"] = ""
|
| 141 |
st.rerun()
|
| 142 |
|
| 143 |
-
#
|
| 144 |
if session["system_result"] is not None:
|
| 145 |
st.markdown("#### Customer Status")
|
| 146 |
st.markdown(f"- **Emotion:** {session['system_result']['emotion'].capitalize()}")
|
|
@@ -159,4 +156,3 @@ if st.button("End Conversation"):
|
|
| 159 |
session["user_input"] = ""
|
| 160 |
st.success("Conversation ended and cleared.")
|
| 161 |
st.rerun()
|
| 162 |
-
|
|
|
|
| 5 |
|
| 6 |
login(os.environ["HF_TOKEN"])
|
| 7 |
|
|
|
|
|
|
|
| 8 |
emotion_classifier = pipeline("text-classification", model="shengqizhao0124/emotion_trainer", return_all_scores=True)
|
| 9 |
intent_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 10 |
text_generator = pipeline("text2text-generation", model="declare-lab/flan-alpaca-base")
|
| 11 |
|
|
|
|
|
|
|
| 12 |
candidate_tasks = [
|
| 13 |
"change mobile plan", "top up balance", "report service outage",
|
| 14 |
"ask for billing support", "reactivate service", "cancel subscription",
|
|
|
|
| 44 |
else:
|
| 45 |
return 0.2
|
| 46 |
|
|
|
|
| 47 |
def generate_response(intent, human=True):
|
| 48 |
if human:
|
| 49 |
prompt = (
|
|
|
|
| 55 |
else:
|
| 56 |
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!]"
|
| 57 |
|
|
|
|
| 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 UI
|
| 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 |
+
if msg["role"] == "user" and "emotion" in msg:
|
| 83 |
+
st.markdown(f"<div style='text-align:right;font-size:0.9em;color:gray;'>Emotion: {msg['emotion'].capitalize()}</div>", unsafe_allow_html=True)
|
| 84 |
st.markdown(msg['content'])
|
| 85 |
|
| 86 |
+
# Input & Analyze
|
| 87 |
col1, col2 = st.columns([6, 1])
|
| 88 |
with col1:
|
| 89 |
user_input = st.text_input("Enter customer message:", key="customer_input")
|
| 90 |
with col2:
|
| 91 |
if st.button("Analyze"):
|
| 92 |
if user_input.strip():
|
|
|
|
| 93 |
emotion_result = emotion_classifier(user_input)
|
| 94 |
emotion_label = get_emotion_label(emotion_result, user_input)
|
| 95 |
emotion_score = get_emotion_score(emotion_label)
|
| 96 |
|
| 97 |
+
session["chat"].append({"role": "user", "content": user_input, "emotion": emotion_label})
|
| 98 |
+
|
| 99 |
intent_result = intent_classifier(user_input, candidate_tasks)
|
| 100 |
top_intents = [label for label, score in zip(intent_result['labels'], intent_result['scores']) if score > 0.15][:3]
|
| 101 |
|
|
|
|
| 123 |
session["agent_reply"] = ""
|
| 124 |
st.rerun()
|
| 125 |
|
| 126 |
+
# Agent panel
|
| 127 |
if session["support_required"]:
|
| 128 |
st.markdown(f"### {session['support_required']}")
|
| 129 |
|
|
|
|
| 137 |
session["support_required"] = ""
|
| 138 |
st.rerun()
|
| 139 |
|
| 140 |
+
# Show customer analysis
|
| 141 |
if session["system_result"] is not None:
|
| 142 |
st.markdown("#### Customer Status")
|
| 143 |
st.markdown(f"- **Emotion:** {session['system_result']['emotion'].capitalize()}")
|
|
|
|
| 156 |
session["user_input"] = ""
|
| 157 |
st.success("Conversation ended and cleared.")
|
| 158 |
st.rerun()
|
|
|