Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,49 @@ 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 |
print("API Key:", os.environ.get("GROQ_API_KEY")) # Debug print
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def generate_tutor_output(subject, grade, student_input, model):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
prompt = f"""
|
12 |
You are an expert tutor in {subject} for a {grade} grade student.
|
13 |
The student has provided the following input: "{student_input}"
|
@@ -37,7 +74,7 @@ def generate_tutor_output(subject, grade, student_input, model):
|
|
37 |
)
|
38 |
return completion.choices[0].message.content
|
39 |
except Exception as e:
|
40 |
-
print(f"Groq API Error: {str(e)}")
|
41 |
return json.dumps({
|
42 |
"lesson": f"Error: Could not generate lesson. API error: {str(e)}",
|
43 |
"question": "No question available",
|
@@ -46,11 +83,44 @@ def generate_tutor_output(subject, grade, student_input, model):
|
|
46 |
"feedback": "No feedback available due to API error"
|
47 |
})
|
48 |
|
49 |
-
def check_answer(selected_answer, correct_answer):
|
50 |
if selected_answer == correct_answer:
|
51 |
-
|
|
|
52 |
else:
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
with gr.Blocks(title="Learn & Practice π") as demo:
|
56 |
gr.Markdown("# π Learn & Practice Zone (Grades 5-10)")
|
@@ -69,11 +139,7 @@ with gr.Blocks(title="Learn & Practice π") as demo:
|
|
69 |
info="Select your grade level"
|
70 |
)
|
71 |
model_select = gr.Dropdown(
|
72 |
-
|
73 |
-
"mixtral-8x7b-32768",
|
74 |
-
"llama3-8b-8192", # Fallback model
|
75 |
-
"qwen-2.5-32b"
|
76 |
-
],
|
77 |
label="AI Tutor Model",
|
78 |
value="mixtral-8x7b-32768",
|
79 |
info="Choose your AI tutor"
|
@@ -104,30 +170,8 @@ with gr.Blocks(title="Learn & Practice π") as demo:
|
|
104 |
5. Earn points for correct answers and keep learning!
|
105 |
""")
|
106 |
|
107 |
-
def process_output(output):
|
108 |
-
print(f"Raw API Output: {output}") # Debug print
|
109 |
-
try:
|
110 |
-
parsed = json.loads(output)
|
111 |
-
options = [f"{k}. {v}" for k, v in zip(["a", "b", "c", "d"], parsed["options"])]
|
112 |
-
return (
|
113 |
-
parsed["lesson"],
|
114 |
-
parsed["question"],
|
115 |
-
options,
|
116 |
-
parsed["correct_answer"],
|
117 |
-
parsed["feedback"]
|
118 |
-
)
|
119 |
-
except Exception as e:
|
120 |
-
print(f"JSON Parsing Error: {str(e)}") # Debug print
|
121 |
-
return (
|
122 |
-
f"Error parsing response: {str(e)}",
|
123 |
-
"No question available",
|
124 |
-
[],
|
125 |
-
"",
|
126 |
-
"No feedback available"
|
127 |
-
)
|
128 |
-
|
129 |
def update_interface(subject, grade, student_input, model):
|
130 |
-
print(f"Selected Model: {model}")
|
131 |
output = generate_tutor_output(subject, grade, student_input, model)
|
132 |
lesson, question, options, correct_answer, feedback = process_output(output)
|
133 |
return (
|
@@ -136,7 +180,7 @@ with gr.Blocks(title="Learn & Practice π") as demo:
|
|
136 |
gr.update(choices=options, visible=True),
|
137 |
feedback,
|
138 |
"", # Clear answer feedback
|
139 |
-
gr.update(value=0) # Reset points
|
140 |
), correct_answer
|
141 |
|
142 |
# State to store correct answer
|
@@ -154,9 +198,12 @@ with gr.Blocks(title="Learn & Practice π") as demo:
|
|
154 |
|
155 |
options_output.change(
|
156 |
fn=check_answer,
|
157 |
-
inputs=[options_output, correct_answer_state],
|
158 |
outputs=[answer_feedback, points]
|
159 |
)
|
160 |
|
161 |
if __name__ == "__main__":
|
162 |
-
|
|
|
|
|
|
|
|
2 |
from groq import Groq
|
3 |
import os
|
4 |
import json
|
5 |
+
import random
|
6 |
+
import sqlite3
|
7 |
|
8 |
# Initialize Groq client
|
9 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
10 |
print("API Key:", os.environ.get("GROQ_API_KEY")) # Debug print
|
11 |
|
12 |
+
# Initialize or connect to SQLite database for points
|
13 |
+
conn = sqlite3.connect("student_points.db", check_same_thread=False)
|
14 |
+
cursor = conn.cursor()
|
15 |
+
cursor.execute('''CREATE TABLE IF NOT EXISTS points (student_id TEXT, points INTEGER, timestamp TEXT)''')
|
16 |
+
conn.commit()
|
17 |
+
|
18 |
def generate_tutor_output(subject, grade, student_input, model):
|
19 |
+
# List of supported Groq models (based on your provided list)
|
20 |
+
valid_models = [
|
21 |
+
"distil-whisper-large-v3-en",
|
22 |
+
"gemma2-9b-it",
|
23 |
+
"llama-3.3-70b-versatile",
|
24 |
+
"llama-3.1-8b-instant",
|
25 |
+
"llama-guard-3-8b",
|
26 |
+
"llama3-70b-8192",
|
27 |
+
"llama3-8b-8192",
|
28 |
+
"mixtral-8x7b-32768",
|
29 |
+
"whisper-large-v3",
|
30 |
+
"whisper-large-v3-turbo",
|
31 |
+
"qwen-qwq-32b",
|
32 |
+
"mistral-saba-24b",
|
33 |
+
"qwen-2.5-coder-32b",
|
34 |
+
"qwen-2.5-32b",
|
35 |
+
"deepseek-r1-distill-qwen-32b",
|
36 |
+
"deepseek-r1-distill-llama-70b-specdec",
|
37 |
+
"deepseek-r1-distill-llama-70b",
|
38 |
+
"llama-3.3-70b-specdec",
|
39 |
+
"llama-3.2-1b-preview",
|
40 |
+
"llama-3.2-3b-preview",
|
41 |
+
"llama-3.2-11b-vision-preview",
|
42 |
+
"llama-3.2-90b-vision-preview"
|
43 |
+
]
|
44 |
+
if model not in valid_models:
|
45 |
+
model = "mixtral-8x7b-32768" # Fallback model
|
46 |
+
print(f"Invalid model selected: {model}. Using fallback: mixtral-8x7b-32768")
|
47 |
+
|
48 |
prompt = f"""
|
49 |
You are an expert tutor in {subject} for a {grade} grade student.
|
50 |
The student has provided the following input: "{student_input}"
|
|
|
74 |
)
|
75 |
return completion.choices[0].message.content
|
76 |
except Exception as e:
|
77 |
+
print(f"Groq API Error: {str(e)}")
|
78 |
return json.dumps({
|
79 |
"lesson": f"Error: Could not generate lesson. API error: {str(e)}",
|
80 |
"question": "No question available",
|
|
|
83 |
"feedback": "No feedback available due to API error"
|
84 |
})
|
85 |
|
86 |
+
def check_answer(selected_answer, correct_answer, current_points, student_id="student1"):
|
87 |
if selected_answer == correct_answer:
|
88 |
+
feedback = "π Awesome job! You got it right! Keep rocking it!"
|
89 |
+
new_points = current_points + 10
|
90 |
else:
|
91 |
+
feedback = f"π
Not quite! The correct answer was '{correct_answer}'. Try again next time!"
|
92 |
+
new_points = current_points
|
93 |
+
# Save points to database
|
94 |
+
cursor.execute("INSERT INTO points (student_id, points, timestamp) VALUES (?, ?, ?)",
|
95 |
+
(student_id, new_points, "2025-03-08 04:25"))
|
96 |
+
conn.commit()
|
97 |
+
return feedback, new_points
|
98 |
+
|
99 |
+
def process_output(output):
|
100 |
+
print(f"Raw API Output: {output}")
|
101 |
+
try:
|
102 |
+
parsed = json.loads(output)
|
103 |
+
# Shuffle options for variety
|
104 |
+
options_list = list(zip(["a", "b", "c", "d"], parsed["options"]))
|
105 |
+
random.shuffle(options_list)
|
106 |
+
options = [f"{k}. {v}" for k, v in options_list]
|
107 |
+
correct_key = [k for k, v in options_list if v == parsed["correct_answer"]][0]
|
108 |
+
return (
|
109 |
+
parsed["lesson"],
|
110 |
+
parsed["question"],
|
111 |
+
options,
|
112 |
+
correct_key,
|
113 |
+
parsed["feedback"]
|
114 |
+
)
|
115 |
+
except Exception as e:
|
116 |
+
print(f"JSON Parsing Error: {str(e)}")
|
117 |
+
return (
|
118 |
+
f"Error parsing response: {str(e)}",
|
119 |
+
"No question available",
|
120 |
+
[],
|
121 |
+
"",
|
122 |
+
"No feedback available"
|
123 |
+
)
|
124 |
|
125 |
with gr.Blocks(title="Learn & Practice π") as demo:
|
126 |
gr.Markdown("# π Learn & Practice Zone (Grades 5-10)")
|
|
|
139 |
info="Select your grade level"
|
140 |
)
|
141 |
model_select = gr.Dropdown(
|
142 |
+
valid_models,
|
|
|
|
|
|
|
|
|
143 |
label="AI Tutor Model",
|
144 |
value="mixtral-8x7b-32768",
|
145 |
info="Choose your AI tutor"
|
|
|
170 |
5. Earn points for correct answers and keep learning!
|
171 |
""")
|
172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
def update_interface(subject, grade, student_input, model):
|
174 |
+
print(f"Selected Model: {model}")
|
175 |
output = generate_tutor_output(subject, grade, student_input, model)
|
176 |
lesson, question, options, correct_answer, feedback = process_output(output)
|
177 |
return (
|
|
|
180 |
gr.update(choices=options, visible=True),
|
181 |
feedback,
|
182 |
"", # Clear answer feedback
|
183 |
+
gr.update(value=0) # Reset points for new session
|
184 |
), correct_answer
|
185 |
|
186 |
# State to store correct answer
|
|
|
198 |
|
199 |
options_output.change(
|
200 |
fn=check_answer,
|
201 |
+
inputs=[options_output, correct_answer_state, points],
|
202 |
outputs=[answer_feedback, points]
|
203 |
)
|
204 |
|
205 |
if __name__ == "__main__":
|
206 |
+
try:
|
207 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
208 |
+
finally:
|
209 |
+
conn.close() # Close database connection on shutdown
|