Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
from groq import Groq
|
3 |
import os
|
4 |
-
import
|
5 |
-
import json
|
6 |
-
from PIL import Image
|
7 |
-
import requests
|
8 |
-
from io import BytesIO
|
9 |
|
10 |
# Initialize Groq client
|
11 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
Generate a visual response based on the subject and topic.
|
16 |
-
"""
|
17 |
-
if subject == "Math":
|
18 |
-
# Example: Generate a simple plot for Math
|
19 |
-
plt.figure()
|
20 |
-
plt.plot([0, 1, 2, 3, 4], [0, 1, 4, 9, 16], label="y = x^2")
|
21 |
-
plt.title(f"Math Graph: {topic}")
|
22 |
-
plt.xlabel("x")
|
23 |
-
plt.ylabel("y")
|
24 |
-
plt.legend()
|
25 |
-
plt.savefig("math_plot.png")
|
26 |
-
return "math_plot.png"
|
27 |
-
|
28 |
-
elif subject == "Science":
|
29 |
-
# Example: Fetch an image from Unsplash for Science
|
30 |
-
response = requests.get(f"https://source.unsplash.com/400x300/?{topic}")
|
31 |
-
img = Image.open(BytesIO(response.content))
|
32 |
-
img.save("science_image.png")
|
33 |
-
return "science_image.png"
|
34 |
-
|
35 |
-
# Add more subjects as needed
|
36 |
-
else:
|
37 |
-
return None
|
38 |
|
39 |
def generate_tutor_output(subject, difficulty, student_input):
|
40 |
prompt = f"""
|
@@ -53,7 +26,7 @@ def generate_tutor_output(subject, difficulty, student_input):
|
|
53 |
messages=[
|
54 |
{
|
55 |
"role": "system",
|
56 |
-
"content":
|
57 |
},
|
58 |
{
|
59 |
"role": "user",
|
@@ -64,10 +37,16 @@ def generate_tutor_output(subject, difficulty, student_input):
|
|
64 |
max_tokens=1000,
|
65 |
)
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
with gr.Blocks() as demo:
|
73 |
gr.Markdown("# 馃帗 Your AI Tutor by Farhan")
|
@@ -95,32 +74,31 @@ with gr.Blocks() as demo:
|
|
95 |
lesson_output = gr.Markdown(label="Lesson")
|
96 |
question_output = gr.Markdown(label="Comprehension Question")
|
97 |
feedback_output = gr.Markdown(label="Feedback")
|
98 |
-
|
99 |
|
100 |
gr.Markdown("""
|
101 |
### How to Use
|
102 |
1. Select a subject from the dropdown.
|
103 |
2. Choose your difficulty level.
|
104 |
3. Enter the topic or question you'd like to explore.
|
105 |
-
4. Click 'Generate Lesson' to receive a personalized lesson, question, and
|
106 |
5. Review the AI-generated content to enhance your learning.
|
107 |
6. Feel free to ask follow-up questions or explore new topics!
|
108 |
""")
|
109 |
|
110 |
-
def process_output(output,
|
111 |
try:
|
112 |
-
parsed =
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
return "Error parsing response", "No question available", "No feedback available", None
|
118 |
|
119 |
submit_button.click(
|
120 |
-
fn=lambda s, d, i: process_output(
|
121 |
inputs=[subject, difficulty, student_input],
|
122 |
-
outputs=[lesson_output, question_output, feedback_output,
|
123 |
)
|
124 |
|
125 |
-
if
|
126 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
1 |
import gradio as gr
|
2 |
from groq import Groq
|
3 |
import os
|
4 |
+
import openai # For integration with OpenAI's image generation API (e.g., DALL路E)
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Initialize Groq client
|
7 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
8 |
|
9 |
+
# Initialize OpenAI API client for image generation
|
10 |
+
openai.api_key = os.environ["OPENAI_API_KEY"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def generate_tutor_output(subject, difficulty, student_input):
|
13 |
prompt = f"""
|
|
|
26 |
messages=[
|
27 |
{
|
28 |
"role": "system",
|
29 |
+
"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.",
|
30 |
},
|
31 |
{
|
32 |
"role": "user",
|
|
|
37 |
max_tokens=1000,
|
38 |
)
|
39 |
|
40 |
+
return completion.choices[0].message.content
|
41 |
+
|
42 |
+
def generate_visual_answer(prompt):
|
43 |
+
response = openai.Image.create(
|
44 |
+
prompt=prompt,
|
45 |
+
n=1,
|
46 |
+
size="1024x1024"
|
47 |
+
)
|
48 |
+
image_url = response['data'][0]['url']
|
49 |
+
return image_url
|
50 |
|
51 |
with gr.Blocks() as demo:
|
52 |
gr.Markdown("# 馃帗 Your AI Tutor by Farhan")
|
|
|
74 |
lesson_output = gr.Markdown(label="Lesson")
|
75 |
question_output = gr.Markdown(label="Comprehension Question")
|
76 |
feedback_output = gr.Markdown(label="Feedback")
|
77 |
+
image_output = gr.Image(label="Visual Answer", elem_id="image-output")
|
78 |
|
79 |
gr.Markdown("""
|
80 |
### How to Use
|
81 |
1. Select a subject from the dropdown.
|
82 |
2. Choose your difficulty level.
|
83 |
3. Enter the topic or question you'd like to explore.
|
84 |
+
4. Click 'Generate Lesson' to receive a personalized lesson, question, feedback, and a visual answer.
|
85 |
5. Review the AI-generated content to enhance your learning.
|
86 |
6. Feel free to ask follow-up questions or explore new topics!
|
87 |
""")
|
88 |
|
89 |
+
def process_output(output, prompt):
|
90 |
try:
|
91 |
+
parsed = eval(output)
|
92 |
+
visual_answer = generate_visual_answer(prompt) # Get the image URL
|
93 |
+
return parsed["lesson"], parsed["question"], parsed["feedback"], visual_answer
|
94 |
+
except:
|
95 |
+
return "Error parsing output", "No question available", "No feedback available", None
|
|
|
96 |
|
97 |
submit_button.click(
|
98 |
+
fn=lambda s, d, i: process_output(generate_tutor_output(s, d, i), i),
|
99 |
inputs=[subject, difficulty, student_input],
|
100 |
+
outputs=[lesson_output, question_output, feedback_output, image_output]
|
101 |
)
|
102 |
|
103 |
+
if __name__ == "__main__":
|
104 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|