import streamlit as st from transformers import pipeline # Configure Streamlit page st.set_page_config( page_title="Customer Service Ticket Analyzer", page_icon="🎫", layout="centered" ) # Initialize pipelines @st.cache_resource def load_models(): sentiment_analyzer = pipeline( "sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english" ) department_classifier = pipeline( "zero-shot-classification", model="devronn/Finetuned_bge" ) return sentiment_analyzer, department_classifier # Load models try: sentiment_analyzer, department_classifier = load_models() except Exception as e: st.error(f"Error loading models: {str(e)}") st.stop() # Define departments and their descriptions departments = { "Customer Support": "Handles customer inquiries and product issues.", "Technical Support": "Provides technical assistance and troubleshooting.", "Billing and Sales": "Handles payment inquiries, sales questions, and general inquiries.", "Product Feedback": "Collects feedback about products and services.", "Account Management": "Manages customer accounts and retention." } # Define potential responses based on keywords response_templates = { "billing": "For billing inquiries, please check your account or contact our billing department directly.", "technical": "For technical support, please provide detailed information about the issue.", "product": "For product inquiries, please specify the product name and your question.", "general": "Thank you for your inquiry! We will get back to you shortly.", "order": "For order-related questions, please provide your order number.", "refund": "For refund inquiries, please allow us to assist you with the process.", } # Title and description st.title("Customer Service Ticket Analyzer") st.markdown("Analyze customer tickets for sentiment and department routing") # Main input form customer_name = st.text_input("Customer Name") ticket_subject = st.text_input("Ticket Subject") ticket_content = st.text_area("Ticket Content", height=150) if st.button("Analyze Ticket") and ticket_content.strip(): try: with st.spinner('Analyzing...'): # Limit input length for faster processing limited_content = ticket_content[:500] # Limit to 500 characters # Sentiment Analysis sentiment = sentiment_analyzer(limited_content)[0] # Department Classification department_result = department_classifier( limited_content, candidate_labels=list(departments.keys()), multi_label=False ) # Keyword Extraction keyword_matches = [] for keyword in response_templates.keys(): if keyword in limited_content.lower(): keyword_matches.append(response_templates[keyword]) # Display results st.markdown("### Analysis Results") # Create three columns for results col1, col2, col3 = st.columns(3) with col1: st.markdown("#### Sentiment") sentiment_color = "green" if sentiment['label'] == "POSITIVE" else "red" st.markdown( f"

{sentiment['label']}

", unsafe_allow_html=True ) st.write(f"Confidence: {sentiment['score']:.2%}") with col2: st.markdown("#### Department") suggested_dept = department_result['labels'][0] st.write(f"**Suggested:** {suggested_dept}") st.write(f"Confidence: {department_result['scores'][0]:.2%}") with col3: st.markdown("#### Priority") priority = "HIGH" if sentiment['label'] == "NEGATIVE" and sentiment['score'] > 0.8 else "MEDIUM" priority_color = "red" if priority == "HIGH" else "orange" st.markdown( f"

{priority}

", unsafe_allow_html=True ) # Ticket Summary st.markdown("### Ticket Details") st.write(f"**Customer:** {customer_name}") st.write(f"**Subject:** {ticket_subject}") st.write(f"**Content:** {ticket_content}") # Recommended Responses st.markdown("### Recommended Responses") if keyword_matches: for response in keyword_matches: st.write(f"- {response}") else: st.write("No specific recommendations available.") except Exception as e: st.error(f"An error occurred during analysis: {str(e)}") # Sidebar information with st.sidebar: st.markdown("### About") st.write(""" This tool analyzes customer service tickets by: - Determining sentiment - Suggesting appropriate department - Setting priority level - Providing confidence scores """) st.markdown("### Departments") for dept, desc in departments.items(): st.write(f"**{dept}**") st.write(desc) st.write("---")