Update file_study_guide_panel.py
Browse files- file_study_guide_panel.py +89 -80
file_study_guide_panel.py
CHANGED
|
@@ -1,84 +1,93 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
import json
|
| 4 |
-
from file_utils import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
)
|
| 10 |
-
|
| 11 |
-
def file_study_guide_dashboard(nickname):
|
| 12 |
-
uploaded_files = gr.State([])
|
| 13 |
-
file_sections = gr.State({})
|
| 14 |
-
file_texts = gr.State({})
|
| 15 |
-
|
| 16 |
-
with gr.Column(scale=4, elem_id="main-column") as guide_content:
|
| 17 |
-
nickname_input = nickname
|
| 18 |
-
file_upload = gr.File(label="π€ Upload File for Study Guide", file_types=[".pdf", ".txt", ".md", ".pptx"])
|
| 19 |
-
study_guide_output = gr.Textbox(label="π Generated Study Guide", lines=10, interactive=False)
|
| 20 |
-
save_guide_btn = gr.Button("π Save Study Guide")
|
| 21 |
-
|
| 22 |
-
saved_guides_dropdown = gr.Dropdown(label="π Your Saved Guides", choices=[], interactive=True)
|
| 23 |
-
saved_guide_display = gr.Textbox(label="π Selected Guide Content", lines=10, interactive=False)
|
| 24 |
-
|
| 25 |
-
def generate_study_guide(file, current_files, section_map, text_map):
|
| 26 |
-
if file is None:
|
| 27 |
-
return current_files, section_map, text_map, "No file uploaded."
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
text = extract_text_from_file(file)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
section_output = gr.Column()
|
| 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"""
|
| 17 |
+
You are an AI tutor. Divide the following content into 5β6 logical sections.
|
| 18 |
+
For each section:
|
| 19 |
+
- Provide a 2β3 line summary (overview)
|
| 20 |
+
- Write a detailed explanation of the topic
|
| 21 |
+
- Create 5 multiple choice questions (MCQs), each with:
|
| 22 |
+
- A question
|
| 23 |
+
- 4 options
|
| 24 |
+
- 1 correct answer
|
| 25 |
+
Return as JSON in this format:
|
| 26 |
+
[
|
| 27 |
+
{
|
| 28 |
+
"section_title": "...",
|
| 29 |
+
"overview": "...",
|
| 30 |
+
"explanation": "...",
|
| 31 |
+
"quiz": [
|
| 32 |
+
{"question": "...", "options": ["A", "B", "C", "D"], "answer": "B"},
|
| 33 |
+
...
|
| 34 |
+
]
|
| 35 |
+
},
|
| 36 |
+
...
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
Text:
|
| 40 |
+
{text}
|
| 41 |
+
"""
|
| 42 |
+
response = call_llm(prompt)
|
| 43 |
+
try:
|
| 44 |
+
study_guide = json.loads(response)
|
| 45 |
+
return render_sections(study_guide)
|
| 46 |
+
except:
|
| 47 |
+
return gr.Textbox(value="β Error generating structured study guide. Please try with a clearer file.", interactive=False)
|
| 48 |
+
|
| 49 |
+
def render_sections(study_guide):
|
| 50 |
+
sections_ui = []
|
| 51 |
+
for i, sec in enumerate(study_guide):
|
| 52 |
+
section_ui = render_section(i + 1, sec)
|
| 53 |
+
sections_ui.append(section_ui)
|
| 54 |
+
return sections_ui
|
| 55 |
+
|
| 56 |
+
def render_section(index, sec):
|
| 57 |
+
section_ui = gr.Column()
|
| 58 |
+
|
| 59 |
+
with section_ui:
|
| 60 |
+
gr.Markdown(f"## π Section {index}: {sec['section_title']}")
|
| 61 |
+
gr.Markdown(f"**Overview:** {sec['overview']}")
|
| 62 |
+
gr.Markdown(f"**Explanation:**\n\n{sec['explanation']}")
|
| 63 |
+
|
| 64 |
+
gr.Markdown("### π§ Quiz")
|
| 65 |
+
quiz_output = gr.Textbox(label="Answer Feedback", interactive=False)
|
| 66 |
+
question_index = gr.State(0)
|
| 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 |
+
return "β
Correct!" if choice == correct else f"β Correct Answer: {correct}"
|
| 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 |
+
btn = gr.Button(str(i + 1), size="sm")
|
| 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 |
+
file_input.change(fn=generate_study_guide, inputs=[file_input], outputs=[section_output])
|
| 92 |
+
|
| 93 |
+
return panel
|