ankban commited on
Commit
d855e37
Β·
verified Β·
1 Parent(s): ed2dde1

Update file_study_guide_panel.py

Browse files
Files changed (1) hide show
  1. file_study_guide_panel.py +52 -40
file_study_guide_panel.py CHANGED
@@ -11,6 +11,22 @@ def file_study_guide_dashboard(nickname_input):
11
  file_text_state = gr.State("")
12
  guide_state = gr.State([])
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def generate_study_guide(file):
15
  if file is None:
16
  return [gr.Markdown("❌ No file uploaded.")], "", []
@@ -46,53 +62,48 @@ Text:
46
  response = call_llm(prompt)
47
  try:
48
  guide = json.loads(response)
49
- return render_sections(guide), text, guide
 
50
  except:
51
- return [gr.Markdown("❌ Failed to parse structured study guide.")], text, []
 
52
 
53
  def render_sections(guide_data):
54
- section_blocks = []
55
  for i, sec in enumerate(guide_data):
56
- section_blocks.append(render_section(i + 1, sec))
57
- return section_blocks
58
 
59
  def render_section(index, sec):
60
- quiz_area = gr.Column()
61
- nav_buttons = gr.Row()
62
- section_ui = gr.Column(
63
- children=[
64
- gr.Markdown(f"## πŸ“˜ Section {index}: {sec['section_title']}"),
65
- gr.Markdown(f"**Overview:** {sec['overview']}"),
66
- gr.Markdown(f"**Explanation:**\n\n{sec['explanation']}"),
67
- gr.Markdown("### 🧠 Quiz"),
68
- quiz_area,
69
- nav_buttons,
70
- gr.Textbox(label="Answer Feedback", interactive=False, elem_id=f"feedback_{index}"),
71
- gr.Textbox(label="Score", interactive=False, value="Score: 0/5", elem_id=f"score_{index}")
72
- ]
73
- )
74
 
75
- def make_question_ui(q_idx):
76
- q = sec['quiz'][q_idx]
77
- question_box = gr.Markdown(f"**Q{q_idx+1}.** {q['question']}")
78
- option_radio = gr.Radio(q['options'], label="Choose an answer")
79
 
80
- def grade(choice):
81
- correct = q['answer']
82
- return "βœ… Correct!" if choice == correct else f"❌ Correct: {correct}"
83
 
84
- option_radio.change(fn=grade, inputs=[option_radio], outputs=[f"feedback_{index}"])
85
- return [question_box, option_radio]
86
 
87
- # Initialize quiz with question 1
88
- quiz_area.children = make_question_ui(0)
89
 
90
- for i in range(5):
91
- q_button = gr.Button(str(i + 1), size="sm")
92
- q_button.click(fn=lambda idx=i: make_question_ui(idx), inputs=[], outputs=quiz_area)
93
- nav_buttons.children.append(q_button)
94
 
95
- return section_ui
96
 
97
  def save_to_user(nickname, filename, guide):
98
  if not guide or not filename:
@@ -107,12 +118,13 @@ Text:
107
  guides = fetch_study_guides(user)
108
  for g in guides:
109
  if label.startswith(g.filename):
110
- return render_sections(json.loads(g.guide))
111
- return [gr.Markdown("❌ Guide not found.")]
 
112
 
113
- file_input.change(fn=generate_study_guide, inputs=[file_input], outputs=[section_output, file_text_state, guide_state])
114
  save_btn.click(fn=save_to_user, inputs=[nickname_input, file_input, guide_state], outputs=[])
115
  nickname_input.change(fn=load_user_guides, inputs=[nickname_input], outputs=[guide_dropdown])
116
- guide_dropdown.change(fn=show_saved_guide, inputs=[nickname_input, guide_dropdown], outputs=[section_output])
117
 
118
- return panel
 
11
  file_text_state = gr.State("")
12
  guide_state = gr.State([])
13
 
14
+ def sample_section():
15
+ return render_sections([{
16
+ "section_title": "Sample Section",
17
+ "overview": "This is a short summary of the topic.",
18
+ "explanation": "This is a detailed explanation to show how learning content will look.",
19
+ "quiz": [
20
+ {"question": "What is 2+2?", "options": ["1", "2", "3", "4"], "answer": "4"},
21
+ {"question": "Sample Q2?", "options": ["A", "B", "C", "D"], "answer": "B"},
22
+ {"question": "Sample Q3?", "options": ["A", "B", "C", "D"], "answer": "C"},
23
+ {"question": "Sample Q4?", "options": ["A", "B", "C", "D"], "answer": "D"},
24
+ {"question": "Sample Q5?", "options": ["A", "B", "C", "D"], "answer": "A"}
25
+ ]
26
+ }])
27
+
28
+ section_output.children = sample_section()
29
+
30
  def generate_study_guide(file):
31
  if file is None:
32
  return [gr.Markdown("❌ No file uploaded.")], "", []
 
62
  response = call_llm(prompt)
63
  try:
64
  guide = json.loads(response)
65
+ section_output.children = render_sections(guide)
66
+ return text, guide
67
  except:
68
+ section_output.children = [gr.Markdown("❌ Failed to parse structured study guide.")]
69
+ return text, []
70
 
71
  def render_sections(guide_data):
72
+ blocks = []
73
  for i, sec in enumerate(guide_data):
74
+ blocks.append(render_section(i + 1, sec))
75
+ return blocks
76
 
77
  def render_section(index, sec):
78
+ block = gr.Column()
79
+ with block:
80
+ gr.Markdown(f"## πŸ“˜ Section {index}: {sec['section_title']}")
81
+ gr.Markdown(f"**Overview:** {sec['overview']}")
82
+ gr.Markdown(f"**Explanation:**\n\n{sec['explanation']}")
83
+
84
+ gr.Markdown("### 🧠 Quiz")
85
+ question_area = gr.Column()
 
 
 
 
 
 
86
 
87
+ def make_question_ui(q_idx):
88
+ q = sec['quiz'][q_idx]
89
+ q_box = gr.Markdown(f"**Q{q_idx+1}.** {q['question']}")
90
+ q_radio = gr.Radio(q['options'], label="Choose an answer")
91
 
92
+ def grade(choice):
93
+ correct = q['answer']
94
+ return "βœ… Correct!" if choice == correct else f"❌ Correct: {correct}"
95
 
96
+ q_radio.change(fn=grade, inputs=[q_radio], outputs=[feedback_box])
97
+ return [q_box, q_radio]
98
 
99
+ feedback_box = gr.Textbox(label="Answer Feedback", interactive=False)
100
+ question_area.children = make_question_ui(0)
101
 
102
+ with gr.Row():
103
+ for i in range(5):
104
+ gr.Button(str(i + 1), size="sm").click(fn=lambda idx=i: make_question_ui(idx), inputs=[], outputs=question_area)
 
105
 
106
+ return block
107
 
108
  def save_to_user(nickname, filename, guide):
109
  if not guide or not filename:
 
118
  guides = fetch_study_guides(user)
119
  for g in guides:
120
  if label.startswith(g.filename):
121
+ section_output.children = render_sections(json.loads(g.guide))
122
+ return
123
+ section_output.children = [gr.Markdown("❌ Guide not found.")]
124
 
125
+ file_input.change(fn=generate_study_guide, inputs=[file_input], outputs=[file_text_state, guide_state])
126
  save_btn.click(fn=save_to_user, inputs=[nickname_input, file_input, guide_state], outputs=[])
127
  nickname_input.change(fn=load_user_guides, inputs=[nickname_input], outputs=[guide_dropdown])
128
+ guide_dropdown.change(fn=show_saved_guide, inputs=[nickname_input, guide_dropdown], outputs=[])
129
 
130
+ return panel