SameerArz commited on
Commit
eb6c04e
·
verified ·
1 Parent(s): b5ecd6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -30
app.py CHANGED
@@ -1,27 +1,12 @@
1
  import gradio as gr
2
  from groq import Groq
3
  import os
4
- import requests # For DeepAI API
5
 
6
- # Initialize Groq client
7
  client = Groq(api_key=os.environ["GROQ_API_KEY"])
8
 
9
- # Function to generate image from DeepAI API
10
- def generate_visual_answer(prompt):
11
- # Replace with DeepAI image generation API URL
12
- url = "https://api.deepai.org/api/text2img"
13
- response = requests.post(
14
- url,
15
- data={'text': prompt}, # The prompt will be the text description for image generation
16
- headers={'api-key': os.environ["DEEP_AI_API_KEY"]} # Use DeepAI's API key here
17
- )
18
- if response.status_code == 200:
19
- image_url = response.json()['output_url'] # URL for the generated image
20
- return image_url
21
- else:
22
- return "Error generating image"
23
-
24
  def generate_tutor_output(subject, difficulty, student_input):
 
25
  prompt = f"""
26
  You are an expert tutor in {subject} at the {difficulty} level.
27
  The student has provided the following input: "{student_input}"
@@ -33,7 +18,8 @@ def generate_tutor_output(subject, difficulty, student_input):
33
 
34
  Format your response as a JSON object with keys: "lesson", "question", "feedback"
35
  """
36
-
 
37
  completion = client.chat.completions.create(
38
  messages=[
39
  {
@@ -45,17 +31,20 @@ def generate_tutor_output(subject, difficulty, student_input):
45
  "content": prompt,
46
  }
47
  ],
48
- model="mixtral-8x7b-32768",
49
  max_tokens=1000,
50
  )
51
-
 
52
  return completion.choices[0].message.content
53
 
 
54
  with gr.Blocks() as demo:
55
- gr.Markdown("# 🎓 Your AI Tutor by Farhan")
56
 
57
  with gr.Row():
58
  with gr.Column(scale=2):
 
59
  subject = gr.Dropdown(
60
  ["Math", "Science", "History", "Literature", "Code", "AI"],
61
  label="Subject",
@@ -74,33 +63,32 @@ with gr.Blocks() as demo:
74
  submit_button = gr.Button("Generate Lesson", variant="primary")
75
 
76
  with gr.Column(scale=3):
 
77
  lesson_output = gr.Markdown(label="Lesson")
78
  question_output = gr.Markdown(label="Comprehension Question")
79
  feedback_output = gr.Markdown(label="Feedback")
80
- image_output = gr.Image(label="Visual Answer", elem_id="image-output")
81
 
82
  gr.Markdown("""
83
  ### How to Use
84
  1. Select a subject from the dropdown.
85
  2. Choose your difficulty level.
86
  3. Enter the topic or question you'd like to explore.
87
- 4. Click 'Generate Lesson' to receive a personalized lesson, question, feedback, and a visual answer.
88
  5. Review the AI-generated content to enhance your learning.
89
  6. Feel free to ask follow-up questions or explore new topics!
90
  """)
91
 
92
- def process_output(output, prompt):
93
  try:
94
- parsed = eval(output)
95
- visual_answer = generate_visual_answer(prompt) # Get the image URL from DeepAI
96
- return parsed["lesson"], parsed["question"], parsed["feedback"], visual_answer
97
  except:
98
- return "Error parsing output", "No question available", "No feedback available", None
99
 
100
  submit_button.click(
101
- fn=lambda s, d, i: process_output(generate_tutor_output(s, d, i), i),
102
  inputs=[subject, difficulty, student_input],
103
- outputs=[lesson_output, question_output, feedback_output, image_output]
104
  )
105
 
106
  if __name__ == "__main__":
 
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}"
 
18
 
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
  {
 
31
  "content": prompt,
32
  }
33
  ],
34
+ model="mixtral-8x7b-32768", # Model for text generation
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")
44
 
45
  with gr.Row():
46
  with gr.Column(scale=2):
47
+ # Input fields for subject, difficulty, and student input
48
  subject = gr.Dropdown(
49
  ["Math", "Science", "History", "Literature", "Code", "AI"],
50
  label="Subject",
 
63
  submit_button = gr.Button("Generate Lesson", variant="primary")
64
 
65
  with gr.Column(scale=3):
66
+ # Output fields for lesson, question, and feedback
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
73
  1. Select a subject from the dropdown.
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. Review the AI-generated content to enhance your learning.
78
  6. Feel free to ask follow-up questions or explore new topics!
79
  """)
80
 
81
+ def process_output(output):
82
  try:
83
+ parsed = eval(output) # Convert string to dictionary
84
+ return parsed["lesson"], parsed["question"], parsed["feedback"]
 
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__":