-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
1315 lines (1121 loc) · 49.6 KB
/
Copy pathapp.py
File metadata and controls
1315 lines (1121 loc) · 49.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
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Streamlit Web Application for Learning Agent System
A professional, modern web interface for the autonomous learning agent.
Provides interactive learning experiences with checkpoint progression,
dynamic materials, and adaptive teaching.
"""
import streamlit as st
import asyncio
import sys
import time
import re
import os
import random
from pathlib import Path
from typing import List, Dict, Optional
import logging
# Bootstrap environment from Streamlit secrets before importing app modules
def _load_env_from_streamlit_secrets():
try:
secrets = st.secrets
except Exception:
return
keys = [
"OLLAMA_BASE_URL",
"OLLAMA_MODEL",
"EMBEDDING_MODEL",
"THRESHOLD",
"HF_TOKEN",
"LANGSMITH_TRACING_ENABLED",
"LANGCHAIN_API_KEY",
"LANGCHAIN_PROJECT",
"LANGCHAIN_ENDPOINT",
]
# Support both top-level keys and [env] table in secrets.toml
env_table = {}
try:
env_table = secrets.get("env", {})
except Exception:
env_table = {}
for key in keys:
if key in os.environ and os.environ.get(key):
continue
if key in secrets:
os.environ[key] = str(secrets[key])
elif isinstance(env_table, dict) and key in env_table:
os.environ[key] = str(env_table[key])
_load_env_from_streamlit_secrets()
# Add project root to path
sys.path.insert(0, str(Path(__file__).parent))
from src.sample_data import create_learning_paths
from src.custom_topics import load_custom_topics, create_topic_wizard, add_custom_topic
from src.multi_checkpoint import run_multi_checkpoint_session
from src.models import LearningPath, Checkpoint
from src.workflow import create_unified_workflow, create_question_generation_workflow
from src.models import LearningAgentState
from src.user_interaction import collect_user_answers, display_score_feedback
from src.file_upload import get_upload_handler
from src.langsmith_config import langsmith_config
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Page configuration
st.set_page_config(
page_title="Learning Agent System",
page_icon="📚",
layout="wide",
initial_sidebar_state="collapsed"
)
# Custom CSS theme: modern "Command Center" look and feel
st.markdown("""
<style>
:root {
--bg-main: #f7f3eb;
--bg-surface: #fffdf8;
--text-primary: #121212;
--text-muted: #3f3f46;
--ink: #111111;
--mint: #77f5cf;
--yellow: #ffe37a;
--blue: #8ec5ff;
--pink: #ff9fbe;
--white: #ffffff;
--shadow: 6px 6px 0 #111111;
--radius: 8px;
}
.stApp {
background: var(--bg-main);
color: var(--text-primary);
}
.main {
padding-top: 0.4rem;
}
.shell {
background: var(--white);
border: 3px solid var(--ink);
border-radius: var(--radius);
box-shadow: var(--shadow);
padding: 1rem;
margin-bottom: 1rem;
}
.hero-title {
font-size: 1.95rem;
font-weight: 800;
letter-spacing: 0;
margin: 0;
}
.hero-subtitle {
font-size: 0.98rem;
color: var(--text-muted);
margin: 0.25rem 0 0 0;
}
.stage-chip {
display: inline-block;
padding: 0.3rem 0.55rem;
border: 2px solid var(--ink);
border-radius: 6px;
font-size: 0.78rem;
font-weight: 700;
background: var(--yellow);
margin-top: 0.6rem;
}
.card {
padding: 0.95rem;
border-radius: var(--radius);
background: var(--white);
border: 3px solid var(--ink);
box-shadow: var(--shadow);
margin-bottom: 0.8rem;
}
.kicker {
font-size: 0.75rem;
font-weight: 800;
text-transform: uppercase;
color: var(--text-muted);
margin-bottom: 0.25rem;
}
.question-card {
background: var(--white);
border: 3px solid var(--ink);
border-radius: var(--radius);
padding: 0.85rem;
margin-bottom: 0.65rem;
line-height: 1.4;
box-shadow: 4px 4px 0 #111111;
}
.q-badge {
display: inline-block;
font-size: 0.72rem;
padding: 0.18rem 0.45rem;
border-radius: 6px;
border: 2px solid var(--ink);
background: var(--mint);
font-weight: 700;
margin-bottom: 0.5rem;
}
.q-badge.long {
background: var(--pink);
}
.q-badge.short {
background: var(--blue);
}
.nav-box {
background: var(--white);
border: 3px solid var(--ink);
border-radius: var(--radius);
box-shadow: var(--shadow);
padding: 0.85rem;
margin-bottom: 1rem;
}
.nav-step {
display: inline-block;
width: 100%;
border: 2px solid var(--ink);
border-radius: 6px;
padding: 0.4rem 0.5rem;
margin-bottom: 0.4rem;
font-size: 0.8rem;
font-weight: 700;
background: #fff;
}
.nav-step.active {
background: var(--yellow);
}
.nav-step.done {
background: var(--mint);
}
.success-box, .warning-box, .error-box {
padding: 0.8rem;
border-radius: var(--radius);
border: 3px solid var(--ink);
box-shadow: 4px 4px 0 #111111;
margin-bottom: 0.6rem;
}
.success-box { background: var(--mint); color: #10241d; }
.warning-box { background: var(--yellow); color: #2a220a; }
.error-box { background: var(--pink); color: #2c0f19; }
.stButton > button {
width: 100%;
border-radius: 8px;
border: 3px solid var(--ink);
background: var(--yellow);
color: var(--ink);
font-weight: 800;
box-shadow: 4px 4px 0 #111111;
}
.stButton > button:hover {
transform: translate(-1px, -1px);
box-shadow: 5px 5px 0 #111111;
}
.stProgress > div > div > div > div {
background: #111;
}
.stTextArea textarea, .stTextInput input {
background: #fff;
color: #111;
border: 3px solid var(--ink);
border-radius: 8px;
}
.stAlert {
border-radius: 8px;
border: 3px solid var(--ink);
}
/* Hide streamlit branding */
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
""", unsafe_allow_html=True)
# Initialize session state
def init_session_state():
"""Initialize session state variables."""
if 'stage' not in st.session_state:
st.session_state.stage = 'select_path' # select_path, upload_files, learning, results
if 'selected_path' not in st.session_state:
st.session_state.selected_path = None
if 'current_checkpoint_index' not in st.session_state:
st.session_state.current_checkpoint_index = 0
if 'uploaded_files' not in st.session_state:
st.session_state.uploaded_files = []
if 'learning_state' not in st.session_state:
st.session_state.learning_state = None
if 'questions' not in st.session_state:
st.session_state.questions = []
if 'answers' not in st.session_state:
st.session_state.answers = {}
if 'show_create_topic' not in st.session_state:
st.session_state.show_create_topic = False
if 'completed_checkpoints' not in st.session_state:
st.session_state.completed_checkpoints = []
def render_header():
"""Render application header."""
stage = st.session_state.get("stage", "select_path")
stage_labels = {
"select_path": "Select Path",
"upload_files": "Upload Materials",
"study": "Study",
"learning": "Assessment",
"results": "Results"
}
st.markdown('<div class="shell">', unsafe_allow_html=True)
st.markdown('<p class="hero-title">Learning Agent Studio</p>', unsafe_allow_html=True)
st.markdown('<p class="hero-subtitle">Neo-brutalist adaptive learning workspace: clear goals, strong feedback, no clutter.</p>', unsafe_allow_html=True)
st.markdown(f'<span class="stage-chip">Current Stage: {stage_labels.get(stage, "Unknown")}</span>', unsafe_allow_html=True)
st.markdown('</div>', unsafe_allow_html=True)
# Check LLM provider status
llm_provider_pref = os.getenv("LLM_PROVIDER", "auto").lower().strip()
hf_token_set = bool(os.getenv("HF_TOKEN", ""))
hf_model = os.getenv("HF_MODEL", "HuggingFaceH4/zephyr-7b-beta")
ollama_base = os.getenv("OLLAMA_BASE_URL", "http://localhost:11434")
ollama_up = False
# Check Ollama status
try:
import httpx
response = httpx.get(f"{ollama_base}/api/tags", timeout=2.0)
if response.status_code == 200:
ollama_up = True
st.success("🟢 Ollama is running", icon="✅")
else:
st.warning("🟡 Ollama connection issue", icon="⚠️")
except:
st.error("🔴 Ollama is not running - AI features will be limited", icon="❌")
with st.expander("How to start Ollama"):
st.code("ollama serve", language="bash")
st.markdown("Or ensure Ollama is running in the background.")
# Explicit provider notification for users
if llm_provider_pref == "huggingface":
st.info(f"🤖 Active LLM Provider: Hugging Face (`{hf_model}`)")
elif llm_provider_pref == "ollama":
st.info("🤖 Active LLM Provider: Ollama")
else:
# auto mode
if ollama_up:
st.info("🤖 Active LLM Provider: Ollama (auto mode)")
elif hf_token_set:
st.warning(f"🤖 Ollama unavailable. Falling back to Hugging Face (`{hf_model}`) in auto mode.")
else:
st.error("🤖 No usable LLM backend: Ollama unavailable and HF_TOKEN missing.")
with st.expander("LangSmith Status", expanded=False):
tracing_env = os.getenv("LANGSMITH_TRACING_ENABLED", "false").lower() == "true"
api_key_set = bool(os.getenv("LANGCHAIN_API_KEY"))
project = os.getenv("LANGCHAIN_PROJECT", "Learning-Agent-System")
endpoint = os.getenv("LANGCHAIN_ENDPOINT", "https://api.smith.langchain.com")
runtime_enabled = bool(getattr(langsmith_config, "enabled", False))
st.write(f"- Env tracing toggle: {'ON' if tracing_env else 'OFF'}")
st.write(f"- API key present: {'YES' if api_key_set else 'NO'}")
st.write(f"- Project: `{project}`")
st.write(f"- Endpoint: `{endpoint}`")
st.write(f"- Runtime tracing active: {'YES' if runtime_enabled else 'NO'}")
if not tracing_env:
st.warning("Set `LANGSMITH_TRACING_ENABLED=true` in `.env`.")
elif not api_key_set:
st.warning("Set `LANGCHAIN_API_KEY` in `.env`.")
elif not runtime_enabled:
st.warning("Tracing is configured but inactive at runtime; restart app and recheck credentials/project.")
else:
st.success("LangSmith tracing is active for this app session.")
st.markdown("")
def _question_type_label(question: Dict) -> str:
q_type = str(question.get('type', '')).lower()
if q_type in ('mcq', 'multiple_choice'):
return "Multiple Choice"
if q_type in ('short_answer', 'short'):
return "Short Answer"
if q_type in ('long_answer', 'long'):
return "Long Answer"
return "Open Answer"
def ensure_question_distribution(questions: List[Dict], checkpoint_requirements: Optional[List[str]] = None) -> List[Dict]:
"""UI-level hard guard: always return 5 MCQ, 3 short, 2 long questions."""
checkpoint_requirements = checkpoint_requirements or []
by_type = {"multiple_choice": [], "short_answer": [], "long_answer": []}
for q in questions or []:
q_type = str(q.get("type", "")).lower()
if q_type in ("mcq", "multiple_choice"):
q["type"] = "multiple_choice"
by_type["multiple_choice"].append(q)
elif q_type in ("long", "long_answer"):
q["type"] = "long_answer"
by_type["long_answer"].append(q)
else:
q["type"] = "short_answer"
by_type["short_answer"].append(q)
def mcq_stub(i: int) -> Dict:
req = checkpoint_requirements[min(i, len(checkpoint_requirements)-1)] if checkpoint_requirements else "the checkpoint topic"
return {
"question": f"Which choice best aligns with the objective: '{req}'?",
"type": "multiple_choice",
"options": [
"A) It captures the core concept with practical relevance.",
"B) It avoids key concepts and remains vague.",
"C) It contradicts the objective directly.",
"D) It is unrelated to the checkpoint topic."
],
"correct_answer": "A",
"expected_concepts": checkpoint_requirements[:2]
}
def short_stub(i: int) -> Dict:
req = checkpoint_requirements[min(i, len(checkpoint_requirements)-1)] if checkpoint_requirements else "the checkpoint topic"
return {
"question": f"In 2-4 sentences, explain the key idea behind '{req}'.",
"type": "short_answer",
"expected_concepts": checkpoint_requirements[:3]
}
def long_stub(i: int) -> Dict:
req = checkpoint_requirements[min(i, len(checkpoint_requirements)-1)] if checkpoint_requirements else "the checkpoint topic"
return {
"question": f"Provide a detailed analysis of '{req}', including reasoning, examples, and limitations.",
"type": "long_answer",
"expected_concepts": checkpoint_requirements
}
while len(by_type["multiple_choice"]) < 5:
by_type["multiple_choice"].append(mcq_stub(len(by_type["multiple_choice"])))
while len(by_type["short_answer"]) < 3:
by_type["short_answer"].append(short_stub(len(by_type["short_answer"])))
while len(by_type["long_answer"]) < 2:
by_type["long_answer"].append(long_stub(len(by_type["long_answer"])))
mcq_selected = random.sample(by_type["multiple_choice"], 5) if len(by_type["multiple_choice"]) > 5 else by_type["multiple_choice"][:5]
short_selected = random.sample(by_type["short_answer"], 3) if len(by_type["short_answer"]) > 3 else by_type["short_answer"][:3]
long_selected = random.sample(by_type["long_answer"], 2) if len(by_type["long_answer"]) > 2 else by_type["long_answer"][:2]
selected = mcq_selected + short_selected + long_selected
random.shuffle(selected)
for idx, q in enumerate(selected, 1):
q["question_id"] = f"q_{idx}"
return selected
def render_stage_rail():
stage = st.session_state.get("stage", "select_path")
order = ["select_path", "upload_files", "study", "learning", "results"]
labels = {
"select_path": "1. Path",
"upload_files": "2. Materials",
"study": "3. Study",
"learning": "4. Assessment",
"results": "5. Results"
}
current_idx = order.index(stage) if stage in order else 0
st.markdown('<div class="nav-box"><div class="kicker">Workflow</div>', unsafe_allow_html=True)
for idx, key in enumerate(order):
cls = "nav-step"
if idx < current_idx:
cls += " done"
elif idx == current_idx:
cls += " active"
st.markdown(f'<div class="{cls}">{labels[key]}</div>', unsafe_allow_html=True)
st.markdown('</div>', unsafe_allow_html=True)
def show_loading_sequence(status, messages: List[str], delay: float = 0.18):
"""Show rotating status messages to keep users engaged during long tasks."""
for msg in messages:
status.write(msg)
time.sleep(delay)
def format_study_markdown(text: str) -> str:
"""Normalize generated study text into readable Markdown blocks."""
cleaned = (text or "").strip()
if not cleaned:
return ""
cleaned = cleaned.replace("\r\n", "\n")
cleaned = re.sub(r"\n{3,}", "\n\n", cleaned)
cleaned = re.sub(r"(?m)^(\d+)\.\s*([A-Z][^\n:]{2,40})$", r"## \1. \2", cleaned)
cleaned = re.sub(r"(?m)^([A-Z][A-Z\s]{4,40}):\s*$", lambda m: f"## {m.group(1).title()}", cleaned)
paragraphs = [p.strip() for p in cleaned.split("\n\n") if p.strip()]
return "\n\n".join(paragraphs)
def is_study_content_sufficient(summary: str, materials: List[Dict]) -> bool:
"""Check whether we have enough high-quality content for pre-assessment study."""
summary_words = len((summary or "").split())
material_chars = sum(len((m.get("content") or "")) for m in materials)
return summary_words >= 220 or material_chars >= 1800
def build_study_pack_markdown(checkpoint: Dict, summary: str, materials: List[Dict]) -> str:
"""Build robust study material markdown from summary + collected sources."""
title = checkpoint.get("title", "This checkpoint")
reqs = checkpoint.get("requirements", [])
req_section = "\n".join([f"- {r}" for r in reqs]) if reqs else "- Understand the main concepts\n- Apply them correctly"
curated_points = []
for material in materials[:6]:
content = (material.get("content") or "").strip()
if not content:
continue
snippet = re.sub(r"\s+", " ", content)[:320].strip()
if len(snippet) > 60:
curated_points.append(f"- **{material.get('title','Source')}**: {snippet}...")
if not curated_points:
curated_points.append("- Review the generated explanation and checkpoint objectives carefully before proceeding.")
summary_md = format_study_markdown(summary) if summary else ""
if not summary_md or len(summary_md.split()) < 120:
summary_md = (
f"## Core Concepts for {title}\n\n"
"Study this section before attempting the assessment:\n\n"
f"{req_section}\n\n"
"### Practical Understanding Notes\n\n"
+ "\n".join(curated_points)
)
return (
f"## Checkpoint Focus: {title}\n\n"
"### Learning Objectives\n"
f"{req_section}\n\n"
"### Comprehensive Explanation\n\n"
f"{summary_md}\n\n"
"### Source-Based Key Notes\n\n"
+ "\n".join(curated_points)
)
def render_path_selection():
"""Render learning path selection interface."""
st.markdown('<div class="card"><div class="kicker">Setup</div><h3 style="margin:0;">Select Learning Path</h3></div>', unsafe_allow_html=True)
# Load paths
default_paths = create_learning_paths()
custom_paths = load_custom_topics()
all_paths = default_paths + custom_paths
# Display paths
col1, col2 = st.columns([3, 1])
with col1:
st.subheader("Available Learning Paths")
for i, path in enumerate(all_paths):
with st.container():
st.markdown(f"""
<div class="card">
<h4 style="margin:0 0 6px 0;">{path['title']}</h4>
<p style="margin:0 0 8px 0; color:#9cb0c3;">{path['description']}</p>
<p style="margin:0; color:#cdd9e5;"><strong>Checkpoints:</strong> {len(path['checkpoints'])}</p>
</div>
""", unsafe_allow_html=True)
if st.button(f"Select", key=f"select_{i}"):
st.session_state.selected_path = path
st.session_state.stage = 'upload_files'
st.rerun()
with col2:
st.subheader("Actions")
if st.button("Create Custom Topic", use_container_width=True):
st.session_state.show_create_topic = True
st.rerun()
# Custom topic creation form
if st.session_state.show_create_topic:
render_custom_topic_form()
def render_custom_topic_form():
"""Render custom topic creation form."""
st.markdown("---")
st.subheader("Create Custom Learning Topic")
st.info("Enter a topic name (e.g., 'Python Basics', 'Machine Learning') and the system will handle everything else.")
with st.form("custom_topic_form"):
topic_name = st.text_input(
"Topic Name",
placeholder="e.g., Python Basics, Machine Learning, Web Development",
help="Enter any topic you want to learn about"
)
col1, col2 = st.columns(2)
with col1:
submitted = st.form_submit_button("Start Learning", use_container_width=True)
with col2:
cancelled = st.form_submit_button("Cancel", use_container_width=True)
if submitted and topic_name and topic_name.strip():
# Auto-generate topic ID from name
topic_id = topic_name.lower().replace(" ", "_").replace("-", "_")
# Remove special characters
topic_id = ''.join(c for c in topic_id if c.isalnum() or c == '_')
# Auto-generate description
description = f"Learn the fundamentals and key concepts of {topic_name}"
# Create a single checkpoint for the custom topic
# The system will dynamically generate materials
new_topic = {
"id": topic_id,
"title": topic_name.strip(),
"description": description,
"checkpoints": [
{
"id": f"{topic_id}_main",
"title": topic_name.strip(),
"description": description,
"requirements": [
f"Understand the fundamentals of {topic_name}",
f"Apply key concepts from {topic_name}",
f"Demonstrate knowledge of {topic_name}"
]
}
]
}
if add_custom_topic(new_topic):
st.success(f"Topic '{topic_name}' created! Starting learning session...")
st.session_state.show_create_topic = False
st.session_state.selected_path = new_topic
st.session_state.stage = 'upload_files'
st.rerun()
else:
st.error("This topic already exists. Please choose a different name.")
if cancelled:
st.session_state.show_create_topic = False
st.rerun()
def render_file_upload():
"""Render file upload interface."""
st.markdown('<div class="card"><div class="kicker">Setup</div><h3 style="margin:0;">Upload Learning Materials (Optional)</h3></div>', unsafe_allow_html=True)
path = st.session_state.selected_path
st.info(f"Selected: {path['title']}")
st.markdown("""
You can upload your own learning materials in the following formats:
- PDF documents
- Word documents (DOCX)
- Markdown files (MD)
- Plain text files (TXT)
If you don't upload materials, the system will generate them dynamically using AI and web search.
""")
uploaded_files = st.file_uploader(
"Upload files",
type=['pdf', 'docx', 'md', 'txt'],
accept_multiple_files=True,
help="Maximum 10MB per file"
)
if uploaded_files:
st.session_state.uploaded_files = uploaded_files
st.success(f"Uploaded {len(uploaded_files)} file(s)")
for file in uploaded_files:
st.text(f"- {file.name} ({file.size / 1024:.1f} KB)")
col1, col2 = st.columns(2)
with col1:
if st.button("Continue with Uploaded Files", use_container_width=True, disabled=not uploaded_files):
st.session_state.stage = 'learning'
st.rerun()
with col2:
if st.button("Skip and Generate Materials", use_container_width=True):
st.session_state.uploaded_files = []
st.session_state.stage = 'learning'
st.rerun()
def render_study_materials():
"""Display learning materials for study before assessment."""
st.markdown('<div class="card"><div class="kicker">Study Zone</div><h3 style="margin:0;">Study Materials</h3></div>', unsafe_allow_html=True)
st.info("Review the learning materials below before starting the assessment.")
state = st.session_state.learning_state
if not state:
st.error("No learning materials available.")
return
# Display learning content as one continuous explanation
summary = state.get('summary', '')
materials = state.get('collected_materials', [])
checkpoint = state.get('current_checkpoint', {})
has_enough_content = is_study_content_sufficient(summary, materials)
study_pack = build_study_pack_markdown(checkpoint, summary, materials)
if study_pack:
st.markdown("### 📚 Study Material")
st.markdown("*Read through this comprehensive explanation carefully to prepare for the assessment.*")
st.markdown("")
formatted_md = format_study_markdown(study_pack)
st.markdown('<div class="card">', unsafe_allow_html=True)
st.markdown(formatted_md)
st.markdown('</div>', unsafe_allow_html=True)
# Show word count for reference
word_count = len(study_pack.split())
st.caption(f"📊 Content length: {word_count} words")
if not has_enough_content:
st.warning("The generated summary was too brief, so this section was expanded using collected source materials.")
else:
st.warning("No learning content available.")
# Extract and display resource links
resource_links = []
for material in materials:
url = material.get('url') or material.get('link') or material.get('source_url')
title = material.get('title', 'Resource')
source = material.get('source', 'Unknown')
if url and url.startswith('http'):
resource_links.append({
'title': title,
'url': url,
'source': source
})
# Display resource links section
if resource_links:
st.markdown("---")
st.markdown("### 🔗 Additional Resources")
st.markdown(f"Explore **{len(resource_links)} external resources** for deeper learning:")
st.markdown("")
for i, resource in enumerate(resource_links, 1):
st.markdown(f"""
<div style="background-color: #e8f4f8; padding: 15px; border-radius: 6px; margin-bottom: 10px; border-left: 3px solid #2196F3;">
<div style="font-size: 15px; font-weight: 600; color: #1a1a1a; margin-bottom: 6px;">
{i}. {resource['title']}
</div>
<div style="margin-bottom: 4px;">
<a href="{resource['url']}" target="_blank" style="color: #1565c0; text-decoration: none; font-size: 13px;">
🔗 {resource['url']}
</a>
</div>
<div style="font-size: 11px; color: #666;">
<em>Source: {resource['source']}</em>
</div>
</div>
""", unsafe_allow_html=True)
# Button to proceed to assessment
st.markdown("")
st.markdown("---")
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
if st.button("✅ Start Assessment", use_container_width=True, type="primary"):
st.session_state.stage = 'learning'
st.rerun()
def render_learning_session():
"""Render the learning session with materials, questions, and assessment."""
path = st.session_state.selected_path
checkpoint_idx = st.session_state.current_checkpoint_index
if checkpoint_idx >= len(path['checkpoints']):
render_completion()
return
checkpoint = path['checkpoints'][checkpoint_idx]
# Progress bar
if checkpoint_idx >= len(path['checkpoints']):
render_completion()
return
checkpoint = path['checkpoints'][checkpoint_idx]
# Progress bar
progress = checkpoint_idx / len(path['checkpoints'])
st.progress(progress, text=f"Progress: {checkpoint_idx}/{len(path['checkpoints'])} checkpoints completed")
st.markdown(
f'<div class="card"><div class="kicker">Checkpoint</div><h3 style="margin:0;">{checkpoint["title"]}</h3><p style="margin:8px 0 0 0;color:#3f3f46;">{checkpoint["description"]}</p></div>',
unsafe_allow_html=True
)
# Learning objectives
with st.expander("Learning Objectives", expanded=False):
for req in checkpoint['requirements']:
st.markdown(f"- {req}")
# Run workflow in background
if st.session_state.learning_state is None:
with st.status("Preparing learning materials...", expanded=True) as prep_status:
show_loading_sequence(prep_status, [
"Warming up retrieval + reasoning pipelines...",
"Aligning checkpoint objectives with source context...",
"Drafting an assessment-focused study brief..."
])
# Process uploaded files
uploaded_paths = None
if st.session_state.uploaded_files:
upload_handler = get_upload_handler()
temp_paths = []
prep_status.write("Processing your uploaded files...")
for uploaded_file in st.session_state.uploaded_files:
file_content = uploaded_file.read()
result = upload_handler.handle_upload(
file_content=file_content,
file_name=uploaded_file.name
)
if result['success']:
temp_paths.append(result['file_path'])
uploaded_paths = temp_paths if temp_paths else None
# Initialize state
initial_state: LearningAgentState = {
"learning_path": path,
"current_checkpoint_index": checkpoint_idx,
"completed_checkpoints": st.session_state.completed_checkpoints,
"total_checkpoints": len(path['checkpoints']),
"has_next_checkpoint": checkpoint_idx < len(path['checkpoints']) - 1,
"learning_path_completed": False,
"current_checkpoint": checkpoint,
"collected_materials": [],
"summary": "",
"milestone1_score": 0.0,
"processed_context": [],
"generated_questions": [],
"verification_results": [],
"score_percentage": 0.0,
"meets_threshold": False,
"feynman_retry_count": 0,
"feynman_retry_requested": False,
"feynman_explanations": [],
"user_uploaded_notes_path": uploaded_paths,
"materials_validation": None,
"workflow_step": "initialized",
"workflow_history": [],
"errors": [],
"user_mode": "streamlit" # Flag to skip CLI input collection
}
# Run workflow up to question generation only (Streamlit mode)
# This stops before verify_understanding so UI can collect answers
workflow = create_question_generation_workflow()
try:
show_loading_sequence(prep_status, [
"Collecting materials from trusted sources...",
"Distilling context into concept chunks...",
"Generating checkpoint question set..."
])
result = asyncio.run(workflow.ainvoke(initial_state))
st.session_state.learning_state = result
st.session_state.questions = ensure_question_distribution(
result.get('generated_questions', []),
checkpoint.get("requirements", [])
)
if st.session_state.questions:
prep_status.write(f"Generated {len(st.session_state.questions)} questions.")
st.session_state.study_materials_ready = True
prep_status.update(label="Study materials ready!", state="complete")
# Redirect to study stage to show materials before assessment
st.session_state.stage = 'study'
else:
prep_status.write("No questions generated.")
prep_status.update(label="Error", state="error")
except Exception as e:
st.error(f"Error running workflow: {e}")
logger.exception("Workflow error")
return
# After processing, redirect to study stage
if st.session_state.study_materials_ready:
st.rerun()
# Display questions only if user has reviewed materials (coming from study stage)
if st.session_state.questions and not st.session_state.answers:
st.markdown("---")
render_questions()
elif st.session_state.answers:
st.info("Answers submitted. Evaluating...")
else:
st.warning("No questions generated. Please try again or contact support.")
def render_questions():
"""Render interactive questions."""
st.markdown('<div class="card"><div class="kicker">Assessment</div><h3 style="margin:0;">Checkpoint Questions</h3></div>', unsafe_allow_html=True)
question_count = len(st.session_state.questions)
st.markdown(
'<div class="card" style="background:#ffe37a;"><strong>Question Mix:</strong> 5 MCQ · 3 Short Answer · 2 Long Answer</div>',
unsafe_allow_html=True
)
st.info(f"Answer all {question_count} questions. Passing threshold remains 70%.")
questions = st.session_state.questions
with st.form("questions_form"):
answers = {}
for i, question in enumerate(questions):
# Display question with clear formatting
st.markdown(f"#### Question {i+1} of {len(questions)}")
q_label = _question_type_label(question)
badge_cls = "q-badge"
if q_label == "Long Answer":
badge_cls += " long"
elif q_label == "Short Answer":
badge_cls += " short"
st.markdown(f'<span class="{badge_cls}">{q_label}</span>', unsafe_allow_html=True)
# Show question text in a box
st.markdown(f"""
<div class="question-card">
{question['question']}
</div>
""", unsafe_allow_html=True)
q_type = str(question.get('type', '')).lower()
is_mcq = q_type in ('mcq', 'multiple_choice')
if is_mcq and question.get('options'):
# Multiple choice
st.markdown("**Select the correct answer:**")
options = question['options']
answer = st.radio(
"Options:",
options,
key=f"q_{i}",
index=None,
label_visibility="collapsed"
)
answers[i] = answer
else:
# Open-ended
st.markdown("**Your answer:**")
q_height = 120
if q_type in ('long', 'long_answer'):
q_height = 180
answer = st.text_area(
"Type your answer here:",
key=f"q_{i}",
height=q_height,
label_visibility="collapsed",
placeholder="Enter your detailed answer here..."
)
answers[i] = answer
st.markdown("---")
submitted = st.form_submit_button("Submit Answers", use_container_width=True)
if submitted:
missing = []
for idx, question in enumerate(questions):
value = answers.get(idx)
q_type = str(question.get('type', '')).lower()
is_mcq = q_type in ('mcq', 'multiple_choice')
if is_mcq and value is None:
missing.append(idx + 1)
if (not is_mcq) and (not value or not str(value).strip()):
missing.append(idx + 1)
if missing:
st.error(f"Please complete all questions before submitting. Missing: {', '.join(map(str, missing))}")
else:
st.session_state.answers = answers
evaluate_answers()
st.rerun()
def evaluate_answers():
"""Evaluate user answers and update state."""
from src.llm_service import LLMService
# Check Ollama connection first
try:
import httpx
response = httpx.get("http://localhost:11434/api/tags", timeout=2.0)
ollama_available = response.status_code == 200
except:
ollama_available = False
if not ollama_available:
st.error("⚠️ Cannot connect to Ollama")
st.warning("""
**Ollama is not running.** Please start Ollama to enable AI evaluation.
To start Ollama:
1. Open a terminal
2. Run: `ollama serve`
Or ensure Ollama is running in the background.
""")