Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,230 +1,51 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
4 |
-
|
5 |
-
import spaces #[uncomment to use ZeroGPU]
|
6 |
-
from diffusers import DiffusionPipeline
|
7 |
import torch
|
8 |
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
torch_dtype = torch.float32
|
16 |
|
|
|
17 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
18 |
-
pipe
|
19 |
-
|
20 |
-
MAX_SEED = np.iinfo(np.int32).max
|
21 |
-
MAX_IMAGE_SIZE = 1024
|
22 |
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
def infer(
|
26 |
-
prompt,
|
27 |
-
negative_prompt,
|
28 |
-
seed,
|
29 |
-
randomize_seed,
|
30 |
-
width,
|
31 |
-
height,
|
32 |
-
guidance_scale,
|
33 |
-
num_inference_steps,
|
34 |
-
progress=gr.Progress(track_tqdm=True),
|
35 |
-
):
|
36 |
-
if randomize_seed:
|
37 |
-
seed = random.randint(0, MAX_SEED)
|
38 |
|
39 |
-
generator = torch.Generator().manual_seed(seed)
|
40 |
|
|
|
|
|
41 |
image = pipe(
|
42 |
prompt=prompt,
|
43 |
-
negative_prompt=negative_prompt,
|
44 |
-
guidance_scale=guidance_scale,
|
45 |
num_inference_steps=num_inference_steps,
|
46 |
-
|
47 |
-
height=height,
|
48 |
generator=generator,
|
49 |
).images[0]
|
|
|
50 |
|
51 |
-
return image, seed
|
52 |
-
|
53 |
-
|
54 |
-
examples = [
|
55 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
56 |
-
"An astronaut riding a green horse",
|
57 |
-
"A delicious ceviche cheesecake slice",
|
58 |
-
]
|
59 |
-
|
60 |
-
css = """
|
61 |
-
/* General Styles */
|
62 |
-
#col-container {
|
63 |
-
margin: 0 auto;
|
64 |
-
max-width: 640px;
|
65 |
-
font-family: 'Arial', sans-serif;
|
66 |
-
color: #333;
|
67 |
-
background-color: #f0f4f8; /* Light gray background for better contrast */
|
68 |
-
border-radius: 15px;
|
69 |
-
padding: 20px;
|
70 |
-
}
|
71 |
-
|
72 |
-
#header {
|
73 |
-
text-align: center;
|
74 |
-
color: #1f5f99; /* Veshup Blue */
|
75 |
-
}
|
76 |
-
|
77 |
-
#title {
|
78 |
-
font-size: 36px;
|
79 |
-
font-weight: bold;
|
80 |
-
margin-bottom: 10px;
|
81 |
-
}
|
82 |
-
|
83 |
-
#subtitle {
|
84 |
-
font-size: 18px;
|
85 |
-
color: #555;
|
86 |
-
margin-bottom: 30px;
|
87 |
-
}
|
88 |
-
|
89 |
-
.gradio-button {
|
90 |
-
background-color: #1f5f99;
|
91 |
-
color: white;
|
92 |
-
font-weight: bold;
|
93 |
-
border-radius: 8px;
|
94 |
-
}
|
95 |
-
|
96 |
-
.gradio-button:hover {
|
97 |
-
background-color: #155b89;
|
98 |
-
}
|
99 |
-
|
100 |
-
.gradio-slider {
|
101 |
-
width: 100%;
|
102 |
-
}
|
103 |
-
|
104 |
-
.gradio-checkbox label {
|
105 |
-
font-weight: normal;
|
106 |
-
}
|
107 |
-
|
108 |
-
.gradio-markdown {
|
109 |
-
font-size: 16px;
|
110 |
-
line-height: 1.6;
|
111 |
-
}
|
112 |
-
|
113 |
-
/* Dark Mode adjustments for browser default theme */
|
114 |
-
@media (prefers-color-scheme: dark) {
|
115 |
-
#col-container {
|
116 |
-
background-color: #2e2e2e; /* Dark background for dark mode */
|
117 |
-
color: #e0e0e0; /* Light text for dark mode */
|
118 |
-
}
|
119 |
-
|
120 |
-
#header {
|
121 |
-
color: #a5c4f6; /* Lighter blue for dark mode */
|
122 |
-
}
|
123 |
-
|
124 |
-
.gradio-button {
|
125 |
-
background-color: #4f89b0;
|
126 |
-
}
|
127 |
-
|
128 |
-
.gradio-button:hover {
|
129 |
-
background-color: #3a6a8b;
|
130 |
-
}
|
131 |
-
|
132 |
-
.gradio-slider,
|
133 |
-
.gradio-checkbox {
|
134 |
-
background-color: #444; /* Darker elements in dark mode */
|
135 |
-
}
|
136 |
-
|
137 |
-
.gradio-markdown {
|
138 |
-
color: #d1d1d1; /* Lighter text for markdown */
|
139 |
-
}
|
140 |
-
}
|
141 |
-
"""
|
142 |
-
|
143 |
-
with gr.Blocks(css=css) as demo:
|
144 |
-
with gr.Column(elem_id="col-container"):
|
145 |
-
gr.Markdown("<div id='header'><h1 id='title'>Veginator: Veshup's Image Generation AI</h1><p id='subtitle'>Create stunning images with just a prompt. Powered by cutting-edge AI technology.</p></div>")
|
146 |
-
|
147 |
-
with gr.Row():
|
148 |
-
prompt = gr.Text(
|
149 |
-
label="Your Creative Prompt",
|
150 |
-
show_label=False,
|
151 |
-
max_lines=1,
|
152 |
-
placeholder="Enter your prompt here...",
|
153 |
-
container=False,
|
154 |
-
)
|
155 |
-
|
156 |
-
run_button = gr.Button("Generate Image", scale=0, variant="primary", elem_classes="gradio-button")
|
157 |
-
|
158 |
-
result = gr.Image(label="Generated Image", show_label=False)
|
159 |
-
|
160 |
-
with gr.Accordion("Advanced Settings", open=False):
|
161 |
-
negative_prompt = gr.Text(
|
162 |
-
label="Negative Prompt",
|
163 |
-
max_lines=1,
|
164 |
-
placeholder="Enter a negative prompt if needed",
|
165 |
-
visible=False,
|
166 |
-
)
|
167 |
-
|
168 |
-
seed = gr.Slider(
|
169 |
-
label="Seed",
|
170 |
-
minimum=0,
|
171 |
-
maximum=MAX_SEED,
|
172 |
-
step=1,
|
173 |
-
value=0,
|
174 |
-
)
|
175 |
-
|
176 |
-
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
177 |
-
|
178 |
-
with gr.Row():
|
179 |
-
width = gr.Slider(
|
180 |
-
label="Width",
|
181 |
-
minimum=256,
|
182 |
-
maximum=MAX_IMAGE_SIZE,
|
183 |
-
step=32,
|
184 |
-
value=1024, # Replace with defaults that work for your model
|
185 |
-
)
|
186 |
-
|
187 |
-
height = gr.Slider(
|
188 |
-
label="Height",
|
189 |
-
minimum=256,
|
190 |
-
maximum=MAX_IMAGE_SIZE,
|
191 |
-
step=32,
|
192 |
-
value=1024, # Replace with defaults that work for your model
|
193 |
-
)
|
194 |
-
|
195 |
-
with gr.Row():
|
196 |
-
guidance_scale = gr.Slider(
|
197 |
-
label="Guidance Scale",
|
198 |
-
minimum=0.0,
|
199 |
-
maximum=10.0,
|
200 |
-
step=0.1,
|
201 |
-
value=0.0, # Replace with defaults that work for your model
|
202 |
-
)
|
203 |
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
|
212 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
213 |
-
gr.on(
|
214 |
-
triggers=[run_button.click, prompt.submit],
|
215 |
-
fn=infer,
|
216 |
-
inputs=[
|
217 |
-
prompt,
|
218 |
-
negative_prompt,
|
219 |
-
seed,
|
220 |
-
randomize_seed,
|
221 |
-
width,
|
222 |
-
height,
|
223 |
-
guidance_scale,
|
224 |
-
num_inference_steps,
|
225 |
-
],
|
226 |
-
outputs=[result, seed],
|
227 |
-
)
|
228 |
|
229 |
if __name__ == "__main__":
|
230 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
4 |
+
from diffusers import DiffusionPipeline, EulerDiscreteScheduler
|
|
|
|
|
5 |
import torch
|
6 |
|
7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
9 |
|
10 |
+
# Define model and checkpoint
|
11 |
+
model_repo_id = "ByteDance/SDXL-Lightning"
|
12 |
+
ckpt = "sdxl_lightning_4step_unet.safetensors"
|
|
|
13 |
|
14 |
+
# Load the diffusion pipeline
|
15 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
16 |
+
pipe.to(device)
|
|
|
|
|
|
|
17 |
|
18 |
+
# Update the scheduler
|
19 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
|
20 |
|
21 |
+
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
|
|
23 |
|
24 |
+
def generate_and_display(prompt, num_inference_steps=10, guidance_scale=0):
|
25 |
+
generator = torch.Generator(device).manual_seed(random.randint(0, MAX_SEED))
|
26 |
image = pipe(
|
27 |
prompt=prompt,
|
|
|
|
|
28 |
num_inference_steps=num_inference_steps,
|
29 |
+
guidance_scale=guidance_scale,
|
|
|
30 |
generator=generator,
|
31 |
).images[0]
|
32 |
+
return image
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
@gr.Interface(
|
36 |
+
fn=generate_and_display,
|
37 |
+
inputs=[
|
38 |
+
gr.Textbox(label="Prompt", placeholder="Enter your creative prompt here"),
|
39 |
+
gr.Slider(1, 50, value=10, label="Number of Inference Steps"),
|
40 |
+
gr.Slider(0.0, 10.0, value=7.5, label="Guidance Scale"),
|
41 |
+
],
|
42 |
+
outputs=gr.Image(label="Generated Image"),
|
43 |
+
title="Veshon: Your Creative AI Assistant",
|
44 |
+
description="Generate stunning visuals effortlessly with cutting-edge technology!",
|
45 |
+
)
|
46 |
+
def launch_demo():
|
47 |
+
gr.launch(server_name="0.0.0.0", server_port=8080) # Use a specified port for local testing
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
if __name__ == "__main__":
|
51 |
+
launch_demo()
|