Uamir commited on
Commit
d4568b9
Β·
verified Β·
1 Parent(s): a582b21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -255
app.py CHANGED
@@ -2,272 +2,82 @@ import streamlit as st
2
  from datetime import datetime
3
  from chatbot_backend import ask_bot
4
 
5
- # --- Page Configuration and Custom CSS ---
6
  st.set_page_config(
7
  page_title="AI Customer Service Bot",
8
  page_icon="πŸ€–",
9
- layout="wide",
10
- initial_sidebar_state="expanded"
11
  )
12
 
13
- st.markdown("""
14
- <style>
15
- /* General app background */
16
- .stApp { background-color: #0f1419; color: #ffffff; }
17
-
18
- /* Header styling */
19
- .chat-header {
20
- text-align: center;
21
- font-size: 1.5rem;
22
- font-weight: 700;
23
- margin-bottom: 1rem;
24
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
25
- -webkit-background-clip: text;
26
- -webkit-text-fill-color: transparent;
27
- }
28
-
29
- /* Message bubble styling */
30
- .user-message, .bot-message {
31
- padding: 0.75rem 1rem;
32
- border-radius: 15px;
33
- font-size: 14px;
34
- line-height: 1.4;
35
- margin: 0.25rem 0;
36
- max-width: 75%;
37
- word-wrap: break-word;
38
- }
39
-
40
- .user-message {
41
- background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
42
- color: white;
43
- margin-left: auto;
44
- border-radius: 15px 15px 5px 15px;
45
- box-shadow: 0 2px 10px rgba(79, 172, 254, 0.2);
46
- }
47
-
48
- .bot-message {
49
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
50
- color: white;
51
- margin-right: auto;
52
- border-radius: 15px 15px 15px 5px;
53
- box-shadow: 0 2px 10px rgba(102, 126, 234, 0.2);
54
- }
55
-
56
- .message-time {
57
- font-size: 0.65rem;
58
- opacity: 0.6;
59
- margin-top: 0.25rem;
60
- }
61
- .user-time { text-align: right; }
62
- .bot-time { text-align: left; }
63
-
64
- /* Input and button styling */
65
- .stTextInput > div > div > input {
66
- background-color: #1a1a1a;
67
- color: white;
68
- border: 1px solid #333;
69
- border-radius: 20px;
70
- padding: 10px 15px;
71
- font-size: 14px;
72
- transition: all 0.2s ease;
73
- }
74
-
75
- .stTextInput > div > div > input:focus {
76
- border-color: #667eea;
77
- box-shadow: 0 0 5px rgba(102, 126, 234, 0.2);
78
- }
79
-
80
- .stButton > button {
81
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
82
- color: white;
83
- border: none;
84
- border-radius: 40px;
85
- padding: 0.5rem 1.5rem;
86
- font-weight: 600;
87
- font-size: 14px;
88
- transition: all 0.2s ease;
89
- box-shadow: 0 2px 10px rgba(102, 126, 234, 0.3);
90
- }
91
-
92
- .stButton > button:hover {
93
- transform: translateY(-1px);
94
- box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
95
- }
96
-
97
- /* Sidebar styling */
98
- .sidebar .stButton > button {
99
- background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
100
- width: 100%;
101
- border-radius: 10px;
102
- margin-bottom: 10px;
103
- }
104
-
105
- /* Chat stats */
106
- .chat-stats {
107
- background: rgba(255, 255, 255, 0.05);
108
- padding: 0.75rem;
109
- border-radius: 8px;
110
- margin: 0.5rem 0;
111
- backdrop-filter: blur(10px);
112
- border: 1px solid rgba(255, 255, 255, 0.1);
113
- font-size: 12px;
114
- }
115
-
116
- /* Quick Questions */
117
- .quick-questions-container {
118
- display: flex;
119
- flex-wrap: wrap;
120
- gap: 5px;
121
- margin-bottom: 10px;
122
- }
123
-
124
- .quick-questions-container .stButton > button {
125
- background: rgba(102, 126, 234, 0.1) !important;
126
- border: 1px solid #667eea !important;
127
- color: #667eea !important;
128
- border-radius: 15px !important;
129
- padding: 5px 10px !important;
130
- font-size: 12px !important;
131
- transition: all 0.3s ease !important;
132
- flex-grow: 1;
133
- min-width: fit-content;
134
- }
135
-
136
- .quick-questions-container .stButton > button:hover {
137
- background: rgba(102, 126, 234, 0.2) !important;
138
- transform: translateY(-1px) !important;
139
- }
140
- </style>
141
- """, unsafe_allow_html=True)
142
 
143
- # --- Session State ---
144
- def initialize_session_state():
145
- if "messages" not in st.session_state:
146
- st.session_state.messages = []
147
- welcome = """πŸŽ‰ Welcome to AI Customer Service!
148
- I'm your intelligent assistant, ready to help you with any questions or concerns.
149
- Feel free to ask me anything! πŸ’¬"""
150
- st.session_state.messages.append({
151
  "role": "assistant",
152
- "content": welcome,
153
  "timestamp": datetime.now().strftime("%H:%M")
154
- })
155
- if "input_counter" not in st.session_state:
156
  st.session_state.input_counter = 0
157
 
158
- # --- Display chat messages ---
159
- def display_chat_messages():
160
- for message in st.session_state.messages:
161
- if message["role"] == "user":
162
- st.markdown(f"""
163
- <div class="user-message">
164
- πŸ’­ {message["content"]}
165
- <div class="message-time user-time">You β€’ {message["timestamp"]}</div>
166
- </div>
167
- """, unsafe_allow_html=True)
168
- else:
169
- st.markdown(f"""
170
- <div class="bot-message">
171
- πŸ€– {message["content"]}
172
- <div class="message-time bot-time">AI Assistant β€’ {message["timestamp"]}</div>
173
- </div>
174
- """, unsafe_allow_html=True)
175
- st.markdown("", unsafe_allow_html=True)
176
-
177
- # --- Handle input ---
178
- def handle_user_input():
179
- col1, col2 = st.columns([4, 1])
180
- with col1:
181
- user_input = st.text_input(
182
- "",
183
- placeholder="πŸ’¬ Type your question here...",
184
- key=f"user_input_{st.session_state.input_counter}",
185
- label_visibility="collapsed"
186
- )
187
- with col2:
188
- send_clicked = st.button("Send ➀", type="primary")
189
- if (send_clicked or user_input) and user_input.strip():
190
- process_message(user_input.strip())
191
-
192
- def process_message(user_message):
193
- st.session_state.messages.append({
194
- "role": "user",
195
- "content": user_message,
196
- "timestamp": datetime.now().strftime("%H:%M")
197
- })
198
- with st.spinner("πŸ€– AI is thinking..."):
199
- try:
200
- bot_response = ask_bot(user_message)
201
- except Exception as e:
202
- bot_response = f"❌ Sorry, I encountered an error: {str(e)}"
203
- st.session_state.messages.append({
204
- "role": "assistant",
205
- "content": bot_response,
206
- "timestamp": datetime.now().strftime("%H:%M")
207
- })
208
- st.session_state.input_counter += 1
209
- st.rerun()
210
-
211
- # --- Sidebar ---
212
- def create_sidebar():
213
- with st.sidebar:
214
- st.markdown("## πŸŽ›οΈ Chat Controls")
215
- if st.button("πŸ—‘οΈ Clear Chat", type="secondary"):
216
- st.session_state.messages = []
217
- st.session_state.input_counter = 0
218
- welcome = """πŸŽ‰ Welcome to AI Customer Service!
219
- I'm your intelligent assistant, ready to help you with any questions or concerns.
220
- Feel free to ask me anything! πŸ’¬"""
221
  st.session_state.messages.append({
222
  "role": "assistant",
223
- "content": welcome,
224
  "timestamp": datetime.now().strftime("%H:%M")
225
  })
226
- st.rerun()
227
-
228
- st.markdown("---")
229
- st.markdown("## ⚑ Quick Questions")
230
- quick_questions = [
231
- "What services do you provide?",
232
- "What are your pricing packages?",
233
- "What are your business hours?",
234
- "How can I contact support?",
235
- "What is your refund policy?"
236
- ]
237
-
238
- cols = st.columns(2)
239
- for i, question in enumerate(quick_questions):
240
- with cols[i % 2]:
241
- if st.button(f"πŸ’‘ {question}", key=f"quick_{i}"):
242
- process_message(question)
243
-
244
- st.markdown("---")
245
- st.markdown("## πŸ“Š Chat Statistics")
246
- st.markdown(f"""
247
- <div class="chat-stats">
248
- <strong>Total Messages:</strong> {len(st.session_state.messages)}<br>
249
- <strong>Your Questions:</strong> {len([m for m in st.session_state.messages if m['role'] == 'user'])}<br>
250
- <strong>AI Responses:</strong> {len([m for m in st.session_state.messages if m['role'] == 'assistant'])}
251
- </div>
252
- """, unsafe_allow_html=True)
253
-
254
- # --- Main ---
255
- def main():
256
- initialize_session_state()
257
- st.markdown('<h1 class="chat-header">πŸ€– AI Customer Service Assistant</h1>', unsafe_allow_html=True)
258
- create_sidebar()
259
- chat_container = st.container()
260
- with chat_container:
261
- st.markdown("---")
262
- display_chat_messages()
263
- st.markdown("---")
264
- handle_user_input()
265
- st.markdown(
266
- "<div style='text-align: center; color: #666; padding: 0.5rem;'>"
267
- "πŸ€– Powered by AI β€’ Built with Streamlit β€’ Always here to help!"
268
- "</div>",
269
- unsafe_allow_html=True
270
- )
271
-
272
- if __name__ == "__main__":
273
- main()
 
2
  from datetime import datetime
3
  from chatbot_backend import ask_bot
4
 
5
+ # --- Page Configuration ---
6
  st.set_page_config(
7
  page_title="AI Customer Service Bot",
8
  page_icon="πŸ€–",
9
+ layout="wide"
 
10
  )
11
 
12
+ # --- Initialize Session State ---
13
+ if "messages" not in st.session_state:
14
+ st.session_state.messages = [{
15
+ "role": "assistant",
16
+ "content": "πŸŽ‰ Welcome! I'm your AI Customer Service Assistant. Ask me anything! πŸ’¬",
17
+ "timestamp": datetime.now().strftime("%H:%M")
18
+ }]
19
+ if "input_counter" not in st.session_state:
20
+ st.session_state.input_counter = 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ # --- Sidebar ---
23
+ with st.sidebar:
24
+ st.title("Controls")
25
+ if st.button("πŸ—‘οΈ Clear Chat"):
26
+ st.session_state.messages = [{
 
 
 
27
  "role": "assistant",
28
+ "content": "πŸŽ‰ Welcome! I'm your AI Customer Service Assistant. Ask me anything! πŸ’¬",
29
  "timestamp": datetime.now().strftime("%H:%M")
30
+ }]
 
31
  st.session_state.input_counter = 0
32
 
33
+ st.markdown("---")
34
+ st.subheader("Quick Questions")
35
+ quick_questions = [
36
+ "What services do you provide?",
37
+ "What are your pricing packages?",
38
+ "What are your business hours?",
39
+ "How can I contact support?",
40
+ "What is your refund policy?"
41
+ ]
42
+ for question in quick_questions:
43
+ if st.button(question):
44
+ st.session_state.messages.append({
45
+ "role": "user",
46
+ "content": question,
47
+ "timestamp": datetime.now().strftime("%H:%M")
48
+ })
49
+ response = ask_bot(question)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  st.session_state.messages.append({
51
  "role": "assistant",
52
+ "content": response,
53
  "timestamp": datetime.now().strftime("%H:%M")
54
  })
55
+ st.experimental_rerun()
56
+
57
+ # --- Chat Display ---
58
+ st.title("πŸ€– AI Customer Service")
59
+ chat_container = st.container()
60
+ for message in st.session_state.messages:
61
+ if message["role"] == "user":
62
+ st.info(f"**You:** {message['content']}\n*{message['timestamp']}*")
63
+ else:
64
+ st.success(f"**AI:** {message['content']}\n*{message['timestamp']}*")
65
+
66
+ # --- Input ---
67
+ user_input = st.text_input("Type your question here...", key=f"user_input_{st.session_state.input_counter}")
68
+ if st.button("Send"):
69
+ if user_input.strip():
70
+ st.session_state.messages.append({
71
+ "role": "user",
72
+ "content": user_input.strip(),
73
+ "timestamp": datetime.now().strftime("%H:%M")
74
+ })
75
+ with st.spinner("AI is thinking..."):
76
+ response = ask_bot(user_input.strip())
77
+ st.session_state.messages.append({
78
+ "role": "assistant",
79
+ "content": response,
80
+ "timestamp": datetime.now().strftime("%H:%M")
81
+ })
82
+ st.session_state.input_counter += 1
83
+ st.experimental_rerun()