ankban commited on
Commit
1c1767c
Β·
verified Β·
1 Parent(s): 9ef5e80

Update file_study_guide_panel.py

Browse files
Files changed (1) hide show
  1. file_study_guide_panel.py +55 -42
file_study_guide_panel.py CHANGED
@@ -3,18 +3,20 @@ import json
3
  from file_utils import extract_text_from_file, call_llm, save_study_guide, fetch_study_guides
4
 
5
  def file_study_guide_dashboard(nickname_input):
6
- section_output = gr.Column()
7
 
8
  with gr.Column() as panel:
9
  file_input = gr.File(label="πŸ“€ Upload a File", file_types=[".pdf", ".txt", ".md", ".pptx"])
10
  save_btn = gr.Button("πŸ’Ύ Save Guide")
11
  guide_dropdown = gr.Dropdown(label="πŸ“š Your Saved Guides", choices=[])
12
- file_text_state = gr.State("")
13
- guide_state = gr.State([])
 
14
 
15
  def generate_study_guide(file):
16
  if file is None:
17
- return gr.update(visible=True), "No file uploaded.", [], []
 
18
  text = extract_text_from_file(file)
19
 
20
  prompt = f"""
@@ -28,15 +30,15 @@ For each section:
28
  - 1 correct answer
29
  Return as JSON in this format:
30
  [
31
- {
32
  "section_title": "...",
33
  "overview": "...",
34
  "explanation": "...",
35
  "quiz": [
36
- {"question": "...", "options": ["A", "B", "C", "D"], "answer": "B"},
37
  ...
38
  ]
39
- },
40
  ...
41
  ]
42
 
@@ -45,58 +47,64 @@ Text:
45
  """
46
  response = call_llm(prompt)
47
  try:
48
- study_guide = json.loads(response)
49
- section_output.children = render_sections(study_guide)
50
- return text, study_guide
51
  except:
52
- section_output.children = [gr.Markdown("❌ Error generating structured study guide.")]
53
- return text, []
54
 
55
- def render_sections(study_guide):
56
- sections_ui = []
57
- for i, sec in enumerate(study_guide):
58
- section_ui = render_section(i + 1, sec)
59
- sections_ui.append(section_ui)
60
- return sections_ui
61
 
62
  def render_section(index, sec):
63
- section_ui = gr.Column()
64
 
65
- with section_ui:
66
  gr.Markdown(f"## πŸ“˜ Section {index}: {sec['section_title']}")
67
  gr.Markdown(f"**Overview:** {sec['overview']}")
68
  gr.Markdown(f"**Explanation:**\n\n{sec['explanation']}")
69
-
70
  gr.Markdown("### 🧠 Quiz")
 
71
  quiz_output = gr.Textbox(label="Answer Feedback", interactive=False)
72
  score_display = gr.Textbox(label="Score", interactive=False, value="Score: 0/5")
73
  score_state = gr.State(0)
74
 
75
- def render_question(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, score):
81
  correct = q['answer']
82
- new_score = score + 1 if choice == correct else score
83
- feedback = "βœ… Correct!" if choice == correct else f"❌ Correct Answer: {correct}"
84
- return feedback, f"Score: {new_score}/5", new_score
85
 
86
- option_radio.change(fn=grade, inputs=[option_radio, score_state], outputs=[quiz_output, score_display, score_state])
87
- return [question_box, option_radio]
88
 
89
- quiz_elements = render_question(0)
 
90
 
91
- for i in range(5):
92
- gr.Button(str(i + 1), size="sm").click(fn=lambda idx=i: render_question(idx), inputs=[], outputs=quiz_elements)
 
 
 
 
 
93
 
94
- return section_ui
95
 
96
  def save_to_user(nickname, filename, guide):
97
  if not guide or not filename:
98
  return
99
- save_study_guide(nickname, filename.name, json.dumps(guide))
100
 
101
  def load_user_guides(user):
102
  guides = fetch_study_guides(user)
@@ -106,13 +114,18 @@ Text:
106
  guides = fetch_study_guides(user)
107
  for g in guides:
108
  if label.startswith(g.filename):
109
- section_output.children = render_sections(json.loads(g.guide))
110
- return
111
- section_output.children = [gr.Markdown("No content found.")]
 
 
 
 
 
 
 
112
 
113
- file_input.change(fn=generate_study_guide, inputs=[file_input], outputs=[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=[])
117
 
118
- return panel
 
3
  from file_utils import extract_text_from_file, call_llm, save_study_guide, fetch_study_guides
4
 
5
  def file_study_guide_dashboard(nickname_input):
6
+ section_output = gr.Column(visible=True)
7
 
8
  with gr.Column() as panel:
9
  file_input = gr.File(label="πŸ“€ Upload a File", file_types=[".pdf", ".txt", ".md", ".pptx"])
10
  save_btn = gr.Button("πŸ’Ύ Save Guide")
11
  guide_dropdown = gr.Dropdown(label="πŸ“š Your Saved Guides", choices=[])
12
+
13
+ guide_state = gr.State([]) # Stores current study guide
14
+ filename_state = gr.State("")
15
 
16
  def generate_study_guide(file):
17
  if file is None:
18
+ return [gr.Markdown("❌ No file uploaded.")], "", []
19
+
20
  text = extract_text_from_file(file)
21
 
22
  prompt = f"""
 
30
  - 1 correct answer
31
  Return as JSON in this format:
32
  [
33
+ {{
34
  "section_title": "...",
35
  "overview": "...",
36
  "explanation": "...",
37
  "quiz": [
38
+ {{"question": "...", "options": ["A", "B", "C", "D"], "answer": "B"}},
39
  ...
40
  ]
41
+ }},
42
  ...
43
  ]
44
 
 
47
  """
48
  response = call_llm(prompt)
49
  try:
50
+ guide = json.loads(response)
51
+ return render_sections(guide), text, guide
 
52
  except:
53
+ return [gr.Markdown("❌ Failed to parse structured study guide.")], text, []
 
54
 
55
+ def render_sections(guide_data):
56
+ components = []
57
+ for i, section in enumerate(guide_data):
58
+ components.append(render_section(i + 1, section))
59
+ return components
 
60
 
61
  def render_section(index, sec):
62
+ section_block = gr.Column()
63
 
64
+ with section_block:
65
  gr.Markdown(f"## πŸ“˜ Section {index}: {sec['section_title']}")
66
  gr.Markdown(f"**Overview:** {sec['overview']}")
67
  gr.Markdown(f"**Explanation:**\n\n{sec['explanation']}")
 
68
  gr.Markdown("### 🧠 Quiz")
69
+
70
  quiz_output = gr.Textbox(label="Answer Feedback", interactive=False)
71
  score_display = gr.Textbox(label="Score", interactive=False, value="Score: 0/5")
72
  score_state = gr.State(0)
73
 
74
+ question_area = gr.Column()
75
+ question_index = gr.State(0)
76
+
77
+ def render_question(idx):
78
+ q = sec['quiz'][idx]
79
+ q_box = gr.Markdown(f"**Q{idx+1}.** {q['question']}")
80
+ q_radio = gr.Radio(q['options'], label="Choose an answer")
81
 
82
  def grade(choice, score):
83
  correct = q['answer']
84
+ updated_score = score + 1 if choice == correct else score
85
+ feedback = "βœ… Correct!" if choice == correct else f"❌ Correct: {correct}"
86
+ return feedback, f"Score: {updated_score}/5", updated_score
87
 
88
+ q_radio.change(fn=grade, inputs=[q_radio, score_state], outputs=[quiz_output, score_display, score_state])
89
+ return [q_box, q_radio]
90
 
91
+ # Load first question by default
92
+ question_area.children = render_question(0)
93
 
94
+ with gr.Row():
95
+ for i in range(5):
96
+ gr.Button(str(i + 1), size="sm").click(
97
+ fn=lambda idx=i: render_question(idx),
98
+ inputs=[],
99
+ outputs=question_area
100
+ )
101
 
102
+ return section_block
103
 
104
  def save_to_user(nickname, filename, guide):
105
  if not guide or not filename:
106
  return
107
+ save_study_guide(nickname, filename, json.dumps(guide))
108
 
109
  def load_user_guides(user):
110
  guides = fetch_study_guides(user)
 
114
  guides = fetch_study_guides(user)
115
  for g in guides:
116
  if label.startswith(g.filename):
117
+ return render_sections(json.loads(g.guide))
118
+ return [gr.Markdown("❌ Guide not found.")]
119
+
120
+ file_input.change(
121
+ fn=generate_study_guide,
122
+ inputs=[file_input],
123
+ outputs=[section_output, filename_state, guide_state]
124
+ )
125
+
126
+ save_btn.click(fn=save_to_user, inputs=[nickname_input, filename_state, guide_state], outputs=[])
127
 
 
 
128
  nickname_input.change(fn=load_user_guides, inputs=[nickname_input], outputs=[guide_dropdown])
129
+ guide_dropdown.change(fn=show_saved_guide, inputs=[nickname_input, guide_dropdown], outputs=[section_output])
130
 
131
+ return panel