Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,45 +2,21 @@ import gradio as gr
|
|
| 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 |
-
# Define
|
| 13 |
valid_models = [
|
| 14 |
-
"distil-whisper-large-v3-en",
|
| 15 |
-
"gemma2-9b-it",
|
| 16 |
-
"llama-3.3-70b-versatile",
|
| 17 |
-
"llama-3.1-8b-instant",
|
| 18 |
-
"llama-guard-3-8b",
|
| 19 |
-
"llama3-70b-8192",
|
| 20 |
-
"llama3-8b-8192",
|
| 21 |
-
"mixtral-8x7b-32768",
|
| 22 |
-
"whisper-large-v3",
|
| 23 |
-
"whisper-large-v3-turbo",
|
| 24 |
"qwen-qwq-32b",
|
| 25 |
-
"mistral-saba-24b",
|
| 26 |
"qwen-2.5-coder-32b",
|
| 27 |
"qwen-2.5-32b",
|
| 28 |
"deepseek-r1-distill-qwen-32b",
|
| 29 |
-
"
|
| 30 |
-
"
|
| 31 |
-
"llama-3.3-70b-specdec",
|
| 32 |
-
"llama-3.2-1b-preview",
|
| 33 |
-
"llama-3.2-3b-preview",
|
| 34 |
-
"llama-3.2-11b-vision-preview",
|
| 35 |
-
"llama-3.2-90b-vision-preview"
|
| 36 |
]
|
| 37 |
|
| 38 |
-
# Initialize or connect to SQLite database for points
|
| 39 |
-
conn = sqlite3.connect("student_points.db", check_same_thread=False)
|
| 40 |
-
cursor = conn.cursor()
|
| 41 |
-
cursor.execute('''CREATE TABLE IF NOT EXISTS points (student_id TEXT, points INTEGER, timestamp TEXT)''')
|
| 42 |
-
conn.commit()
|
| 43 |
-
|
| 44 |
def generate_tutor_output(subject, grade, student_input, model):
|
| 45 |
if model not in valid_models:
|
| 46 |
model = "mixtral-8x7b-32768" # Fallback model
|
|
@@ -51,11 +27,11 @@ def generate_tutor_output(subject, grade, student_input, model):
|
|
| 51 |
The student has provided the following input: "{student_input}"
|
| 52 |
|
| 53 |
Please generate:
|
| 54 |
-
1. A
|
| 55 |
-
2. A thought-provoking
|
| 56 |
-
3. Constructive feedback on the student's input
|
| 57 |
|
| 58 |
-
Format your response as a JSON object with keys: "lesson", "question", "
|
| 59 |
"""
|
| 60 |
|
| 61 |
try:
|
|
@@ -63,7 +39,7 @@ def generate_tutor_output(subject, grade, student_input, model):
|
|
| 63 |
messages=[
|
| 64 |
{
|
| 65 |
"role": "system",
|
| 66 |
-
"content": f"You are
|
| 67 |
},
|
| 68 |
{
|
| 69 |
"role": "user",
|
|
@@ -71,7 +47,7 @@ def generate_tutor_output(subject, grade, student_input, model):
|
|
| 71 |
}
|
| 72 |
],
|
| 73 |
model=model,
|
| 74 |
-
max_tokens=
|
| 75 |
)
|
| 76 |
return completion.choices[0].message.content
|
| 77 |
except Exception as e:
|
|
@@ -79,60 +55,18 @@ def generate_tutor_output(subject, grade, student_input, model):
|
|
| 79 |
return json.dumps({
|
| 80 |
"lesson": f"Error: Could not generate lesson. API error: {str(e)}",
|
| 81 |
"question": "No question available",
|
| 82 |
-
"options": [],
|
| 83 |
-
"correct_answer": "",
|
| 84 |
"feedback": "No feedback available due to API error"
|
| 85 |
})
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
feedback = "🎉 Awesome job! You got it right! Keep rocking it!"
|
| 90 |
-
new_points = current_points + 10
|
| 91 |
-
else:
|
| 92 |
-
feedback = f"😅 Not quite! The correct answer was '{correct_answer}'. Try again next time!"
|
| 93 |
-
new_points = current_points
|
| 94 |
-
# Save points to database
|
| 95 |
-
cursor.execute("INSERT INTO points (student_id, points, timestamp) VALUES (?, ?, ?)",
|
| 96 |
-
(student_id, new_points, "2025-03-08 04:25"))
|
| 97 |
-
conn.commit()
|
| 98 |
-
return feedback, new_points
|
| 99 |
-
|
| 100 |
-
def process_output(output):
|
| 101 |
-
print(f"Raw API Output: {output}")
|
| 102 |
-
try:
|
| 103 |
-
parsed = json.loads(output)
|
| 104 |
-
# Shuffle options for variety
|
| 105 |
-
options_list = list(zip(["a", "b", "c", "d"], parsed["options"]))
|
| 106 |
-
random.shuffle(options_list)
|
| 107 |
-
options = [f"{k}. {v}" for k, v in options_list]
|
| 108 |
-
correct_key = [k for k, v in options_list if v == parsed["correct_answer"]][0]
|
| 109 |
-
return (
|
| 110 |
-
parsed["lesson"],
|
| 111 |
-
parsed["question"],
|
| 112 |
-
options,
|
| 113 |
-
correct_key,
|
| 114 |
-
parsed["feedback"]
|
| 115 |
-
)
|
| 116 |
-
except Exception as e:
|
| 117 |
-
print(f"JSON Parsing Error: {str(e)}")
|
| 118 |
-
return (
|
| 119 |
-
f"Error parsing response: {str(e)}",
|
| 120 |
-
"No question available",
|
| 121 |
-
[],
|
| 122 |
-
"",
|
| 123 |
-
"No feedback available"
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
with gr.Blocks(title="Learn & Practice 🚀") as demo:
|
| 127 |
-
gr.Markdown("# 🚀 Learn & Practice Zone (Grades 5-10)")
|
| 128 |
|
| 129 |
-
# Input Section
|
| 130 |
with gr.Row():
|
| 131 |
with gr.Column(scale=2):
|
| 132 |
subject = gr.Dropdown(
|
| 133 |
["Math", "Science", "History", "Geography", "Economics"],
|
| 134 |
label="Subject",
|
| 135 |
-
info="
|
| 136 |
)
|
| 137 |
grade = gr.Dropdown(
|
| 138 |
["5th Grade", "6th Grade", "7th Grade", "8th Grade", "9th Grade", "10th Grade"],
|
|
@@ -141,70 +75,47 @@ with gr.Blocks(title="Learn & Practice 🚀") as demo:
|
|
| 141 |
)
|
| 142 |
model_select = gr.Dropdown(
|
| 143 |
valid_models,
|
| 144 |
-
label="AI
|
| 145 |
value="mixtral-8x7b-32768",
|
| 146 |
-
info="
|
| 147 |
)
|
| 148 |
student_input = gr.Textbox(
|
| 149 |
-
placeholder="
|
| 150 |
-
label="Your
|
| 151 |
-
info="
|
| 152 |
)
|
| 153 |
-
submit_button = gr.Button("
|
| 154 |
|
| 155 |
-
# Output Section
|
| 156 |
with gr.Column(scale=3):
|
| 157 |
-
lesson_output = gr.Markdown(label="
|
| 158 |
-
question_output = gr.Markdown(label="
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
answer_feedback = gr.Markdown(label="Answer Feedback")
|
| 162 |
-
points = gr.Number(label="Your Points", value=0)
|
| 163 |
-
|
| 164 |
-
# Instructions
|
| 165 |
gr.Markdown("""
|
| 166 |
-
### How to
|
| 167 |
-
1.
|
| 168 |
-
2. Choose
|
| 169 |
-
3.
|
| 170 |
-
4.
|
| 171 |
-
5.
|
|
|
|
|
|
|
| 172 |
""")
|
| 173 |
-
|
| 174 |
-
def
|
| 175 |
-
print(f"
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
"", # Clear answer feedback
|
| 184 |
-
gr.update(value=0) # Reset points for new session
|
| 185 |
-
), correct_answer
|
| 186 |
-
|
| 187 |
-
# State to store correct answer
|
| 188 |
-
correct_answer_state = gr.State()
|
| 189 |
-
|
| 190 |
submit_button.click(
|
| 191 |
-
fn=
|
| 192 |
inputs=[subject, grade, student_input, model_select],
|
| 193 |
-
outputs=[lesson_output, question_output,
|
| 194 |
-
).then(
|
| 195 |
-
fn=lambda x: x,
|
| 196 |
-
inputs=[gr.State()],
|
| 197 |
-
outputs=[correct_answer_state]
|
| 198 |
-
)
|
| 199 |
-
|
| 200 |
-
options_output.change(
|
| 201 |
-
fn=check_answer,
|
| 202 |
-
inputs=[options_output, correct_answer_state, points],
|
| 203 |
-
outputs=[answer_feedback, points]
|
| 204 |
)
|
| 205 |
|
| 206 |
if __name__ == "__main__":
|
| 207 |
-
|
| 208 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 209 |
-
finally:
|
| 210 |
-
conn.close() # Close database connection on shutdown
|
|
|
|
| 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 |
+
# Define valid models (only those starting with "qwen" or "mistral")
|
| 11 |
valid_models = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
"qwen-qwq-32b",
|
|
|
|
| 13 |
"qwen-2.5-coder-32b",
|
| 14 |
"qwen-2.5-32b",
|
| 15 |
"deepseek-r1-distill-qwen-32b",
|
| 16 |
+
"mixtral-8x7b-32768",
|
| 17 |
+
"mistral-saba-24b"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
]
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def generate_tutor_output(subject, grade, student_input, model):
|
| 21 |
if model not in valid_models:
|
| 22 |
model = "mixtral-8x7b-32768" # Fallback model
|
|
|
|
| 27 |
The student has provided the following input: "{student_input}"
|
| 28 |
|
| 29 |
Please generate:
|
| 30 |
+
1. A brief, engaging lesson on the topic (2-3 paragraphs)
|
| 31 |
+
2. A thought-provoking question to check understanding
|
| 32 |
+
3. Constructive feedback on the student's input
|
| 33 |
|
| 34 |
+
Format your response as a JSON object with keys: "lesson", "question", "feedback"
|
| 35 |
"""
|
| 36 |
|
| 37 |
try:
|
|
|
|
| 39 |
messages=[
|
| 40 |
{
|
| 41 |
"role": "system",
|
| 42 |
+
"content": f"You are the world's best AI tutor, renowned for your ability to explain complex concepts in an engaging, clear, and memorable way with examples suitable for {grade} graders. Your expertise in {subject} is unparalleled, and you're adept at tailoring your teaching to {grade} grade students. Your goal is to not just impart knowledge, but to inspire a love for learning and critical thinking.",
|
| 43 |
},
|
| 44 |
{
|
| 45 |
"role": "user",
|
|
|
|
| 47 |
}
|
| 48 |
],
|
| 49 |
model=model,
|
| 50 |
+
max_tokens=1000,
|
| 51 |
)
|
| 52 |
return completion.choices[0].message.content
|
| 53 |
except Exception as e:
|
|
|
|
| 55 |
return json.dumps({
|
| 56 |
"lesson": f"Error: Could not generate lesson. API error: {str(e)}",
|
| 57 |
"question": "No question available",
|
|
|
|
|
|
|
| 58 |
"feedback": "No feedback available due to API error"
|
| 59 |
})
|
| 60 |
|
| 61 |
+
with gr.Blocks() as demo:
|
| 62 |
+
gr.Markdown("# 🎓 Learn & Explore (Grades 5-10)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
|
|
|
| 64 |
with gr.Row():
|
| 65 |
with gr.Column(scale=2):
|
| 66 |
subject = gr.Dropdown(
|
| 67 |
["Math", "Science", "History", "Geography", "Economics"],
|
| 68 |
label="Subject",
|
| 69 |
+
info="Choose the subject of your lesson"
|
| 70 |
)
|
| 71 |
grade = gr.Dropdown(
|
| 72 |
["5th Grade", "6th Grade", "7th Grade", "8th Grade", "9th Grade", "10th Grade"],
|
|
|
|
| 75 |
)
|
| 76 |
model_select = gr.Dropdown(
|
| 77 |
valid_models,
|
| 78 |
+
label="AI Model",
|
| 79 |
value="mixtral-8x7b-32768",
|
| 80 |
+
info="Select the AI model to use"
|
| 81 |
)
|
| 82 |
student_input = gr.Textbox(
|
| 83 |
+
placeholder="Type your query here...",
|
| 84 |
+
label="Your Input",
|
| 85 |
+
info="Enter the topic you want to learn"
|
| 86 |
)
|
| 87 |
+
submit_button = gr.Button("Generate Lesson and Question", variant="primary")
|
| 88 |
|
|
|
|
| 89 |
with gr.Column(scale=3):
|
| 90 |
+
lesson_output = gr.Markdown(label="Lesson")
|
| 91 |
+
question_output = gr.Markdown(label="Comprehension Question")
|
| 92 |
+
feedback_output = gr.Markdown(label="Feedback")
|
| 93 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
gr.Markdown("""
|
| 95 |
+
### How to Use
|
| 96 |
+
1. Select a subject from the dropdown.
|
| 97 |
+
2. Choose your grade level.
|
| 98 |
+
3. Select an AI model to power your lesson.
|
| 99 |
+
4. Enter the topic or question you'd like to explore.
|
| 100 |
+
5. Click 'Generate Lesson' to receive a personalized lesson, question, and feedback.
|
| 101 |
+
6. Review the AI-generated content to enhance your learning.
|
| 102 |
+
7. Feel free to ask follow-up questions or explore new topics!
|
| 103 |
""")
|
| 104 |
+
|
| 105 |
+
def process_output(output):
|
| 106 |
+
print(f"Raw API Output: {output}") # Debug print
|
| 107 |
+
try:
|
| 108 |
+
parsed = json.loads(output)
|
| 109 |
+
return parsed["lesson"], parsed["question"], parsed["feedback"]
|
| 110 |
+
except Exception as e:
|
| 111 |
+
print(f"JSON Parsing Error: {str(e)}")
|
| 112 |
+
return "Error parsing output", "No question available", "No feedback available"
|
| 113 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
submit_button.click(
|
| 115 |
+
fn=lambda s, g, i, m: process_output(generate_tutor_output(s, g, i, m)),
|
| 116 |
inputs=[subject, grade, student_input, model_select],
|
| 117 |
+
outputs=[lesson_output, question_output, feedback_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
)
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
| 121 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|