forked from 25CD3014/vectortutor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
654 lines (574 loc) Β· 25.4 KB
/
Copy pathapp.py
File metadata and controls
654 lines (574 loc) Β· 25.4 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
"""
VectorTutor - Streamlit App
Modern single-page UI with all features accessible from home
"""
import streamlit as st
import os
from pathlib import Path
import sys
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Add project root to path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from utils.groq_client import GroqClient
from utils.memory import KnowledgeMemory
from agents.reader_agent import ReaderAgent
from agents.flashcard_agent import FlashcardAgent
from agents.quiz_agent import QuizAgent
from agents.planner_agent import PlannerAgent
from agents.chat_agent import ChatAgent
# Page configuration
st.set_page_config(
page_title="VectorTutor - AI Learning Companion",
page_icon="",
layout="wide",
initial_sidebar_state="collapsed"
)
# Custom CSS for modern UI
st.markdown("""
<style>
.stApp {
background-color: #000000;
}
.main-header {
background: linear-gradient(135deg, #000000 0%, #1a1a1a 100%);
padding: 2rem;
border-radius: 15px;
color: #ffa500;
margin-bottom: 2rem;
box-shadow: 0 10px 30px rgba(255, 165, 0, 0.2);
border: 2px solid #ffa500;
}
.feature-card {
background: #1a1a1a;
padding: 1.5rem;
border-radius: 12px;
box-shadow: 0 4px 15px rgba(255, 165, 0, 0.1);
margin-bottom: 1.5rem;
border-left: 4px solid #ffa500;
transition: transform 0.2s;
}
.feature-card:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(255, 165, 0, 0.3);
border-left-color: #ffd700;
}
.stat-card {
background: linear-gradient(135deg, #000000 0%, #1a1a1a 100%);
padding: 1.5rem;
border-radius: 12px;
color: #ffa500;
text-align: center;
box-shadow: 0 4px 15px rgba(255, 165, 0, 0.2);
border: 2px solid #ffa500;
}
.stat-number {
font-size: 2.5rem;
font-weight: bold;
margin: 0.5rem 0;
color: #ffa500;
}
.stat-label {
font-size: 0.9rem;
opacity: 0.9;
color: #ffd700;
}
.stButton>button {
background: linear-gradient(135deg, #ffa500 0%, #ff8c00 100%);
color: #000000;
border: 2px solid #ffa500;
border-radius: 8px;
padding: 0.5rem 2rem;
font-weight: 700;
transition: all 0.3s;
}
.stButton>button:hover {
transform: scale(1.05);
background: linear-gradient(135deg, #ffd700 0%, #ffa500 100%);
box-shadow: 0 5px 15px rgba(255, 165, 0, 0.5);
border-color: #ffd700;
color: #000000;
}
.section-header {
font-size: 1.8rem;
font-weight: 700;
color: #ffa500;
margin: 2rem 0 1rem 0;
padding-bottom: 0.5rem;
border-bottom: 3px solid #ffa500;
}
.agent-badge {
display: inline-block;
background: #ffa500;
color: #000000;
padding: 0.3rem 0.8rem;
border-radius: 20px;
font-size: 0.85rem;
margin: 0.2rem;
border: 1px solid #ffd700;
font-weight: 600;
}
.sidebar .sidebar-content {
background: linear-gradient(180deg, #000000 0%, #1a1a1a 100%);
}
.stExpander {
border: 2px solid #ffa500;
border-radius: 8px;
background-color: #1a1a1a;
}
.stExpander label {
color: #ffa500;
font-weight: 700;
}
.stExpander label:hover {
color: #ffd700;
}
.stSelectbox label, .stTextInput label, .stNumberInput label {
color: #ffa500;
font-weight: 600;
}
.stMarkdown {
color: #ffffff;
}
.stInfo {
background-color: #1a1a1a;
border-left: 4px solid #ffa500;
}
.stSuccess {
background-color: #1a1a1a;
border-left: 4px solid #ffa500;
color: #ffa500;
}
.stWarning {
background-color: #1a1a1a;
border-left: 4px solid #ffa500;
color: #ffa500;
}
.stError {
background-color: #1a1a1a;
border-left: 4px solid #ff0000;
color: #ff6666;
}
h1, h2, h3, h4, h5, h6 {
color: #ffa500;
}
p, div, span {
color: #ffffff;
}
.stMetric {
background-color: #1a1a1a;
}
.stMetric label {
color: #ffd700;
}
.stMetric [data-testid="stMetricValue"] {
color: #ffa500;
}
</style>
""", unsafe_allow_html=True)
# Initialize session state
if "groq_client" not in st.session_state:
try:
st.session_state.groq_client = GroqClient()
except Exception as e:
st.error(f"Error initializing Groq client: {str(e)}")
st.stop()
if "memory" not in st.session_state:
st.session_state.memory = KnowledgeMemory()
if "reader_agent" not in st.session_state:
st.session_state.reader_agent = ReaderAgent(
st.session_state.groq_client,
st.session_state.memory
)
if "flashcard_agent" not in st.session_state:
st.session_state.flashcard_agent = FlashcardAgent(
st.session_state.groq_client,
st.session_state.memory
)
if "quiz_agent" not in st.session_state:
st.session_state.quiz_agent = QuizAgent(
st.session_state.groq_client,
st.session_state.memory
)
if "planner_agent" not in st.session_state:
st.session_state.planner_agent = PlannerAgent(
st.session_state.groq_client,
st.session_state.memory
)
if "chat_agent" not in st.session_state:
st.session_state.chat_agent = ChatAgent(
st.session_state.groq_client,
st.session_state.memory
)
# Initialize session state for UI
if "active_section" not in st.session_state:
st.session_state.active_section = None
if "chat_messages" not in st.session_state:
st.session_state.chat_messages = []
# Sidebar - Hidden by default, only for quick navigation
with st.sidebar:
st.markdown("### VectorTutor Navigation")
nav_option = st.selectbox(
"Jump to Section",
["π Home", "π Upload PDF", "π΄ Flashcards", "π Quizzes", "π
Planner", "π¬ Chat", "π Performance"],
index=0,
label_visibility="collapsed"
)
if nav_option != "π Home":
st.session_state.active_section = nav_option
# Main Header
st.markdown("""
<div class="main-header">
<h1 style="margin: 0; font-size: 3rem;">VectorTutor</h1>
<p style="margin: 0.5rem 0 0 0; font-size: 1.2rem; opacity: 0.95;">Your AI-Powered Learning Companion</p>
<div style="margin-top: 1rem;">
<span class="agent-badge">Reader Agent</span>
<span class="agent-badge">Flashcard Agent</span>
<span class="agent-badge">Quiz Agent</span>
<span class="agent-badge">Planner Agent</span>
<span class="agent-badge">Chat Agent</span>
</div>
</div>
""", unsafe_allow_html=True)
# Statistics Dashboard
documents = st.session_state.memory.get_all_documents()
flashcards = st.session_state.memory.get_flashcards()
quizzes = st.session_state.memory.get_quizzes()
performance = st.session_state.memory.get_performance_stats()
st.markdown('<div class="section-header">π Your Dashboard</div>', unsafe_allow_html=True)
col1, col2, col3, col4 = st.columns(4)
with col1:
st.markdown(f"""
<div class="stat-card">
<div class="stat-label">Documents</div>
<div class="stat-number">{len(documents)}</div>
</div>
""", unsafe_allow_html=True)
with col2:
st.markdown(f"""
<div class="stat-card">
<div class="stat-label">Flashcards</div>
<div class="stat-number">{len(flashcards)}</div>
</div>
""", unsafe_allow_html=True)
with col3:
st.markdown(f"""
<div class="stat-card">
<div class="stat-label">Quiz Questions</div>
<div class="stat-number">{len(quizzes)}</div>
</div>
""", unsafe_allow_html=True)
with col4:
st.markdown(f"""
<div class="stat-card">
<div class="stat-label">Quiz Accuracy</div>
<div class="stat-number">{performance['accuracy']}%</div>
</div>
""", unsafe_allow_html=True)
st.markdown("<br>", unsafe_allow_html=True)
# Feature Sections with Expanders
# 1. Upload & Read PDF
with st.expander("π **Upload & Read PDF** - Extract and structure your documents", expanded=st.session_state.active_section == "π Upload PDF"):
st.session_state.active_section = None # Reset after opening
col1, col2 = st.columns([2, 1])
with col1:
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"], key="pdf_uploader")
with col2:
st.markdown("<br>", unsafe_allow_html=True)
process_btn = st.button("π Process PDF", type="primary", use_container_width=True)
if uploaded_file is not None and process_btn:
temp_path = f"temp_{uploaded_file.name}"
with open(temp_path, "wb") as f:
f.write(uploaded_file.getbuffer())
with st.spinner("π Processing PDF with Reader Agent..."):
try:
result = st.session_state.reader_agent.process_pdf(temp_path, uploaded_file.name)
st.success(f"β
PDF processed successfully! Document ID: {result['document_id']}")
col1, col2 = st.columns(2)
with col1:
st.info(f"**Filename:** {result['filename']}\n\n**Text Length:** {result['text_length']:,} characters")
with col2:
if result['metadata']:
meta = result['metadata']
st.info(f"**Pages:** {meta.get('page_count', 'N/A')}\n\n**Author:** {meta.get('author', 'N/A')}")
if result.get('structured_content'):
st.markdown("---")
st.markdown("#### π Structured Content")
structured = result['structured_content']
if structured.get('structured_analysis'):
with st.container():
st.markdown("**Analysis:**")
st.write(structured['structured_analysis'])
if structured.get('topics'):
st.markdown("**Main Topics:**")
for topic in structured['topics']:
st.write(f"β’ {topic}")
st.session_state.current_document_id = result['document_id']
st.rerun()
except Exception as e:
st.error(f"β Error processing PDF: {str(e)}")
finally:
if os.path.exists(temp_path):
os.remove(temp_path)
# Document List
if documents:
st.markdown("#### π Your Documents")
for doc in documents[:5]:
with st.container():
col1, col2, col3 = st.columns([3, 1, 1])
with col1:
st.write(f"**{doc['filename']}**")
with col2:
st.caption(f"ID: {doc['id']}")
with col3:
if st.button(f"Select", key=f"select_doc_{doc['id']}"):
st.session_state.current_document_id = doc['id']
st.success(f"β
Selected: {doc['filename']}")
st.rerun()
# 2. Flashcards
with st.expander("π΄ **Flashcards** - Generate Q&A flashcards for active recall", expanded=st.session_state.active_section == "π΄ Flashcards"):
st.session_state.active_section = None
if not documents:
st.warning("β οΈ Please upload a PDF first!")
else:
doc_options = {f"{doc['filename']} (ID: {doc['id']})": doc['id'] for doc in documents}
selected_doc = st.selectbox("Select Document", list(doc_options.keys()), key="flashcard_doc")
document_id = doc_options[selected_doc]
col1, col2 = st.columns(2)
with col1:
num_flashcards = st.number_input("Number of Flashcards", min_value=1, max_value=50, value=10, key="num_fc")
with col2:
topic_filter = st.text_input("Topic Filter (optional)", key="fc_topic")
if st.button("β¨ Generate Flashcards", type="primary", key="gen_fc"):
with st.spinner("π΄ Generating flashcards..."):
try:
flashcards = st.session_state.flashcard_agent.generate_flashcards(
document_id, num_flashcards, topic_filter if topic_filter else None
)
st.success(f"β
Generated {len(flashcards)} flashcards!")
st.rerun()
except Exception as e:
st.error(f"β Error: {str(e)}")
# Display Flashcards
st.markdown("---")
st.markdown("#### π Your Flashcards")
flashcards_list = st.session_state.flashcard_agent.get_flashcards(document_id)
if flashcards_list:
study_mode = st.checkbox("π Study Mode (Show/Hide Answers)", key="study_mode")
for i, fc in enumerate(flashcards_list):
with st.container():
st.markdown(f"**Flashcard {i+1}**")
st.write(f"β **Q:** {fc['question']}")
if study_mode:
if st.button(f"Show Answer", key=f"show_fc_{fc['id']}"):
st.session_state[f"show_fc_{fc['id']}"] = True
if st.session_state.get(f"show_fc_{fc['id']}", False):
st.write(f"β
**A:** {fc['answer']}")
else:
st.write(f"β
**A:** {fc['answer']}")
if fc.get('topic'):
st.caption(f"π Topic: {fc['topic']} | Difficulty: {fc.get('difficulty', 'N/A')}")
st.markdown("---")
else:
st.info("π‘ No flashcards yet. Generate some to get started!")
# 3. Quizzes
with st.expander("π **Quizzes** - Test your knowledge with adaptive MCQs", expanded=st.session_state.active_section == "π Quizzes"):
st.session_state.active_section = None
if not documents:
st.warning("β οΈ Please upload a PDF first!")
else:
doc_options = {f"{doc['filename']} (ID: {doc['id']})": doc['id'] for doc in documents}
selected_doc = st.selectbox("Select Document", list(doc_options.keys()), key="quiz_doc")
document_id = doc_options[selected_doc]
col1, col2, col3 = st.columns(3)
with col1:
num_questions = st.number_input("Number of Questions", min_value=1, max_value=30, value=10, key="num_q")
with col2:
difficulty = st.selectbox("Difficulty", ["Mixed", "Easy", "Medium", "Hard"], key="quiz_diff")
with col3:
topic_filter = st.text_input("Topic Filter (optional)", key="quiz_topic")
if st.button("π― Generate Quiz", type="primary", key="gen_quiz"):
with st.spinner("π Generating quiz..."):
try:
diff = None if difficulty == "Mixed" else difficulty.lower()
quizzes = st.session_state.quiz_agent.generate_quiz(
document_id, num_questions, diff, topic_filter if topic_filter else None
)
st.success(f"β
Generated {len(quizzes)} questions!")
st.session_state.current_quiz = quizzes
st.session_state.quiz_answers = {}
st.rerun()
except Exception as e:
st.error(f"β Error: {str(e)}")
# Take Quiz
if st.session_state.get('current_quiz'):
st.markdown("---")
st.markdown("#### π Take Quiz")
quiz_to_take = st.session_state.current_quiz
for i, q in enumerate(quiz_to_take):
st.markdown(f"### Question {i+1}")
st.write(f"**{q['question']}**")
options = q['options']
selected = st.radio(
"Select your answer:",
options,
key=f"quiz_{q['id']}",
index=None
)
if selected:
selected_index = options.index(selected)
st.session_state.quiz_answers[q['id']] = selected_index
st.markdown("---")
if st.button("π€ Submit Quiz", type="primary", key="submit_quiz"):
score = 0
total = len(quiz_to_take)
for q in quiz_to_take:
user_answer = st.session_state.quiz_answers.get(q['id'])
if user_answer is not None:
is_correct = st.session_state.quiz_agent.submit_answer(q['id'], user_answer)
if is_correct:
score += 1
accuracy = (score/total*100) if total > 0 else 0
st.success(f"π Quiz Complete! Score: **{score}/{total}** ({accuracy:.1f}%)")
st.session_state.quiz_answers = {}
st.session_state.current_quiz = None
st.rerun()
# 4. Revision Planner
with st.expander("π
**Revision Planner** - Create personalized study schedules", expanded=st.session_state.active_section == "π
Planner"):
st.session_state.active_section = None
if not documents:
st.warning("β οΈ Please upload a PDF first!")
else:
doc_options = {f"{doc['filename']} (ID: {doc['id']})": doc['id'] for doc in documents}
selected_doc = st.selectbox("Select Document", list(doc_options.keys()), key="plan_doc")
document_id = doc_options[selected_doc]
col1, col2 = st.columns(2)
with col1:
days_until_exam = st.number_input("Days Until Exam", min_value=1, max_value=90, value=7, key="days_exam")
with col2:
hours_per_day = st.number_input("Hours Per Day", min_value=0.5, max_value=12.0, value=2.0, step=0.5, key="hours_day")
focus_topics = st.text_input("Focus Topics (comma-separated, optional)", key="focus_topics")
topics_list = [t.strip() for t in focus_topics.split(",") if t.strip()] if focus_topics else None
if st.button("π
Create Revision Plan", type="primary", key="create_plan"):
with st.spinner("π
Creating revision plan..."):
try:
plan = st.session_state.planner_agent.create_revision_plan(
document_id, days_until_exam, hours_per_day, topics_list
)
st.success("β
Revision plan created successfully!")
st.session_state.current_plan = plan
st.rerun()
except Exception as e:
st.error(f"β Error: {str(e)}")
# Display Plans
plans = st.session_state.planner_agent.get_revision_plans(document_id)
if plans:
st.markdown("---")
st.markdown("#### π
Your Revision Plans")
for plan in plans:
plan_data = plan['plan_data']
with st.expander(f"π
{plan_data.get('plan_name', 'Revision Plan')} - {plan['created_at']}"):
st.write(f"**Total Days:** {plan_data.get('total_days', 'N/A')} | **Hours/Day:** {plan_data.get('hours_per_day', 'N/A')}")
schedule = plan_data.get('schedule', [])
for day_plan in schedule:
st.markdown(f"**Day {day_plan.get('day', 'N/A')} - {day_plan.get('date', 'N/A')}**")
st.write(f"π Topics: {', '.join(day_plan.get('topics', []))}")
st.write(f"π {day_plan.get('notes', 'N/A')}")
activities = day_plan.get('activities', [])
if activities:
for activity in activities:
st.write(f" β’ {activity.get('type', 'N/A')}: {activity.get('description', 'N/A')} ({activity.get('duration_minutes', 0)} min)")
st.markdown("---")
tips = plan_data.get('tips', [])
if tips:
st.markdown("**π‘ Tips:**")
for tip in tips:
st.write(f" β’ {tip}")
# 5. Chat & Doubts
with st.expander("π¬ **Chat & Doubts** - Ask questions and get AI-powered answers", expanded=st.session_state.active_section == "π¬ Chat"):
st.session_state.active_section = None
if not documents:
st.warning("β οΈ Please upload a PDF first!")
else:
doc_options = {f"{doc['filename']} (ID: {doc['id']})": doc['id'] for doc in documents}
selected_doc = st.selectbox("Select Document", list(doc_options.keys()), key="chat_doc")
document_id = doc_options[selected_doc]
st.markdown("#### π¬ Ask a Question")
# Display chat history
for msg in st.session_state.chat_messages:
if msg["role"] == "user":
with st.chat_message("user"):
st.write(msg["content"])
else:
with st.chat_message("assistant"):
st.write(msg["content"])
question = st.chat_input("Ask a question about the document...", key="chat_input")
if question:
st.session_state.chat_messages.append({"role": "user", "content": question})
with st.spinner("π€ Thinking..."):
try:
result = st.session_state.chat_agent.answer_question(document_id, question)
answer = result["answer"]
st.session_state.chat_messages.append({"role": "assistant", "content": answer})
st.rerun()
except Exception as e:
st.error(f"β Error: {str(e)}")
st.markdown("---")
# Summarize Notes
st.markdown("#### π Summarize Notes")
col1, col2 = st.columns([3, 1])
with col1:
focus_topic = st.text_input("Focus Topic (optional)", key="summary_topic")
with col2:
st.markdown("<br>", unsafe_allow_html=True)
if st.button("π Generate Summary", type="primary", key="gen_summary"):
with st.spinner("π Generating summary..."):
try:
summary = st.session_state.chat_agent.summarize_notes(
document_id, focus_topic if focus_topic else None
)
st.markdown("#### Summary")
st.write(summary)
except Exception as e:
st.error(f"β Error: {str(e)}")
# 6. Performance & Summary
with st.expander("π **Performance & Summary** - Track your learning progress", expanded=st.session_state.active_section == "π Performance"):
st.session_state.active_section = None
if not documents:
st.warning("β οΈ Please upload a PDF first!")
else:
doc_options = {f"{doc['filename']} (ID: {doc['id']})": doc['id'] for doc in documents}
selected_doc = st.selectbox("Select Document", list(doc_options.keys()), key="perf_doc")
document_id = doc_options[selected_doc]
st.markdown("#### π Performance Statistics")
performance = st.session_state.memory.get_performance_stats(document_id)
flashcards = st.session_state.memory.get_flashcards(document_id)
quizzes = st.session_state.memory.get_quizzes(document_id)
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Total Quiz Attempts", performance['total_attempts'])
with col2:
st.metric("Correct Answers", performance['correct_attempts'])
with col3:
st.metric("Accuracy", f"{performance['accuracy']}%")
with col2:
st.metric("Flashcards", len(flashcards))
st.markdown("---")
st.markdown("#### π‘ Recommendations")
if performance['total_attempts'] == 0:
st.info("π Start taking quizzes to track your performance!")
elif performance['accuracy'] < 50:
st.warning("π Your accuracy is below 50%. Consider reviewing the material and creating more flashcards.")
elif performance['accuracy'] < 70:
st.info("π Your accuracy is good! Keep practicing to improve further.")
else:
st.success("π Excellent performance! You're mastering the material!")
# Footer
st.markdown("---")
st.markdown("""
<div style="text-align: center; color: #666; padding: 2rem;">
<p>Built with β€οΈ using Streamlit & Groq API</p>
<p>VectorTutor - 5-Agent AI Learning System</p>
</div>
""", unsafe_allow_html=True)