Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,13 +22,33 @@ candidate_tasks = [
|
|
| 22 |
"upgrade device"
|
| 23 |
]
|
| 24 |
|
| 25 |
-
# Emotion
|
| 26 |
urgent_emotions = {"anger", "frustration", "anxiety", "urgency", "afraid", "annoyed"}
|
| 27 |
-
moderate_emotions = {"confused", "sad", "tired", "concerned"}
|
| 28 |
|
| 29 |
-
|
|
|
|
| 30 |
sorted_emotions = sorted(emotion_result[0], key=lambda x: x['score'], reverse=True)
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
def get_emotion_score(emotion):
|
| 34 |
if emotion.lower() in urgent_emotions:
|
|
@@ -50,7 +70,6 @@ def get_content_score(text, top_intents):
|
|
| 50 |
return min(score + 0.1, 1.0)
|
| 51 |
|
| 52 |
def generate_reply(input_text, intent):
|
| 53 |
-
# Intent-specific suggestion sentences
|
| 54 |
intent_solutions = {
|
| 55 |
"top up balance": "Your balance is ¥12. Current promo: recharge ¥100 get ¥5 bonus.",
|
| 56 |
"reactivate service": "Your service is suspended due to unpaid ¥38. Recharge to restore within 30 mins.",
|
|
@@ -61,8 +80,6 @@ def generate_reply(input_text, intent):
|
|
| 61 |
"upgrade device": "You're eligible for upgrade. New iPhone plan starts at ¥399/month.",
|
| 62 |
"report service outage": "We're detecting signal issues in your area (ZIP: XXX). Engineers notified."
|
| 63 |
}
|
| 64 |
-
|
| 65 |
-
# Intent-specific closing question
|
| 66 |
intent_closings = {
|
| 67 |
"top up balance": "Would you like to proceed with a recharge now?",
|
| 68 |
"reactivate service": "Shall I help you restart your service now?",
|
|
@@ -77,10 +94,9 @@ def generate_reply(input_text, intent):
|
|
| 77 |
solution = intent_solutions.get(intent.lower(), "Here's how we can assist you with this issue.")
|
| 78 |
closing = intent_closings.get(intent.lower(), "Is there anything else I can help with?")
|
| 79 |
opening = "Thank you for contacting us. I understand your concern."
|
| 80 |
-
|
| 81 |
return f"{opening} {solution} {closing}"
|
| 82 |
|
| 83 |
-
#
|
| 84 |
st.set_page_config(page_title="Customer Support Assistant", layout="centered")
|
| 85 |
st.title("📞 Smart Customer Support Assistant (for Agents Only)")
|
| 86 |
|
|
@@ -92,9 +108,9 @@ if st.button("Analyze Message"):
|
|
| 92 |
else:
|
| 93 |
with st.spinner("Processing..."):
|
| 94 |
|
| 95 |
-
# Emotion detection (
|
| 96 |
emotion_result = emotion_classifier(user_input)
|
| 97 |
-
emotion_label = get_emotion_label(emotion_result)
|
| 98 |
emotion_score = get_emotion_score(emotion_label)
|
| 99 |
|
| 100 |
# Intent detection
|
|
|
|
| 22 |
"upgrade device"
|
| 23 |
]
|
| 24 |
|
| 25 |
+
# Emotion scoring
|
| 26 |
urgent_emotions = {"anger", "frustration", "anxiety", "urgency", "afraid", "annoyed"}
|
| 27 |
+
moderate_emotions = {"confused", "sad", "tired", "concerned", "sadness"}
|
| 28 |
|
| 29 |
+
# ⬇️ 情绪判断(融合模型输出 + 语言信号)
|
| 30 |
+
def get_emotion_label(emotion_result, text):
|
| 31 |
sorted_emotions = sorted(emotion_result[0], key=lambda x: x['score'], reverse=True)
|
| 32 |
+
top_emotion = sorted_emotions[0]['label']
|
| 33 |
+
return refine_emotion_label(text, top_emotion)
|
| 34 |
+
|
| 35 |
+
def refine_emotion_label(text, model_emotion):
|
| 36 |
+
text_lower = text.lower()
|
| 37 |
+
urgent_keywords = ["fix", "now", "immediately", "urgent", "can't", "need", "asap"]
|
| 38 |
+
exclamations = text.count("!")
|
| 39 |
+
upper_words = sum(1 for word in text.split() if word.isupper())
|
| 40 |
+
|
| 41 |
+
signal_score = 0
|
| 42 |
+
if any(word in text_lower for word in urgent_keywords):
|
| 43 |
+
signal_score += 1
|
| 44 |
+
if exclamations >= 2:
|
| 45 |
+
signal_score += 1
|
| 46 |
+
if upper_words >= 1:
|
| 47 |
+
signal_score += 1
|
| 48 |
+
|
| 49 |
+
if model_emotion.lower() in {"joy", "neutral", "sadness"} and signal_score >= 2:
|
| 50 |
+
return "urgency"
|
| 51 |
+
return model_emotion
|
| 52 |
|
| 53 |
def get_emotion_score(emotion):
|
| 54 |
if emotion.lower() in urgent_emotions:
|
|
|
|
| 70 |
return min(score + 0.1, 1.0)
|
| 71 |
|
| 72 |
def generate_reply(input_text, intent):
|
|
|
|
| 73 |
intent_solutions = {
|
| 74 |
"top up balance": "Your balance is ¥12. Current promo: recharge ¥100 get ¥5 bonus.",
|
| 75 |
"reactivate service": "Your service is suspended due to unpaid ¥38. Recharge to restore within 30 mins.",
|
|
|
|
| 80 |
"upgrade device": "You're eligible for upgrade. New iPhone plan starts at ¥399/month.",
|
| 81 |
"report service outage": "We're detecting signal issues in your area (ZIP: XXX). Engineers notified."
|
| 82 |
}
|
|
|
|
|
|
|
| 83 |
intent_closings = {
|
| 84 |
"top up balance": "Would you like to proceed with a recharge now?",
|
| 85 |
"reactivate service": "Shall I help you restart your service now?",
|
|
|
|
| 94 |
solution = intent_solutions.get(intent.lower(), "Here's how we can assist you with this issue.")
|
| 95 |
closing = intent_closings.get(intent.lower(), "Is there anything else I can help with?")
|
| 96 |
opening = "Thank you for contacting us. I understand your concern."
|
|
|
|
| 97 |
return f"{opening} {solution} {closing}"
|
| 98 |
|
| 99 |
+
# UI
|
| 100 |
st.set_page_config(page_title="Customer Support Assistant", layout="centered")
|
| 101 |
st.title("📞 Smart Customer Support Assistant (for Agents Only)")
|
| 102 |
|
|
|
|
| 108 |
else:
|
| 109 |
with st.spinner("Processing..."):
|
| 110 |
|
| 111 |
+
# Emotion detection (model + rule)
|
| 112 |
emotion_result = emotion_classifier(user_input)
|
| 113 |
+
emotion_label = get_emotion_label(emotion_result, user_input)
|
| 114 |
emotion_score = get_emotion_score(emotion_label)
|
| 115 |
|
| 116 |
# Intent detection
|