ankban commited on
Commit
6e3a396
Β·
verified Β·
1 Parent(s): 9ec6ecf

Update file_study_guide_panel.py

Browse files
Files changed (1) hide show
  1. 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
- extract_text_from_file,
6
- call_llm,
7
- save_study_guide,
8
- fetch_study_guides
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
- filename = os.path.basename(file.name)
31
-
32
- prompt = f"Divide this document into 4–6 learning sections. For each section, include a bold title and 2–3 line explanation.\n\n{text}"
33
- response = call_llm(prompt, system_message="You are an AI tutor creating study guides.")
34
-
35
- updated_files = current_files + [filename] if filename not in current_files else current_files
36
- section_map[filename] = response
37
- text_map[filename] = text
38
-
39
- return updated_files, section_map, text_map, response
40
-
41
- def save_guide_to_db(user, uploaded_files, section_map):
42
- if not uploaded_files or user.strip() == "":
43
- return
44
- filename = uploaded_files[-1]
45
- guide = section_map.get(filename, "")
46
- if guide:
47
- save_study_guide(user=user, filename=filename, guide=guide)
48
-
49
- def load_user_guides(user):
50
- guides = fetch_study_guides(user)
51
- return [f"{g.filename} ({g.timestamp})" for g in guides]
52
-
53
- def show_saved_guide(user, label):
54
- guides = fetch_study_guides(user)
55
- for g in guides:
56
- if label.startswith(g.filename):
57
- return g.guide
58
- return ""
59
-
60
- file_upload.change(
61
- fn=generate_study_guide,
62
- inputs=[file_upload, uploaded_files, file_sections, file_texts],
63
- outputs=[uploaded_files, file_sections, file_texts, study_guide_output]
64
- )
65
-
66
- save_guide_btn.click(
67
- fn=save_guide_to_db,
68
- inputs=[nickname_input, uploaded_files, file_sections],
69
- outputs=[]
70
- )
71
-
72
- nickname_input.change(
73
- fn=load_user_guides,
74
- inputs=[nickname_input],
75
- outputs=[saved_guides_dropdown]
76
- )
77
-
78
- saved_guides_dropdown.change(
79
- fn=show_saved_guide,
80
- inputs=[nickname_input, saved_guides_dropdown],
81
- outputs=[saved_guide_display]
82
- )
83
-
84
- return guide_content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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