Update file_study_guide_panel.py
Browse files- file_study_guide_panel.py +40 -15
file_study_guide_panel.py
CHANGED
|
@@ -1,16 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
-
from file_utils import extract_text_from_file, call_llm
|
| 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 |
-
|
|
|
|
| 9 |
file_text_state = gr.State("")
|
|
|
|
| 10 |
|
| 11 |
def generate_study_guide(file):
|
| 12 |
if file is None:
|
| 13 |
-
return "No file uploaded."
|
| 14 |
text = extract_text_from_file(file)
|
| 15 |
|
| 16 |
prompt = f"""
|
|
@@ -42,9 +46,11 @@ Text:
|
|
| 42 |
response = call_llm(prompt)
|
| 43 |
try:
|
| 44 |
study_guide = json.loads(response)
|
| 45 |
-
|
|
|
|
| 46 |
except:
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
def render_sections(study_guide):
|
| 50 |
sections_ui = []
|
|
@@ -63,31 +69,50 @@ Text:
|
|
| 63 |
|
| 64 |
gr.Markdown("### π§ Quiz")
|
| 65 |
quiz_output = gr.Textbox(label="Answer Feedback", interactive=False)
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
def render_question(q_idx):
|
| 69 |
q = sec['quiz'][q_idx]
|
| 70 |
question_box = gr.Markdown(f"**Q{q_idx+1}.** {q['question']}")
|
| 71 |
option_radio = gr.Radio(q['options'], label="Choose an answer")
|
| 72 |
|
| 73 |
-
def grade(choice):
|
| 74 |
correct = q['answer']
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
option_radio.change(fn=grade, inputs=[option_radio], outputs=[quiz_output])
|
| 78 |
return [question_box, option_radio]
|
| 79 |
|
| 80 |
-
# Display first question initially
|
| 81 |
quiz_elements = render_question(0)
|
| 82 |
|
| 83 |
-
nav_buttons = []
|
| 84 |
for i in range(5):
|
| 85 |
-
|
| 86 |
-
btn.click(fn=lambda idx=i: render_question(idx), inputs=[], outputs=quiz_elements)
|
| 87 |
-
nav_buttons.append(btn)
|
| 88 |
|
| 89 |
return section_ui
|
| 90 |
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
return panel
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
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"""
|
|
|
|
| 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 = []
|
|
|
|
| 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)
|
| 103 |
+
return [f"{g.filename} ({g.timestamp})" for g in guides]
|
| 104 |
+
|
| 105 |
+
def show_saved_guide(user, label):
|
| 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
|