Spaces:
Runtime error
Runtime error
Clean-up code.
Browse files
app.py
CHANGED
@@ -13,13 +13,13 @@ import torch
|
|
13 |
from diffusers import DiffusionPipeline
|
14 |
from typing import Tuple
|
15 |
|
16 |
-
#Check for the Model Base..//
|
17 |
|
18 |
|
|
|
|
|
|
|
19 |
|
20 |
-
bad_words = json.loads(os.getenv('BAD_WORDS', "[]"))
|
21 |
-
bad_words_negative = json.loads(os.getenv('BAD_WORDS_NEGATIVE', "[]"))
|
22 |
-
default_negative = os.getenv("default_negative","")
|
23 |
|
24 |
def check_text(prompt, negative=""):
|
25 |
for i in bad_words:
|
@@ -31,27 +31,22 @@ def check_text(prompt, negative=""):
|
|
31 |
return False
|
32 |
|
33 |
|
34 |
-
|
35 |
style_list = [
|
36 |
-
|
37 |
{
|
38 |
"name": "2560 x 1440",
|
39 |
"prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
40 |
"negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
41 |
},
|
42 |
-
|
43 |
{
|
44 |
"name": "Photo",
|
45 |
"prompt": "cinematic photo {prompt}. 35mm photograph, film, bokeh, professional, 4k, highly detailed",
|
46 |
"negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
|
47 |
-
},
|
48 |
-
|
49 |
{
|
50 |
"name": "Cinematic",
|
51 |
"prompt": "cinematic still {prompt}. emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
|
52 |
"negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
|
53 |
},
|
54 |
-
|
55 |
{
|
56 |
"name": "Anime",
|
57 |
"prompt": "anime artwork {prompt}. anime style, key visual, vibrant, studio anime, highly detailed",
|
@@ -73,6 +68,7 @@ styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
|
73 |
STYLE_NAMES = list(styles.keys())
|
74 |
DEFAULT_STYLE_NAME = "2560 x 1440"
|
75 |
|
|
|
76 |
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
77 |
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
78 |
if not negative:
|
@@ -80,18 +76,12 @@ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str
|
|
80 |
return p.replace("{prompt}", positive), n + negative
|
81 |
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
DESCRIPTION = """## MidJourney
|
87 |
|
88 |
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)
|
89 |
"""
|
90 |
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
if not torch.cuda.is_available():
|
96 |
DESCRIPTION += "\n<p>⚠️Running on CPU, This may not work on CPU.</p>"
|
97 |
|
@@ -111,38 +101,41 @@ if torch.cuda.is_available():
|
|
111 |
torch_dtype=torch.float16,
|
112 |
use_safetensors=True,
|
113 |
add_watermarker=False,
|
114 |
-
variant="fp16"
|
115 |
)
|
116 |
pipe2 = DiffusionPipeline.from_pretrained(
|
117 |
"SG161222/RealVisXL_V2.02_Turbo",
|
118 |
torch_dtype=torch.float16,
|
119 |
use_safetensors=True,
|
120 |
add_watermarker=False,
|
121 |
-
variant="fp16"
|
122 |
)
|
123 |
if ENABLE_CPU_OFFLOAD:
|
124 |
pipe.enable_model_cpu_offload()
|
125 |
pipe2.enable_model_cpu_offload()
|
126 |
else:
|
127 |
-
pipe.to(device)
|
128 |
-
pipe2.to(device)
|
129 |
print("Loaded on Device!")
|
130 |
-
|
131 |
if USE_TORCH_COMPILE:
|
132 |
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
133 |
pipe2.unet = torch.compile(pipe2.unet, mode="reduce-overhead", fullgraph=True)
|
134 |
print("Model Compiled!")
|
135 |
|
|
|
136 |
def save_image(img):
|
137 |
unique_name = str(uuid.uuid4()) + ".png"
|
138 |
img.save(unique_name)
|
139 |
return unique_name
|
140 |
|
|
|
141 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
142 |
if randomize_seed:
|
143 |
seed = random.randint(0, MAX_SEED)
|
144 |
return seed
|
145 |
|
|
|
146 |
@spaces.GPU(enable_queue=True)
|
147 |
def generate(
|
148 |
prompt: str,
|
@@ -159,14 +152,14 @@ def generate(
|
|
159 |
):
|
160 |
if check_text(prompt, negative_prompt):
|
161 |
raise ValueError("Prompt contains restricted words.")
|
162 |
-
|
163 |
prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
|
164 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
165 |
generator = torch.Generator().manual_seed(seed)
|
166 |
|
167 |
if not use_negative_prompt:
|
168 |
negative_prompt = "" # type: ignore
|
169 |
-
negative_prompt += default_negative
|
170 |
|
171 |
options = {
|
172 |
"prompt": prompt,
|
@@ -180,23 +173,24 @@ def generate(
|
|
180 |
"use_resolution_binning": use_resolution_binning,
|
181 |
"output_type": "pil",
|
182 |
}
|
183 |
-
|
184 |
images = pipe(**options).images + pipe2(**options).images
|
185 |
|
186 |
image_paths = [save_image(img) for img in images]
|
187 |
return image_paths, seed
|
188 |
|
|
|
189 |
examples = [
|
190 |
"A closeup of a cat, a window, in a rustic cabin, close up, with a shallow depth of field, with a vintage film grain, in the style of Annie Leibovitz and in the style of Wes Anderson. --ar 85:128 --v 6.0 --style raw",
|
191 |
"Daria Morgendorffer the main character of the animated series Daria, serious expression, very excites sultry look, so hot girl, beautiful charismatic girl, so hot shot, a woman wearing eye glasses, gorgeous figure, interesting shapes, life-size figures",
|
192 |
"Dark green large leaves of anthurium, close up, photography, aerial view, in the style of unsplash, hasselblad h6d400c --ar 85:128 --v 6.0 --style raw",
|
193 |
-
"Closeup of blonde woman depth of field, bokeh, shallow focus, minimalism, fujifilm xh2s with Canon EF lens, cinematic --ar 85:128 --v 6.0 --style raw"
|
194 |
]
|
195 |
|
196 |
-
css =
|
197 |
.gradio-container{max-width: 700px !important}
|
198 |
h1{text-align:center}
|
199 |
-
|
200 |
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
201 |
gr.Markdown(DESCRIPTION)
|
202 |
gr.DuplicateButton(
|
@@ -216,7 +210,9 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
216 |
run_button = gr.Button("Run")
|
217 |
result = gr.Gallery(label="Result", columns=1, preview=True)
|
218 |
with gr.Accordion("Advanced options", open=False):
|
219 |
-
use_negative_prompt = gr.Checkbox(
|
|
|
|
|
220 |
negative_prompt = gr.Text(
|
221 |
label="Negative prompt",
|
222 |
max_lines=1,
|
@@ -241,12 +237,7 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
241 |
value=2,
|
242 |
)
|
243 |
seed = gr.Slider(
|
244 |
-
label="Seed",
|
245 |
-
minimum=0,
|
246 |
-
maximum=MAX_SEED,
|
247 |
-
step=1,
|
248 |
-
value=0,
|
249 |
-
visible=True
|
250 |
)
|
251 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
252 |
with gr.Row(visible=True):
|
|
|
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:
|
|
|
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",
|
|
|
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:
|
|
|
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 |
|
|
|
101 |
torch_dtype=torch.float16,
|
102 |
use_safetensors=True,
|
103 |
add_watermarker=False,
|
104 |
+
variant="fp16",
|
105 |
)
|
106 |
pipe2 = DiffusionPipeline.from_pretrained(
|
107 |
"SG161222/RealVisXL_V2.02_Turbo",
|
108 |
torch_dtype=torch.float16,
|
109 |
use_safetensors=True,
|
110 |
add_watermarker=False,
|
111 |
+
variant="fp16",
|
112 |
)
|
113 |
if ENABLE_CPU_OFFLOAD:
|
114 |
pipe.enable_model_cpu_offload()
|
115 |
pipe2.enable_model_cpu_offload()
|
116 |
else:
|
117 |
+
pipe.to(device)
|
118 |
+
pipe2.to(device)
|
119 |
print("Loaded on Device!")
|
120 |
+
|
121 |
if USE_TORCH_COMPILE:
|
122 |
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
123 |
pipe2.unet = torch.compile(pipe2.unet, mode="reduce-overhead", fullgraph=True)
|
124 |
print("Model Compiled!")
|
125 |
|
126 |
+
|
127 |
def save_image(img):
|
128 |
unique_name = str(uuid.uuid4()) + ".png"
|
129 |
img.save(unique_name)
|
130 |
return unique_name
|
131 |
|
132 |
+
|
133 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
134 |
if randomize_seed:
|
135 |
seed = random.randint(0, MAX_SEED)
|
136 |
return seed
|
137 |
|
138 |
+
|
139 |
@spaces.GPU(enable_queue=True)
|
140 |
def generate(
|
141 |
prompt: str,
|
|
|
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,
|
|
|
173 |
"use_resolution_binning": use_resolution_binning,
|
174 |
"output_type": "pil",
|
175 |
}
|
176 |
+
|
177 |
images = pipe(**options).images + pipe2(**options).images
|
178 |
|
179 |
image_paths = [save_image(img) for img in images]
|
180 |
return image_paths, seed
|
181 |
|
182 |
+
|
183 |
examples = [
|
184 |
"A closeup of a cat, a window, in a rustic cabin, close up, with a shallow depth of field, with a vintage film grain, in the style of Annie Leibovitz and in the style of Wes Anderson. --ar 85:128 --v 6.0 --style raw",
|
185 |
"Daria Morgendorffer the main character of the animated series Daria, serious expression, very excites sultry look, so hot girl, beautiful charismatic girl, so hot shot, a woman wearing eye glasses, gorgeous figure, interesting shapes, life-size figures",
|
186 |
"Dark green large leaves of anthurium, close up, photography, aerial view, in the style of unsplash, hasselblad h6d400c --ar 85:128 --v 6.0 --style raw",
|
187 |
+
"Closeup of blonde woman depth of field, bokeh, shallow focus, minimalism, fujifilm xh2s with Canon EF lens, cinematic --ar 85:128 --v 6.0 --style raw",
|
188 |
]
|
189 |
|
190 |
+
css = """
|
191 |
.gradio-container{max-width: 700px !important}
|
192 |
h1{text-align:center}
|
193 |
+
"""
|
194 |
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
195 |
gr.Markdown(DESCRIPTION)
|
196 |
gr.DuplicateButton(
|
|
|
210 |
run_button = gr.Button("Run")
|
211 |
result = gr.Gallery(label="Result", columns=1, preview=True)
|
212 |
with gr.Accordion("Advanced options", open=False):
|
213 |
+
use_negative_prompt = gr.Checkbox(
|
214 |
+
label="Use negative prompt", value=True, visible=True
|
215 |
+
)
|
216 |
negative_prompt = gr.Text(
|
217 |
label="Negative prompt",
|
218 |
max_lines=1,
|
|
|
237 |
value=2,
|
238 |
)
|
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="Randomize seed", value=True)
|
243 |
with gr.Row(visible=True):
|