Update app.py
Browse files
app.py
CHANGED
|
@@ -1,346 +1,265 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
payload = {'prompt': prompt, 'negative_prompt': negative, 'guidance_scale': scale}
|
| 34 |
-
images_request = requests.post(url, json = payload)
|
| 35 |
-
for image in images_request.json()["images"]:
|
| 36 |
-
file_path = f"{uuid.uuid4()}.jpg"
|
| 37 |
-
with open(file_path, "wb") as f:
|
| 38 |
-
f.write(base64.b64decode(image))
|
| 39 |
-
images.append(file_path)
|
| 40 |
-
|
| 41 |
-
return images
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
css = """
|
| 45 |
-
.gradio-container {
|
| 46 |
-
max-width: 768px !important;
|
| 47 |
-
}
|
| 48 |
-
.gradio-container {
|
| 49 |
-
font-family: 'IBM Plex Sans', sans-serif;
|
| 50 |
-
}
|
| 51 |
-
.gr-button {
|
| 52 |
-
color: white;
|
| 53 |
-
border-color: black;
|
| 54 |
-
background: black;
|
| 55 |
-
}
|
| 56 |
-
input[type='range'] {
|
| 57 |
-
accent-color: black;
|
| 58 |
-
}
|
| 59 |
-
.dark input[type='range'] {
|
| 60 |
-
accent-color: #dfdfdf;
|
| 61 |
-
}
|
| 62 |
-
.container {
|
| 63 |
-
max-width: 730px;
|
| 64 |
-
margin: auto;
|
| 65 |
-
}
|
| 66 |
-
#gallery {
|
| 67 |
-
min-height: 22rem;
|
| 68 |
-
margin-bottom: 15px;
|
| 69 |
-
margin-left: auto;
|
| 70 |
-
margin-right: auto;
|
| 71 |
-
border-bottom-right-radius: .5rem !important;
|
| 72 |
-
border-bottom-left-radius: .5rem !important;
|
| 73 |
-
}
|
| 74 |
-
#gallery>div>.h-full {
|
| 75 |
-
min-height: 20rem;
|
| 76 |
-
}
|
| 77 |
-
.details:hover {
|
| 78 |
-
text-decoration: underline;
|
| 79 |
-
}
|
| 80 |
-
.gr-button {
|
| 81 |
-
white-space: nowrap;
|
| 82 |
-
}
|
| 83 |
-
.gr-button:focus {
|
| 84 |
-
border-color: rgb(147 197 253 / var(--tw-border-opacity));
|
| 85 |
-
outline: none;
|
| 86 |
-
box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
|
| 87 |
-
--tw-border-opacity: 1;
|
| 88 |
-
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
|
| 89 |
-
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
|
| 90 |
-
--tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
|
| 91 |
-
--tw-ring-opacity: .5;
|
| 92 |
-
}
|
| 93 |
-
#advanced-btn {
|
| 94 |
-
font-size: .7rem !important;
|
| 95 |
-
line-height: 19px;
|
| 96 |
-
margin-top: 12px;
|
| 97 |
-
margin-bottom: 12px;
|
| 98 |
-
padding: 2px 8px;
|
| 99 |
-
border-radius: 14px !important;
|
| 100 |
-
}
|
| 101 |
-
#advanced-options {
|
| 102 |
-
display: none;
|
| 103 |
-
margin-bottom: 20px;
|
| 104 |
-
}
|
| 105 |
-
.footer {
|
| 106 |
-
margin-bottom: 45px;
|
| 107 |
-
margin-top: 35px;
|
| 108 |
-
text-align: center;
|
| 109 |
-
border-bottom: 1px solid #e5e5e5;
|
| 110 |
-
}
|
| 111 |
-
.footer>p {
|
| 112 |
-
font-size: .8rem;
|
| 113 |
-
display: inline-block;
|
| 114 |
-
padding: 0 10px;
|
| 115 |
-
transform: translateY(10px);
|
| 116 |
-
background: white;
|
| 117 |
-
}
|
| 118 |
-
.dark .footer {
|
| 119 |
-
border-color: #303030;
|
| 120 |
-
}
|
| 121 |
-
.dark .footer>p {
|
| 122 |
-
background: #0b0f19;
|
| 123 |
-
}
|
| 124 |
-
.acknowledgments h4{
|
| 125 |
-
margin: 1.25em 0 .25em 0;
|
| 126 |
-
font-weight: bold;
|
| 127 |
-
font-size: 115%;
|
| 128 |
-
}
|
| 129 |
-
.animate-spin {
|
| 130 |
-
animation: spin 1s linear infinite;
|
| 131 |
-
}
|
| 132 |
-
@keyframes spin {
|
| 133 |
-
from {
|
| 134 |
-
transform: rotate(0deg);
|
| 135 |
-
}
|
| 136 |
-
to {
|
| 137 |
-
transform: rotate(360deg);
|
| 138 |
-
}
|
| 139 |
-
}
|
| 140 |
-
#share-btn-container {
|
| 141 |
-
display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
|
| 142 |
-
margin-top: 10px;
|
| 143 |
-
margin-left: auto;
|
| 144 |
-
}
|
| 145 |
-
#share-btn-container .styler{
|
| 146 |
-
background-color: #000000;
|
| 147 |
-
}
|
| 148 |
-
#share-btn {
|
| 149 |
-
all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;right:0;
|
| 150 |
-
}
|
| 151 |
-
#share-btn * {
|
| 152 |
-
all: unset;
|
| 153 |
-
}
|
| 154 |
-
#share-btn-container div:nth-child(-n+2){
|
| 155 |
-
width: auto !important;
|
| 156 |
-
min-height: 0px !important;
|
| 157 |
-
}
|
| 158 |
-
#share-btn-container .wrap {
|
| 159 |
-
display: none !important;
|
| 160 |
-
}
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
],
|
| 187 |
-
[
|
| 188 |
-
'A mecha robot in a favela in expressionist style',
|
| 189 |
-
'low quality, 3d, photorealistic',
|
| 190 |
-
9
|
| 191 |
-
],
|
| 192 |
-
[
|
| 193 |
-
'an insect robot preparing a delicious meal',
|
| 194 |
-
'low quality, illustration',
|
| 195 |
-
9
|
| 196 |
-
],
|
| 197 |
-
[
|
| 198 |
-
"A small cabin on top of a snowy mountain in the style of Disney, artstation",
|
| 199 |
-
'low quality, ugly',
|
| 200 |
-
9
|
| 201 |
-
],
|
| 202 |
-
]
|
| 203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
<rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
| 228 |
-
<rect x="46" width="23" height="23" fill="white"></rect>
|
| 229 |
-
<rect x="46" y="69" width="23" height="23" fill="white"></rect>
|
| 230 |
-
<rect x="69" width="23" height="23" fill="black"></rect>
|
| 231 |
-
<rect x="69" y="69" width="23" height="23" fill="black"></rect>
|
| 232 |
-
<rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
|
| 233 |
-
<rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
| 234 |
-
<rect x="115" y="46" width="23" height="23" fill="white"></rect>
|
| 235 |
-
<rect x="115" y="115" width="23" height="23" fill="white"></rect>
|
| 236 |
-
<rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
| 237 |
-
<rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
|
| 238 |
-
<rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
| 239 |
-
<rect x="92" y="69" width="23" height="23" fill="white"></rect>
|
| 240 |
-
<rect x="69" y="46" width="23" height="23" fill="white"></rect>
|
| 241 |
-
<rect x="69" y="115" width="23" height="23" fill="white"></rect>
|
| 242 |
-
<rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
| 243 |
-
<rect x="46" y="46" width="23" height="23" fill="black"></rect>
|
| 244 |
-
<rect x="46" y="115" width="23" height="23" fill="black"></rect>
|
| 245 |
-
<rect x="46" y="69" width="23" height="23" fill="black"></rect>
|
| 246 |
-
<rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
|
| 247 |
-
<rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
| 248 |
-
<rect x="23" y="69" width="23" height="23" fill="black"></rect>
|
| 249 |
-
</svg>
|
| 250 |
-
<h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
|
| 251 |
-
Stable Diffusion 2.1 Demo
|
| 252 |
-
</h1>
|
| 253 |
-
</div>
|
| 254 |
-
<p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
|
| 255 |
-
Stable Diffusion 2.1 is the latest text-to-image model from StabilityAI. <a style="text-decoration: underline;" href="https://huggingface.co/spaces/stabilityai/stable-diffusion-1">Access Stable Diffusion 1 Space here</a><br>For faster generation and API
|
| 256 |
-
access you can try
|
| 257 |
-
<a
|
| 258 |
-
href="http://beta.dreamstudio.ai/"
|
| 259 |
-
style="text-decoration: underline;"
|
| 260 |
-
target="_blank"
|
| 261 |
-
>DreamStudio Beta</a
|
| 262 |
-
>.</a>
|
| 263 |
-
</p>
|
| 264 |
-
</div>
|
| 265 |
-
"""
|
| 266 |
-
)
|
| 267 |
-
with gr.Group():
|
| 268 |
-
with gr.Row(elem_id="prompt-container"):
|
| 269 |
-
with gr.Column(scale=3):
|
| 270 |
-
text = gr.Textbox(
|
| 271 |
-
label="Enter your prompt",
|
| 272 |
-
show_label=False,
|
| 273 |
-
max_lines=1,
|
| 274 |
-
placeholder="Enter your prompt",
|
| 275 |
-
elem_id="prompt-text-input",
|
| 276 |
)
|
| 277 |
-
|
| 278 |
-
label="
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
elem_id="negative-prompt-text-input",
|
| 283 |
)
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
"""
|
| 329 |
-
<div class="footer">
|
| 330 |
-
<p>Model by <a href="https://huggingface.co/stabilityai" style="text-decoration: underline;" target="_blank">StabilityAI</a> - backend running JAX on TPUs due to generous support of <a href="https://sites.research.google/trc/about/" style="text-decoration: underline;" target="_blank">Google TRC program</a> - Gradio Demo by 🤗 Hugging Face
|
| 331 |
-
</p>
|
| 332 |
-
</div>
|
| 333 |
-
"""
|
| 334 |
-
)
|
| 335 |
-
with gr.Accordion(label="License", open=False):
|
| 336 |
-
gr.HTML(
|
| 337 |
-
"""<div class="acknowledgments">
|
| 338 |
-
<p><h4>LICENSE</h4>
|
| 339 |
-
The model is licensed with a <a href="https://huggingface.co/Manojb/stable-diffusion-2-1-base/blob/main/LICENSE-MODEL" style="text-decoration: underline;" target="_blank">CreativeML OpenRAIL++</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a></p>
|
| 340 |
-
<p><h4>Biases and content acknowledgment</h4>
|
| 341 |
-
Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. The model was trained on the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a>, which scraped non-curated image-text-pairs from the internet (the exception being the removal of illegal content) and is meant for research purposes. You can read more in the <a href="https://huggingface.co/CompVis/stable-diffusion-v1-4" style="text-decoration: underline;" target="_blank">model card</a></p>
|
| 342 |
-
</div>
|
| 343 |
-
"""
|
| 344 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
|
| 346 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import DiffusionPipeline
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import random
|
| 7 |
+
from typing import List, Tuple, Optional
|
| 8 |
+
import time
|
| 9 |
|
| 10 |
+
# Initialize the model
|
| 11 |
+
def load_model():
|
| 12 |
+
"""Load the Stable Diffusion XL model"""
|
| 13 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 14 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 15 |
+
torch_dtype=torch.float16,
|
| 16 |
+
use_safetensors=True,
|
| 17 |
+
variant="fp16"
|
| 18 |
+
)
|
| 19 |
+
if torch.cuda.is_available():
|
| 20 |
+
pipe = pipe.to("cuda")
|
| 21 |
+
return pipe
|
| 22 |
|
| 23 |
+
# Global model instance
|
| 24 |
+
model = None
|
| 25 |
|
| 26 |
+
def get_model():
|
| 27 |
+
"""Get or create the model instance"""
|
| 28 |
+
global model
|
| 29 |
+
if model is None:
|
| 30 |
+
model = load_model()
|
| 31 |
+
return model
|
| 32 |
|
| 33 |
+
def generate_images(
|
| 34 |
+
prompt: str,
|
| 35 |
+
negative_prompt: str = "",
|
| 36 |
+
uploaded_images: Optional[List] = None,
|
| 37 |
+
guidance_scale: float = 7.5,
|
| 38 |
+
num_inference_steps: int = 50,
|
| 39 |
+
strength: float = 0.8
|
| 40 |
+
) -> Tuple[List[Image.Image], str]:
|
| 41 |
+
"""
|
| 42 |
+
Generate 4 images using Stable Diffusion XL
|
| 43 |
+
"""
|
| 44 |
+
try:
|
| 45 |
+
pipe = get_model()
|
| 46 |
|
| 47 |
+
# Default prompt if empty
|
| 48 |
+
if not prompt.strip():
|
| 49 |
+
prompt = "a beautiful landscape, professional photography, high quality"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Check if we're doing img2img or text2img
|
| 52 |
+
if uploaded_images and len(uploaded_images) > 0:
|
| 53 |
+
# Use the first uploaded image for img2img
|
| 54 |
+
init_image = uploaded_images[0]
|
| 55 |
+
if isinstance(init_image, str):
|
| 56 |
+
init_image = Image.open(init_image)
|
| 57 |
+
|
| 58 |
+
# Generate 4 images with img2img
|
| 59 |
+
images = []
|
| 60 |
+
for i in range(4):
|
| 61 |
+
result = pipe(
|
| 62 |
+
prompt=prompt,
|
| 63 |
+
negative_prompt=negative_prompt,
|
| 64 |
+
image=init_image,
|
| 65 |
+
num_images_per_prompt=1,
|
| 66 |
+
guidance_scale=guidance_scale,
|
| 67 |
+
num_inference_steps=num_inference_steps,
|
| 68 |
+
strength=strength
|
| 69 |
+
)
|
| 70 |
+
images.append(result.images[0])
|
| 71 |
+
else:
|
| 72 |
+
# Generate 4 images with text2img
|
| 73 |
+
result = pipe(
|
| 74 |
+
prompt=prompt,
|
| 75 |
+
negative_prompt=negative_prompt,
|
| 76 |
+
num_images_per_prompt=4,
|
| 77 |
+
guidance_scale=guidance_scale,
|
| 78 |
+
num_inference_steps=num_inference_steps
|
| 79 |
+
)
|
| 80 |
+
images = result.images
|
| 81 |
+
|
| 82 |
+
return images, "✅ Images generated successfully!"
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
error_msg = f"❌ Error generating images: {str(e)}"
|
| 86 |
+
return [], error_msg
|
| 87 |
|
| 88 |
+
def select_image_for_detail(gallery_data: List, evt: gr.SelectData) -> Tuple[Optional[Image.Image], str]:
|
| 89 |
+
"""
|
| 90 |
+
Handle image selection from gallery for detailed view
|
| 91 |
+
"""
|
| 92 |
+
if gallery_data and evt.index < len(gallery_data):
|
| 93 |
+
selected_image = gallery_data[evt.index]
|
| 94 |
+
if isinstance(selected_image, str):
|
| 95 |
+
selected_image = Image.open(selected_image)
|
| 96 |
+
return selected_image, f"📸 Selected image {evt.index + 1} for detailed view"
|
| 97 |
+
return None, "No image selected"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
# Custom CSS for better styling
|
| 100 |
+
custom_css = """
|
| 101 |
+
.main-container {
|
| 102 |
+
max-width: 1200px;
|
| 103 |
+
margin: 0 auto;
|
| 104 |
+
}
|
| 105 |
+
.gallery-container {
|
| 106 |
+
border: 2px solid #e0e0e0;
|
| 107 |
+
border-radius: 8px;
|
| 108 |
+
padding: 10px;
|
| 109 |
+
}
|
| 110 |
+
.selected-image-container {
|
| 111 |
+
border: 3px solid #4CAF50;
|
| 112 |
+
border-radius: 8px;
|
| 113 |
+
padding: 10px;
|
| 114 |
+
}
|
| 115 |
+
.generate-btn {
|
| 116 |
+
background: linear-gradient(45deg, #667eea 0%, #764ba2 100%) !important;
|
| 117 |
+
}
|
| 118 |
+
"""
|
| 119 |
|
| 120 |
+
# Create the Gradio interface
|
| 121 |
+
with gr.Blocks(css=custom_css, title="Stable Diffusion XL Demo") as demo:
|
| 122 |
+
gr.HTML("""
|
| 123 |
+
<div style="text-align: center; margin-bottom: 20px;">
|
| 124 |
+
<h1>🎨 Stable Diffusion XL Image Generator</h1>
|
| 125 |
+
<p>Generate amazing images with AI - Upload up to 4 images for img2img or use text prompts</p>
|
| 126 |
+
<p style="font-size: 0.9em; color: #666;">
|
| 127 |
+
Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #667eea;">anycoder</a>
|
| 128 |
+
</p>
|
| 129 |
+
</div>
|
| 130 |
+
""")
|
| 131 |
+
|
| 132 |
+
with gr.Row():
|
| 133 |
+
with gr.Column(scale=2):
|
| 134 |
+
# Input controls
|
| 135 |
+
with gr.Group():
|
| 136 |
+
gr.Markdown("### 📝 Text Prompts")
|
| 137 |
+
prompt = gr.Textbox(
|
| 138 |
+
label="Prompt",
|
| 139 |
+
placeholder="Describe the image you want to generate...",
|
| 140 |
+
lines=3,
|
| 141 |
+
value="a beautiful landscape, professional photography, high quality"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
)
|
| 143 |
+
negative_prompt = gr.Textbox(
|
| 144 |
+
label="Negative Prompt",
|
| 145 |
+
placeholder="What you don't want in the image...",
|
| 146 |
+
lines=2,
|
| 147 |
+
value="blurry, low quality, distorted, deformed"
|
|
|
|
| 148 |
)
|
| 149 |
+
|
| 150 |
+
with gr.Group():
|
| 151 |
+
gr.Markdown("### 🖼️ Image Upload (Optional)")
|
| 152 |
+
uploaded_images = gr.File(
|
| 153 |
+
label="Upload images for img2img (up to 4)",
|
| 154 |
+
file_count="multiple",
|
| 155 |
+
file_types=["image"],
|
| 156 |
+
height=120
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
with gr.Group():
|
| 160 |
+
gr.Markdown("### ⚙️ Generation Settings")
|
| 161 |
+
with gr.Row():
|
| 162 |
+
guidance_scale = gr.Slider(
|
| 163 |
+
label="Guidance Scale",
|
| 164 |
+
minimum=1.0,
|
| 165 |
+
maximum=20.0,
|
| 166 |
+
value=7.5,
|
| 167 |
+
step=0.5,
|
| 168 |
+
info="Higher = more prompt adherence"
|
| 169 |
+
)
|
| 170 |
+
num_inference_steps = gr.Slider(
|
| 171 |
+
label="Steps",
|
| 172 |
+
minimum=10,
|
| 173 |
+
maximum=100,
|
| 174 |
+
value=50,
|
| 175 |
+
step=5,
|
| 176 |
+
info="More steps = higher quality"
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
strength = gr.Slider(
|
| 180 |
+
label="Img2Img Strength",
|
| 181 |
+
minimum=0.1,
|
| 182 |
+
maximum=1.0,
|
| 183 |
+
value=0.8,
|
| 184 |
+
step=0.1,
|
| 185 |
+
info="How much to transform the input image"
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
generate_btn = gr.Button(
|
| 189 |
+
"🚀 Generate Images",
|
| 190 |
+
variant="primary",
|
| 191 |
+
size="lg",
|
| 192 |
+
elem_classes=["generate-btn"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
)
|
| 194 |
+
|
| 195 |
+
status_msg = gr.Textbox(label="Status", interactive=False)
|
| 196 |
+
|
| 197 |
+
with gr.Column(scale=3):
|
| 198 |
+
# Output area
|
| 199 |
+
with gr.Group(elem_classes=["gallery-container"]):
|
| 200 |
+
gr.Markdown("### 🎯 Generated Images (Click to select)")
|
| 201 |
+
gallery = gr.Gallery(
|
| 202 |
+
label="Generated Images",
|
| 203 |
+
columns=2,
|
| 204 |
+
rows=2,
|
| 205 |
+
height=400,
|
| 206 |
+
object_fit="cover",
|
| 207 |
+
allow_preview=True,
|
| 208 |
+
show_label=False
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
with gr.Group(elem_classes=["selected-image-container"]):
|
| 212 |
+
gr.Markdown("### 🔍 Selected Image (Close-up View)")
|
| 213 |
+
selected_image = gr.Image(
|
| 214 |
+
label="Selected Image",
|
| 215 |
+
height=400,
|
| 216 |
+
show_label=False
|
| 217 |
+
)
|
| 218 |
+
selection_info = gr.Textbox(label="Selection Info", interactive=False)
|
| 219 |
+
|
| 220 |
+
# Event handlers
|
| 221 |
+
generate_btn.click(
|
| 222 |
+
fn=generate_images,
|
| 223 |
+
inputs=[
|
| 224 |
+
prompt,
|
| 225 |
+
negative_prompt,
|
| 226 |
+
uploaded_images,
|
| 227 |
+
guidance_scale,
|
| 228 |
+
num_inference_steps,
|
| 229 |
+
strength
|
| 230 |
+
],
|
| 231 |
+
outputs=[gallery, status_msg],
|
| 232 |
+
show_progress=True
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
gallery.select(
|
| 236 |
+
fn=select_image_for_detail,
|
| 237 |
+
inputs=[gallery],
|
| 238 |
+
outputs=[selected_image, selection_info]
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
# Examples
|
| 242 |
+
gr.Examples(
|
| 243 |
+
examples=[
|
| 244 |
+
["a majestic dragon flying over mountains, fantasy art, highly detailed", "cartoon, blurry", None],
|
| 245 |
+
["a futuristic city skyline at sunset, cyberpunk, neon lights", "daylight, rural", None],
|
| 246 |
+
["a portrait of a wise old wizard with a long beard, fantasy", "young, modern", None],
|
| 247 |
+
["a serene japanese garden with cherry blossoms, peaceful", "chaotic, urban", None]
|
| 248 |
+
],
|
| 249 |
+
inputs=[prompt, negative_prompt, uploaded_images],
|
| 250 |
+
cache_examples=False
|
| 251 |
+
)
|
| 252 |
+
|
| 253 |
+
# Footer
|
| 254 |
+
gr.HTML("""
|
| 255 |
+
<div style="text-align: center; margin-top: 30px; padding-top: 20px; border-top: 1px solid #e0e0e0;">
|
| 256 |
+
<p style="color: #666; font-size: 0.9em;">
|
| 257 |
+
Powered by <a href="https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0" target="_blank">Stability AI</a> •
|
| 258 |
+
Model: SDXL Base 1.0 •
|
| 259 |
+
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank">Built with anycoder</a>
|
| 260 |
+
</p>
|
| 261 |
+
</div>
|
| 262 |
+
""")
|
| 263 |
|
| 264 |
+
if __name__ == "__main__":
|
| 265 |
+
demo.launch(share=True)
|