Spaces:
Running
Running
Commit
•
f3742f4
1
Parent(s):
951b27e
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
+
import os
|
4 |
+
import random
|
5 |
+
import uuid
|
6 |
+
|
7 |
+
import gradio as gr
|
8 |
+
import numpy as np
|
9 |
+
from PIL import Image
|
10 |
+
import spaces
|
11 |
+
import torch
|
12 |
+
from diffusers import StableDiffusionPipeline, StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
13 |
+
|
14 |
+
DESCRIPTION = """
|
15 |
+
# DALL•E 3 XL v2
|
16 |
+
"""
|
17 |
+
|
18 |
+
def save_image(img):
|
19 |
+
unique_name = str(uuid.uuid4()) + ".png"
|
20 |
+
img.save(unique_name)
|
21 |
+
return unique_name
|
22 |
+
|
23 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
24 |
+
if randomize_seed:
|
25 |
+
seed = random.randint(0, MAX_SEED)
|
26 |
+
return seed
|
27 |
+
|
28 |
+
MAX_SEED = np.iinfo(np.int32).max
|
29 |
+
|
30 |
+
if not torch.cuda.is_available():
|
31 |
+
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
|
32 |
+
|
33 |
+
MAX_SEED = np.iinfo(np.int32).max
|
34 |
+
|
35 |
+
USE_TORCH_COMPILE = 0
|
36 |
+
ENABLE_CPU_OFFLOAD = 0
|
37 |
+
|
38 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
39 |
+
|
40 |
+
|
41 |
+
if torch.cuda.is_available():
|
42 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
43 |
+
"fluently/Fluently-XL-v2",
|
44 |
+
torch_dtype=torch.float16,
|
45 |
+
use_safetensors=True,
|
46 |
+
)
|
47 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
48 |
+
|
49 |
+
pipe.load_lora_weights("ehristoforu/dalle-3-xl-v2", weight_name="dalle-3-xl-lora-v2.safetensors", adapter_name="dalle")
|
50 |
+
pipe.fuse_lora(lora_scale=0.8)
|
51 |
+
|
52 |
+
pipe.to(device)
|
53 |
+
|
54 |
+
@spaces.GPU(enable_queue=True)
|
55 |
+
def generate(
|
56 |
+
prompt: str,
|
57 |
+
negative_prompt: str = "",
|
58 |
+
use_negative_prompt: bool = False,
|
59 |
+
seed: int = 0,
|
60 |
+
width: int = 1024,
|
61 |
+
height: int = 1024,
|
62 |
+
guidance_scale: float = 3,
|
63 |
+
randomize_seed: bool = False,
|
64 |
+
progress=gr.Progress(track_tqdm=True),
|
65 |
+
):
|
66 |
+
|
67 |
+
|
68 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
69 |
+
|
70 |
+
if not use_negative_prompt:
|
71 |
+
negative_prompt = "" # type: ignore
|
72 |
+
|
73 |
+
images = pipe_3_5(
|
74 |
+
prompt=prompt,
|
75 |
+
negative_prompt=negative_prompt,
|
76 |
+
width=width,
|
77 |
+
height=height,
|
78 |
+
guidance_scale=guidance_scale,
|
79 |
+
num_inference_steps=25,
|
80 |
+
num_images_per_prompt=1,
|
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) 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=False)
|
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""",
|
129 |
+
placeholder="Enter a negative prompt",
|
130 |
+
visible=False,
|
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)
|