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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +262 -41
app.py CHANGED
@@ -2,51 +2,272 @@ import streamlit as st
2
  from datetime import datetime
3
  from chatbot_backend import ask_bot
4
 
5
- st.set_page_config(page_title="AI Chatbot", layout="wide")
 
 
 
 
 
 
6
 
7
- # --- Minimal CSS ---
8
  st.markdown("""
9
  <style>
10
- .stApp { background: #0f1419; color: #fff; font-family: sans-serif; }
11
- .chat-container { max-height:70vh; overflow-y:auto; padding:10px; margin-bottom:10px; }
12
- .user-message, .bot-message { padding:0.6rem 1rem; border-radius:15px; max-width:70%; margin:0.4rem 0; }
13
- .user-message { background:#4facfe; margin-left:auto; color:#fff; }
14
- .bot-message { background:#667eea; margin-right:auto; color:#fff; }
15
- .quick-questions { display:flex; gap:6px; overflow-x:auto; margin-bottom:10px; }
16
- .quick-questions .stButton>button { border-radius:15px; padding:4px 10px; font-size:12px; background:#667eea33; color:#667eea; border:1px solid #667eea; white-space:nowrap; }
17
- .quick-questions .stButton>button:hover { background:#667eea55; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  </style>
19
  """, unsafe_allow_html=True)
20
 
21
  # --- Session State ---
22
- if "messages" not in st.session_state:
23
- st.session_state.messages = [{"role":"assistant","content":"Hello! Ask me anything.","timestamp":datetime.now().strftime("%H:%M")}]
24
- if "counter" not in st.session_state:
25
- st.session_state.counter = 0
26
-
27
- # --- Display Messages ---
28
- st.markdown('<div class="chat-container">', unsafe_allow_html=True)
29
- for msg in st.session_state.messages:
30
- if msg["role"]=="user":
31
- st.markdown(f'<div class="user-message">πŸ’­ {msg["content"]} <br><small>{msg["timestamp"]}</small></div>', unsafe_allow_html=True)
32
- else:
33
- st.markdown(f'<div class="bot-message">πŸ€– {msg["content"]} <br><small>{msg["timestamp"]}</small></div>', unsafe_allow_html=True)
34
- st.markdown('</div>', unsafe_allow_html=True)
35
-
36
- # --- Quick Questions ---
37
- quick_questions = ["Services","Pricing","Business Hours","Contact","Refund Policy"]
38
- st.markdown('<div class="quick-questions">', unsafe_allow_html=True)
39
- for i,q in enumerate(quick_questions):
40
- if st.button(q, key=f"q{i}"):
41
- st.session_state.messages.append({"role":"user","content":q,"timestamp":datetime.now().strftime("%H:%M")})
42
- st.session_state.messages.append({"role":"assistant","content":ask_bot(q),"timestamp":datetime.now().strftime("%H:%M")})
43
- st.markdown('</div>', unsafe_allow_html=True)
44
-
45
- # --- User Input ---
46
- user_input = st.text_input("πŸ’¬ Type your question here...", key=f"input_{st.session_state.counter}")
47
- if st.button("Send"):
48
- if user_input.strip():
49
- st.session_state.messages.append({"role":"user","content":user_input,"timestamp":datetime.now().strftime("%H:%M")})
50
- st.session_state.messages.append({"role":"assistant","content":ask_bot(user_input),"timestamp":datetime.now().strftime("%H:%M")})
51
- st.session_state.counter += 1
52
- st.experimental_rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()