Spaces:
Runtime error
Runtime error
File size: 4,511 Bytes
e4f36a2 c5b8c4a e4f36a2 eb6c04e e4f36a2 eb6c04e e4f36a2 2f3f6c8 e4f36a2 eb6c04e e4f36a2 eb6c04e 2f3f6c8 c5b8c4a eb6c04e e4f36a2 eb6c04e e4f36a2 eb6c04e e4f36a2 eb6c04e e4f36a2 c5b8c4a e4f36a2 eb6c04e c5b8c4a e4f36a2 eb6c04e e4f36a2 eb6c04e c5b8c4a 2f3f6c8 c5b8c4a e4f36a2 eb6c04e e4f36a2 c5b8c4a e4f36a2 2f3f6c8 |
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 |
import gradio as gr
from groq import Groq
import os
import matplotlib.pyplot as plt
import numpy as np
# Initialize Groq client with your API key
client = Groq(api_key=os.environ["GROQ_API_KEY"])
def generate_tutor_output(subject, difficulty, student_input):
prompt = f"""
You are an expert tutor in {subject} at the {difficulty} level.
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"
"""
completion = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are the world's best AI tutor, renowned for your ability to explain complex concepts in an engaging, clear, and memorable way and giving math examples. Your expertise in {subject} is unparalleled, and you're adept at tailoring your teaching to {difficulty} level students. Your goal is to not just impart knowledge, but to inspire a love for learning and critical thinking.",
},
{
"role": "user",
"content": prompt,
}
],
model="mixtral-8x7b-32768", # Model for text generation
max_tokens=1000,
)
return completion.choices[0].message.content
# Function to generate a simple graph (e.g., bar chart)
def generate_graph():
# Example data
x = ['A', 'B', 'C', 'D']
y = [10, 20, 15, 25]
fig, ax = plt.subplots()
ax.bar(x, y)
ax.set_title("Example Bar Chart")
ax.set_xlabel("Categories")
ax.set_ylabel("Values")
# Save the plot to a file
plt.tight_layout()
plt.savefig("/tmp/bar_chart.png") # Save to temp directory
plt.close(fig)
return "/tmp/bar_chart.png" # Return the path to the saved image
# Set up the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# 🎓 Your AI Tutor")
with gr.Row():
with gr.Column(scale=2):
# Input fields for subject, difficulty, and student input
subject = gr.Dropdown(
["Math", "Science", "History", "Literature", "Code", "AI"],
label="Subject",
info="Choose the subject of your lesson"
)
difficulty = gr.Radio(
["Beginner", "Intermediate", "Advanced"],
label="Difficulty Level",
info="Select your proficiency level"
)
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", variant="primary")
with gr.Column(scale=3):
# Output fields for lesson, question, and feedback
lesson_output = gr.Markdown(label="Lesson")
question_output = gr.Markdown(label="Comprehension Question")
feedback_output = gr.Markdown(label="Feedback")
graph_output = gr.Image(label="Generated Graph")
gr.Markdown("""
### How to Use
1. Select a subject from the dropdown.
2. Choose your difficulty level.
3. Enter the topic or question you'd like to explore.
4. Click 'Generate Lesson' to receive a personalized lesson, question, and feedback.
5. The AI will also generate a simple bar chart as a visual representation.
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):
try:
parsed = eval(output) # Convert string to dictionary
graph_path = generate_graph() # Generate graph
return parsed["lesson"], parsed["question"], parsed["feedback"], graph_path
except:
return "Error parsing output", "No question available", "No feedback available", None
submit_button.click(
fn=lambda s, d, i: process_output(generate_tutor_output(s, d, i)),
inputs=[subject, difficulty, student_input],
outputs=[lesson_output, question_output, feedback_output, graph_output]
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)
|