File size: 6,104 Bytes
58d0a49
 
9ef5e80
6e3a396
 
 
 
9ef5e80
 
ed2dde1
 
 
58d0a49
d855e37
 
 
 
 
 
 
 
 
 
 
 
 
 
3316153
 
d855e37
6e3a396
 
1c1767c
 
58d0a49
6e3a396
 
 
 
 
 
 
 
 
 
 
 
1c1767c
6e3a396
 
 
 
1c1767c
6e3a396
 
1c1767c
6e3a396
 
 
 
 
 
 
 
1c1767c
d855e37
 
6e3a396
d855e37
 
6e3a396
1c1767c
d855e37
ed2dde1
d855e37
 
6e3a396
 
d855e37
 
 
 
 
 
 
 
ed2dde1
d855e37
 
 
 
ed2dde1
d855e37
 
 
ed2dde1
d855e37
 
ed2dde1
d855e37
 
ed2dde1
d855e37
 
 
ed2dde1
d855e37
6e3a396
9ef5e80
 
 
ed2dde1
9ef5e80
 
 
 
 
3316153
 
 
 
 
 
 
 
 
 
 
 
 
 
9ef5e80
 
 
 
d855e37
 
 
1c1767c
d855e37
ed2dde1
9ef5e80
d855e37
6e3a396
3316153
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import gradio as gr
import json
from file_utils import extract_text_from_file, call_llm, save_study_guide, fetch_study_guides

def file_study_guide_dashboard(nickname_input):
    with gr.Column() as panel:
        file_input = gr.File(label="πŸ“€ Upload a File", file_types=[".pdf", ".txt", ".md", ".pptx"])
        save_btn = gr.Button("πŸ’Ύ Save Guide")
        guide_dropdown = gr.Dropdown(label="πŸ“š Your Saved Guides", choices=[])
        section_output = gr.Column()
        file_text_state = gr.State("")
        guide_state = gr.State([])

        def sample_section():
            return render_sections([{
                "section_title": "Sample Section",
                "overview": "This is a short summary of the topic.",
                "explanation": "This is a detailed explanation to show how learning content will look.",
                "quiz": [
                    {"question": "What is 2+2?", "options": ["1", "2", "3", "4"], "answer": "4"},
                    {"question": "Sample Q2?", "options": ["A", "B", "C", "D"], "answer": "B"},
                    {"question": "Sample Q3?", "options": ["A", "B", "C", "D"], "answer": "C"},
                    {"question": "Sample Q4?", "options": ["A", "B", "C", "D"], "answer": "D"},
                    {"question": "Sample Q5?", "options": ["A", "B", "C", "D"], "answer": "A"}
                ]
            }])

        # Move this after render_sections is defined
        pass

        def generate_study_guide(file):
            if file is None:
                return [gr.Markdown("❌ No file uploaded.")], "", []

            text = extract_text_from_file(file)

            prompt = f"""
You are an AI tutor. Divide the following content into 5–6 logical sections.
For each section:
- Provide a 2–3 line summary (overview)
- Write a detailed explanation of the topic
- Create 5 multiple choice questions (MCQs), each with:
  - A question
  - 4 options
  - 1 correct answer
Return as JSON in this format:
[
  {{
    "section_title": "...",
    "overview": "...",
    "explanation": "...",
    "quiz": [
      {{"question": "...", "options": ["A", "B", "C", "D"], "answer": "B"}},
      ...
    ]
  }},
  ...
]

Text:
{text}
"""
            response = call_llm(prompt)
            try:
                guide = json.loads(response)
                section_output.children = render_sections(guide)
                return text, guide
            except:
                section_output.children = [gr.Markdown("❌ Failed to parse structured study guide.")]
                return text, []

        def render_sections(guide_data):
            blocks = []
            for i, sec in enumerate(guide_data):
                blocks.append(render_section(i + 1, sec))
            return blocks

        def render_section(index, sec):
            block = gr.Column()
            with block:
                gr.Markdown(f"## πŸ“˜ Section {index}: {sec['section_title']}")
                gr.Markdown(f"**Overview:** {sec['overview']}")
                gr.Markdown(f"**Explanation:**\n\n{sec['explanation']}")

                gr.Markdown("### 🧠 Quiz")
                question_area = gr.Column()

                def make_question_ui(q_idx):
                    q = sec['quiz'][q_idx]
                    q_box = gr.Markdown(f"**Q{q_idx+1}.** {q['question']}")
                    q_radio = gr.Radio(q['options'], label="Choose an answer")

                    def grade(choice):
                        correct = q['answer']
                        return "βœ… Correct!" if choice == correct else f"❌ Correct: {correct}"

                    q_radio.change(fn=grade, inputs=[q_radio], outputs=[feedback_box])
                    return [q_box, q_radio]

                feedback_box = gr.Textbox(label="Answer Feedback", interactive=False)
                question_area.children = make_question_ui(0)

                with gr.Row():
                    for i in range(5):
                        gr.Button(str(i + 1), size="sm").click(fn=lambda idx=i: make_question_ui(idx), inputs=[], outputs=question_area)

            return block

        def save_to_user(nickname, filename, guide):
            if not guide or not filename:
                return
            save_study_guide(nickname, filename.name, json.dumps(guide))

        def load_user_guides(user):
            guides = fetch_study_guides(user)
            return [f"{g.filename} ({g.timestamp})" for g in guides]

                # Initialize with sample content after all functions are defined
        section_output.children = render_sections([{
            "section_title": "Sample Section",
            "overview": "This is a short summary of the topic.",
            "explanation": "This is a detailed explanation to show how learning content will look.",
            "quiz": [
                {"question": "What is 2+2?", "options": ["1", "2", "3", "4"], "answer": "4"},
                {"question": "Sample Q2?", "options": ["A", "B", "C", "D"], "answer": "B"},
                {"question": "Sample Q3?", "options": ["A", "B", "C", "D"], "answer": "C"},
                {"question": "Sample Q4?", "options": ["A", "B", "C", "D"], "answer": "D"},
                {"question": "Sample Q5?", "options": ["A", "B", "C", "D"], "answer": "A"}
            ]
        }])

        def show_saved_guide(user, label):
            guides = fetch_study_guides(user)
            for g in guides:
                if label.startswith(g.filename):
                    section_output.children = render_sections(json.loads(g.guide))
                    return
            section_output.children = [gr.Markdown("❌ Guide not found.")]

        file_input.change(fn=generate_study_guide, inputs=[file_input], outputs=[file_text_state, guide_state])
        save_btn.click(fn=save_to_user, inputs=[nickname_input, file_input, guide_state], outputs=[])
        nickname_input.change(fn=load_user_guides, inputs=[nickname_input], outputs=[guide_dropdown])
        guide_dropdown.change(fn=show_saved_guide, inputs=[nickname_input, guide_dropdown], outputs=[])

    return panel