Spaces:
Running
on
Zero
Running
on
Zero
prithivMLmods
commited on
Commit
•
9d8f34f
1
Parent(s):
cfb06b1
Update app.py
Browse files
app.py
CHANGED
@@ -1,166 +1,113 @@
|
|
1 |
#!/usr/bin/env python
|
2 |
-
#
|
3 |
import os
|
4 |
import random
|
5 |
import uuid
|
6 |
-
import
|
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
|
14 |
-
from typing import Tuple
|
15 |
|
16 |
-
|
17 |
-
bad_words = json.loads(os.getenv('BAD_WORDS', "[]"))
|
18 |
-
bad_words_negative = json.loads(os.getenv('BAD_WORDS_NEGATIVE', "[]"))
|
19 |
-
default_negative = os.getenv("default_negative","")
|
20 |
|
21 |
-
def
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
for i in bad_words_negative:
|
26 |
-
if i in negative:
|
27 |
-
return True
|
28 |
-
return False
|
29 |
-
# End of the - Prompt Con
|
30 |
-
|
31 |
-
style_list = [
|
32 |
-
{
|
33 |
-
"name": "3840 x 2160",
|
34 |
-
"prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
35 |
-
"negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
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 |
-
"name": "3D Model",
|
44 |
-
"prompt": "professional 3d model {prompt}. octane render, highly detailed, volumetric, dramatic lighting",
|
45 |
-
"negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
|
46 |
-
},
|
47 |
-
]
|
48 |
-
|
49 |
-
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
50 |
-
STYLE_NAMES = list(styles.keys())
|
51 |
-
DEFAULT_STYLE_NAME = "3840 x 2160"
|
52 |
-
|
53 |
-
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
54 |
-
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
55 |
-
if not negative:
|
56 |
-
negative = ""
|
57 |
-
return p.replace("{prompt}", positive), n + negative
|
58 |
-
|
59 |
-
DESCRIPTION = """## MidJourney 3D
|
60 |
-
"""
|
61 |
|
62 |
-
|
63 |
-
|
|
|
|
|
64 |
|
65 |
MAX_SEED = np.iinfo(np.int32).max
|
66 |
-
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "0") == "1"
|
67 |
-
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "2048"))
|
68 |
-
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
69 |
-
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
70 |
|
71 |
-
|
|
|
72 |
|
73 |
-
|
|
|
74 |
|
75 |
if torch.cuda.is_available():
|
76 |
-
pipe =
|
77 |
-
"
|
78 |
torch_dtype=torch.float16,
|
79 |
use_safetensors=True,
|
80 |
-
add_watermarker=False,
|
81 |
-
variant="fp16"
|
82 |
)
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
print("Model Compiled!")
|
92 |
-
|
93 |
-
def save_image(img):
|
94 |
-
unique_name = str(uuid.uuid4()) + ".png"
|
95 |
-
img.save(unique_name)
|
96 |
-
return unique_name
|
97 |
|
98 |
-
|
99 |
-
if randomize_seed:
|
100 |
-
seed = random.randint(0, MAX_SEED)
|
101 |
-
return seed
|
102 |
|
103 |
@spaces.GPU(enable_queue=True)
|
104 |
def generate(
|
105 |
prompt: str,
|
106 |
negative_prompt: str = "",
|
107 |
use_negative_prompt: bool = False,
|
108 |
-
style: str = DEFAULT_STYLE_NAME,
|
109 |
seed: int = 0,
|
110 |
width: int = 1024,
|
111 |
height: int = 1024,
|
112 |
guidance_scale: float = 3,
|
113 |
randomize_seed: bool = False,
|
114 |
-
use_resolution_binning: bool = True,
|
115 |
progress=gr.Progress(track_tqdm=True),
|
116 |
):
|
117 |
-
if check_text(prompt, negative_prompt):
|
118 |
-
raise ValueError("Prompt contains restricted words.")
|
119 |
-
|
120 |
-
prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
|
121 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
122 |
-
generator = torch.Generator().manual_seed(seed)
|
123 |
|
124 |
if not use_negative_prompt:
|
125 |
negative_prompt = "" # type: ignore
|
126 |
-
negative_prompt += default_negative
|
127 |
-
|
128 |
-
options = {
|
129 |
-
"prompt": prompt,
|
130 |
-
"negative_prompt": negative_prompt,
|
131 |
-
"width": width,
|
132 |
-
"height": height,
|
133 |
-
"guidance_scale": guidance_scale,
|
134 |
-
"num_inference_steps": 25,
|
135 |
-
"generator": generator,
|
136 |
-
"num_images_per_prompt": NUM_IMAGES_PER_PROMPT,
|
137 |
-
"use_resolution_binning": use_resolution_binning,
|
138 |
-
"output_type": "pil",
|
139 |
-
}
|
140 |
-
|
141 |
-
images = pipe(**options).images
|
142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
image_paths = [save_image(img) for img in images]
|
|
|
144 |
return image_paths, seed
|
145 |
|
146 |
examples = [
|
147 |
-
"
|
148 |
-
"
|
149 |
-
"
|
150 |
-
"
|
|
|
|
|
151 |
]
|
152 |
|
153 |
css = '''
|
154 |
-
.gradio-container{max-width:
|
155 |
h1{text-align:center}
|
|
|
|
|
|
|
156 |
'''
|
157 |
-
with gr.Blocks(css=css, theme="
|
158 |
gr.Markdown(DESCRIPTION)
|
159 |
gr.DuplicateButton(
|
160 |
value="Duplicate Space for private use",
|
161 |
elem_id="duplicate-button",
|
162 |
-
visible=
|
163 |
)
|
|
|
164 |
with gr.Group():
|
165 |
with gr.Row():
|
166 |
prompt = gr.Text(
|
@@ -170,33 +117,18 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
170 |
placeholder="Enter your prompt",
|
171 |
container=False,
|
172 |
)
|
173 |
-
run_button = gr.Button("Run")
|
174 |
-
result = gr.Gallery(label="Result", columns=1, preview=True)
|
175 |
with gr.Accordion("Advanced options", open=False):
|
176 |
-
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True
|
177 |
negative_prompt = gr.Text(
|
178 |
label="Negative prompt",
|
179 |
-
|
|
|
|
|
180 |
placeholder="Enter a negative prompt",
|
181 |
-
value="deformed iris, deformed pupils, semi-realistic, text, close up, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck",
|
182 |
visible=True,
|
183 |
)
|
184 |
-
with gr.Row():
|
185 |
-
num_inference_steps = gr.Slider(
|
186 |
-
label="Steps",
|
187 |
-
minimum=10,
|
188 |
-
maximum=60,
|
189 |
-
step=1,
|
190 |
-
value=30,
|
191 |
-
)
|
192 |
-
with gr.Row():
|
193 |
-
num_images_per_prompt = gr.Slider(
|
194 |
-
label="Images",
|
195 |
-
minimum=1,
|
196 |
-
maximum=5,
|
197 |
-
step=1,
|
198 |
-
value=2,
|
199 |
-
)
|
200 |
seed = gr.Slider(
|
201 |
label="Seed",
|
202 |
minimum=0,
|
@@ -229,21 +161,13 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
229 |
step=0.1,
|
230 |
value=6,
|
231 |
)
|
232 |
-
|
233 |
-
style_selection = gr.Radio(
|
234 |
-
show_label=True,
|
235 |
-
container=True,
|
236 |
-
interactive=True,
|
237 |
-
choices=STYLE_NAMES,
|
238 |
-
value=DEFAULT_STYLE_NAME,
|
239 |
-
label="Image Style",
|
240 |
-
)
|
241 |
gr.Examples(
|
242 |
examples=examples,
|
243 |
inputs=prompt,
|
244 |
outputs=[result, seed],
|
245 |
fn=generate,
|
246 |
-
cache_examples=
|
247 |
)
|
248 |
|
249 |
use_negative_prompt.change(
|
@@ -264,7 +188,6 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
264 |
prompt,
|
265 |
negative_prompt,
|
266 |
use_negative_prompt,
|
267 |
-
style_selection,
|
268 |
seed,
|
269 |
width,
|
270 |
height,
|
@@ -274,6 +197,6 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
274 |
outputs=[result, seed],
|
275 |
api_name="run",
|
276 |
)
|
277 |
-
|
278 |
if __name__ == "__main__":
|
279 |
-
demo.queue(max_size=
|
|
|
1 |
#!/usr/bin/env python
|
2 |
+
# Patch3.09
|
3 |
import os
|
4 |
import random
|
5 |
import uuid
|
6 |
+
import gdown
|
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 StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
|
|
14 |
|
15 |
+
DESCRIPTION = """ """
|
|
|
|
|
|
|
16 |
|
17 |
+
def save_image(img):
|
18 |
+
unique_name = str(uuid.uuid4()) + ".png"
|
19 |
+
img.save(unique_name)
|
20 |
+
return unique_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
23 |
+
if randomize_seed:
|
24 |
+
seed = random.randint(0, MAX_SEED)
|
25 |
+
return seed
|
26 |
|
27 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
if not torch.cuda.is_available():
|
30 |
+
DESCRIPTION += "\n<p>Running on CPU, This Space may not work on CPU.</p>"
|
31 |
|
32 |
+
USE_TORCH_COMPILE = 0
|
33 |
+
ENABLE_CPU_OFFLOAD = 0
|
34 |
|
35 |
if torch.cuda.is_available():
|
36 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
37 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
38 |
torch_dtype=torch.float16,
|
39 |
use_safetensors=True,
|
|
|
|
|
40 |
)
|
41 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
42 |
+
|
43 |
+
# Download the LoRA weights from Google Drive
|
44 |
+
drive_folder_url = "https://drive.google.com/drive/folders/1ExL5VNChyYWXho1QbgNbOkTK3xc8mhHW"
|
45 |
+
weight_file_id = "18n6gF7Jda92MpqK7cYs0Gv2IqLAVnltZ"
|
46 |
+
weight_file_name = "pytorch_lora_weights.safetensors"
|
47 |
+
# Use gdown to download the file
|
48 |
+
gdown.download(f"https://drive.google.com/uc?id={weight_file_id}", weight_file_name, quiet=False)
|
49 |
|
50 |
+
pipe.load_lora_weights(weight_file_name, adapter_name="icon")
|
51 |
+
pipe.set_adapters("icon")
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
pipe.to("cuda")
|
|
|
|
|
|
|
54 |
|
55 |
@spaces.GPU(enable_queue=True)
|
56 |
def generate(
|
57 |
prompt: str,
|
58 |
negative_prompt: str = "",
|
59 |
use_negative_prompt: bool = False,
|
|
|
60 |
seed: int = 0,
|
61 |
width: int = 1024,
|
62 |
height: int = 1024,
|
63 |
guidance_scale: float = 3,
|
64 |
randomize_seed: bool = False,
|
|
|
65 |
progress=gr.Progress(track_tqdm=True),
|
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 |
+
"1boy, male focus, sky, star (sky), night, pointing up, night sky, hood down, starry sky, hood, blue theme, outdoors, long sleeves, shooting star, hoodie, short hair, jacket, scenery, cloud, from behind, blue eyes, best quality, amazing quality, best aesthetic, absurdres",
|
89 |
+
"1boy, male focus, bishounen, holding sword, holding weapon, katana, sword, japanese clothes, haori, east asian architecture, solo, looking at viewer, expressionless, blue hair, purple eyes, long hair, best quality, amazing quality, best aesthetic, absurdres",
|
90 |
+
"1boy, male focus, holding drink, holding, drink, toned male, toned, pectorals, jacket, open jacket, open clothes, tank top, chain necklace, necklace, stud earrings, earrings, jewelry, cafe, plant, indoors, lens flare, solo, looking at viewer, open mouth, fang, white hair, yellow eyes, short hair, best quality, amazing quality, best aesthetic, absurdres, year 2023",
|
91 |
+
"1boy, male focus, dark-skinned male, dark skin, squatting, heart hands, bara, wooden floor, floor, indoors, gym uniform, sneakers, shoes, solo, looking at viewer, frown, sweatdrop, very short hair, best quality, amazing quality, best aesthetic, absurdres, year 2023",
|
92 |
+
"1boy, male focus, short hair, blue hair, blue eyes, graphic t-shirt, punk t-shirt, digital illustration, cyan and black, looking at viewer, busy city street, belt, black pants, atmospheric lighting, midriff peek, night, blurry, best quality, amazing quality, best aesthetic, absurdres",
|
93 |
+
"Ultra realistic close up portrait ((beautiful pale cyberpunk female with heavy black eyeliner)), blue eyes, shaved side haircut, hyper detail, cinematic lighting, magic neon, dark red city, Canon EOS R3, nikon, f/1.4, ISO 200, 1/160s, 8K, RAW, unedited, symmetrical balance, in-frame, 8K"
|
94 |
]
|
95 |
|
96 |
css = '''
|
97 |
+
.gradio-container{max-width: 600px !important}
|
98 |
h1{text-align:center}
|
99 |
+
footer {
|
100 |
+
visibility: hidden
|
101 |
+
}
|
102 |
'''
|
103 |
+
with gr.Blocks(css=css, theme="ParityError/Anime") 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(
|
|
|
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""",
|
129 |
placeholder="Enter a negative prompt",
|
|
|
130 |
visible=True,
|
131 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
seed = gr.Slider(
|
133 |
label="Seed",
|
134 |
minimum=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(
|
|
|
188 |
prompt,
|
189 |
negative_prompt,
|
190 |
use_negative_prompt,
|
|
|
191 |
seed,
|
192 |
width,
|
193 |
height,
|
|
|
197 |
outputs=[result, seed],
|
198 |
api_name="run",
|
199 |
)
|
200 |
+
|
201 |
if __name__ == "__main__":
|
202 |
+
demo.queue(max_size=20).launch(show_api=False, debug=False)
|