-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
464 lines (393 loc) · 14.8 KB
/
Copy pathapp.py
File metadata and controls
464 lines (393 loc) · 14.8 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import os
import streamlit as st
from datetime import datetime
import json
from agent import Tutor
from quick_actbtns import render_sticky_buttons, process_quick_action
from record_manager import render_record_manager
# ============================================================
# PAGE CONFIG
# ============================================================
st.set_page_config(
page_title="3Ts Study Assistant",
page_icon="images/3TinfotinkerIcon-multiLayer.png",
layout="wide",
initial_sidebar_state="expanded"
)
# ============================================================
# DARK THEME CSS STYLING
# ============================================================
st.markdown("""
<style>
/* Balanced Dark Theme - Easy on the eyes */
.stApp {
background-color: #2b2d31;
color: #dcddee;
}
.main-header {
font-size: 2rem;
color: #5865f2;
text-align: center;
padding: 1rem 0;
text-shadow: 0 2px 10px rgba(88, 101, 242, 0.3);
}
.chat-message {
padding: 1rem;
border-radius: 0.8rem;
margin-bottom: 1rem;
border-left: 4px solid;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}
.user-message {
background-color: #404249;
border-left-color: #5865f2;
color: #ffffff;
}
.bot-message {
background-color: #383a40;
border-left-color: #57f287;
color: #dcddde;
}
.stButton>button {
width: 100%;
background-color: #5865f2;
color: #ffffff;
border: none;
font-weight: 500;
transition: all 0.2s;
}
.stButton>button:hover {
background-color: #4752c4;
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(88, 101, 242, 0.4);
}
/* Sidebar styling */
section[data-testid="stSidebar"] {
background-color: #1e1f22;
}
#--------------------------------------------------------------------
# File Uploader Styling
#--------------------------------------------------------------------
/* File uploader section - Dark theme */
[data-testid="stFileUploader"] > div {
background-color: #2b2d31;
border: 2px dashed #4e5058;
border-radius: 8px;
padding: 1rem;
transition: all 0.3s;
}
[data-testid="stFileUploader"] > div:hover {
border-color: #5865f2;
background-color: #383a40;
}
/* Drag and drop zone text color */
[data-testid="stFileUploader"] label {
color: #dcddde !important;
}
[data-testid="stFileUploader"] small {
color: #b5bac1 !important;
}
/* Browse files button */
[data-testid="stFileUploader"] button {
background-color: #5865f2 !important;
border: none !important;
color: white !important;
padding: 0.5rem 1.2rem !important;
border-radius: 6px !important;
font-weight: 600 !important;
cursor: pointer !important;
transition: all 0.3s !important;
}
[data-testid="stFileUploader"] button:hover {
background-color: #4752c4 !important;
transform: translateY(-1px) !important;
box-shadow: 0 4px 8px rgba(88, 101, 242, 0.3) !important;
}
/* Process & Build Buttons in sidebar */
section[data-testid="stSidebar"] .stButton button {
width: 100%;
background-color: #5865f2;
color: white;
border: none;
border-radius: 6px !important; /* from 8px to 6px */
padding: 0.35rem 0.6rem !important; /* affects Buttons - from 0.6rem to 0.4rem, 1rem to 0.8rem */
font-weight: 600; /*
font-size: 0.85rem !important; /* from 0.95rem to 0.85rem */
cursor: pointer;
transition: all 0.3s;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
section[data-testid="stSidebar"] .stButton button:hover {
background-color: #4752c4;
transform: translateY(-1px) !important; /* affects Buttons */
box-shadow: 0 4px 8px rgba(88, 101, 242, 0.4);
cursor: pointer !important;
}
/* Fix cursor on entire file upload area */
[data-testid="stFileUploadDropzone"] {
cursor: pointer !important;
pointer-events: none !important; /* ← Blocks all clicks on dropzone */
}
/* Uploaded files display - dark theme */
[data-testid="stFileUploader"] section {
background-color: #383a40 !important;
border-radius: 6px;
padding: 0.5rem;
margin-top: 0.5rem;
}
[data-testid="stFileUploader"] section div {
color: #dcddde !important;
}
/* ===== FILE UPLOADER CURSOR FIX ===== */
/* Force default cursor on ALL file uploader elements */
[data-testid="stFileUploader"],
[data-testid="stFileUploader"] *,
[data-testid="stFileUploadDropzone"],
[data-testid="stFileUploadDropzone"] * {
cursor: default !important;
}
/* ONLY the Browse files button gets pointer */
[data-testid="stFileUploader"] button,
[data-testid="stFileUploader"] button * {
cursor: pointer !important;
}
/* Browse files button styling */
[data-testid="stFileUploader"] button {
background-color: #5865f2 !important;
border: none !important;
color: white !important;
padding: 0.5rem 1.2rem !important;
border-radius: 6px !important;
font-weight: 600 !important;
transition: all 0.3s !important;
}
[data-testid="stFileUploader"] button:hover {
background-color: #4752c4 !important;
transform: translateY(-1px) !important;
box-shadow: 0 4px 8px rgba(88, 101, 242, 0.3) !important;
}
#--------------------------------------------------------------------
section[data-testid="stSidebar"] > div {
background-color: #1e1f22;
}
/* Input box styling */
.stTextInput>div>div>input {
background-color: #383a40;
color: #dcddde;
border: 2px solid #4e5058;
}
.stTextInput>div>div>input:focus {
border-color: #5865f2;
box-shadow: 0 0 0 2px rgba(88, 101, 242, 0.2);
}
/* File uploader */
.stFileUploader {
background-color: #383a40;
border: 2px solid #4e5058;
border-radius: 0.5rem;
}
/* Headers */
h3 {
color: #ffffff;
}
/* Markdown text */
.stMarkdown {
color: #dcddde;
}
/* Dividers */
hr {
border-color: #4e5058;
}
</style>
""", unsafe_allow_html=True)
# ============================================================
# SESSION STATE INIT
# ============================================================
if 'tutor' not in st.session_state:
st.session_state.tutor = Tutor()
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
if 'history_file' not in st.session_state:
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
HISTORY_DIR = os.path.join(SCRIPT_DIR, "chat_history")
os.makedirs(HISTORY_DIR, exist_ok=True)
st.session_state.history_file = os.path.join(
HISTORY_DIR,
f"chat_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
)
if 'input_key' not in st.session_state:
st.session_state.input_key = 0
# ============================================================
# HELPER FUNCTIONS
# ============================================================
def save_chat_history():
"""Save chat history to JSON file"""
with open(st.session_state.history_file, 'w') as f:
json.dump(st.session_state.chat_history, f, indent=2)
def load_previous_chats():
"""Load list of previous chat sessions"""
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
HISTORY_DIR = os.path.join(SCRIPT_DIR, "chat_history")
if not os.path.exists(HISTORY_DIR):
return []
files = [f for f in os.listdir(HISTORY_DIR) if f.endswith('.json')]
return sorted(files, reverse=True)
def add_to_chat(role, content):
"""Add message to chat history"""
st.session_state.chat_history.append({
'role': role,
'content': content,
'timestamp': datetime.now().isoformat()
})
save_chat_history()
def clear_input():
"""Clear input by incrementing key"""
st.session_state.input_key += 1
# ============================================================
# SIDEBAR
# ============================================================
with st.sidebar:
# Display logo
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
#logo_path = os.path.join(SCRIPT_DIR, "images", "3T_AnimLogo_L.gif")
logo_path = os.path.join(SCRIPT_DIR, "images", "3TinfoTinkerLogo.png")
if os.path.exists(logo_path):
# New Eddition
col1, col2, col3 = st.columns([1, 3, 1])
with col2:
st.image(logo_path, width='stretch') # For - use_container_width=True, use:- width='stretch'. For use_container_width=False, use:- width='content'`.
else:
st.warning("⚠️ Logo not found. Place it in: Tutor/images/3T_AnimLogo_L.gif")
st.markdown("---")
st.markdown("### 📚 Book Management")
# File uploader
uploaded_files = st.file_uploader(
"Upload Books (PDF/Images)",
type=['pdf', 'png', 'jpg', 'jpeg'],
accept_multiple_files=True,
help="Upload your textbooks"
)
if uploaded_files:
if st.button("🔨 Process & Build Index"):
with st.spinner("Processing books... This may take a few minutes."):
try:
# Create uploads directory
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
UPLOAD_DIR = os.path.join(SCRIPT_DIR, "uploads")
os.makedirs(UPLOAD_DIR, exist_ok=True)
# Save uploaded files
for uploaded_file in uploaded_files:
file_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
with open(file_path, "wb") as f:
f.write(uploaded_file.getbuffer())
st.info("📖 Extracting text from books...")
# Build knowledge base
st.session_state.tutor.build_knowledge_base(UPLOAD_DIR)
st.success("✅ Books processed successfully!")
# Show sample of extracted text for verification
st.info("💡 Try asking questions about the book content now!")
st.rerun()
except Exception as e:
st.error(f"❌ Error processing books: {e}")
st.warning("⚠️ If OCR failed, check your hybrid_processor.py configuration")
st.markdown("---")
st.markdown("### 💾 Chat History")
# Load previous chats
previous_chats = load_previous_chats()
if previous_chats:
selected_chat = st.selectbox(
"Load Previous Session",
["Current Session"] + previous_chats
)
if selected_chat != "Current Session" and st.button("Load"):
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
HISTORY_DIR = os.path.join(SCRIPT_DIR, "chat_history")
with open(os.path.join(HISTORY_DIR, selected_chat), 'r') as f:
st.session_state.chat_history = json.load(f)
st.rerun()
if st.button("🗑️ Clear Current Chat"):
st.session_state.chat_history = []
save_chat_history()
st.rerun()
st.markdown("---")
st.markdown("### 🔧 Debug Info")
if st.checkbox("Show Index Status"):
try:
num_chunks = len(st.session_state.tutor.vector_store.chunks)
st.success(f"✅ Index loaded: {num_chunks} chunks")
if st.button("Show Sample Text"):
if num_chunks > 0:
st.code(st.session_state.tutor.vector_store.chunks[0][:300])
except:
st.error("❌ No index found - please build first")
render_record_manager()
st.markdown("---")
st.markdown("### ℹ️ About")
st.info("""
**3Ts Study Assistant v1.0**
AI-powered Study Assistant.
Features:
- 💬 Natural conversation
- 📝 Quiz generation
- 📖 Topic summaries
- 🎯 Concept explanations
- 🗃 Record Manager
""")
# ============================================================
# MAIN CONTENT
# ============================================================
st.markdown('<h1 class="main-header">📚 3Ts Study Assistant - Your Personal Study Assistant</h1>',
unsafe_allow_html=True)
render_sticky_buttons()
process_quick_action()
# Display chat history
chat_container = st.container()
with chat_container:
for idx, message in enumerate(st.session_state.chat_history):
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>🤖 Tutor:</strong><br>
{message['content'].replace(chr(10), '<br>')}
</div>
""", unsafe_allow_html=True)
# # Add copy button for bot messages
# if st.button(f"📋 Copy", key=f"copy_{idx}"):
# st.code(message['content'], language=None)
# Chat input
st.markdown("---")
st.markdown("### 💬 Ask me anything from the book!")
user_input = st.text_input(
"Type your question here...",
placeholder="e.g., What is Newton's first law? or Give me a quiz on vectors",
key=f"main_input_{st.session_state.input_key}"
)
if st.button("Send 🚀") and user_input:
# Add user message
add_to_chat('user', user_input)
# Get tutor response
with st.spinner("Thinking..."):
try:
response = st.session_state.tutor.chat(user_input)
add_to_chat('assistant', response)
clear_input() # Clear the input box
st.rerun()
except Exception as e:
st.error(f"❌ Error: {e}")
st.info("💡 Make sure you've built the knowledge base first!")
# ============================================================
# FOOTER
# ============================================================
st.markdown("---")
st.markdown("""
<div style='text-align: center; color: #b5b5b5; padding: 1rem;'>
<h3>| Built with ❤️ for Students and Learning enthusiasts |</h3><small> Powered by Google Gemini & FAISS</small>
</div>
""", unsafe_allow_html=True)