Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import re | |
| import random | |
| # Page configuration | |
| st.set_page_config( | |
| page_title="Customer Support Assistant", | |
| page_icon="💬", | |
| layout="centered" | |
| ) | |
| # Customer support context | |
| SYSTEM_INFO = """ | |
| This is a customer support assistant that helps with common inquiries about products, services, | |
| policies, and general information. It provides accurate and helpful responses to assist customers. | |
| """ | |
| # Sample company information and policies | |
| COMPANY_INFO = { | |
| "return_policy": "Our return policy allows returns within 30 days of purchase with original receipt. Items must be unused and in original packaging.", | |
| "tracking_order": "You can track your order by logging into your account or using the tracking number provided in your confirmation email.", | |
| "account_issues": "For account login issues, try resetting your password. If problems persist, please provide your email address so we can investigate.", | |
| "subscription": "Yes, you can cancel your subscription at any time through your account settings. There are no cancellation fees.", | |
| "support_hours": "Our customer support team is available 24/7 via this chat. For phone support, our hours are 9 AM - 8 PM Monday through Friday, and 10 AM - 6 PM on weekends.", | |
| "contact": "For complex issues, you can reach our human support team at support@example.com or call 1-800-123-4567.", | |
| "shipping": "Standard shipping takes 3-5 business days. Express shipping takes 1-2 business days. International shipping may take 7-14 business days.", | |
| "payment": "We accept all major credit cards, PayPal, and Apple Pay. Payment information is securely processed and encrypted.", | |
| "refund": "Refunds are processed within 5-7 business days of receiving your returned item. The refund will be issued to the original payment method.", | |
| "warranty": "Our products come with a standard 1-year warranty that covers manufacturing defects. Extended warranties are available for purchase." | |
| } | |
| # Response templates with variations for more natural conversation | |
| RESPONSE_TEMPLATES = { | |
| "greeting": [ | |
| "Hello! Welcome to our customer support. How can I assist you today?", | |
| "Hi there! I'm here to help with any questions you might have. What can I do for you?", | |
| "Welcome! How may I assist you with our products or services today?" | |
| ], | |
| "thanks": [ | |
| "You're welcome! Is there anything else I can help you with?", | |
| "Happy to help! Do you have any other questions I can assist with?", | |
| "My pleasure! Is there something else you'd like to know?" | |
| ], | |
| "goodbye": [ | |
| "Thank you for contacting our support. Have a great day!", | |
| "I appreciate your time today. If you need anything else, don't hesitate to reach out!", | |
| "Thanks for chatting with us. Feel free to return if you have any other questions!" | |
| ], | |
| "unclear": [ | |
| "I'm not sure I understand your question. Could you please provide more details?", | |
| "I'd like to help, but I need a bit more information. Could you clarify what you're asking about?", | |
| "I'm sorry, but I don't have enough information to answer that. Could you be more specific?" | |
| ], | |
| "human": [ | |
| f"I understand you'd like to speak with a human representative. {COMPANY_INFO['contact']}", | |
| f"For more complex issues, our human support team is available. {COMPANY_INFO['contact']}", | |
| f"I'd be happy to connect you with one of our human agents. {COMPANY_INFO['contact']}" | |
| ] | |
| } | |
| # Patterns for matching different types of queries | |
| PATTERNS = { | |
| "greeting": r"\b(hi|hello|hey|good morning|good afternoon|good evening)\b", | |
| "thanks": r"\b(thanks|thank you|appreciate it)\b", | |
| "goodbye": r"\b(bye|goodbye|see you|farewell)\b", | |
| "return_policy": r"\b(return|return policy|send back|give back|exchange|bring back|refund policy)\b", | |
| "tracking": r"\b(track|track order|tracking|shipping status|where is|delivery status|package|when.*arrive|where.*order)\b", | |
| "account": r"\b(account|log in|login|sign in|password|forgot password|reset password|username|can\'?t log in|trouble logging in)\b", | |
| "subscription": r"\b(subscription|cancel|cancellation|unsubscribe|stop.*subscription|end.*subscription|pause)\b", | |
| "hours": r"\b(hours|time|availability|when are you|schedule|open|closed|available|support hours|customer service hours)\b", | |
| "human": r"\b(human|person|agent|representative|speak to someone|talk to someone|real person|live agent|customer service rep)\b", | |
| "shipping": r"\b(shipping|delivery|ship|deliver|how long|how soon|when will it arrive|shipping time|delivery time|express shipping)\b", | |
| "payment": r"\b(payment|pay|credit card|debit card|paypal|apple pay|billing|payment method|transaction|purchase|buying)\b", | |
| "refund": r"\b(refund|money back|reimburse|reimbursement|get.*money back|return.*money|credit back|refund process)\b", | |
| "warranty": r"\b(warranty|guarantee|broken|defective|repair|fix|not working|doesn\'?t work|stopped working|damaged|replacement)\b" | |
| } | |
| def get_response(user_input): | |
| """Generate a response based on the user's input""" | |
| if not user_input or user_input.strip() == "": | |
| return "Hello! How can I help you today? Feel free to ask about our return policy, shipping, account help, or any other questions." | |
| # Convert input to lowercase for easier matching | |
| input_lower = user_input.lower() | |
| # Check for greetings, thanks, or goodbyes first | |
| if re.search(PATTERNS["greeting"], input_lower): | |
| return random.choice(RESPONSE_TEMPLATES["greeting"]) | |
| if re.search(PATTERNS["thanks"], input_lower): | |
| return random.choice(RESPONSE_TEMPLATES["thanks"]) | |
| if re.search(PATTERNS["goodbye"], input_lower): | |
| return random.choice(RESPONSE_TEMPLATES["goodbye"]) | |
| # Store all matching responses and pick the most relevant | |
| matched_responses = [] | |
| # Check for specific product/policy queries | |
| if re.search(PATTERNS["return_policy"], input_lower): | |
| matched_responses.append(f"Here's our return policy: {COMPANY_INFO['return_policy']}") | |
| if re.search(PATTERNS["tracking"], input_lower): | |
| matched_responses.append(f"To track your order: {COMPANY_INFO['tracking_order']}") | |
| if re.search(PATTERNS["account"], input_lower): | |
| matched_responses.append(f"For account-related issues: {COMPANY_INFO['account_issues']}") | |
| if re.search(PATTERNS["subscription"], input_lower): | |
| matched_responses.append(f"Regarding subscriptions: {COMPANY_INFO['subscription']}") | |
| if re.search(PATTERNS["hours"], input_lower): | |
| matched_responses.append(f"Our support hours are: {COMPANY_INFO['support_hours']}") | |
| if re.search(PATTERNS["shipping"], input_lower): | |
| matched_responses.append(f"Shipping information: {COMPANY_INFO['shipping']}") | |
| if re.search(PATTERNS["payment"], input_lower): | |
| matched_responses.append(f"Payment methods: {COMPANY_INFO['payment']}") | |
| if re.search(PATTERNS["refund"], input_lower): | |
| matched_responses.append(f"Our refund process: {COMPANY_INFO['refund']}") | |
| if re.search(PATTERNS["warranty"], input_lower): | |
| matched_responses.append(f"Warranty information: {COMPANY_INFO['warranty']}") | |
| if re.search(PATTERNS["human"], input_lower): | |
| matched_responses.append(random.choice(RESPONSE_TEMPLATES["human"])) | |
| # Return a matched response if any were found | |
| if matched_responses: | |
| return matched_responses[0] | |
| # Try broader keyword matching if no specific pattern matched | |
| for key, info in COMPANY_INFO.items(): | |
| keywords = key.replace("_", " ").split() | |
| for word in keywords: | |
| if word in input_lower and len(word) > 3: # Only match significant words | |
| return f"Regarding {key.replace('_', ' ')}: {info}" | |
| # Check if asking a general question | |
| if any(q in input_lower for q in ["what", "how", "when", "where", "why", "who", "can", "do", "is", "are"]): | |
| general_response = "Based on your question, you might be interested in one of these topics:\n" | |
| general_response += "• Return policy\n• Order tracking\n• Account issues\n• Subscriptions\n" | |
| general_response += "• Support hours\n• Shipping information\n• Payment methods\n• Refunds\n• Warranty" | |
| return general_response | |
| # If no specific pattern is matched, respond with a general answer or ask for clarification | |
| return random.choice(RESPONSE_TEMPLATES["unclear"]) | |
| # App title and intro | |
| st.title("Customer Support Assistant") | |
| st.markdown("Welcome to our customer support chat! How can I help you today?") | |
| # Initialize session state for chat history | |
| if "chat_history" not in st.session_state: | |
| st.session_state.chat_history = [] | |
| # Sidebar with information and options | |
| with st.sidebar: | |
| st.title("Customer Support") | |
| st.markdown("### About") | |
| st.info(SYSTEM_INFO) | |
| if st.button("Clear Conversation"): | |
| st.session_state.chat_history = [] | |
| st.rerun() | |
| # Company info quick links | |
| st.markdown("---") | |
| st.markdown("### Quick Information") | |
| if st.button("Return Policy"): | |
| st.info(COMPANY_INFO["return_policy"]) | |
| if st.button("Shipping Info"): | |
| st.info(COMPANY_INFO["shipping"]) | |
| if st.button("Support Hours"): | |
| st.info(COMPANY_INFO["support_hours"]) | |
| if st.button("Contact Information"): | |
| st.info(COMPANY_INFO["contact"]) | |
| # Display chat history | |
| for user_msg, bot_msg in st.session_state.chat_history: | |
| with st.chat_message("user"): | |
| st.write(user_msg) | |
| with st.chat_message("assistant", avatar="🧑💼"): | |
| st.write(bot_msg) | |
| # Input for new message | |
| user_input = st.chat_input("Type your message here...") | |
| if user_input: | |
| # Display user message | |
| with st.chat_message("user"): | |
| st.write(user_input) | |
| # Get bot response | |
| with st.chat_message("assistant", avatar="🧑💼"): | |
| with st.spinner("Thinking..."): | |
| bot_response = get_response(user_input) | |
| st.write(bot_response) | |
| # Add to chat history | |
| st.session_state.chat_history.append((user_input, bot_response)) |