SameerArz commited on
Commit
996d3d2
·
verified ·
1 Parent(s): 781c41b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -95
app.py CHANGED
@@ -1,124 +1,150 @@
1
  import gradio as gr
2
- import os
3
- import threading
4
- import base64
5
- from io import BytesIO
6
  from groq import Groq
7
- from diffusers import StableDiffusionPipeline
8
- import torch
9
 
10
- # 🔹 Initialize Groq API Client (FREE)
11
- groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
12
 
13
- # 🔹 Load Text-to-Image Models (Updated to use diffusers for Stable Diffusion)
14
  model1 = gr.load("models/prithivMLmods/SD3.5-Turbo-Realism-2.0-LoRA")
15
  model2 = gr.load("models/Purz/face-projection")
16
 
17
- # Load Stable Diffusion Model Properly (Using diffusers)
18
- model3 = StableDiffusionPipeline.from_pretrained(
19
- "stablediffusion/stable-diffusion-2-1",
20
- torch_dtype=torch.float16
21
- ).to("cuda") # Move to GPU if available
22
-
23
- # 🔹 Stop Event for Threading
24
  stop_event = threading.Event()
25
 
26
- # 🔹 Convert PIL image to Base64
27
- def pil_to_base64(pil_image, image_format="jpeg"):
28
- buffered = BytesIO()
29
- pil_image.save(buffered, format=image_format)
30
- base64_string = base64.b64encode(buffered.getvalue()).decode("utf-8")
31
- return base64_string, image_format
32
-
33
- # 🔹 Function for Visual Question Answering (VQA) with Mixtral-8x7B
34
- def answer_question(text, image, temperature=0.0, max_tokens=1024):
35
- base64_string, file_format = pil_to_base64(image)
36
-
37
- messages = [
38
- {
39
- "role": "user",
40
- "content": [
41
- {"type": "text", "text": text},
42
- {"type": "image_url", "image_url": f"data:image/{file_format};base64,{base64_string}"}
43
- ]
44
- }
45
- ]
46
 
47
- chat_response = groq_client.chat.completions.create(
48
- model="mixtral-8x7b-32768",
49
- messages=messages,
50
- temperature=temperature,
51
- max_tokens=max_tokens
 
 
 
 
 
 
 
 
52
  )
 
 
53
 
54
- return chat_response.choices[0].message.content
55
-
56
- # 🔹 Function to Generate Three Images (Multi-Output)
57
- def generate_images(prompt):
58
  stop_event.clear()
59
- img1 = model1.predict(prompt)
60
- img2 = model2.predict(prompt)
61
 
62
- # Fix: Use Stable Diffusion correctly
63
- img3 = model3(prompt).images[0] # Get first image
 
 
 
 
64
 
65
- return img1, img2, img3
 
 
 
66
 
67
- # 🔹 Clear All Fields
68
- def clear_all():
69
- return "", None, "", None, None, None
70
 
71
- # 🔹 Set up Gradio Interface
 
 
72
  with gr.Blocks() as demo:
73
- gr.Markdown("# 🎓 AI Tutor, VQA & Image Generation")
74
 
75
- # 🔹 Section 1: Visual Question Answering (Groq)
76
- gr.Markdown("## 🖼️ Visual Question Answering (Mixtral-8x7B)")
77
  with gr.Row():
78
  with gr.Column(scale=2):
79
- question = gr.Textbox(placeholder="Ask about the image...", lines=2)
80
- image = gr.Image(type="pil")
81
- with gr.Row():
82
- temperature = gr.Slider(label="Temperature", minimum=0.0, maximum=1.0, value=0.0, step=0.1)
83
- max_tokens = gr.Slider(label="Max Tokens", minimum=128, maximum=2048, value=1024, step=128)
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  with gr.Column(scale=3):
86
- output_text = gr.Textbox(lines=10, label="Mixtral VQA Response")
 
 
 
87
 
 
88
  with gr.Row():
89
- clear_btn = gr.Button("Clear", variant="secondary")
90
- submit_btn_vqa = gr.Button("Submit", variant="primary")
91
-
92
- # 🔹 Section 2: Image Generation (3 Outputs)
93
- gr.Markdown("## 🎨 AI-Generated Images (3 Variations)")
94
- with gr.Row():
95
- prompt = gr.Textbox(placeholder="Describe the image you want...", lines=2)
96
- generate_btn = gr.Button("Generate Images", variant="primary")
97
-
98
- with gr.Row():
99
- image1 = gr.Image(label="Image 1")
100
- image2 = gr.Image(label="Image 2")
101
- image3 = gr.Image(label="Image 3")
102
-
103
- # 🔹 VQA Processing
104
- submit_btn_vqa.click(
105
- fn=answer_question,
106
- inputs=[question, image, temperature, max_tokens],
107
- outputs=[output_text]
108
- )
109
-
110
- # 🔹 Image Generation Processing
111
- generate_btn.click(
112
- fn=generate_images,
113
- inputs=[prompt],
114
- outputs=[image1, image2, image3]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  )
116
-
117
- # 🔹 Clear All Inputs
118
- clear_btn.click(
119
- fn=clear_all,
120
- inputs=[],
121
- outputs=[question, image, output_text, image1, image2, image3]
122
  )
123
 
124
  if __name__ == "__main__":
 
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.
20
+ The student has provided the following input: "{student_input}"
21
+
22
+ Please generate:
23
+ 1. A brief, engaging lesson on the topic (2-3 paragraphs)
24
+ 2. A thought-provoking question to check understanding
25
+ 3. Constructive feedback on the student's input
 
 
 
 
 
 
 
 
 
 
26
 
27
+ Format your response as a JSON object with keys: "lesson", "question", "feedback"
28
+ """
29
+
30
+ completion = client.chat.completions.create(
31
+ messages=[{
32
+ "role": "system",
33
+ "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."
34
+ }, {
35
+ "role": "user",
36
+ "content": prompt,
37
+ }],
38
+ model="mixtral-8x7b-32768", # Model for text generation
39
+ max_tokens=1000,
40
  )
41
+
42
+ return completion.choices[0].message.content
43
 
44
+ # Function to generate images based on model selection
45
+ def generate_images(text, selected_model):
 
 
46
  stop_event.clear()
 
 
47
 
48
+ if selected_model == "Model 1 (Turbo Realism)":
49
+ model = model1
50
+ elif selected_model == "Model 2 (Face Projection)":
51
+ model = model2
52
+ else:
53
+ return ["Invalid model selection."] * 3
54
 
55
+ results = []
56
+ for i in range(3):
57
+ if stop_event.is_set():
58
+ return ["Image generation stopped by user."] * 3
59
 
60
+ modified_text = f"{text} variation {i+1}"
61
+ result = model(modified_text)
62
+ results.append(result)
63
 
64
+ return results
65
+
66
+ # Set up the Gradio interface
67
  with gr.Blocks() as demo:
68
+ gr.Markdown("# 🎓 Your AI Tutor with Visuals & Images")
69
 
70
+ # Section for generating Text-based output (lesson, question, feedback)
 
71
  with gr.Row():
72
  with gr.Column(scale=2):
73
+ # Input fields for subject, difficulty, and student input for textual output
74
+ subject = gr.Dropdown(
75
+ ["Math", "Science", "History", "Literature", "Code", "AI"],
76
+ label="Subject",
77
+ info="Choose the subject of your lesson"
78
+ )
79
+ difficulty = gr.Radio(
80
+ ["Beginner", "Intermediate", "Advanced"],
81
+ label="Difficulty Level",
82
+ info="Select your proficiency level"
83
+ )
84
+ student_input = gr.Textbox(
85
+ placeholder="Type your query here...",
86
+ label="Your Input",
87
+ info="Enter the topic you want to learn"
88
+ )
89
+ submit_button_text = gr.Button("Generate Lesson & Question", variant="primary")
90
 
91
  with gr.Column(scale=3):
92
+ # Output fields for lesson, question, and feedback
93
+ lesson_output = gr.Markdown(label="Lesson")
94
+ question_output = gr.Markdown(label="Comprehension Question")
95
+ feedback_output = gr.Markdown(label="Feedback")
96
 
97
+ # Section for generating Visual output
98
  with gr.Row():
99
+ with gr.Column(scale=2):
100
+ # Input fields for text and model selection for image generation
101
+ model_selector = gr.Radio(
102
+ ["Model 1 (Turbo Realism)", "Model 2 (Face Projection)"],
103
+ label="Select Image Generation Model",
104
+ value="Model 1 (Turbo Realism)"
105
+ )
106
+ submit_button_visual = gr.Button("Generate Visuals", variant="primary")
107
+
108
+ with gr.Column(scale=3):
109
+ # Output fields for generated images
110
+ output1 = gr.Image(label="Generated Image 1")
111
+ output2 = gr.Image(label="Generated Image 2")
112
+ output3 = gr.Image(label="Generated Image 3")
113
+
114
+ gr.Markdown("""
115
+ ### How to Use
116
+ 1. **Text Section**: Select a subject and difficulty, type your query, and click 'Generate Lesson & Question' to get your personalized lesson, comprehension question, and feedback.
117
+ 2. **Visual Section**: Select the model for image generation, then click 'Generate Visuals' to receive 3 variations of an image based on your topic.
118
+ 3. Review the AI-generated content to enhance your learning experience!
119
+ """)
120
+
121
+ def process_output_text(subject, difficulty, student_input):
122
+ try:
123
+ tutor_output = generate_tutor_output(subject, difficulty, student_input)
124
+ parsed = eval(tutor_output) # Convert string to dictionary
125
+ return parsed["lesson"], parsed["question"], parsed["feedback"]
126
+ except:
127
+ return "Error parsing output", "No question available", "No feedback available"
128
+
129
+ def process_output_visual(text, selected_model):
130
+ try:
131
+ images = generate_images(text, selected_model) # Generate images
132
+ return images[0], images[1], images[2]
133
+ except:
134
+ return None, None, None
135
+
136
+ # Generate Text-based Output
137
+ submit_button_text.click(
138
+ fn=process_output_text,
139
+ inputs=[subject, difficulty, student_input],
140
+ outputs=[lesson_output, question_output, feedback_output]
141
  )
142
+
143
+ # Generate Visual Output
144
+ submit_button_visual.click(
145
+ fn=process_output_visual,
146
+ inputs=[student_input, model_selector],
147
+ outputs=[output1, output2, output3]
148
  )
149
 
150
  if __name__ == "__main__":