Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
import re | |
# Load models | |
emotion_classifier = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion", top_k=1) | |
intent_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli") | |
text_generator = pipeline("text2text-generation", model="declare-lab/flan-alpaca-base", max_new_tokens=200) | |
# Candidate intents | |
candidate_tasks = [ | |
"change mobile plan", | |
"top up balance", | |
"report service outage", | |
"ask for billing support", | |
"reactivate service", | |
"cancel subscription", | |
"check account status", | |
"upgrade device" | |
] | |
# Emotion scoring | |
urgent_emotions = {"anger", "annoyance", "disgust", "frustration", "sadness"} | |
moderate_emotions = {"confusion", "concern", "nervousness", "fear"} | |
def get_emotion_score(emotion): | |
if emotion in urgent_emotions: | |
return 1.0 | |
elif emotion in moderate_emotions: | |
return 0.6 | |
else: | |
return 0.2 | |
def get_content_score(text, top_intents): | |
score = 0.0 | |
trigger_words = ["out of service", "urgent", "not working", "stopped", "can't", "immediately"] | |
if any(kw in text.lower() for kw in trigger_words): | |
score += 0.3 | |
if any(label in ["top up balance", "reactivate service", "report service outage"] for label in top_intents): | |
score += 0.4 | |
if any(label in ["change mobile plan", "ask for billing support"] for label in top_intents): | |
score += 0.2 | |
return min(score + 0.1, 1.0) | |
# Updated reply generator with intent-specific follow-up | |
def generate_reply(input_text, intent): | |
# Defined intent-specific closing prompts | |
intent_closings = { | |
"top up balance": "Would you like to see recharge options and prices?", | |
"reactivate service": "Shall I help you reactivate your number now?", | |
"report service outage": "Would you like me to file a service report for you?", | |
"change mobile plan": "Need me to compare available plans for you?", | |
"ask for billing support": "Would you like me to check your latest bill details?", | |
"cancel subscription": "Should I guide you through the cancellation process?", | |
"check account status": "Would you like an overview of your account status?", | |
"upgrade device": "Would you like me to show available upgrade options?" | |
} | |
closing = intent_closings.get(intent.lower(), "Is there anything else I can help you with?") | |
opening = "Thanks for reaching out. I understand your concern." | |
action = f"Here's how we can assist with '{intent.lower()}'." | |
reply = f"{opening} {action} {closing}" | |
return reply | |
# UI | |
st.set_page_config(page_title="Customer Support Assistant", layout="centered") | |
st.title("π Smart Customer Support Assistant (for Agents Only)") | |
user_input = st.text_area("Enter customer's message or complaint:", height=150) | |
if st.button("Analyze Message"): | |
if user_input.strip() == "": | |
st.warning("Please enter a customer message.") | |
else: | |
with st.spinner("Processing..."): | |
# Emotion detection | |
emotion_result = emotion_classifier(user_input) | |
emotion_data = emotion_result[0][0] if isinstance(emotion_result[0], list) else emotion_result[0] | |
emotion_label = emotion_data['label'] | |
emotion_score = get_emotion_score(emotion_label) | |
# Intent detection | |
intent_result = intent_classifier(user_input, candidate_tasks) | |
top_intents = [label for label, score in zip(intent_result['labels'], intent_result['scores']) if score > 0.15][:3] | |
# Content score | |
content_score = get_content_score(user_input, top_intents) | |
# Final decision score | |
final_score = (0.5 * emotion_score) + (0.5 * content_score) | |
st.subheader("π§Ύ System Summary") | |
if final_score < 0.5: | |
st.markdown("### π’ This message was handled automatically.") | |
if top_intents: | |
auto_intent = top_intents[0] | |
auto_reply = generate_reply(user_input, auto_intent) | |
st.markdown("#### π€ Auto-Response Sent to User:") | |
st.success(auto_reply) | |
else: | |
st.info("No clear intent detected. A general auto-reply was used.") | |
else: | |
st.markdown("### π΄ Human Support Required") | |
# Customer Profile Summary | |
st.markdown("#### π€ Customer Status:") | |
st.write(f"- **Emotion detected**: {emotion_label.capitalize()}") | |
st.write(f"- **Tone**: {'Urgent' if emotion_score > 0.8 else 'Concerned' if emotion_score > 0.5 else 'Calm'}") | |
if top_intents: | |
st.markdown("#### π§© Detected Customer Needs:") | |
for intent in top_intents: | |
reply = generate_reply(user_input, intent) | |
st.markdown(f"**β’ {intent.capitalize()}**") | |
st.write(reply) | |
else: | |
st.warning("No clear intent detected. Manual review recommended.") | |