import streamlit as st from transformers import pipeline import os from huggingface_hub import login login(os.environ["HF_TOKEN"]) emotion_classifier = pipeline("text-classification", model="shengqizhao0124/emotion_trainer", return_all_scores=True) intent_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli") text_generator = pipeline("text2text-generation", model="declare-lab/flan-alpaca-base") candidate_tasks = [ "change mobile plan", "top up balance", "report service outage", "ask for billing support", "reactivate service", "cancel subscription", "check account status", "upgrade device" ] urgent_emotions = {"anger", "frustration", "anxiety", "urgency", "afraid", "annoyed"} moderate_emotions = {"confused", "sad", "tired", "concerned", "sadness"} def refine_emotion_label(text, model_emotion): text_lower = text.lower() urgent_keywords = ["fix", "now", "immediately", "urgent", "can't", "need", "asap"] exclamations = text.count("!") upper_words = sum(1 for w in text.split() if w.isupper()) signal_score = sum([ any(word in text_lower for word in urgent_keywords), exclamations >= 2, upper_words >= 1 ]) if model_emotion.lower() in {"joy", "neutral", "sadness"} and signal_score >= 2: return "urgency" return model_emotion def get_emotion_label(result, text): sorted_emotions = sorted(result[0], key=lambda x: x['score'], reverse=True) return refine_emotion_label(text, sorted_emotions[0]['label']) def get_emotion_score(emotion): if emotion.lower() in urgent_emotions: return 1.0 elif emotion.lower() in moderate_emotions: return 0.6 else: return 0.2 def generate_response(intent, human=True): if human: prompt = ( f"You are a telecom agent. The customer intends to '{intent}'. " "Give a 3-part polite reply: 1) Greeting, 2) Mention current plan (fictional) and suggest better one, 3) Ask if want to proceed." ) result = text_generator(prompt, max_new_tokens=150, do_sample=False) return result[0]['generated_text'].strip() else: 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!]" st.set_page_config(page_title="Smart Customer Support Assistant", layout="wide") st.sidebar.title("π Customer Selector") if "customers" not in st.session_state: st.session_state.customers = {"Customer A": [], "Customer B": [], "Customer C": []} if "chat_sessions" not in st.session_state: st.session_state.chat_sessions = {} selected_customer = st.sidebar.selectbox("Choose a customer:", list(st.session_state.customers.keys())) if selected_customer not in st.session_state.chat_sessions: st.session_state.chat_sessions[selected_customer] = { "chat": [], "system_result": None, "agent_reply": "", "support_required": "", "user_input": "" } session = st.session_state.chat_sessions[selected_customer] st.title("Smart Customer Support Assistant (for Agents Only)") # Conversation UI st.markdown("### Conversation") for msg in session["chat"]: avatar = "π€" if msg['role'] == 'user' else ("π€" if msg.get("auto") else "π¨βπΌ") with st.chat_message(msg['role'], avatar=avatar): if msg["role"] == "user" and "emotion" in msg: st.markdown(f"