Update file_study_guide_panel.py
Browse files- 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 |
-
|
|
|
|
| 50 |
except:
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
def render_sections(guide_data):
|
| 54 |
-
|
| 55 |
for i, sec in enumerate(guide_data):
|
| 56 |
-
|
| 57 |
-
return
|
| 58 |
|
| 59 |
def render_section(index, sec):
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 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 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
nav_buttons.children.append(q_button)
|
| 94 |
|
| 95 |
-
return
|
| 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 |
-
|
| 111 |
-
|
|
|
|
| 112 |
|
| 113 |
-
file_input.change(fn=generate_study_guide, inputs=[file_input], outputs=[
|
| 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
|
|
|
|
| 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
|