Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,29 @@
|
|
1 |
import gradio as gr
|
2 |
from groq import Groq
|
3 |
import os
|
|
|
4 |
|
5 |
# Initialize Groq client
|
6 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
7 |
|
8 |
-
def generate_tutor_output(subject,
|
9 |
prompt = f"""
|
10 |
-
You are an expert tutor in {subject}
|
11 |
The student has provided the following input: "{student_input}"
|
12 |
|
13 |
Please generate:
|
14 |
-
1. A
|
15 |
-
2. A thought-provoking question to
|
16 |
-
3. Constructive feedback on the student's input
|
17 |
|
18 |
-
Format your response as a JSON object with keys: "lesson", "question", "feedback"
|
19 |
"""
|
20 |
|
21 |
completion = client.chat.completions.create(
|
22 |
messages=[
|
23 |
{
|
24 |
"role": "system",
|
25 |
-
"content": f"You are
|
26 |
},
|
27 |
{
|
28 |
"role": "user",
|
@@ -30,25 +31,32 @@ def generate_tutor_output(subject, difficulty, student_input, model):
|
|
30 |
}
|
31 |
],
|
32 |
model=model,
|
33 |
-
max_tokens=
|
34 |
)
|
35 |
|
36 |
return completion.choices[0].message.content
|
37 |
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
|
|
41 |
with gr.Row():
|
42 |
with gr.Column(scale=2):
|
43 |
subject = gr.Dropdown(
|
44 |
["Math", "Science", "History", "Geography", "Economics"],
|
45 |
label="Subject",
|
46 |
-
info="
|
47 |
)
|
48 |
-
|
49 |
-
["
|
50 |
-
label="
|
51 |
-
info="Select your
|
52 |
)
|
53 |
model_select = gr.Dropdown(
|
54 |
[
|
@@ -56,44 +64,86 @@ with gr.Blocks() as demo:
|
|
56 |
"qwen-2.5-coder-32b",
|
57 |
"qwen-2.5-32b"
|
58 |
],
|
59 |
-
label="AI Model",
|
60 |
value="mixtral-8x7b-32768",
|
61 |
-
info="
|
62 |
)
|
63 |
student_input = gr.Textbox(
|
64 |
-
placeholder="
|
65 |
-
label="Your
|
66 |
-
info="
|
67 |
)
|
68 |
-
submit_button = gr.Button("
|
69 |
|
|
|
70 |
with gr.Column(scale=3):
|
71 |
-
lesson_output = gr.Markdown(label="Lesson")
|
72 |
-
question_output = gr.Markdown(label="
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
75 |
gr.Markdown("""
|
76 |
-
### How to
|
77 |
-
1.
|
78 |
-
2. Choose
|
79 |
-
3.
|
80 |
-
4.
|
81 |
-
5.
|
82 |
-
6. Review the AI-generated content to enhance your learning.
|
83 |
-
7. Feel free to ask follow-up questions or explore new topics!
|
84 |
""")
|
85 |
-
|
86 |
def process_output(output):
|
87 |
try:
|
88 |
-
parsed =
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
except:
|
91 |
-
return
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
submit_button.click(
|
94 |
-
fn=
|
95 |
-
inputs=[subject,
|
96 |
-
outputs=[lesson_output, question_output, feedback_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
)
|
98 |
|
99 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
from groq import Groq
|
3 |
import os
|
4 |
+
import json
|
5 |
|
6 |
# Initialize Groq client
|
7 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
8 |
|
9 |
+
def generate_tutor_output(subject, grade, student_input, model):
|
10 |
prompt = f"""
|
11 |
+
You are an expert tutor in {subject} for a {grade} grade student.
|
12 |
The student has provided the following input: "{student_input}"
|
13 |
|
14 |
Please generate:
|
15 |
+
1. A fun, engaging lesson (2-3 paragraphs) tailored to a {grade} grader's understanding.
|
16 |
+
2. A thought-provoking multiple-choice question (with 4 options: a, b, c, d) to test understanding.
|
17 |
+
3. Constructive feedback on the student's input.
|
18 |
|
19 |
+
Format your response as a JSON object with keys: "lesson", "question", "options", "correct_answer", "feedback"
|
20 |
"""
|
21 |
|
22 |
completion = client.chat.completions.create(
|
23 |
messages=[
|
24 |
{
|
25 |
"role": "system",
|
26 |
+
"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!",
|
27 |
},
|
28 |
{
|
29 |
"role": "user",
|
|
|
31 |
}
|
32 |
],
|
33 |
model=model,
|
34 |
+
max_tokens=1200,
|
35 |
)
|
36 |
|
37 |
return completion.choices[0].message.content
|
38 |
|
39 |
+
def check_answer(selected_answer, correct_answer):
|
40 |
+
if selected_answer == correct_answer:
|
41 |
+
return "๐ Awesome job! You got it right! Keep rocking it!", 10
|
42 |
+
else:
|
43 |
+
return f"๐
Not quite! The correct answer was '{correct_answer}'. Try again next time!", 0
|
44 |
+
|
45 |
+
with gr.Blocks(title="Learn & Practice ๐") as demo:
|
46 |
+
gr.Markdown("# ๐ Learn & Practice Zone (Grades 5-10)")
|
47 |
|
48 |
+
# Input Section
|
49 |
with gr.Row():
|
50 |
with gr.Column(scale=2):
|
51 |
subject = gr.Dropdown(
|
52 |
["Math", "Science", "History", "Geography", "Economics"],
|
53 |
label="Subject",
|
54 |
+
info="Pick your favorite subject!"
|
55 |
)
|
56 |
+
grade = gr.Dropdown(
|
57 |
+
["5th Grade", "6th Grade", "7th Grade", "8th Grade", "9th Grade", "10th Grade"],
|
58 |
+
label="Your Grade",
|
59 |
+
info="Select your grade level"
|
60 |
)
|
61 |
model_select = gr.Dropdown(
|
62 |
[
|
|
|
64 |
"qwen-2.5-coder-32b",
|
65 |
"qwen-2.5-32b"
|
66 |
],
|
67 |
+
label="AI Tutor Model",
|
68 |
value="mixtral-8x7b-32768",
|
69 |
+
info="Choose your AI tutor"
|
70 |
)
|
71 |
student_input = gr.Textbox(
|
72 |
+
placeholder="What do you want to learn today?",
|
73 |
+
label="Your Question",
|
74 |
+
info="Ask anything about the subject!"
|
75 |
)
|
76 |
+
submit_button = gr.Button("Get Lesson & Practice", variant="primary")
|
77 |
|
78 |
+
# Output Section
|
79 |
with gr.Column(scale=3):
|
80 |
+
lesson_output = gr.Markdown(label="Your Lesson")
|
81 |
+
question_output = gr.Markdown(label="Test Your Skills")
|
82 |
+
options_output = gr.Radio(label="Choose an Answer", choices=[], visible=False)
|
83 |
+
feedback_output = gr.Markdown(label="Feedback on Your Question")
|
84 |
+
answer_feedback = gr.Markdown(label="Answer Feedback")
|
85 |
+
points = gr.Number(label="Your Points", value=0)
|
86 |
+
|
87 |
+
# Instructions
|
88 |
gr.Markdown("""
|
89 |
+
### How to Play & Learn
|
90 |
+
1. Pick a subject and your grade.
|
91 |
+
2. Choose an AI tutor model.
|
92 |
+
3. Ask a question or topic youโre curious about.
|
93 |
+
4. Read the fun lesson, then answer the question to test yourself.
|
94 |
+
5. Earn points for correct answers and keep learning!
|
|
|
|
|
95 |
""")
|
96 |
+
|
97 |
def process_output(output):
|
98 |
try:
|
99 |
+
parsed = json.loads(output)
|
100 |
+
options = [f"{k}. {v}" for k, v in zip(["a", "b", "c", "d"], parsed["options"])]
|
101 |
+
return (
|
102 |
+
parsed["lesson"],
|
103 |
+
parsed["question"],
|
104 |
+
options,
|
105 |
+
parsed["correct_answer"],
|
106 |
+
parsed["feedback"]
|
107 |
+
)
|
108 |
except:
|
109 |
+
return (
|
110 |
+
"Error generating lesson",
|
111 |
+
"No question available",
|
112 |
+
[],
|
113 |
+
"",
|
114 |
+
"No feedback available"
|
115 |
+
)
|
116 |
+
|
117 |
+
def update_interface(subject, grade, student_input, model):
|
118 |
+
output = generate_tutor_output(subject, grade, student_input, model)
|
119 |
+
lesson, question, options, correct_answer, feedback = process_output(output)
|
120 |
+
return (
|
121 |
+
lesson,
|
122 |
+
question,
|
123 |
+
gr.update(choices=options, visible=True),
|
124 |
+
feedback,
|
125 |
+
"", # Clear answer feedback
|
126 |
+
gr.update(value=0) # Reset points
|
127 |
+
), correct_answer
|
128 |
+
|
129 |
+
# State to store correct answer
|
130 |
+
correct_answer_state = gr.State()
|
131 |
+
|
132 |
submit_button.click(
|
133 |
+
fn=update_interface,
|
134 |
+
inputs=[subject, grade, student_input, model_select],
|
135 |
+
outputs=[lesson_output, question_output, options_output, feedback_output, answer_feedback, points],
|
136 |
+
_js="() => {document.querySelector('.output-container').scrollIntoView({behavior: 'smooth'})}"
|
137 |
+
).then(
|
138 |
+
fn=lambda x: x,
|
139 |
+
inputs=[gr.State()],
|
140 |
+
outputs=[correct_answer_state]
|
141 |
+
)
|
142 |
+
|
143 |
+
options_output.change(
|
144 |
+
fn=check_answer,
|
145 |
+
inputs=[options_output, correct_answer_state],
|
146 |
+
outputs=[answer_feedback, points]
|
147 |
)
|
148 |
|
149 |
if __name__ == "__main__":
|