Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 48 |
-
def
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 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
|
| 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 |
-
|
| 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,
|
| 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.
|
| 113 |
-
5.
|
| 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 |
-
|
|
|
|
| 119 |
try:
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
fn=
|
| 129 |
-
inputs=[subject, difficulty, student_input
|
| 130 |
-
outputs=[lesson_output, question_output, feedback_output,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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__":
|