Spaces:
Sleeping
Sleeping
File size: 4,778 Bytes
e4f36a2 2e8325f 996d3d2 58ab6ac e4f36a2 99db205 4eceb48 e9e10d6 e4f36a2 f946452 ee16ad4 f946452 ee16ad4 58ab6ac 64745ad 996d3d2 58ab6ac 996d3d2 f946452 0576dea f946452 996d3d2 99db205 e9e10d6 f946452 e9e10d6 f946452 e9e10d6 64745ad e9e10d6 996d3d2 f946452 99db205 0576dea 996d3d2 99db205 996d3d2 f946452 996d3d2 58ab6ac 996d3d2 99db205 64745ad f946452 99db205 f946452 99db205 996d3d2 f946452 996d3d2 f946452 0576dea f946452 2e8325f f946452 2e8325f f946452 99db205 f946452 58ab6ac f946452 3afc8c8 0576dea e4f36a2 f946452 |
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 |
import gradio as gr
from groq import Groq
import os
import json
# Initialize Groq client
client = Groq(api_key=os.environ["GROQ_API_KEY"])
print("API Key:", os.environ.get("GROQ_API_KEY")) # Debug print
# Define valid models (only those starting with "qwen" or "mistral")
valid_models = [
"qwen-qwq-32b",
"qwen-2.5-coder-32b",
"qwen-2.5-32b",
"deepseek-r1-distill-qwen-32b",
"mixtral-8x7b-32768",
"mistral-saba-24b"
]
def generate_tutor_output(subject, grade, student_input, model):
if model not in valid_models:
model = "mixtral-8x7b-32768" # Fallback model
print(f"Invalid model selected: {model}. Using fallback: mixtral-8x7b-32768")
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 brief, engaging lesson on the topic (2-3 paragraphs)
2. A thought-provoking question to check understanding
3. Constructive feedback on the student's input
Format your response as a JSON object with keys: "lesson", "question", "feedback"
"""
try:
completion = client.chat.completions.create(
messages=[
{
"role": "system",
"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.",
},
{
"role": "user",
"content": prompt,
}
],
model=model,
max_tokens=1000,
)
return completion.choices[0].message.content
except Exception as e:
print(f"Groq API Error: {str(e)}")
return json.dumps({
"lesson": f"Error: Could not generate lesson. API error: {str(e)}",
"question": "No question available",
"feedback": "No feedback available due to API error"
})
with gr.Blocks() as demo:
gr.Markdown("# 🎓 Learn & Explore (Grades 5-10)")
with gr.Row():
with gr.Column(scale=2):
subject = gr.Dropdown(
["Math", "Science", "History", "Geography", "Economics"],
label="Subject",
info="Choose the subject of your lesson"
)
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(
valid_models,
label="AI Model",
value="mixtral-8x7b-32768",
info="Select the AI model to use"
)
student_input = gr.Textbox(
placeholder="Type your query here...",
label="Your Input",
info="Enter the topic you want to learn"
)
submit_button = gr.Button("Generate Lesson and Question", variant="primary")
with gr.Column(scale=3):
lesson_output = gr.Markdown(label="Lesson")
question_output = gr.Markdown(label="Comprehension Question")
feedback_output = gr.Markdown(label="Feedback")
gr.Markdown("""
### How to Use
1. Select a subject from the dropdown.
2. Choose your grade level.
3. Select an AI model to power your lesson.
4. Enter the topic or question you'd like to explore.
5. Click 'Generate Lesson' to receive a personalized lesson, question, and feedback.
6. Review the AI-generated content to enhance your learning.
7. Feel free to ask follow-up questions or explore new topics!
""")
def process_output(output):
print(f"Raw API Output: {output}") # Debug print
try:
parsed = json.loads(output)
return parsed["lesson"], parsed["question"], parsed["feedback"]
except Exception as e:
print(f"JSON Parsing Error: {str(e)}")
return "Error parsing output", "No question available", "No feedback available"
submit_button.click(
fn=lambda s, g, i, m: process_output(generate_tutor_output(s, g, i, m)),
inputs=[subject, grade, student_input, model_select],
outputs=[lesson_output, question_output, feedback_output]
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860) |