File size: 5,492 Bytes
e4f36a2
2e8325f
996d3d2
58ab6ac
e4f36a2
99db205
4eceb48
e4f36a2
58ab6ac
996d3d2
58ab6ac
996d3d2
 
 
58ab6ac
 
 
0576dea
58ab6ac
996d3d2
99db205
996d3d2
99db205
 
 
58ab6ac
99db205
 
 
 
 
 
 
58ab6ac
0576dea
781c41b
99db205
996d3d2
58ab6ac
 
 
 
 
 
 
 
99db205
58ab6ac
0576dea
 
996d3d2
99db205
996d3d2
58ab6ac
996d3d2
58ab6ac
 
 
 
996d3d2
99db205
 
 
 
 
 
58ab6ac
99db205
58ab6ac
99db205
996d3d2
58ab6ac
 
 
996d3d2
58ab6ac
0576dea
58ab6ac
0576dea
58ab6ac
 
 
 
 
 
 
 
2e8325f
58ab6ac
 
 
 
 
 
2e8325f
58ab6ac
99db205
996d3d2
58ab6ac
 
 
 
 
 
 
 
 
996d3d2
58ab6ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99db205
58ab6ac
 
afa20b8
58ab6ac
 
 
 
 
 
 
 
 
 
3afc8c8
0576dea
e4f36a2
99db205
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
146
147
148
149
import gradio as gr
from groq import Groq
import os
import json

# Initialize Groq client
client = Groq(api_key=os.environ["GROQ_API_KEY"])

def generate_tutor_output(subject, grade, student_input, model):
    prompt = f"""
    You are an expert tutor in {subject} for a {grade} grade student. 
    The student has provided the following input: "{student_input}"
    
    Please generate:
    1. A fun, engaging lesson (2-3 paragraphs) tailored to a {grade} grader's understanding.
    2. A thought-provoking multiple-choice question (with 4 options: a, b, c, d) to test understanding.
    3. Constructive feedback on the student's input.
    
    Format your response as a JSON object with keys: "lesson", "question", "options", "correct_answer", "feedback"
    """

    completion = client.chat.completions.create(
        messages=[
            {
                "role": "system",
                "content": f"You are a fun, creative AI tutor for {grade} graders, expert in {subject}. You explain concepts in a simple, exciting way with relatable examples (like math problems for their age). Your goal is to spark curiosity and help students practice what they learn!",
            },
            {
                "role": "user",
                "content": prompt,
            }
        ],
        model=model,
        max_tokens=1200,
    )

    return completion.choices[0].message.content

def check_answer(selected_answer, correct_answer):
    if selected_answer == correct_answer:
        return "๐ŸŽ‰ Awesome job! You got it right! Keep rocking it!", 10
    else:
        return f"๐Ÿ˜… Not quite! The correct answer was '{correct_answer}'. Try again next time!", 0

with gr.Blocks(title="Learn & Practice ๐Ÿš€") as demo:
    gr.Markdown("# ๐Ÿš€ Learn & Practice Zone (Grades 5-10)")
    
    # Input Section
    with gr.Row():
        with gr.Column(scale=2):
            subject = gr.Dropdown(
                ["Math", "Science", "History", "Geography", "Economics"], 
                label="Subject", 
                info="Pick your favorite subject!"
            )
            grade = gr.Dropdown(
                ["5th Grade", "6th Grade", "7th Grade", "8th Grade", "9th Grade", "10th Grade"], 
                label="Your Grade", 
                info="Select your grade level"
            )
            model_select = gr.Dropdown(
                [
                    "mixtral-8x7b-32768",
                    "qwen-2.5-coder-32b",
                    "qwen-2.5-32b"
                ],
                label="AI Tutor Model",
                value="mixtral-8x7b-32768",
                info="Choose your AI tutor"
            )
            student_input = gr.Textbox(
                placeholder="What do you want to learn today?", 
                label="Your Question", 
                info="Ask anything about the subject!"
            )
            submit_button = gr.Button("Get Lesson & Practice", variant="primary")
        
        # Output Section
        with gr.Column(scale=3):
            lesson_output = gr.Markdown(label="Your Lesson")
            question_output = gr.Markdown(label="Test Your Skills")
            options_output = gr.Radio(label="Choose an Answer", choices=[], visible=False)
            feedback_output = gr.Markdown(label="Feedback on Your Question")
            answer_feedback = gr.Markdown(label="Answer Feedback")
            points = gr.Number(label="Your Points", value=0)

    # Instructions
    gr.Markdown("""
    ### How to Play & Learn
    1. Pick a subject and your grade.
    2. Choose an AI tutor model.
    3. Ask a question or topic youโ€™re curious about.
    4. Read the fun lesson, then answer the question to test yourself.
    5. Earn points for correct answers and keep learning!
    """)

    def process_output(output):
        try:
            parsed = json.loads(output)
            options = [f"{k}. {v}" for k, v in zip(["a", "b", "c", "d"], parsed["options"])]
            return (
                parsed["lesson"],
                parsed["question"],
                options,
                parsed["correct_answer"],
                parsed["feedback"]
            )
        except:
            return (
                "Error generating lesson",
                "No question available",
                [],
                "",
                "No feedback available"
            )

    def update_interface(subject, grade, student_input, model):
        output = generate_tutor_output(subject, grade, student_input, model)
        lesson, question, options, correct_answer, feedback = process_output(output)
        return (
            lesson,
            question,
            gr.update(choices=options, visible=True),
            feedback,
            "",  # Clear answer feedback
            gr.update(value=0)  # Reset points
        ), correct_answer

    # State to store correct answer
    correct_answer_state = gr.State()

    submit_button.click(
        fn=update_interface,
        inputs=[subject, grade, student_input, model_select],
        outputs=[lesson_output, question_output, options_output, feedback_output, answer_feedback, points]
    ).then(
        fn=lambda x: x,
        inputs=[gr.State()],
        outputs=[correct_answer_state]
    )

    options_output.change(
        fn=check_answer,
        inputs=[options_output, correct_answer_state],
        outputs=[answer_feedback, points]
    )

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)