SameerArz commited on
Commit
d15776e
·
verified ·
1 Parent(s): c5b8c4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -34
app.py CHANGED
@@ -1,12 +1,18 @@
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.
@@ -37,28 +43,31 @@ def generate_tutor_output(subject, difficulty, student_input):
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")
62
 
63
  with gr.Row():
64
  with gr.Column(scale=2):
@@ -78,38 +87,46 @@ with gr.Blocks() as demo:
78
  label="Your Input",
79
  info="Enter the topic you want to learn"
80
  )
81
- submit_button = gr.Button("Generate Lesson", variant="primary")
 
 
 
 
 
82
 
83
  with gr.Column(scale=3):
84
- # Output fields for lesson, question, and feedback
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
92
  1. Select a subject from the dropdown.
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__":
 
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
+ # Load Text-to-Image Models
9
+ model1 = gr.load("models/prithivMLmods/SD3.5-Turbo-Realism-2.0-LoRA")
10
+ model2 = gr.load("models/Purz/face-projection")
11
+
12
+ # Stop event for threading (image generation)
13
+ stop_event = threading.Event()
14
+
15
+ # Function to generate tutor output (lesson, question, feedback)
16
  def generate_tutor_output(subject, difficulty, student_input):
17
  prompt = f"""
18
  You are an expert tutor in {subject} at the {difficulty} level.
 
43
 
44
  return completion.choices[0].message.content
45
 
46
+ # Function to generate images based on model selection
47
+ def generate_images(text, selected_model):
48
+ stop_event.clear()
49
+
50
+ if selected_model == "Model 1 (Turbo Realism)":
51
+ model = model1
52
+ elif selected_model == "Model 2 (Face Projection)":
53
+ model = model2
54
+ else:
55
+ return ["Invalid model selection."] * 3
56
+
57
+ results = []
58
+ for i in range(3):
59
+ if stop_event.is_set():
60
+ return ["Image generation stopped by user."] * 3
61
+
62
+ modified_text = f"{text} variation {i+1}"
63
+ result = model(modified_text)
64
+ results.append(result)
65
+
66
+ return results
67
 
68
  # Set up the Gradio interface
69
  with gr.Blocks() as demo:
70
+ gr.Markdown("# 🎓 Your AI Tutor with Visuals & Images")
71
 
72
  with gr.Row():
73
  with gr.Column(scale=2):
 
87
  label="Your Input",
88
  info="Enter the topic you want to learn"
89
  )
90
+ model_selector = gr.Radio(
91
+ ["Model 1 (Turbo Realism)", "Model 2 (Face Projection)"],
92
+ label="Select Image Generation Model",
93
+ value="Model 1 (Turbo Realism)"
94
+ )
95
+ submit_button = gr.Button("Generate Lesson & Images", variant="primary")
96
 
97
  with gr.Column(scale=3):
98
+ # Output fields for lesson, question, feedback, and images
99
  lesson_output = gr.Markdown(label="Lesson")
100
  question_output = gr.Markdown(label="Comprehension Question")
101
  feedback_output = gr.Markdown(label="Feedback")
102
+ output1 = gr.Image(label="Generated Image 1")
103
+ output2 = gr.Image(label="Generated Image 2")
104
+ output3 = gr.Image(label="Generated Image 3")
105
 
106
  gr.Markdown("""
107
  ### How to Use
108
  1. Select a subject from the dropdown.
109
  2. Choose your difficulty level.
110
  3. Enter the topic or question you'd like to explore.
111
+ 4. Choose the model for image generation.
112
+ 5. Click 'Generate Lesson & Images' to receive a personalized lesson, question, feedback, and images.
113
  6. Review the AI-generated content to enhance your learning.
114
  7. Feel free to ask follow-up questions or explore new topics!
115
  """)
116
 
117
+ def process_output(subject, difficulty, student_input, selected_model):
118
  try:
119
+ tutor_output = generate_tutor_output(subject, difficulty, student_input)
120
+ parsed = eval(tutor_output) # Convert string to dictionary
121
+ images = generate_images(student_input, selected_model) # Generate images
122
+ return parsed["lesson"], parsed["question"], parsed["feedback"], images[0], images[1], images[2]
123
  except:
124
+ return "Error parsing output", "No question available", "No feedback available", None, None, None
125
 
126
  submit_button.click(
127
+ fn=process_output,
128
+ inputs=[subject, difficulty, student_input, model_selector],
129
+ outputs=[lesson_output, question_output, feedback_output, output1, output2, output3]
130
  )
131
 
132
  if __name__ == "__main__":