Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from groq import Groq
|
| 3 |
import os
|
| 4 |
-
import threading
|
| 5 |
|
| 6 |
-
# Initialize Groq client
|
| 7 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 8 |
|
| 9 |
# Load Text-to-Image Models
|
|
@@ -30,7 +30,7 @@ def generate_tutor_output(subject, difficulty, student_input):
|
|
| 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. 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,
|
|
@@ -63,40 +63,14 @@ def generate_images(text, selected_model):
|
|
| 63 |
|
| 64 |
return results
|
| 65 |
|
| 66 |
-
# New function for processing visual input
|
| 67 |
-
def process_visual_input(image, task, question=""):
|
| 68 |
-
"""Processes the uploaded image based on the selected task."""
|
| 69 |
-
if task == "Image Captioning":
|
| 70 |
-
prompt = "Describe this image in detail."
|
| 71 |
-
elif task == "OCR (Text Extraction)":
|
| 72 |
-
prompt = "Extract all readable text from this image."
|
| 73 |
-
elif task == "Visual Question Answering":
|
| 74 |
-
prompt = f"Answer this question based on the image: {question}"
|
| 75 |
-
else:
|
| 76 |
-
return "Invalid task selected."
|
| 77 |
-
|
| 78 |
-
# Sending image + prompt to the model
|
| 79 |
-
completion = client.chat.completions.create(
|
| 80 |
-
messages=[{
|
| 81 |
-
"role": "system",
|
| 82 |
-
"content": "You are an expert AI that analyzes images and provides captions, extracts text, or answers visual questions."
|
| 83 |
-
}, {
|
| 84 |
-
"role": "user",
|
| 85 |
-
"content": prompt,
|
| 86 |
-
}],
|
| 87 |
-
model="llava-1.5-7b", # Using a vision-language model
|
| 88 |
-
max_tokens=500,
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
return completion.choices[0].message.content
|
| 92 |
-
|
| 93 |
# Set up the Gradio interface
|
| 94 |
with gr.Blocks() as demo:
|
| 95 |
gr.Markdown("# 🎓 Your AI Tutor with Visuals & Images")
|
| 96 |
|
| 97 |
-
# Section
|
| 98 |
with gr.Row():
|
| 99 |
with gr.Column(scale=2):
|
|
|
|
| 100 |
subject = gr.Dropdown(
|
| 101 |
["Math", "Science", "History", "Literature", "Code", "AI"],
|
| 102 |
label="Subject",
|
|
@@ -115,13 +89,15 @@ with gr.Blocks() as demo:
|
|
| 115 |
submit_button_text = gr.Button("Generate Lesson & Question", variant="primary")
|
| 116 |
|
| 117 |
with gr.Column(scale=3):
|
|
|
|
| 118 |
lesson_output = gr.Markdown(label="Lesson")
|
| 119 |
question_output = gr.Markdown(label="Comprehension Question")
|
| 120 |
feedback_output = gr.Markdown(label="Feedback")
|
| 121 |
|
| 122 |
-
# Section
|
| 123 |
with gr.Row():
|
| 124 |
with gr.Column(scale=2):
|
|
|
|
| 125 |
model_selector = gr.Radio(
|
| 126 |
["Model 1 (Turbo Realism)", "Model 2 (Face Projection)"],
|
| 127 |
label="Select Image Generation Model",
|
|
@@ -130,40 +106,18 @@ with gr.Blocks() as demo:
|
|
| 130 |
submit_button_visual = gr.Button("Generate Visuals", variant="primary")
|
| 131 |
|
| 132 |
with gr.Column(scale=3):
|
|
|
|
| 133 |
output1 = gr.Image(label="Generated Image 1")
|
| 134 |
output2 = gr.Image(label="Generated Image 2")
|
| 135 |
output3 = gr.Image(label="Generated Image 3")
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
value="Image Captioning"
|
| 145 |
-
)
|
| 146 |
-
question_input = gr.Textbox(
|
| 147 |
-
placeholder="Enter question (only for VQA)",
|
| 148 |
-
label="Question (Optional)",
|
| 149 |
-
visible=False
|
| 150 |
-
)
|
| 151 |
-
submit_button_visual_input = gr.Button("Process Image", variant="primary")
|
| 152 |
-
|
| 153 |
-
with gr.Column(scale=3):
|
| 154 |
-
visual_output = gr.Markdown(label="Image Analysis Result")
|
| 155 |
-
|
| 156 |
-
# Toggle visibility of question input for VQA
|
| 157 |
-
def toggle_question_visibility(task):
|
| 158 |
-
return gr.update(visible=(task == "Visual Question Answering"))
|
| 159 |
-
|
| 160 |
-
task_selector.change(
|
| 161 |
-
fn=toggle_question_visibility,
|
| 162 |
-
inputs=[task_selector],
|
| 163 |
-
outputs=[question_input]
|
| 164 |
-
)
|
| 165 |
-
|
| 166 |
-
# Process text-based learning
|
| 167 |
def process_output_text(subject, difficulty, student_input):
|
| 168 |
try:
|
| 169 |
tutor_output = generate_tutor_output(subject, difficulty, student_input)
|
|
@@ -172,27 +126,21 @@ with gr.Blocks() as demo:
|
|
| 172 |
except:
|
| 173 |
return "Error parsing output", "No question available", "No feedback available"
|
| 174 |
|
| 175 |
-
# Process image generation
|
| 176 |
def process_output_visual(text, selected_model):
|
| 177 |
try:
|
| 178 |
-
images = generate_images(text, selected_model)
|
| 179 |
return images[0], images[1], images[2]
|
| 180 |
except:
|
| 181 |
return None, None, None
|
| 182 |
-
|
| 183 |
-
#
|
| 184 |
-
submit_button_visual_input.click(
|
| 185 |
-
fn=process_visual_input,
|
| 186 |
-
inputs=[image_input, task_selector, question_input],
|
| 187 |
-
outputs=[visual_output]
|
| 188 |
-
)
|
| 189 |
-
|
| 190 |
submit_button_text.click(
|
| 191 |
fn=process_output_text,
|
| 192 |
inputs=[subject, difficulty, student_input],
|
| 193 |
outputs=[lesson_output, question_output, feedback_output]
|
| 194 |
)
|
| 195 |
|
|
|
|
| 196 |
submit_button_visual.click(
|
| 197 |
fn=process_output_visual,
|
| 198 |
inputs=[student_input, model_selector],
|
|
|
|
| 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
|
|
|
|
| 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,
|
|
|
|
| 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",
|
|
|
|
| 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",
|
|
|
|
| 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)
|
|
|
|
| 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],
|