SameerArz commited on
Commit
9e9bc7c
·
verified ·
1 Parent(s): 054ae65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -8
app.py CHANGED
@@ -1,10 +1,41 @@
1
  import gradio as gr
2
  from groq import Groq
3
  import os
 
 
 
 
 
4
 
5
  # Initialize Groq client
6
  client = Groq(api_key=os.environ["GROQ_API_KEY"])
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def generate_tutor_output(subject, difficulty, student_input):
9
  prompt = f"""
10
  You are an expert tutor in {subject} at the {difficulty} level.
@@ -22,7 +53,7 @@ def generate_tutor_output(subject, difficulty, student_input):
22
  messages=[
23
  {
24
  "role": "system",
25
- "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.",
26
  },
27
  {
28
  "role": "user",
@@ -33,7 +64,10 @@ def generate_tutor_output(subject, difficulty, student_input):
33
  max_tokens=1000,
34
  )
35
 
36
- return completion.choices[0].message.content
 
 
 
37
 
38
  with gr.Blocks() as demo:
39
  gr.Markdown("# 🎓 Your AI Tutor by Farhan")
@@ -61,6 +95,7 @@ with gr.Blocks() as demo:
61
  lesson_output = gr.Markdown(label="Lesson")
62
  question_output = gr.Markdown(label="Comprehension Question")
63
  feedback_output = gr.Markdown(label="Feedback")
 
64
 
65
  gr.Markdown("""
66
  ### How to Use
@@ -72,17 +107,17 @@ with gr.Blocks() as demo:
72
  6. Feel free to ask follow-up questions or explore new topics!
73
  """)
74
 
75
- def process_output(output):
76
  try:
77
- parsed = eval(output)
78
- return parsed["lesson"], parsed["question"], parsed["feedback"]
79
  except:
80
- return "Error parsing output", "No question available", "No feedback available"
81
 
82
  submit_button.click(
83
- fn=lambda s, d, i: process_output(generate_tutor_output(s, d, i)),
84
  inputs=[subject, difficulty, student_input],
85
- outputs=[lesson_output, question_output, feedback_output]
86
  )
87
 
88
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from groq import Groq
3
  import os
4
+ import matplotlib.pyplot as plt
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
+ def generate_visual_response(subject, topic):
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"""
41
  You are an expert tutor in {subject} at the {difficulty} level.
 
53
  messages=[
54
  {
55
  "role": "system",
56
+ "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 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.",
57
  },
58
  {
59
  "role": "user",
 
64
  max_tokens=1000,
65
  )
66
 
67
+ output = completion.choices[0].message.content
68
+ visual_response = generate_visual_response(subject, student_input)
69
+
70
+ return output, visual_response
71
 
72
  with gr.Blocks() as demo:
73
  gr.Markdown("# 🎓 Your AI Tutor by Farhan")
 
95
  lesson_output = gr.Markdown(label="Lesson")
96
  question_output = gr.Markdown(label="Comprehension Question")
97
  feedback_output = gr.Markdown(label="Feedback")
98
+ visual_output = gr.Image(label="Visual Explanation")
99
 
100
  gr.Markdown("""
101
  ### How to Use
 
107
  6. Feel free to ask follow-up questions or explore new topics!
108
  """)
109
 
110
+ def process_output(output, visual_response):
111
  try:
112
+ parsed = json.loads(output)
113
+ return parsed["lesson"], parsed["question"], parsed["feedback"], visual_response
114
  except:
115
+ return "Error parsing output", "No question available", "No feedback available", None
116
 
117
  submit_button.click(
118
+ fn=lambda s, d, i: process_output(*generate_tutor_output(s, d, i)),
119
  inputs=[subject, difficulty, student_input],
120
+ outputs=[lesson_output, question_output, feedback_output, visual_output]
121
  )
122
 
123
  if __name__ == "__main__":