Jayanth-MKV commited on
Commit
ba83806
·
1 Parent(s): b83877a
Files changed (2) hide show
  1. app.py +168 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spacy
2
+ import streamlit as st
3
+ import re
4
+ import random
5
+ import json
6
+
7
+ # Load the trained model
8
+ nlp = spacy.load("en_pipeline")
9
+
10
+ # Load FAQs
11
+ with open("faq.json") as f:
12
+ faqs = json.load(f)["faqs"]
13
+
14
+ start_index = random.randint(0, len(faqs) - 5)
15
+ end_index = start_index + 5
16
+
17
+ # Define intents and responses
18
+ intents_responses = {
19
+ "get_invoice": "Here is a link to download your invoice: [link_to_invoice].",
20
+ "payment_issue": "If you're facing payment issues, please check your payment method and try again.",
21
+ "check_invoice": "Your invoice details are available here: [link_to_invoice_details].",
22
+ "contact_customer_service": "You can reach our customer service at 1-800-123-456 or email [email protected].",
23
+ "complain": "We have logged your complaint. Our team will review it and get back to you shortly.",
24
+ "review": "Thank you for your review. We value your feedback and will use it to improve our services.",
25
+ "cancel_order": "Your order has been successfully canceled.",
26
+ "check_cancellation_fee": "There is no cancellation fee if you cancel within 24 hours of placing the order.",
27
+ "check_payment_methods": "We accept Visa, MasterCard, American Express, and PayPal.",
28
+ "set_up_shipping_address": "Your shipping address has been updated successfully.",
29
+ "track_order": "Your order is currently in transit and will be delivered by the estimated date.",
30
+ "place_order": "Your order has been placed successfully. You will receive a confirmation email shortly.",
31
+ "check_refund_policy": "Our refund policy states that refunds can be requested within 30 days of purchase.",
32
+ "get_refund": "Your refund has been processed and will be credited to your account within 5-7 business days.",
33
+ "registration_problems": "If you're having trouble registering, please try clearing your browser cache and cookies.",
34
+ "recover_password": "To recover your password, please click the 'Forgot Password' link on the login page.",
35
+ "track_refund": "Your refund is being processed and should be completed within 5-7 business days.",
36
+ "newsletter_subscription": "You have successfully subscribed to our newsletter.",
37
+ "create_account": "Your account has been created successfully. You can now log in using your credentials.",
38
+ "delivery_options": "We offer standard, express, and next-day delivery options.",
39
+ "delete_account": "Your account has been deleted successfully.",
40
+ "delivery_period": "Standard delivery takes 3-5 business days, while express delivery takes 1-2 business days.",
41
+ "edit_account": "Your account details have been updated successfully.",
42
+ "change_order": "Your order has been updated successfully.",
43
+ "switch_account": "You have successfully switched to your new account.",
44
+ "change_shipping_address": "You can change the shipping address in the orders section.",
45
+ "contact_human_agent": "You are now being connected to a human agent. Please hold on."
46
+ }
47
+
48
+
49
+ # Greeting and farewell patterns
50
+ greeting_patterns = re.compile(r'\b(hi|hello|hey|greetings|good morning|good afternoon|good evening|how are you|hiya|howdy|what’s up|sup|yo)\b', re.IGNORECASE)
51
+ farewell_patterns = re.compile(r'\b(bye|thankyou|thanks|okay|goodbye|see you|farewell|take care|later|peace|cheers|goodnight|cya)\b', re.IGNORECASE)
52
+
53
+ # Set the confidence threshold
54
+ CONFIDENCE_THRESHOLD = 0.7
55
+
56
+ def get_intent(text):
57
+ doc = nlp(text)
58
+ intent = max(doc.cats, key=doc.cats.get)
59
+ confidence = doc.cats[intent]
60
+ return intent, confidence
61
+
62
+ def get_response(text):
63
+ if greeting_patterns.search(text):
64
+ return "Hello! How can I assist you today?"
65
+ elif farewell_patterns.search(text):
66
+ return "Thank you for chatting. Have a great day!"
67
+ else:
68
+ intent, confidence = get_intent(text)
69
+ # print(intent,confidence)
70
+ if confidence >= CONFIDENCE_THRESHOLD:
71
+ return intents_responses.get(intent, "I'm not sure how to respond to that. Can you please rephrase your question?")
72
+ else:
73
+ return "I'm not quite sure what you're asking. Could you please provide more details or rephrase your question?"
74
+
75
+
76
+
77
+ # Initialize session state
78
+ if 'chat_history' not in st.session_state:
79
+ st.session_state.chat_history = []
80
+ if 'processing' not in st.session_state:
81
+ st.session_state.processing = False
82
+
83
+ # Streamlit app
84
+ st.title("Customer Support Chatbot")
85
+ multi = '''
86
+ Here are some topics you can ask about:
87
+ - Return policy
88
+ - Order tracking
89
+ - Shipping information
90
+ - Payment methods
91
+ - Customer support
92
+ '''
93
+
94
+ with st.expander("**Hi, ask me anything related to our policies and services!**"):
95
+ st.markdown(multi)
96
+
97
+ # CSS styles for chat messages
98
+ st.markdown("""
99
+ <style>
100
+ .user-msg {
101
+ color: #fff;
102
+ padding: 8px 12px;
103
+ border-radius: 10px;
104
+ margin-block: 5px;
105
+ left-margin:auto;
106
+ text-align: right;
107
+ }
108
+ .bot-msg {
109
+ background-color: #F1F0F0;
110
+ color: #000000;
111
+ padding: 8px 12px;
112
+ border-radius: 10px;
113
+ margin: 5px;
114
+ text-align: left;
115
+ width:fit-content;
116
+ }
117
+ .chat-container {
118
+ display: flex;
119
+ flex-direction: column;
120
+ }
121
+ </style>
122
+ """, unsafe_allow_html=True)
123
+
124
+ # Display chat history
125
+ st.subheader("Chat History")
126
+ chat_container = st.container()
127
+
128
+ # Create a form to handle user input
129
+ with st.form(key='chat_form', clear_on_submit=True):
130
+ user_input = st.text_input("You:", key="user_input")
131
+ submit_button = st.form_submit_button(label='Send')
132
+
133
+ # Handle user input
134
+ if submit_button and user_input and not st.session_state.processing:
135
+ st.session_state.processing = True
136
+ with st.spinner('Processing...'):
137
+ response = get_response(user_input)
138
+ # Append user query and response to chat history
139
+ st.session_state.chat_history.append(("You", user_input))
140
+ st.session_state.chat_history.append(("Bot", response))
141
+ st.session_state.processing = False
142
+
143
+ st.write("Frequently Asked Questions:")
144
+ faq_buttons = [st.button(faq['question'], key=f"faq_{i}") for i, faq in enumerate(faqs[0:5])]
145
+
146
+ # Checking if any FAQ button is clicked
147
+ for i, faq_button in enumerate(faq_buttons):
148
+ if faq_button and not st.session_state.processing:
149
+ st.session_state.processing = True
150
+ user_input = faqs[i]['question']
151
+ with st.spinner('Processing...'):
152
+ response = get_response(user_input)
153
+ # Append user query and response to chat history
154
+ st.session_state.chat_history.append(("You", user_input))
155
+ st.session_state.chat_history.append(("Bot", response))
156
+ # Display chat history in the container
157
+ st.session_state.processing = False
158
+
159
+ # Display chat history in the container
160
+ with chat_container:
161
+ for role, message in st.session_state.chat_history:
162
+ if role == "You":
163
+ st.markdown(f'<div class="chat-container"><div class="user-msg">{message}</div></div>', unsafe_allow_html=True)
164
+ else:
165
+ st.markdown(f'<div class="chat-container"><div class="bot-msg">{message}</div></div>', unsafe_allow_html=True)
166
+
167
+ # Automatically scroll to the bottom of the chat
168
+ st.markdown('<script>window.scrollTo(0,document.body.scrollHeight);</script>', unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ spacy
4
+ en_pipeline@https://huggingface.co/Jayanth-MKV/en_pipeline/resolve/main/en_pipeline-any-py3-none-any.whl