Spaces:
Sleeping
Sleeping
Kvikontent
commited on
Commit
•
6d92ca7
1
Parent(s):
634caec
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
import uuid
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import numpy as np
|
7 |
+
from PIL import Image
|
8 |
+
import spaces
|
9 |
+
import torch
|
10 |
+
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
11 |
+
|
12 |
+
DESCRIPTION = """
|
13 |
+
# DALL•E 3 XL v2
|
14 |
+
"""
|
15 |
+
|
16 |
+
def save_image(img):
|
17 |
+
unique_name = str(uuid.uuid4()) + ".png"
|
18 |
+
img.save(unique_name)
|
19 |
+
return unique_name
|
20 |
+
|
21 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
22 |
+
if randomize_seed:
|
23 |
+
seed = random.randint(0, MAX_SEED)
|
24 |
+
return seed
|
25 |
+
|
26 |
+
MAX_SEED = np.iinfo(np.int32).max
|
27 |
+
|
28 |
+
if not torch.cuda.is_available():
|
29 |
+
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
|
30 |
+
|
31 |
+
MAX_SEED = np.iinfo(np.int32).max
|
32 |
+
|
33 |
+
USE_TORCH_COMPILE = 0
|
34 |
+
ENABLE_CPU_OFFLOAD = 0
|
35 |
+
|
36 |
+
|
37 |
+
if torch.cuda.is_available():
|
38 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
39 |
+
"fluently/Fluently-XL-v2",
|
40 |
+
torch_dtype=torch.float16,
|
41 |
+
use_safetensors=True,
|
42 |
+
)
|
43 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
44 |
+
|
45 |
+
|
46 |
+
pipe.load_lora_weights("ehristoforu/dalle-3-xl-v2", weight_name="dalle-3-xl-lora-v2.safetensors", adapter_name="dalle")
|
47 |
+
pipe.set_adapters("dalle")
|
48 |
+
|
49 |
+
pipe.to("cuda")
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
@spaces.GPU(enable_queue=True)
|
54 |
+
def generate(
|
55 |
+
prompt: str,
|
56 |
+
negative_prompt: str = "",
|
57 |
+
use_negative_prompt: bool = False,
|
58 |
+
seed: int = 0,
|
59 |
+
width: int = 1024,
|
60 |
+
height: int = 1024,
|
61 |
+
guidance_scale: float = 3,
|
62 |
+
randomize_seed: bool = False,
|
63 |
+
progress=gr.Progress(track_tqdm=True),
|
64 |
+
):
|
65 |
+
|
66 |
+
|
67 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
68 |
+
|
69 |
+
if not use_negative_prompt:
|
70 |
+
negative_prompt = "" # type: ignore
|
71 |
+
|
72 |
+
images = pipe(
|
73 |
+
prompt=prompt,
|
74 |
+
negative_prompt=negative_prompt,
|
75 |
+
width=width,
|
76 |
+
height=height,
|
77 |
+
guidance_scale=guidance_scale,
|
78 |
+
num_inference_steps=25,
|
79 |
+
num_images_per_prompt=1,
|
80 |
+
cross_attention_kwargs={"scale": 0.65},
|
81 |
+
output_type="pil",
|
82 |
+
).images
|
83 |
+
image_paths = [save_image(img) for img in images]
|
84 |
+
print(image_paths)
|
85 |
+
return image_paths, seed
|
86 |
+
|
87 |
+
examples = [
|
88 |
+
"neon holography crystal cat",
|
89 |
+
"a cat eating a piece of cheese",
|
90 |
+
"an astronaut riding a horse in space",
|
91 |
+
"a cartoon of a boy playing with a tiger",
|
92 |
+
"a cute robot artist painting on an easel, concept art",
|
93 |
+
"a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the should pose, brown skin-tone"
|
94 |
+
]
|
95 |
+
|
96 |
+
css = '''
|
97 |
+
.gradio-container{max-width: 560px !important}
|
98 |
+
h1{text-align:center}
|
99 |
+
footer {
|
100 |
+
visibility: hidden
|
101 |
+
}
|
102 |
+
'''
|
103 |
+
with gr.Blocks(css=css, theme="soft") as demo:
|
104 |
+
gr.Markdown(DESCRIPTION)
|
105 |
+
gr.DuplicateButton(
|
106 |
+
value="Duplicate Space for private use",
|
107 |
+
elem_id="duplicate-button",
|
108 |
+
visible=False,
|
109 |
+
)
|
110 |
+
|
111 |
+
with gr.Group():
|
112 |
+
with gr.Row():
|
113 |
+
prompt = gr.Text(
|
114 |
+
label="Prompt",
|
115 |
+
show_label=False,
|
116 |
+
max_lines=1,
|
117 |
+
placeholder="Enter your prompt",
|
118 |
+
container=False,
|
119 |
+
)
|
120 |
+
run_button = gr.Button("Run", scale=0)
|
121 |
+
result = gr.Gallery(label="Result", columns=1, preview=True, show_label=False)
|
122 |
+
with gr.Accordion("Advanced options", open=False):
|
123 |
+
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
124 |
+
negative_prompt = gr.Text(
|
125 |
+
label="Negative prompt",
|
126 |
+
lines=4,
|
127 |
+
max_lines=6,
|
128 |
+
value="""(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, (NSFW:1.25)""",
|
129 |
+
placeholder="Enter a negative prompt",
|
130 |
+
visible=True,
|
131 |
+
)
|
132 |
+
seed = gr.Slider(
|
133 |
+
label="Seed",
|
134 |
+
minimum=0,
|
135 |
+
maximum=MAX_SEED,
|
136 |
+
step=1,
|
137 |
+
value=0,
|
138 |
+
visible=True
|
139 |
+
)
|
140 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
141 |
+
with gr.Row(visible=True):
|
142 |
+
width = gr.Slider(
|
143 |
+
label="Width",
|
144 |
+
minimum=512,
|
145 |
+
maximum=2048,
|
146 |
+
step=8,
|
147 |
+
value=1024,
|
148 |
+
)
|
149 |
+
height = gr.Slider(
|
150 |
+
label="Height",
|
151 |
+
minimum=512,
|
152 |
+
maximum=2048,
|
153 |
+
step=8,
|
154 |
+
value=1024,
|
155 |
+
)
|
156 |
+
with gr.Row():
|
157 |
+
guidance_scale = gr.Slider(
|
158 |
+
label="Guidance Scale",
|
159 |
+
minimum=0.1,
|
160 |
+
maximum=20.0,
|
161 |
+
step=0.1,
|
162 |
+
value=6,
|
163 |
+
)
|
164 |
+
|
165 |
+
gr.Examples(
|
166 |
+
examples=examples,
|
167 |
+
inputs=prompt,
|
168 |
+
outputs=[result, seed],
|
169 |
+
fn=generate,
|
170 |
+
cache_examples=False,
|
171 |
+
)
|
172 |
+
|
173 |
+
use_negative_prompt.change(
|
174 |
+
fn=lambda x: gr.update(visible=x),
|
175 |
+
inputs=use_negative_prompt,
|
176 |
+
outputs=negative_prompt,
|
177 |
+
api_name=False,
|
178 |
+
)
|
179 |
+
|
180 |
+
|
181 |
+
gr.on(
|
182 |
+
triggers=[
|
183 |
+
prompt.submit,
|
184 |
+
negative_prompt.submit,
|
185 |
+
run_button.click,
|
186 |
+
],
|
187 |
+
fn=generate,
|
188 |
+
inputs=[
|
189 |
+
prompt,
|
190 |
+
negative_prompt,
|
191 |
+
use_negative_prompt,
|
192 |
+
seed,
|
193 |
+
width,
|
194 |
+
height,
|
195 |
+
guidance_scale,
|
196 |
+
randomize_seed,
|
197 |
+
],
|
198 |
+
outputs=[result, seed],
|
199 |
+
api_name="run",
|
200 |
+
)
|
201 |
+
|
202 |
+
if __name__ == "__main__":
|
203 |
+
demo.queue(max_size=20).launch(show_api=False, debug=False)
|