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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import io
|
| 4 |
+
import os
|
| 5 |
+
import warnings
|
| 6 |
+
import torch
|
| 7 |
+
import numpy as np
|
| 8 |
+
from diffusers import DiffusionPipeline
|
| 9 |
+
from huggingface_hub import login
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
login(token=os.environ.get('HF_KEY'))
|
| 13 |
+
|
| 14 |
+
# theme = gr.themes.Monochrome(
|
| 15 |
+
# primary_hue="indigo",
|
| 16 |
+
# secondary_hue="blue",
|
| 17 |
+
# neutral_hue="slate",
|
| 18 |
+
# radius_size=gr.themes.sizes.radius_sm,
|
| 19 |
+
# font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 20 |
+
# )
|
| 21 |
+
|
| 22 |
+
device = 'cpu'
|
| 23 |
+
seed = 10000
|
| 24 |
+
torch.cuda.max_memory_allocated(device='cpu')
|
| 25 |
+
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", use_safetensors=True)
|
| 26 |
+
pipe = pipe.to(device)
|
| 27 |
+
# pipe.enable_xformers_memory_efficient_attention()
|
| 28 |
+
# torch.cuda.empty_cache()
|
| 29 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 30 |
+
|
| 31 |
+
def infer(prompt):
|
| 32 |
+
int_image = pipe(prompt,num_images_per_prompt=1, generator=generator).images
|
| 33 |
+
torch.cuda.empty_cache()
|
| 34 |
+
return int_image
|
| 35 |
+
|
| 36 |
+
css = """
|
| 37 |
+
.gradio-container {
|
| 38 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
| 39 |
+
}
|
| 40 |
+
.gr-button {
|
| 41 |
+
color: white;
|
| 42 |
+
border-color: black;
|
| 43 |
+
background: black;
|
| 44 |
+
}
|
| 45 |
+
input[type='range'] {
|
| 46 |
+
accent-color: black;
|
| 47 |
+
}
|
| 48 |
+
.dark input[type='range'] {
|
| 49 |
+
accent-color: #dfdfdf;
|
| 50 |
+
}
|
| 51 |
+
.container {
|
| 52 |
+
max-width: 730px;
|
| 53 |
+
margin: auto;
|
| 54 |
+
padding-top: 1.5rem;
|
| 55 |
+
}
|
| 56 |
+
#gallery {
|
| 57 |
+
min-height: 22rem;
|
| 58 |
+
margin-bottom: 15px;
|
| 59 |
+
margin-left: auto;
|
| 60 |
+
margin-right: auto;
|
| 61 |
+
border-bottom-right-radius: .5rem !important;
|
| 62 |
+
border-bottom-left-radius: .5rem !important;
|
| 63 |
+
}
|
| 64 |
+
#gallery>div>.h-full {
|
| 65 |
+
min-height: 20rem;
|
| 66 |
+
}
|
| 67 |
+
.details:hover {
|
| 68 |
+
text-decoration: underline;
|
| 69 |
+
}
|
| 70 |
+
.gr-button {
|
| 71 |
+
white-space: nowrap;
|
| 72 |
+
}
|
| 73 |
+
.gr-button:focus {
|
| 74 |
+
border-color: rgb(147 197 253 / var(--tw-border-opacity));
|
| 75 |
+
outline: none;
|
| 76 |
+
box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
|
| 77 |
+
--tw-border-opacity: 1;
|
| 78 |
+
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
|
| 79 |
+
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
|
| 80 |
+
--tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
|
| 81 |
+
--tw-ring-opacity: .5;
|
| 82 |
+
}
|
| 83 |
+
#advanced-btn {
|
| 84 |
+
font-size: .7rem !important;
|
| 85 |
+
line-height: 19px;
|
| 86 |
+
margin-top: 12px;
|
| 87 |
+
margin-bottom: 12px;
|
| 88 |
+
padding: 2px 8px;
|
| 89 |
+
border-radius: 14px !important;
|
| 90 |
+
}
|
| 91 |
+
#advanced-options {
|
| 92 |
+
display: none;
|
| 93 |
+
margin-bottom: 20px;
|
| 94 |
+
}
|
| 95 |
+
.footer {
|
| 96 |
+
margin-bottom: 45px;
|
| 97 |
+
margin-top: 35px;
|
| 98 |
+
text-align: center;
|
| 99 |
+
border-bottom: 1px solid #e5e5e5;
|
| 100 |
+
}
|
| 101 |
+
.footer>p {
|
| 102 |
+
font-size: .8rem;
|
| 103 |
+
display: inline-block;
|
| 104 |
+
padding: 0 10px;
|
| 105 |
+
transform: translateY(10px);
|
| 106 |
+
background: white;
|
| 107 |
+
}
|
| 108 |
+
.dark .footer {
|
| 109 |
+
border-color: #303030;
|
| 110 |
+
}
|
| 111 |
+
.dark .footer>p {
|
| 112 |
+
background: #0b0f19;
|
| 113 |
+
}
|
| 114 |
+
.acknowledgments h4{
|
| 115 |
+
margin: 1.25em 0 .25em 0;
|
| 116 |
+
font-weight: bold;
|
| 117 |
+
font-size: 115%;
|
| 118 |
+
}
|
| 119 |
+
.animate-spin {
|
| 120 |
+
animation: spin 1s linear infinite;
|
| 121 |
+
}
|
| 122 |
+
@keyframes spin {
|
| 123 |
+
from {
|
| 124 |
+
transform: rotate(0deg);
|
| 125 |
+
}
|
| 126 |
+
to {
|
| 127 |
+
transform: rotate(360deg);
|
| 128 |
+
}
|
| 129 |
+
}
|
| 130 |
+
#share-btn-container {
|
| 131 |
+
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;
|
| 132 |
+
margin-top: 10px;
|
| 133 |
+
margin-left: auto;
|
| 134 |
+
}
|
| 135 |
+
#share-btn {
|
| 136 |
+
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;
|
| 137 |
+
}
|
| 138 |
+
#share-btn * {
|
| 139 |
+
all: unset;
|
| 140 |
+
}
|
| 141 |
+
#share-btn-container div:nth-child(-n+2){
|
| 142 |
+
width: auto !important;
|
| 143 |
+
min-height: 0px !important;
|
| 144 |
+
}
|
| 145 |
+
#share-btn-container .wrap {
|
| 146 |
+
display: none !important;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
.gr-form{
|
| 150 |
+
flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
|
| 151 |
+
}
|
| 152 |
+
#prompt-container{
|
| 153 |
+
gap: 0;
|
| 154 |
+
}
|
| 155 |
+
#prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
|
| 156 |
+
#component-16{border-top-width: 1px!important;margin-top: 1em}
|
| 157 |
+
.image_duplication{position: absolute; width: 100px; left: 50px}
|
| 158 |
+
"""
|
| 159 |
+
|
| 160 |
+
with gr.Blocks(css = css) as demo:
|
| 161 |
+
gr.HTML(
|
| 162 |
+
"""
|
| 163 |
+
<div style="text-align: center; margin: 0 auto;">
|
| 164 |
+
<div
|
| 165 |
+
style="
|
| 166 |
+
display: inline-flex;
|
| 167 |
+
align-items: center;
|
| 168 |
+
gap: 0.8rem;
|
| 169 |
+
font-size: 1.75rem;
|
| 170 |
+
"
|
| 171 |
+
>
|
| 172 |
+
<svg
|
| 173 |
+
width="0.65em"
|
| 174 |
+
height="0.65em"
|
| 175 |
+
viewBox="0 0 115 115"
|
| 176 |
+
fill="none"
|
| 177 |
+
xmlns="http://www.w3.org/2000/svg"
|
| 178 |
+
>
|
| 179 |
+
<rect width="23" height="23" fill="white"></rect>
|
| 180 |
+
<rect y="69" width="23" height="23" fill="white"></rect>
|
| 181 |
+
<rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
|
| 182 |
+
<rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
| 183 |
+
<rect x="46" width="23" height="23" fill="white"></rect>
|
| 184 |
+
<rect x="46" y="69" width="23" height="23" fill="white"></rect>
|
| 185 |
+
<rect x="69" width="23" height="23" fill="black"></rect>
|
| 186 |
+
<rect x="69" y="69" width="23" height="23" fill="black"></rect>
|
| 187 |
+
<rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
|
| 188 |
+
<rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
| 189 |
+
<rect x="115" y="46" width="23" height="23" fill="white"></rect>
|
| 190 |
+
<rect x="115" y="115" width="23" height="23" fill="white"></rect>
|
| 191 |
+
<rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
| 192 |
+
<rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
|
| 193 |
+
<rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
| 194 |
+
<rect x="92" y="69" width="23" height="23" fill="white"></rect>
|
| 195 |
+
<rect x="69" y="46" width="23" height="23" fill="white"></rect>
|
| 196 |
+
<rect x="69" y="115" width="23" height="23" fill="white"></rect>
|
| 197 |
+
<rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
| 198 |
+
<rect x="46" y="46" width="23" height="23" fill="black"></rect>
|
| 199 |
+
<rect x="46" y="115" width="23" height="23" fill="black"></rect>
|
| 200 |
+
<rect x="46" y="69" width="23" height="23" fill="black"></rect>
|
| 201 |
+
<rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
|
| 202 |
+
<rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
| 203 |
+
<rect x="23" y="69" width="23" height="23" fill="black"></rect>
|
| 204 |
+
</svg>
|
| 205 |
+
<h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
|
| 206 |
+
Stable Diffusion XL Demo
|
| 207 |
+
</h1>
|
| 208 |
+
</div>
|
| 209 |
+
<p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
|
| 210 |
+
This is an unoffical demo for Stable Diffusion XL, which is the latest stable diffusion model released by Stability AI. The main features include Next-level photorealism capabilities, image composition and face generation, use of shorter prompts to create descriptive imagery, greater capability to produce legible text and rich visuals and jaw-dropping aesthetics. Please refer to
|
| 211 |
+
<a
|
| 212 |
+
href="https://stability.ai/stable-diffusion"
|
| 213 |
+
style="text-decoration: underline;"
|
| 214 |
+
target="_blank"
|
| 215 |
+
>the official website</a
|
| 216 |
+
>.</a> for further information
|
| 217 |
+
</p>
|
| 218 |
+
</div>
|
| 219 |
+
"""
|
| 220 |
+
)
|
| 221 |
+
gr.HTML("<p>You can duplicate this Space to run it privately without a queue for shorter queue times : <a style='display:inline-block' href='https://huggingface.co/spaces/RamAnanth1/stable-diffusion-xl?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14' alt='Duplicate Space'></a> </p>")
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
|
| 225 |
+
with gr.Column():
|
| 226 |
+
text = gr.Textbox(
|
| 227 |
+
label="Enter your prompt",
|
| 228 |
+
show_label=False,
|
| 229 |
+
max_lines=1,
|
| 230 |
+
placeholder="Enter your prompt",
|
| 231 |
+
elem_id="prompt-text-input",
|
| 232 |
+
).style(
|
| 233 |
+
border=(True, False, True, True),
|
| 234 |
+
rounded=(True, False, False, True),
|
| 235 |
+
container=False,
|
| 236 |
+
)
|
| 237 |
+
btn = gr.Button("Generate image").style(
|
| 238 |
+
margin=False,
|
| 239 |
+
rounded=(False, True, True, False),
|
| 240 |
+
full_width=False,
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
gallery = gr.Gallery(
|
| 244 |
+
label="Generated images", show_label=False, elem_id="gallery"
|
| 245 |
+
).style(grid=[2], height="auto")
|
| 246 |
+
|
| 247 |
+
btn.click(infer, inputs=[text], outputs=[gallery])
|
| 248 |
+
examples = [
|
| 249 |
+
["Vintage hot rod with custom flame paint job"],
|
| 250 |
+
["Ancient, mysterious temple in a mountain range, surrounded by misty clouds and tall peaks"],
|
| 251 |
+
["Glimpses of a herd of wild elephants crossing a savanna"],
|
| 252 |
+
["Beautiful waterfall in a lush jungle, with sunlight shining through the trees,"]
|
| 253 |
+
]
|
| 254 |
+
ex = gr.Examples(examples=examples,inputs=[text], cache_examples=False)
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
demo.launch()
|