Update file_study_guide_panel.py
Browse files- 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 |
-
|
| 13 |
-
guide_state = gr.State([])
|
|
|
|
| 14 |
|
| 15 |
def generate_study_guide(file):
|
| 16 |
if file is None:
|
| 17 |
-
return gr.
|
|
|
|
| 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 |
-
|
| 49 |
-
|
| 50 |
-
return text, study_guide
|
| 51 |
except:
|
| 52 |
-
|
| 53 |
-
return text, []
|
| 54 |
|
| 55 |
-
def render_sections(
|
| 56 |
-
|
| 57 |
-
for i,
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
return sections_ui
|
| 61 |
|
| 62 |
def render_section(index, sec):
|
| 63 |
-
|
| 64 |
|
| 65 |
-
with
|
| 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 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
def grade(choice, score):
|
| 81 |
correct = q['answer']
|
| 82 |
-
|
| 83 |
-
feedback = "β
Correct!" if choice == correct else f"β Correct
|
| 84 |
-
return feedback, f"Score: {
|
| 85 |
|
| 86 |
-
|
| 87 |
-
return [
|
| 88 |
|
| 89 |
-
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
return
|
| 95 |
|
| 96 |
def save_to_user(nickname, filename, guide):
|
| 97 |
if not guide or not filename:
|
| 98 |
return
|
| 99 |
-
save_study_guide(nickname, filename
|
| 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 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|