-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
396 lines (338 loc) · 14.6 KB
/
streamlit_app.py
File metadata and controls
396 lines (338 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import streamlit as st
import time
from typing import List, Dict, Any
import os
from datetime import datetime
from pathlib import Path
import shutil
# Import your existing RAG chatbot
from rag_chatbot import TunisiaPolytechnicRAGBot
# Page configuration
st.set_page_config(
page_title="ChatEPT - École Polytechnique de Tunisie",
page_icon="🎓",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for better styling
st.markdown("""
<style>
.main-header {
text-align: center;
color: #1f4e79;
margin-bottom: 2rem;
}
.chat-message {
padding: 1rem;
border-radius: 10px;
margin-bottom: 1rem;
border-left: 4px solid;
}
.user-message {
background-color: #e3f2fd;
border-left-color: #2196f3;
}
.bot-message {
background-color: #000;
border-left-color: #28a745;
}
.source-box {
background-color: #f5f5f5;
border: 1px solid #ddd;
border-radius: 5px;
padding: 0.5rem;
margin-top: 0.5rem;
font-size: 0.9rem;
}
.stTextInput > div > div > input {
border-radius: 20px;
}
</style>
""", unsafe_allow_html=True)
# Initialize session state
if "messages" not in st.session_state:
st.session_state.messages = []
if "chatbot" not in st.session_state:
st.session_state.chatbot = None
st.session_state.chatbot_initialized = False
if "api_key" not in st.session_state:
st.session_state.api_key = None
if "documents_count" not in st.session_state:
st.session_state.documents_count = 0
# Sidebar configuration
with st.sidebar:
st.image("https://via.placeholder.com/200x100/1f4e79/white?text=EPT", width=200)
st.title("⚙️ Configuration")
# API Key input
if not st.session_state.api_key:
st.subheader("🔑 API Configuration")
api_key_input = st.text_input(
"OpenRouter API Key:",
type="password",
help="Enter your OpenRouter API key to use the chatbot"
)
if st.button("Connect"):
if api_key_input:
st.session_state.api_key = api_key_input
st.success("API Key saved!")
st.rerun()
else:
st.error("Please enter a valid API key")
st.info("💡 Get your free API key from [OpenRouter](https://openrouter.ai/)")
st.stop()
else:
st.success("🔑 API Key connected")
if st.button("Reset API Key"):
st.session_state.api_key = None
st.session_state.chatbot = None
st.session_state.chatbot_initialized = False
st.rerun()
st.divider()
# Document upload section
st.subheader("📄 Document Management")
# Show current documents count
documents_dir = Path("documents")
if documents_dir.exists():
pdf_files = list(documents_dir.glob("*.pdf"))
st.session_state.documents_count = len(pdf_files)
st.info(f"📚 Current documents: {len(pdf_files)}")
# List current documents
if pdf_files:
with st.expander("View Documents"):
for pdf in pdf_files:
st.text(f"📄 {pdf.name}")
else:
st.info("📚 No documents directory found")
uploaded_files = st.file_uploader(
"Upload PDF documents:",
type=['pdf'],
accept_multiple_files=True,
help="Upload PDF documents to expand the knowledge base"
)
if uploaded_files:
if st.button("Process Documents"):
with st.spinner("Processing documents..."):
# Create documents directory if it doesn't exist
documents_dir.mkdir(exist_ok=True)
success_count = 0
for uploaded_file in uploaded_files:
try:
# Save uploaded file
file_path = documents_dir / uploaded_file.name
with open(file_path, "wb") as f:
f.write(uploaded_file.getbuffer())
# Add to chatbot if initialized
if st.session_state.chatbot:
st.session_state.chatbot.add_document(str(file_path))
success_count += 1
except Exception as e:
st.error(f"Error processing {uploaded_file.name}: {str(e)}")
if success_count > 0:
st.success(f"✅ Processed {success_count} documents!")
st.session_state.documents_count += success_count
# Reinitialize chatbot to reload vector store
if st.session_state.chatbot_initialized:
st.session_state.chatbot_initialized = False
st.rerun()
st.divider()
# Statistics
st.subheader("📊 Statistics")
col1, col2 = st.columns(2)
with col1:
st.metric("Total Messages", len(st.session_state.messages))
with col2:
st.metric("Documents", st.session_state.documents_count)
# Vector store status
if st.session_state.chatbot and st.session_state.chatbot.vectorstore:
chunks_count = st.session_state.chatbot.vectorstore.index.ntotal
st.metric("Document Chunks", chunks_count)
# Clear chat button
if st.button("🗑️ Clear Chat", type="secondary"):
st.session_state.messages = []
st.rerun()
# Rebuild knowledge base button
if st.button("🔄 Rebuild Knowledge Base", help="Rebuild the vector store from documents"):
if st.session_state.chatbot:
with st.spinner("Rebuilding knowledge base..."):
# Force rebuild by removing vectorstore and reinitializing
vectorstore_path = Path("vectorstore")
if vectorstore_path.exists():
shutil.rmtree(vectorstore_path)
st.session_state.chatbot_initialized = False
st.success("Knowledge base will be rebuilt on next query")
st.rerun()
# Main content area
st.markdown("<h1 class='main-header'>🎓 ChatEPT - École Polytechnique de Tunisie</h1>",
unsafe_allow_html=True)
st.markdown("""
**Welcome to ChatEPT!** 🤖 I'm here to help you with questions about École Polytechnique de Tunisie.
Ask me about admissions, courses, facilities, regulations, or any other information you need.
""")
# Initialize chatbot
@st.cache_resource
def initialize_chatbot(api_key):
"""Initialize the TunisiaPolytechnicRAGBot"""
try:
return TunisiaPolytechnicRAGBot(api_key, pdf_directory="documents/")
except Exception as e:
st.error(f"Failed to initialize chatbot: {str(e)}")
return None
if not st.session_state.chatbot_initialized and st.session_state.api_key:
with st.spinner("Initializing ChatEPT... This may take a moment to load the knowledge base."):
st.session_state.chatbot = initialize_chatbot(st.session_state.api_key)
if st.session_state.chatbot:
st.session_state.chatbot_initialized = True
# Update documents count
if st.session_state.chatbot.vectorstore:
chunks_count = st.session_state.chatbot.vectorstore.index.ntotal
st.success(f"✅ ChatEPT initialized with {chunks_count} document chunks!")
else:
st.warning("⚠️ ChatEPT initialized without knowledge base. Add documents to enable RAG functionality.")
else:
st.error("Failed to initialize ChatEPT. Please check your API key and try again.")
# Chat interface
chat_container = st.container()
# Display chat messages
with chat_container:
for i, message in enumerate(st.session_state.messages):
if message["role"] == "user":
st.markdown(f"""
<div class="chat-message user-message">
<strong>👤 You:</strong><br>
{message["content"]}
</div>
""", unsafe_allow_html=True)
else:
st.markdown(f"""
<div class="chat-message bot-message">
<strong>🤖 ChatEPT:</strong><br>
{message["content"]}
</div>
""", unsafe_allow_html=True)
# Display sources if available
if "sources" in message and message["sources"]:
with st.expander("📚 Sources", expanded=False):
for source in message["sources"]:
st.markdown(f"""
<div class="source-box">
<strong>📄 Document:</strong> {source}<br>
</div>
""", unsafe_allow_html=True)
# Chat input
with st.container():
col1, col2 = st.columns([6, 1])
with col1:
user_input = st.text_input(
"Ask your question about EPT:",
placeholder="e.g., What are the admission requirements for engineering programs?",
key="user_input"
)
with col2:
send_button = st.button("Send", type="primary", key="send_button")
# Handle user input
if send_button and user_input:
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": user_input})
# Show typing indicator
with st.spinner("ChatEPT is thinking..."):
if st.session_state.chatbot and st.session_state.chatbot_initialized:
try:
# Get response from RAG chatbot
start_time = time.time()
response = st.session_state.chatbot.get_response(user_input)
end_time = time.time()
# Extract sources from response if available
sources = []
if "📚 Sources:" in response:
response_parts = response.split("📚 Sources:")
main_response = response_parts[0].strip()
sources_text = response_parts[1].strip()
# Extract source lines
source_lines = [line.strip()[2:] for line in sources_text.split('\n') if line.strip().startswith('-')]
sources = source_lines
response = main_response
# Add bot response to chat history
message_data = {
"role": "assistant",
"content": response,
"timestamp": datetime.now(),
"response_time": f"{end_time - start_time:.2f}s"
}
if sources:
message_data["sources"] = sources
st.session_state.messages.append(message_data)
except Exception as e:
error_msg = f"Sorry, I encountered an error: {str(e)}"
st.session_state.messages.append({
"role": "assistant",
"content": error_msg,
"timestamp": datetime.now()
})
else:
# Chatbot not initialized
error_msg = "Please ensure the chatbot is properly initialized with a valid API key."
st.session_state.messages.append({
"role": "assistant",
"content": error_msg,
"timestamp": datetime.now()
})
# Clear input and rerun to show new messages
st.rerun()
# Footer
st.markdown("---")
st.markdown("""
<div style='text-align: center; color: #666;'>
<small>
ChatEPT v1.0 | Built with Streamlit & LangChain |
École Polytechnique de Tunisie © 2024
</small>
</div>
""", unsafe_allow_html=True)
# Quick action buttons
st.markdown("### 🚀 Quick Questions")
quick_questions = [
"What are the admission requirements?",
"Tell me about the engineering programs",
"What facilities are available on campus?",
"How can I apply for scholarships?"
]
cols = st.columns(len(quick_questions))
for i, question in enumerate(quick_questions):
with cols[i]:
if st.button(question, key=f"quick_{i}"):
st.session_state.messages.append({"role": "user", "content": question})
# Process the quick question
with st.spinner("Processing..."):
if st.session_state.chatbot and st.session_state.chatbot_initialized:
try:
response = st.session_state.chatbot.get_response(question)
# Extract sources from response if available
sources = []
if "📚 Sources:" in response:
response_parts = response.split("📚 Sources:")
main_response = response_parts[0].strip()
sources_text = response_parts[1].strip()
source_lines = [line.strip()[2:] for line in sources_text.split('\n') if line.strip().startswith('-')]
sources = source_lines
response = main_response
message_data = {
"role": "assistant",
"content": response,
"timestamp": datetime.now()
}
if sources:
message_data["sources"] = sources
st.session_state.messages.append(message_data)
except Exception as e:
st.session_state.messages.append({
"role": "assistant",
"content": f"Sorry, I encountered an error: {str(e)}",
"timestamp": datetime.now()
})
else:
st.session_state.messages.append({
"role": "assistant",
"content": "Please ensure the chatbot is properly initialized.",
"timestamp": datetime.now()
})
st.rerun()