Update file_study_guide_panel.py
Browse files- file_study_guide_panel.py +47 -60
file_study_guide_panel.py
CHANGED
|
@@ -3,15 +3,13 @@ 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(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 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
def generate_study_guide(file):
|
| 17 |
if file is None:
|
|
@@ -53,58 +51,53 @@ Text:
|
|
| 53 |
return [gr.Markdown("β Failed to parse structured study guide.")], text, []
|
| 54 |
|
| 55 |
def render_sections(guide_data):
|
| 56 |
-
|
| 57 |
-
for i,
|
| 58 |
-
|
| 59 |
-
return
|
| 60 |
|
| 61 |
def render_section(index, sec):
|
| 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 |
-
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)
|
|
@@ -117,15 +110,9 @@ Text:
|
|
| 117 |
return render_sections(json.loads(g.guide))
|
| 118 |
return [gr.Markdown("β Guide not found.")]
|
| 119 |
|
| 120 |
-
file_input.change(
|
| 121 |
-
|
| 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
|
|
|
|
| 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 |
with gr.Column() as panel:
|
| 7 |
file_input = gr.File(label="π€ Upload a File", file_types=[".pdf", ".txt", ".md", ".pptx"])
|
| 8 |
save_btn = gr.Button("πΎ Save Guide")
|
| 9 |
guide_dropdown = gr.Dropdown(label="π Your Saved Guides", choices=[])
|
| 10 |
+
section_output = gr.Column()
|
| 11 |
+
file_text_state = gr.State("")
|
| 12 |
+
guide_state = gr.State([])
|
| 13 |
|
| 14 |
def generate_study_guide(file):
|
| 15 |
if file is None:
|
|
|
|
| 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:
|
| 99 |
return
|
| 100 |
+
save_study_guide(nickname, filename.name, json.dumps(guide))
|
| 101 |
|
| 102 |
def load_user_guides(user):
|
| 103 |
guides = fetch_study_guides(user)
|
|
|
|
| 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
|