SameerArz commited on
Commit
c295df7
·
verified ·
1 Parent(s): 2135d8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -57
app.py CHANGED
@@ -1,19 +1,10 @@
1
  import gradio as gr
2
  from groq import Groq
3
  import os
4
- import threading # Import threading module
5
 
6
  # Initialize Groq client with your API key
7
  client = Groq(api_key=os.environ["GROQ_API_KEY"])
8
 
9
- # Load Text-to-Image Models
10
- model1 = gr.load("models/prithivMLmods/SD3.5-Turbo-Realism-2.0-LoRA")
11
- model2 = gr.load("models/Purz/face-projection")
12
-
13
- # Stop event for threading (image generation)
14
- stop_event = threading.Event()
15
-
16
- # Function to generate tutor output (lesson, question, feedback)
17
  def generate_tutor_output(subject, difficulty, student_input):
18
  prompt = f"""
19
  You are an expert tutor in {subject} at the {difficulty} level.
@@ -44,31 +35,19 @@ def generate_tutor_output(subject, difficulty, student_input):
44
 
45
  return completion.choices[0].message.content
46
 
47
- # Function to generate images based on model selection
48
- def generate_images(text, selected_model):
49
- stop_event.clear()
50
-
51
- if selected_model == "Model 1 (Turbo Realism)":
52
- model = model1
53
- elif selected_model == "Model 2 (Face Projection)":
54
- model = model2
55
- else:
56
- return ["Invalid model selection."] * 3
57
-
58
- results = []
59
- for i in range(3):
60
- if stop_event.is_set():
61
- return ["Image generation stopped by user."] * 3
62
-
63
- modified_text = f"{text} variation {i+1}"
64
- result = model(modified_text)
65
- results.append(result)
66
-
67
- return results
68
 
69
  # Set up the Gradio interface
70
  with gr.Blocks() as demo:
71
- gr.Markdown("# 🎓 Your AI Tutor with Visuals & Images")
72
 
73
  with gr.Row():
74
  with gr.Column(scale=2):
@@ -88,46 +67,52 @@ with gr.Blocks() as demo:
88
  label="Your Input",
89
  info="Enter the topic you want to learn"
90
  )
91
- model_selector = gr.Radio(
92
- ["Model 1 (Turbo Realism)", "Model 2 (Face Projection)"],
93
- label="Select Image Generation Model",
94
- value="Model 1 (Turbo Realism)"
95
- )
96
- submit_button = gr.Button("Generate Lesson & Images", variant="primary")
97
 
98
  with gr.Column(scale=3):
99
- # Output fields for lesson, question, feedback, and images
100
  lesson_output = gr.Markdown(label="Lesson")
101
  question_output = gr.Markdown(label="Comprehension Question")
102
  feedback_output = gr.Markdown(label="Feedback")
103
- output1 = gr.Image(label="Generated Image 1")
104
- output2 = gr.Image(label="Generated Image 2")
105
- output3 = gr.Image(label="Generated Image 3")
106
 
 
 
 
 
 
 
 
107
  gr.Markdown("""
108
  ### How to Use
109
  1. Select a subject from the dropdown.
110
  2. Choose your difficulty level.
111
  3. Enter the topic or question you'd like to explore.
112
- 4. Choose the model for image generation.
113
- 5. Click 'Generate Lesson & Images' to receive a personalized lesson, question, feedback, and images.
114
  6. Review the AI-generated content to enhance your learning.
115
- 7. Feel free to ask follow-up questions or explore new topics!
116
  """)
117
-
118
- def process_output(subject, difficulty, student_input, selected_model):
 
119
  try:
120
- tutor_output = generate_tutor_output(subject, difficulty, student_input)
121
- parsed = eval(tutor_output) # Convert string to dictionary
122
- images = generate_images(student_input, selected_model) # Generate images
123
- return parsed["lesson"], parsed["question"], parsed["feedback"], images[0], images[1], images[2]
124
- except:
125
- return "Error parsing output", "No question available", "No feedback available", None, None, None
126
-
127
- submit_button.click(
128
- fn=process_output,
129
- inputs=[subject, difficulty, student_input, model_selector],
130
- outputs=[lesson_output, question_output, feedback_output, output1, output2, output3]
 
 
 
 
 
 
 
131
  )
132
 
133
  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
  prompt = f"""
10
  You are an expert tutor in {subject} at the {difficulty} level.
 
35
 
36
  return completion.choices[0].message.content
37
 
38
+ # Function to generate visual output (image) based on the lesson/topic
39
+ def generate_visual(topic):
40
+ # You can integrate your image generation model here.
41
+ # For simplicity, let's assume you have an image generation model available.
42
+ # Here's an example where we generate a simple placeholder image based on the topic.
43
+
44
+ # Example image generation logic (you can replace this with your actual model).
45
+ # For now, returning a placeholder image.
46
+ return "https://via.placeholder.com/500x300.png?text=" + topic.replace(" ", "+")
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  # Set up the Gradio interface
49
  with gr.Blocks() as demo:
50
+ gr.Markdown("# 🎓 Your AI Tutor")
51
 
52
  with gr.Row():
53
  with gr.Column(scale=2):
 
67
  label="Your Input",
68
  info="Enter the topic you want to learn"
69
  )
70
+ generate_lesson_button = gr.Button("Generate Lesson", variant="primary")
 
 
 
 
 
71
 
72
  with gr.Column(scale=3):
73
+ # Output fields for lesson, question, and feedback
74
  lesson_output = gr.Markdown(label="Lesson")
75
  question_output = gr.Markdown(label="Comprehension Question")
76
  feedback_output = gr.Markdown(label="Feedback")
 
 
 
77
 
78
+ # Separate section for visual generation
79
+ with gr.Row():
80
+ topic_for_visual = gr.Textbox(label="Topic for Visual", placeholder="Generated Topic")
81
+ generate_visual_button = gr.Button("Generate Visual Output")
82
+ visual_output = gr.Image(label="Generated Visual")
83
+
84
+ # Markdown instructions
85
  gr.Markdown("""
86
  ### How to Use
87
  1. Select a subject from the dropdown.
88
  2. Choose your difficulty level.
89
  3. Enter the topic or question you'd like to explore.
90
+ 4. Click 'Generate Lesson' to receive a personalized lesson, question, and feedback.
91
+ 5. After generating the lesson, you can click 'Generate Visual Output' to create a related visual representation.
92
  6. Review the AI-generated content to enhance your learning.
 
93
  """)
94
+
95
+ # Function to process lesson and pass the topic for visual generation
96
+ def process_lesson_output(subject, difficulty, student_input):
97
  try:
98
+ parsed = eval(generate_tutor_output(subject, difficulty, student_input)) # Convert string to dictionary
99
+ # Store the lesson topic (you can use the lesson or question as a topic for visual generation)
100
+ return parsed["lesson"], parsed["question"], parsed["feedback"], student_input
101
+ except Exception as e:
102
+ return f"Error processing output: {e}", "No question available", "No feedback available", ""
103
+
104
+ # Generate Lesson Button
105
+ generate_lesson_button.click(
106
+ fn=process_lesson_output,
107
+ inputs=[subject, difficulty, student_input],
108
+ outputs=[lesson_output, question_output, feedback_output, topic_for_visual]
109
+ )
110
+
111
+ # Generate Visual Button
112
+ generate_visual_button.click(
113
+ fn=generate_visual,
114
+ inputs=topic_for_visual,
115
+ outputs=visual_output
116
  )
117
 
118
  if __name__ == "__main__":