SameerArz's picture
Update app.py
d15776e verified
raw
history blame
5.38 kB
import gradio as gr
from groq import Groq
import os
# Initialize Groq client with your API key
client = Groq(api_key=os.environ["GROQ_API_KEY"])
# Load Text-to-Image Models
model1 = gr.load("models/prithivMLmods/SD3.5-Turbo-Realism-2.0-LoRA")
model2 = gr.load("models/Purz/face-projection")
# Stop event for threading (image generation)
stop_event = threading.Event()
# Function to generate tutor output (lesson, question, feedback)
def generate_tutor_output(subject, difficulty, student_input):
prompt = f"""
You are an expert tutor in {subject} at the {difficulty} level.
The student has provided the following input: "{student_input}"
Please generate:
1. A brief, engaging lesson on the topic (2-3 paragraphs)
2. A thought-provoking question to check understanding
3. Constructive feedback on the student's input
Format your response as a JSON object with keys: "lesson", "question", "feedback"
"""
completion = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "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. Your goal is to not just impart knowledge, but to inspire a love for learning and critical thinking.",
},
{
"role": "user",
"content": prompt,
}
],
model="mixtral-8x7b-32768", # Model for text generation
max_tokens=1000,
)
return completion.choices[0].message.content
# Function to generate images based on model selection
def generate_images(text, selected_model):
stop_event.clear()
if selected_model == "Model 1 (Turbo Realism)":
model = model1
elif selected_model == "Model 2 (Face Projection)":
model = model2
else:
return ["Invalid model selection."] * 3
results = []
for i in range(3):
if stop_event.is_set():
return ["Image generation stopped by user."] * 3
modified_text = f"{text} variation {i+1}"
result = model(modified_text)
results.append(result)
return results
# Set up the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# 🎓 Your AI Tutor with Visuals & Images")
with gr.Row():
with gr.Column(scale=2):
# Input fields for subject, difficulty, and student input
subject = gr.Dropdown(
["Math", "Science", "History", "Literature", "Code", "AI"],
label="Subject",
info="Choose the subject of your lesson"
)
difficulty = gr.Radio(
["Beginner", "Intermediate", "Advanced"],
label="Difficulty Level",
info="Select your proficiency level"
)
student_input = gr.Textbox(
placeholder="Type your query here...",
label="Your Input",
info="Enter the topic you want to learn"
)
model_selector = gr.Radio(
["Model 1 (Turbo Realism)", "Model 2 (Face Projection)"],
label="Select Image Generation Model",
value="Model 1 (Turbo Realism)"
)
submit_button = gr.Button("Generate Lesson & Images", variant="primary")
with gr.Column(scale=3):
# Output fields for lesson, question, feedback, and images
lesson_output = gr.Markdown(label="Lesson")
question_output = gr.Markdown(label="Comprehension Question")
feedback_output = gr.Markdown(label="Feedback")
output1 = gr.Image(label="Generated Image 1")
output2 = gr.Image(label="Generated Image 2")
output3 = gr.Image(label="Generated Image 3")
gr.Markdown("""
### How to Use
1. Select a subject from the dropdown.
2. Choose your difficulty level.
3. Enter the topic or question you'd like to explore.
4. Choose the model for image generation.
5. Click 'Generate Lesson & Images' to receive a personalized lesson, question, feedback, and images.
6. Review the AI-generated content to enhance your learning.
7. Feel free to ask follow-up questions or explore new topics!
""")
def process_output(subject, difficulty, student_input, selected_model):
try:
tutor_output = generate_tutor_output(subject, difficulty, student_input)
parsed = eval(tutor_output) # Convert string to dictionary
images = generate_images(student_input, selected_model) # Generate images
return parsed["lesson"], parsed["question"], parsed["feedback"], images[0], images[1], images[2]
except:
return "Error parsing output", "No question available", "No feedback available", None, None, None
submit_button.click(
fn=process_output,
inputs=[subject, difficulty, student_input, model_selector],
outputs=[lesson_output, question_output, feedback_output, output1, output2, output3]
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)