Spaces:
Running
Running
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) |