Spaces:
Runtime error
Runtime error
Stuff.
Browse files
app.py
CHANGED
@@ -1,92 +1,71 @@
|
|
1 |
#!/usr/bin/env python
|
2 |
|
|
|
3 |
import os
|
4 |
import random
|
|
|
5 |
import uuid
|
6 |
-
import json
|
7 |
|
|
|
8 |
import gradio as gr
|
9 |
import numpy as np
|
10 |
from PIL import Image
|
11 |
import spaces
|
12 |
import torch
|
13 |
-
from diffusers import DiffusionPipeline
|
14 |
-
from typing import Tuple
|
15 |
-
|
16 |
-
# Check for the Model Base..//
|
17 |
-
|
18 |
-
|
19 |
-
bad_words = json.loads(os.getenv("BAD_WORDS", "[]"))
|
20 |
-
bad_words_negative = json.loads(os.getenv("BAD_WORDS_NEGATIVE", "[]"))
|
21 |
-
default_negative = os.getenv("default_negative", "")
|
22 |
-
|
23 |
-
|
24 |
-
def check_text(prompt, negative=""):
|
25 |
-
for i in bad_words:
|
26 |
-
if i in prompt:
|
27 |
-
return True
|
28 |
-
for i in bad_words_negative:
|
29 |
-
if i in negative:
|
30 |
-
return True
|
31 |
-
return False
|
32 |
-
|
33 |
-
|
34 |
-
style_list = [
|
35 |
-
{
|
36 |
-
"name": "2560 x 1440",
|
37 |
-
"prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
38 |
-
"negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
39 |
-
},
|
40 |
-
{
|
41 |
-
"name": "Photo",
|
42 |
-
"prompt": "cinematic photo {prompt}. 35mm photograph, film, bokeh, professional, 4k, highly detailed",
|
43 |
-
"negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
|
44 |
-
},
|
45 |
-
{
|
46 |
-
"name": "Cinematic",
|
47 |
-
"prompt": "cinematic still {prompt}. emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
|
48 |
-
"negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
|
49 |
-
},
|
50 |
-
{
|
51 |
-
"name": "Anime",
|
52 |
-
"prompt": "anime artwork {prompt}. anime style, key visual, vibrant, studio anime, highly detailed",
|
53 |
-
"negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast",
|
54 |
-
},
|
55 |
-
{
|
56 |
-
"name": "3D Model",
|
57 |
-
"prompt": "professional 3d model {prompt}. octane render, highly detailed, volumetric, dramatic lighting",
|
58 |
-
"negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
|
59 |
-
},
|
60 |
-
{
|
61 |
-
"name": "(No style)",
|
62 |
-
"prompt": "{prompt}",
|
63 |
-
"negative_prompt": "",
|
64 |
-
},
|
65 |
-
]
|
66 |
-
|
67 |
-
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
68 |
-
STYLE_NAMES = list(styles.keys())
|
69 |
-
DEFAULT_STYLE_NAME = "2560 x 1440"
|
70 |
-
|
71 |
-
|
72 |
-
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
73 |
-
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
74 |
-
if not negative:
|
75 |
-
negative = ""
|
76 |
-
return p.replace("{prompt}", positive), n + negative
|
77 |
-
|
78 |
-
|
79 |
-
DESCRIPTION = """## MidJourney
|
80 |
-
|
81 |
-
Drop your best results in the community: [rb.gy/klkbs7](http://rb.gy/klkbs7), Have you tried the dalle collage space? [rb.gy/xkmlh4](http://rb.gy/xkmlh4)
|
82 |
-
"""
|
83 |
-
|
84 |
-
|
85 |
-
if not torch.cuda.is_available():
|
86 |
-
DESCRIPTION += "\n<p>⚠️Running on CPU, This may not work on CPU.</p>"
|
87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
MAX_SEED = np.iinfo(np.int32).max
|
89 |
-
CACHE_EXAMPLES =
|
90 |
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "2048"))
|
91 |
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
92 |
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
@@ -119,8 +98,8 @@ if torch.cuda.is_available():
|
|
119 |
print("Loaded on Device!")
|
120 |
|
121 |
if USE_TORCH_COMPILE:
|
122 |
-
pipe.unet = torch.compile(pipe.unet, mode="
|
123 |
-
pipe2.unet = torch.compile(pipe2.unet, mode="
|
124 |
print("Model Compiled!")
|
125 |
|
126 |
|
@@ -143,24 +122,17 @@ def generate(
|
|
143 |
use_negative_prompt: bool = False,
|
144 |
style: str = DEFAULT_STYLE_NAME,
|
145 |
seed: int = 0,
|
146 |
-
width: int =
|
147 |
-
height: int =
|
148 |
guidance_scale: float = 3,
|
149 |
randomize_seed: bool = False,
|
150 |
use_resolution_binning: bool = True,
|
151 |
progress=gr.Progress(track_tqdm=True),
|
152 |
):
|
153 |
-
if check_text(prompt, negative_prompt):
|
154 |
-
raise ValueError("Prompt contains restricted words.")
|
155 |
-
|
156 |
prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
|
157 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
158 |
generator = torch.Generator().manual_seed(seed)
|
159 |
|
160 |
-
if not use_negative_prompt:
|
161 |
-
negative_prompt = "" # type: ignore
|
162 |
-
negative_prompt += default_negative
|
163 |
-
|
164 |
options = {
|
165 |
"prompt": prompt,
|
166 |
"negative_prompt": negative_prompt,
|
@@ -181,43 +153,39 @@ def generate(
|
|
181 |
|
182 |
|
183 |
examples = [
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
|
|
188 |
]
|
189 |
|
190 |
css = """
|
191 |
-
.gradio-container{max-width: 700px !important}
|
192 |
-
h1{text-align:center}
|
193 |
"""
|
194 |
-
|
195 |
-
|
196 |
-
gr.DuplicateButton(
|
197 |
-
value="Duplicate Space for private use",
|
198 |
-
elem_id="duplicate-button",
|
199 |
-
visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
|
200 |
-
)
|
201 |
with gr.Group():
|
202 |
with gr.Row():
|
203 |
prompt = gr.Text(
|
204 |
label="Prompt",
|
205 |
show_label=False,
|
206 |
-
max_lines=
|
207 |
-
placeholder="Enter
|
208 |
container=False,
|
209 |
)
|
210 |
run_button = gr.Button("Run")
|
211 |
-
result =
|
212 |
-
|
|
|
|
|
213 |
use_negative_prompt = gr.Checkbox(
|
214 |
-
label="Use
|
215 |
)
|
216 |
negative_prompt = gr.Text(
|
217 |
-
label="Negative
|
218 |
-
max_lines=
|
219 |
-
placeholder="
|
220 |
-
value="
|
221 |
visible=True,
|
222 |
)
|
223 |
with gr.Row():
|
@@ -230,7 +198,7 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
230 |
)
|
231 |
with gr.Row():
|
232 |
num_images_per_prompt = gr.Slider(
|
233 |
-
label="
|
234 |
minimum=1,
|
235 |
maximum=5,
|
236 |
step=1,
|
@@ -239,25 +207,25 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
239 |
seed = gr.Slider(
|
240 |
label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, visible=True
|
241 |
)
|
242 |
-
randomize_seed = gr.Checkbox(label="
|
243 |
with gr.Row(visible=True):
|
244 |
width = gr.Slider(
|
245 |
label="Width",
|
246 |
minimum=512,
|
247 |
maximum=2048,
|
248 |
-
step=
|
249 |
-
value=
|
250 |
)
|
251 |
height = gr.Slider(
|
252 |
label="Height",
|
253 |
minimum=512,
|
254 |
maximum=2048,
|
255 |
-
step=
|
256 |
-
value=
|
257 |
)
|
258 |
with gr.Row():
|
259 |
guidance_scale = gr.Slider(
|
260 |
-
label="Guidance
|
261 |
minimum=0.1,
|
262 |
maximum=20.0,
|
263 |
step=0.1,
|
@@ -268,9 +236,9 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
268 |
show_label=True,
|
269 |
container=True,
|
270 |
interactive=True,
|
271 |
-
choices=
|
272 |
value=DEFAULT_STYLE_NAME,
|
273 |
-
label="
|
274 |
)
|
275 |
gr.Examples(
|
276 |
examples=examples,
|
|
|
1 |
#!/usr/bin/env python
|
2 |
|
3 |
+
import json
|
4 |
import os
|
5 |
import random
|
6 |
+
from typing import Tuple
|
7 |
import uuid
|
|
|
8 |
|
9 |
+
from diffusers import DiffusionPipeline
|
10 |
import gradio as gr
|
11 |
import numpy as np
|
12 |
from PIL import Image
|
13 |
import spaces
|
14 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
from gradio_imagefeed import ImageFeed
|
17 |
+
|
18 |
+
|
19 |
+
DEFAULT_STYLE = "Photograph"
|
20 |
+
DEFAULT_NEGATIVE = (
|
21 |
+
"(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon,"
|
22 |
+
" drawing, anime, asian, bad anatomy:1.4), text, close up, cropped, out of frame,"
|
23 |
+
" worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated,"
|
24 |
+
" extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation,"
|
25 |
+
" deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned"
|
26 |
+
" face, disfigured, gross proportions, malformed limbs, missing arms, missing legs,"
|
27 |
+
" extra arms, extra legs, fused fingers, too many fingers, long neck, {negative}"
|
28 |
+
)
|
29 |
+
STYLES = {
|
30 |
+
"Photograph": (
|
31 |
+
(
|
32 |
+
"realistic photograph of {positive}, ultra fine detail, lifelike,"
|
33 |
+
" high-resolution, sharp, realistic colors, photorealistic, Nikon, 35mm"
|
34 |
+
),
|
35 |
+
DEFAULT_NEGATIVE,
|
36 |
+
),
|
37 |
+
"Cinematic": (
|
38 |
+
(
|
39 |
+
"cinematic photograph of {positive}, 35mm photograph, film, bokeh,"
|
40 |
+
" professional, 4k, highly detailed"
|
41 |
+
),
|
42 |
+
DEFAULT_NEGATIVE,
|
43 |
+
),
|
44 |
+
"Still Photo": (
|
45 |
+
(
|
46 |
+
"cinematic still photograph of {positive}, emotional, harmonious, vignette,"
|
47 |
+
" highly detailed, bokeh, cinemascope, moody, epic, gorgeous, film grain,"
|
48 |
+
" grainy, high resolution"
|
49 |
+
),
|
50 |
+
DEFAULT_NEGATIVE,
|
51 |
+
),
|
52 |
+
}
|
53 |
+
|
54 |
+
|
55 |
+
def apply_style(name: str, pos: str, neg: str) -> Tuple[str, str]:
|
56 |
+
try:
|
57 |
+
def_pos, def_neg = STYLES[name]
|
58 |
+
except KeyError:
|
59 |
+
def_pos, def_neg = "{positive}", "{negative}"
|
60 |
+
finally:
|
61 |
+
pos = def_pos.replace("{positive}", pos).strip().strip(",")
|
62 |
+
neg = def_neg.replace("{negative}", ", " + neg).strip().strip(",")
|
63 |
+
return (pos, neg)
|
64 |
+
|
65 |
+
|
66 |
+
DESCRIPTION = ""
|
67 |
MAX_SEED = np.iinfo(np.int32).max
|
68 |
+
CACHE_EXAMPLES = os.getenv("CACHE_EXAMPLES", "0")
|
69 |
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "2048"))
|
70 |
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
71 |
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
|
|
98 |
print("Loaded on Device!")
|
99 |
|
100 |
if USE_TORCH_COMPILE:
|
101 |
+
pipe.unet = torch.compile(pipe.unet, mode="max-autotune", fullgraph=True)
|
102 |
+
pipe2.unet = torch.compile(pipe2.unet, mode="max-autotune", fullgraph=True)
|
103 |
print("Model Compiled!")
|
104 |
|
105 |
|
|
|
122 |
use_negative_prompt: bool = False,
|
123 |
style: str = DEFAULT_STYLE_NAME,
|
124 |
seed: int = 0,
|
125 |
+
width: int = 896,
|
126 |
+
height: int = 1152,
|
127 |
guidance_scale: float = 3,
|
128 |
randomize_seed: bool = False,
|
129 |
use_resolution_binning: bool = True,
|
130 |
progress=gr.Progress(track_tqdm=True),
|
131 |
):
|
|
|
|
|
|
|
132 |
prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
|
133 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
134 |
generator = torch.Generator().manual_seed(seed)
|
135 |
|
|
|
|
|
|
|
|
|
136 |
options = {
|
137 |
"prompt": prompt,
|
138 |
"negative_prompt": negative_prompt,
|
|
|
153 |
|
154 |
|
155 |
examples = [
|
156 |
+
(
|
157 |
+
"college life of 21 year old college woman, depth of field, bokeh, shallow"
|
158 |
+
" focus, minimalism, fujifilm xh2s with Canon EF lens, cinematic --ar 85:128"
|
159 |
+
" --v 6.0 --style raw"
|
160 |
+
),
|
161 |
]
|
162 |
|
163 |
css = """
|
|
|
|
|
164 |
"""
|
165 |
+
|
166 |
+
with gr.Blocks(css=css, theme="rawrsor1/Everforest") as demo:
|
|
|
|
|
|
|
|
|
|
|
167 |
with gr.Group():
|
168 |
with gr.Row():
|
169 |
prompt = gr.Text(
|
170 |
label="Prompt",
|
171 |
show_label=False,
|
172 |
+
max_lines=4,
|
173 |
+
placeholder="Enter a Prompt",
|
174 |
container=False,
|
175 |
)
|
176 |
run_button = gr.Button("Run")
|
177 |
+
result = ImageFeed(label="Result")
|
178 |
+
# result = gr.Gallery(label="Result", columns=1, preview=True)
|
179 |
+
|
180 |
+
with gr.Accordion("Advanced", open=False):
|
181 |
use_negative_prompt = gr.Checkbox(
|
182 |
+
label="Use Negative", value=True, visible=True
|
183 |
)
|
184 |
negative_prompt = gr.Text(
|
185 |
+
label="Negative Prompt",
|
186 |
+
max_lines=4,
|
187 |
+
placeholder="",
|
188 |
+
value="",
|
189 |
visible=True,
|
190 |
)
|
191 |
with gr.Row():
|
|
|
198 |
)
|
199 |
with gr.Row():
|
200 |
num_images_per_prompt = gr.Slider(
|
201 |
+
label="Image Count",
|
202 |
minimum=1,
|
203 |
maximum=5,
|
204 |
step=1,
|
|
|
207 |
seed = gr.Slider(
|
208 |
label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, visible=True
|
209 |
)
|
210 |
+
randomize_seed = gr.Checkbox(label="New Seed", value=True)
|
211 |
with gr.Row(visible=True):
|
212 |
width = gr.Slider(
|
213 |
label="Width",
|
214 |
minimum=512,
|
215 |
maximum=2048,
|
216 |
+
step=16,
|
217 |
+
value=896,
|
218 |
)
|
219 |
height = gr.Slider(
|
220 |
label="Height",
|
221 |
minimum=512,
|
222 |
maximum=2048,
|
223 |
+
step=16,
|
224 |
+
value=1152,
|
225 |
)
|
226 |
with gr.Row():
|
227 |
guidance_scale = gr.Slider(
|
228 |
+
label="Guidance",
|
229 |
minimum=0.1,
|
230 |
maximum=20.0,
|
231 |
step=0.1,
|
|
|
236 |
show_label=True,
|
237 |
container=True,
|
238 |
interactive=True,
|
239 |
+
choices=list(styles.keys()),
|
240 |
value=DEFAULT_STYLE_NAME,
|
241 |
+
label="Style",
|
242 |
)
|
243 |
gr.Examples(
|
244 |
examples=examples,
|