Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,31 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
from groq import Groq
|
3 |
import os
|
4 |
-
import
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
|
13 |
-
#
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# Function to generate tutor output (lesson, question, feedback)
|
17 |
def generate_tutor_output(subject, difficulty, student_input):
|
@@ -35,117 +49,102 @@ def generate_tutor_output(subject, difficulty, student_input):
|
|
35 |
"role": "user",
|
36 |
"content": prompt,
|
37 |
}],
|
38 |
-
model="mixtral-8x7b-32768",
|
39 |
max_tokens=1000,
|
40 |
)
|
41 |
-
|
42 |
return completion.choices[0].message.content
|
43 |
|
44 |
-
# Function to generate images
|
45 |
def generate_images(text, selected_model):
|
46 |
-
stop_event.clear()
|
47 |
-
|
48 |
if selected_model == "Model 1 (Turbo Realism)":
|
49 |
-
|
50 |
elif selected_model == "Model 2 (Face Projection)":
|
51 |
-
|
52 |
else:
|
53 |
return ["Invalid model selection."] * 3
|
54 |
|
55 |
results = []
|
56 |
for i in range(3):
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
modified_text = f"{text} variation {i+1}"
|
61 |
-
result = model(modified_text)
|
62 |
-
results.append(result)
|
63 |
-
|
64 |
return results
|
65 |
|
66 |
-
#
|
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,
|
117 |
-
2. **Visual Section**: Select
|
118 |
-
3. Review the AI-generated content to enhance your learning
|
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 =
|
125 |
return parsed["lesson"], parsed["question"], parsed["feedback"]
|
126 |
-
except:
|
127 |
-
return "Error
|
128 |
-
|
129 |
def process_output_visual(text, selected_model):
|
130 |
try:
|
131 |
-
images = generate_images(text, selected_model)
|
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 |
-
|
151 |
-
|
|
|
1 |
+
# app.py
|
2 |
import gradio as gr
|
3 |
from groq import Groq
|
4 |
import os
|
5 |
+
import json
|
6 |
+
import torch
|
7 |
+
from diffusers import AutoPipelineForText2Image
|
8 |
|
9 |
+
# Get API keys from environment variables (set in Space settings)
|
10 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
11 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
12 |
|
13 |
+
# Check if keys are provided
|
14 |
+
if not GROQ_API_KEY or not HF_TOKEN:
|
15 |
+
raise ValueError("Please set GROQ_API_KEY and HF_TOKEN in the Space settings under 'Variables'.")
|
16 |
|
17 |
+
# Initialize Groq client
|
18 |
+
client = Groq(api_key=GROQ_API_KEY)
|
19 |
+
|
20 |
+
# Set up device and image generation pipeline
|
21 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
22 |
+
pipeline = AutoPipelineForText2Image.from_pretrained(
|
23 |
+
"black-forest-labs/FLUX.1-dev",
|
24 |
+
torch_dtype=torch.bfloat16,
|
25 |
+
use_auth_token=HF_TOKEN # Authenticate directly with HF token
|
26 |
+
).to(device)
|
27 |
+
pipeline.load_lora_weights("Purz/face-projection", weight_name="purz-f4c3_p40j3ct10n.safetensors")
|
28 |
+
pipeline.enable_model_cpu_offload() # Optimize memory usage
|
29 |
|
30 |
# Function to generate tutor output (lesson, question, feedback)
|
31 |
def generate_tutor_output(subject, difficulty, student_input):
|
|
|
49 |
"role": "user",
|
50 |
"content": prompt,
|
51 |
}],
|
52 |
+
model="mixtral-8x7b-32768",
|
53 |
max_tokens=1000,
|
54 |
)
|
|
|
55 |
return completion.choices[0].message.content
|
56 |
|
57 |
+
# Function to generate images
|
58 |
def generate_images(text, selected_model):
|
|
|
|
|
59 |
if selected_model == "Model 1 (Turbo Realism)":
|
60 |
+
prompt_prefix = "realistic, high detail, "
|
61 |
elif selected_model == "Model 2 (Face Projection)":
|
62 |
+
prompt_prefix = "f4c3_p40j3ct10n, projection on a face, "
|
63 |
else:
|
64 |
return ["Invalid model selection."] * 3
|
65 |
|
66 |
results = []
|
67 |
for i in range(3):
|
68 |
+
modified_text = f"{prompt_prefix}{text} variation {i+1}"
|
69 |
+
image = pipeline(modified_text, num_inference_steps=20).images[0] # Faster inference
|
70 |
+
results.append(image)
|
|
|
|
|
|
|
|
|
71 |
return results
|
72 |
|
73 |
+
# Gradio interface
|
74 |
+
with gr.Blocks(title="AI Tutor with Visuals") as demo:
|
75 |
gr.Markdown("# 🎓 Your AI Tutor with Visuals & Images")
|
76 |
|
|
|
77 |
with gr.Row():
|
78 |
with gr.Column(scale=2):
|
|
|
79 |
subject = gr.Dropdown(
|
80 |
+
["Math", "Science", "History", "Literature", "Code", "AI"],
|
81 |
+
label="Subject",
|
82 |
info="Choose the subject of your lesson"
|
83 |
)
|
84 |
difficulty = gr.Radio(
|
85 |
+
["Beginner", "Intermediate", "Advanced"],
|
86 |
+
label="Difficulty Level",
|
87 |
info="Select your proficiency level"
|
88 |
)
|
89 |
student_input = gr.Textbox(
|
90 |
+
placeholder="Type your query here...",
|
91 |
+
label="Your Input",
|
92 |
info="Enter the topic you want to learn"
|
93 |
)
|
94 |
submit_button_text = gr.Button("Generate Lesson & Question", variant="primary")
|
95 |
+
|
96 |
with gr.Column(scale=3):
|
|
|
97 |
lesson_output = gr.Markdown(label="Lesson")
|
98 |
question_output = gr.Markdown(label="Comprehension Question")
|
99 |
feedback_output = gr.Markdown(label="Feedback")
|
100 |
+
|
|
|
101 |
with gr.Row():
|
102 |
with gr.Column(scale=2):
|
|
|
103 |
model_selector = gr.Radio(
|
104 |
["Model 1 (Turbo Realism)", "Model 2 (Face Projection)"],
|
105 |
label="Select Image Generation Model",
|
106 |
value="Model 1 (Turbo Realism)"
|
107 |
)
|
108 |
submit_button_visual = gr.Button("Generate Visuals", variant="primary")
|
109 |
+
|
110 |
with gr.Column(scale=3):
|
|
|
111 |
output1 = gr.Image(label="Generated Image 1")
|
112 |
output2 = gr.Image(label="Generated Image 2")
|
113 |
output3 = gr.Image(label="Generated Image 3")
|
114 |
+
|
115 |
gr.Markdown("""
|
116 |
### How to Use
|
117 |
+
1. **Text Section**: Select a subject and difficulty, type your query, and click 'Generate Lesson & Question' to get your personalized lesson, question, and feedback.
|
118 |
+
2. **Visual Section**: Select a model and click 'Generate Visuals' to see 3 image variations based on your input.
|
119 |
+
3. Review the AI-generated content to enhance your learning!
|
120 |
""")
|
121 |
+
|
122 |
def process_output_text(subject, difficulty, student_input):
|
123 |
try:
|
124 |
tutor_output = generate_tutor_output(subject, difficulty, student_input)
|
125 |
+
parsed = json.loads(tutor_output)
|
126 |
return parsed["lesson"], parsed["question"], parsed["feedback"]
|
127 |
+
except Exception as e:
|
128 |
+
return f"Error: {str(e)}", "No question available", "No feedback available"
|
129 |
+
|
130 |
def process_output_visual(text, selected_model):
|
131 |
try:
|
132 |
+
images = generate_images(text, selected_model)
|
133 |
return images[0], images[1], images[2]
|
134 |
+
except Exception as e:
|
135 |
return None, None, None
|
136 |
+
|
|
|
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 |
submit_button_visual.click(
|
144 |
fn=process_output_visual,
|
145 |
inputs=[student_input, model_selector],
|
146 |
outputs=[output1, output2, output3]
|
147 |
)
|
148 |
|
149 |
+
# In Hugging Face Spaces, this variable is automatically used as the app entry point
|
150 |
+
demo.launch()
|