Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Initialize Groq client with your API key
|
| 6 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 7 |
|
| 8 |
def generate_tutor_output(subject, difficulty, student_input):
|
| 9 |
-
# Construct the prompt for text generation
|
| 10 |
prompt = f"""
|
| 11 |
You are an expert tutor in {subject} at the {difficulty} level.
|
| 12 |
The student has provided the following input: "{student_input}"
|
|
@@ -19,7 +20,6 @@ def generate_tutor_output(subject, difficulty, student_input):
|
|
| 19 |
Format your response as a JSON object with keys: "lesson", "question", "feedback"
|
| 20 |
"""
|
| 21 |
|
| 22 |
-
# Generate completion from the Groq API
|
| 23 |
completion = client.chat.completions.create(
|
| 24 |
messages=[
|
| 25 |
{
|
|
@@ -35,9 +35,27 @@ def generate_tutor_output(subject, difficulty, student_input):
|
|
| 35 |
max_tokens=1000,
|
| 36 |
)
|
| 37 |
|
| 38 |
-
# Return the generated content (lesson, question, feedback)
|
| 39 |
return completion.choices[0].message.content
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
# Set up the Gradio interface
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
gr.Markdown("# 🎓 Your AI Tutor")
|
|
@@ -67,6 +85,7 @@ with gr.Blocks() as demo:
|
|
| 67 |
lesson_output = gr.Markdown(label="Lesson")
|
| 68 |
question_output = gr.Markdown(label="Comprehension Question")
|
| 69 |
feedback_output = gr.Markdown(label="Feedback")
|
|
|
|
| 70 |
|
| 71 |
gr.Markdown("""
|
| 72 |
### How to Use
|
|
@@ -74,21 +93,23 @@ with gr.Blocks() as demo:
|
|
| 74 |
2. Choose your difficulty level.
|
| 75 |
3. Enter the topic or question you'd like to explore.
|
| 76 |
4. Click 'Generate Lesson' to receive a personalized lesson, question, and feedback.
|
| 77 |
-
5.
|
| 78 |
-
6.
|
|
|
|
| 79 |
""")
|
| 80 |
|
| 81 |
def process_output(output):
|
| 82 |
try:
|
| 83 |
parsed = eval(output) # Convert string to dictionary
|
| 84 |
-
|
|
|
|
| 85 |
except:
|
| 86 |
-
return "Error parsing output", "No question available", "No feedback available"
|
| 87 |
|
| 88 |
submit_button.click(
|
| 89 |
fn=lambda s, d, i: process_output(generate_tutor_output(s, d, i)),
|
| 90 |
inputs=[subject, difficulty, student_input],
|
| 91 |
-
outputs=[lesson_output, question_output, feedback_output]
|
| 92 |
)
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
# Initialize Groq client with your API key
|
| 8 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 9 |
|
| 10 |
def generate_tutor_output(subject, difficulty, student_input):
|
|
|
|
| 11 |
prompt = f"""
|
| 12 |
You are an expert tutor in {subject} at the {difficulty} level.
|
| 13 |
The student has provided the following input: "{student_input}"
|
|
|
|
| 20 |
Format your response as a JSON object with keys: "lesson", "question", "feedback"
|
| 21 |
"""
|
| 22 |
|
|
|
|
| 23 |
completion = client.chat.completions.create(
|
| 24 |
messages=[
|
| 25 |
{
|
|
|
|
| 35 |
max_tokens=1000,
|
| 36 |
)
|
| 37 |
|
|
|
|
| 38 |
return completion.choices[0].message.content
|
| 39 |
|
| 40 |
+
# Function to generate a simple graph (e.g., bar chart)
|
| 41 |
+
def generate_graph():
|
| 42 |
+
# Example data
|
| 43 |
+
x = ['A', 'B', 'C', 'D']
|
| 44 |
+
y = [10, 20, 15, 25]
|
| 45 |
+
|
| 46 |
+
fig, ax = plt.subplots()
|
| 47 |
+
ax.bar(x, y)
|
| 48 |
+
ax.set_title("Example Bar Chart")
|
| 49 |
+
ax.set_xlabel("Categories")
|
| 50 |
+
ax.set_ylabel("Values")
|
| 51 |
+
|
| 52 |
+
# Save the plot to a file
|
| 53 |
+
plt.tight_layout()
|
| 54 |
+
plt.savefig("/tmp/bar_chart.png") # Save to temp directory
|
| 55 |
+
plt.close(fig)
|
| 56 |
+
|
| 57 |
+
return "/tmp/bar_chart.png" # Return the path to the saved image
|
| 58 |
+
|
| 59 |
# Set up the Gradio interface
|
| 60 |
with gr.Blocks() as demo:
|
| 61 |
gr.Markdown("# 🎓 Your AI Tutor")
|
|
|
|
| 85 |
lesson_output = gr.Markdown(label="Lesson")
|
| 86 |
question_output = gr.Markdown(label="Comprehension Question")
|
| 87 |
feedback_output = gr.Markdown(label="Feedback")
|
| 88 |
+
graph_output = gr.Image(label="Generated Graph")
|
| 89 |
|
| 90 |
gr.Markdown("""
|
| 91 |
### How to Use
|
|
|
|
| 93 |
2. Choose your difficulty level.
|
| 94 |
3. Enter the topic or question you'd like to explore.
|
| 95 |
4. Click 'Generate Lesson' to receive a personalized lesson, question, and feedback.
|
| 96 |
+
5. The AI will also generate a simple bar chart as a visual representation.
|
| 97 |
+
6. Review the AI-generated content to enhance your learning.
|
| 98 |
+
7. Feel free to ask follow-up questions or explore new topics!
|
| 99 |
""")
|
| 100 |
|
| 101 |
def process_output(output):
|
| 102 |
try:
|
| 103 |
parsed = eval(output) # Convert string to dictionary
|
| 104 |
+
graph_path = generate_graph() # Generate graph
|
| 105 |
+
return parsed["lesson"], parsed["question"], parsed["feedback"], graph_path
|
| 106 |
except:
|
| 107 |
+
return "Error parsing output", "No question available", "No feedback available", None
|
| 108 |
|
| 109 |
submit_button.click(
|
| 110 |
fn=lambda s, d, i: process_output(generate_tutor_output(s, d, i)),
|
| 111 |
inputs=[subject, difficulty, student_input],
|
| 112 |
+
outputs=[lesson_output, question_output, feedback_output, graph_output]
|
| 113 |
)
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|