Commit
·
395897a
1
Parent(s):
13f8f79
optimzie
Browse files- anime_app.py +70 -52
- local_anime_app.py +26 -15
anime_app.py
CHANGED
@@ -14,13 +14,15 @@ import numpy as np
|
|
14 |
# import imageio
|
15 |
from huggingface_hub import HfApi
|
16 |
import torch
|
|
|
17 |
from PIL import Image
|
18 |
from diffusers import (
|
19 |
ControlNetModel,
|
20 |
DPMSolverMultistepScheduler,
|
21 |
StableDiffusionControlNetPipeline,
|
22 |
-
AutoencoderKL,
|
23 |
)
|
|
|
24 |
from diffusers.models.attention_processor import AttnProcessor2_0
|
25 |
MAX_SEED = np.iinfo(np.int32).max
|
26 |
API_KEY = os.environ.get("API_KEY", None)
|
@@ -30,7 +32,28 @@ print("loading everything")
|
|
30 |
compiled = False
|
31 |
api = HfApi()
|
32 |
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
torch.cuda.max_memory_allocated(device="cuda")
|
36 |
# Controlnet Normal
|
@@ -59,10 +82,10 @@ scheduler = DPMSolverMultistepScheduler.from_pretrained(
|
|
59 |
# Stable Diffusion Pipeline URL
|
60 |
base_model_url = "https://huggingface.co/broyang/hentaidigitalart_v20/blob/main/realcartoon3d_v15.safetensors"
|
61 |
# base_model = "broyang/realcartoon3d"
|
62 |
-
print('loading vae')
|
63 |
-
vae_url = "https://huggingface.co/stabilityai/sd-vae-ft-mse-original/blob/main/vae-ft-mse-840000-ema-pruned.safetensors"
|
64 |
-
vae = AutoencoderKL.from_single_file(vae_url, torch_dtype=torch.float16).to("cuda")
|
65 |
-
vae.to(memory_format=torch.channels_last)
|
66 |
|
67 |
print('loading pipe')
|
68 |
pipe = StableDiffusionControlNetPipeline.from_single_file(
|
@@ -71,30 +94,21 @@ pipe = StableDiffusionControlNetPipeline.from_single_file(
|
|
71 |
# load_safety_checker=True,
|
72 |
controlnet=controlnet,
|
73 |
scheduler=scheduler,
|
74 |
-
vae=vae,
|
75 |
torch_dtype=torch.float16,
|
76 |
-
)
|
77 |
-
|
78 |
-
# pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
79 |
-
# base_model,
|
80 |
-
# safety_checker=None,
|
81 |
-
# controlnet=controlnet,
|
82 |
-
# scheduler=scheduler,
|
83 |
-
# vae=vae,
|
84 |
-
# torch_dtype=torch.float16,
|
85 |
-
# )
|
86 |
|
87 |
-
pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="EasyNegativeV2.safetensors", token="EasyNegativeV2",)
|
88 |
-
pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="badhandv4.pt", token="badhandv4")
|
89 |
-
pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_Ahegao.pt", token="HDA_Ahegao")
|
90 |
-
pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_Bondage.pt", token="HDA_Bondage")
|
91 |
-
pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_pet_play.pt", token="HDA_pet_play")
|
92 |
-
pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="fcNeg-neg.pt", token="fcNeg-neg")
|
93 |
-
pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_unconventional maid.pt", token="HDA_unconventional_maid")
|
94 |
-
pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_NakedHoodie.pt", token="HDA_NakedHoodie")
|
95 |
-
pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_NunDress.pt", token="HDA_NunDress")
|
96 |
-
pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_Shibari.pt", token="HDA_Shibari")
|
97 |
-
pipe.to("cuda")
|
98 |
|
99 |
print("loading preprocessor")
|
100 |
from preprocess import Preprocessor
|
@@ -103,14 +117,10 @@ preprocessor = Preprocessor()
|
|
103 |
|
104 |
|
105 |
print("---------------Loaded controlnet pipeline---------------")
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
111 |
-
pipe.unet.set_attn_processor(AttnProcessor2_0())
|
112 |
-
print("Model Compiled!")
|
113 |
-
init(pipe)
|
114 |
|
115 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
116 |
if randomize_seed:
|
@@ -160,19 +170,30 @@ def get_prompt(prompt, additional_prompt):
|
|
160 |
return prompt
|
161 |
|
162 |
css = """
|
163 |
-
h1 {
|
164 |
text-align: center;
|
165 |
-
display:block;
|
166 |
}
|
167 |
-
|
168 |
-
|
169 |
-
display:block;
|
170 |
}
|
171 |
-
|
172 |
-
|
173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
}
|
175 |
-
footer {visibility: hidden}
|
176 |
"""
|
177 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
178 |
#############################################################################
|
@@ -216,7 +237,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
216 |
with gr.Column():
|
217 |
prompt = gr.Textbox(
|
218 |
label="Description",
|
219 |
-
placeholder="
|
220 |
)
|
221 |
# input image
|
222 |
with gr.Row():
|
@@ -230,7 +251,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
230 |
)
|
231 |
# run button
|
232 |
with gr.Column():
|
233 |
-
run_button = gr.Button(value="Use this one", size=
|
234 |
# output image
|
235 |
with gr.Column():
|
236 |
result = gr.Image(
|
@@ -241,7 +262,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
241 |
)
|
242 |
# Use this image button
|
243 |
with gr.Column():
|
244 |
-
use_ai_button = gr.Button(value="Use this one", size=
|
245 |
config = [
|
246 |
image,
|
247 |
prompt,
|
@@ -308,11 +329,8 @@ def process_image(
|
|
308 |
torch.cuda.synchronize()
|
309 |
preprocess_start = time.time()
|
310 |
print("processing image")
|
|
|
311 |
preprocessor.load("NormalBae")
|
312 |
-
global compiled
|
313 |
-
if not compiled:
|
314 |
-
print("Not Compiled")
|
315 |
-
compiled = True
|
316 |
|
317 |
seed = random.randint(0, MAX_SEED)
|
318 |
generator = torch.cuda.manual_seed(seed)
|
|
|
14 |
# import imageio
|
15 |
from huggingface_hub import HfApi
|
16 |
import torch
|
17 |
+
import spaces
|
18 |
from PIL import Image
|
19 |
from diffusers import (
|
20 |
ControlNetModel,
|
21 |
DPMSolverMultistepScheduler,
|
22 |
StableDiffusionControlNetPipeline,
|
23 |
+
# AutoencoderKL,
|
24 |
)
|
25 |
+
from controlnet_aux_local import NormalBaeDetector
|
26 |
from diffusers.models.attention_processor import AttnProcessor2_0
|
27 |
MAX_SEED = np.iinfo(np.int32).max
|
28 |
API_KEY = os.environ.get("API_KEY", None)
|
|
|
32 |
compiled = False
|
33 |
api = HfApi()
|
34 |
|
35 |
+
class Preprocessor:
|
36 |
+
MODEL_ID = "lllyasviel/Annotators"
|
37 |
+
|
38 |
+
def __init__(self):
|
39 |
+
self.model = None
|
40 |
+
self.name = ""
|
41 |
+
|
42 |
+
def load(self, name: str) -> None:
|
43 |
+
if name == self.name:
|
44 |
+
return
|
45 |
+
elif name == "NormalBae":
|
46 |
+
print("Loading NormalBae")
|
47 |
+
self.model = NormalBaeDetector.from_pretrained(self.MODEL_ID).to("cuda")
|
48 |
+
torch.cuda.empty_cache()
|
49 |
+
self.name = name
|
50 |
+
else:
|
51 |
+
raise ValueError
|
52 |
+
return
|
53 |
+
|
54 |
+
def __call__(self, image: Image.Image, **kwargs) -> Image.Image:
|
55 |
+
return self.model(image, **kwargs)
|
56 |
+
|
57 |
|
58 |
torch.cuda.max_memory_allocated(device="cuda")
|
59 |
# Controlnet Normal
|
|
|
82 |
# Stable Diffusion Pipeline URL
|
83 |
base_model_url = "https://huggingface.co/broyang/hentaidigitalart_v20/blob/main/realcartoon3d_v15.safetensors"
|
84 |
# base_model = "broyang/realcartoon3d"
|
85 |
+
# print('loading vae')
|
86 |
+
# vae_url = "https://huggingface.co/stabilityai/sd-vae-ft-mse-original/blob/main/vae-ft-mse-840000-ema-pruned.safetensors"
|
87 |
+
# vae = AutoencoderKL.from_single_file(vae_url, torch_dtype=torch.float16).to("cuda")
|
88 |
+
# vae.to(memory_format=torch.channels_last)
|
89 |
|
90 |
print('loading pipe')
|
91 |
pipe = StableDiffusionControlNetPipeline.from_single_file(
|
|
|
94 |
# load_safety_checker=True,
|
95 |
controlnet=controlnet,
|
96 |
scheduler=scheduler,
|
97 |
+
# vae=vae,
|
98 |
torch_dtype=torch.float16,
|
99 |
+
).to("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
+
# pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="EasyNegativeV2.safetensors", token="EasyNegativeV2",)
|
102 |
+
# pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="badhandv4.pt", token="badhandv4")
|
103 |
+
# pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_Ahegao.pt", token="HDA_Ahegao")
|
104 |
+
# pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_Bondage.pt", token="HDA_Bondage")
|
105 |
+
# pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_pet_play.pt", token="HDA_pet_play")
|
106 |
+
# pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="fcNeg-neg.pt", token="fcNeg-neg")
|
107 |
+
# pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_unconventional maid.pt", token="HDA_unconventional_maid")
|
108 |
+
# pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_NakedHoodie.pt", token="HDA_NakedHoodie")
|
109 |
+
# pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_NunDress.pt", token="HDA_NunDress")
|
110 |
+
# pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_Shibari.pt", token="HDA_Shibari")
|
111 |
+
# pipe.to("cuda")
|
112 |
|
113 |
print("loading preprocessor")
|
114 |
from preprocess import Preprocessor
|
|
|
117 |
|
118 |
|
119 |
print("---------------Loaded controlnet pipeline---------------")
|
120 |
+
torch.cuda.empty_cache()
|
121 |
+
gc.collect()
|
122 |
+
print(f"CUDA memory allocated: {torch.cuda.max_memory_allocated(device='cuda') / 1e9:.2f} GB")
|
123 |
+
print("Model Compiled!")
|
|
|
|
|
|
|
|
|
124 |
|
125 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
126 |
if randomize_seed:
|
|
|
170 |
return prompt
|
171 |
|
172 |
css = """
|
173 |
+
h1, h2, h3 {
|
174 |
text-align: center;
|
175 |
+
display: block;
|
176 |
}
|
177 |
+
footer {
|
178 |
+
visibility: hidden;
|
|
|
179 |
}
|
180 |
+
.gradio-container {
|
181 |
+
max-width: 900px !important;
|
182 |
+
}
|
183 |
+
.gr-image {
|
184 |
+
display: flex;
|
185 |
+
justify-content: center;
|
186 |
+
align-items: center;
|
187 |
+
width: 100%;
|
188 |
+
height: 512px;
|
189 |
+
overflow: hidden;
|
190 |
+
}
|
191 |
+
.gr-image img {
|
192 |
+
width: 100%;
|
193 |
+
height: 100%;
|
194 |
+
object-fit: cover;
|
195 |
+
object-position: center;
|
196 |
}
|
|
|
197 |
"""
|
198 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
199 |
#############################################################################
|
|
|
237 |
with gr.Column():
|
238 |
prompt = gr.Textbox(
|
239 |
label="Description",
|
240 |
+
placeholder="Enter a description (optional)",
|
241 |
)
|
242 |
# input image
|
243 |
with gr.Row():
|
|
|
251 |
)
|
252 |
# run button
|
253 |
with gr.Column():
|
254 |
+
run_button = gr.Button(value="Use this one", size="lg", visible=False)
|
255 |
# output image
|
256 |
with gr.Column():
|
257 |
result = gr.Image(
|
|
|
262 |
)
|
263 |
# Use this image button
|
264 |
with gr.Column():
|
265 |
+
use_ai_button = gr.Button(value="Use this one", size="lg", visible=False)
|
266 |
config = [
|
267 |
image,
|
268 |
prompt,
|
|
|
329 |
torch.cuda.synchronize()
|
330 |
preprocess_start = time.time()
|
331 |
print("processing image")
|
332 |
+
global preprocessor
|
333 |
preprocessor.load("NormalBae")
|
|
|
|
|
|
|
|
|
334 |
|
335 |
seed = random.randint(0, MAX_SEED)
|
336 |
generator = torch.cuda.manual_seed(seed)
|
local_anime_app.py
CHANGED
@@ -1,8 +1,7 @@
|
|
1 |
prod = True
|
2 |
-
port = 8080
|
3 |
show_options = False
|
4 |
if prod:
|
5 |
-
port =
|
6 |
# show_options = False
|
7 |
|
8 |
import os
|
@@ -81,6 +80,11 @@ if gr.NO_RELOAD:
|
|
81 |
pipe.to("cuda")
|
82 |
|
83 |
print("---------------Loaded controlnet pipeline---------------")
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
86 |
if randomize_seed:
|
@@ -129,19 +133,30 @@ def get_prompt(prompt, additional_prompt):
|
|
129 |
return prompt
|
130 |
|
131 |
css = """
|
132 |
-
h1 {
|
133 |
text-align: center;
|
134 |
-
display:block;
|
135 |
}
|
136 |
-
|
137 |
-
|
138 |
-
display:block;
|
139 |
}
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
}
|
144 |
-
footer {visibility: hidden}
|
145 |
"""
|
146 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
147 |
#############################################################################
|
@@ -277,10 +292,6 @@ def process_image(
|
|
277 |
preprocess_start = time.time()
|
278 |
print("processing image")
|
279 |
preprocessor.load("NormalBae")
|
280 |
-
global compiled
|
281 |
-
if not compiled:
|
282 |
-
print("Not Compiled")
|
283 |
-
compiled = True
|
284 |
|
285 |
seed = random.randint(0, MAX_SEED)
|
286 |
generator = torch.cuda.manual_seed(seed)
|
|
|
1 |
prod = True
|
|
|
2 |
show_options = False
|
3 |
if prod:
|
4 |
+
port = 8082
|
5 |
# show_options = False
|
6 |
|
7 |
import os
|
|
|
80 |
pipe.to("cuda")
|
81 |
|
82 |
print("---------------Loaded controlnet pipeline---------------")
|
83 |
+
pipe.unet.set_attn_processor(AttnProcessor2_0())
|
84 |
+
torch.cuda.empty_cache()
|
85 |
+
gc.collect()
|
86 |
+
print(f"CUDA memory allocated: {torch.cuda.max_memory_allocated(device='cuda') / 1e9:.2f} GB")
|
87 |
+
print("Model Compiled!")
|
88 |
|
89 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
90 |
if randomize_seed:
|
|
|
133 |
return prompt
|
134 |
|
135 |
css = """
|
136 |
+
h1, h2, h3 {
|
137 |
text-align: center;
|
138 |
+
display: block;
|
139 |
}
|
140 |
+
footer {
|
141 |
+
visibility: hidden;
|
|
|
142 |
}
|
143 |
+
.gradio-container {
|
144 |
+
max-width: 900px !important;
|
145 |
+
}
|
146 |
+
.gr-image {
|
147 |
+
display: flex;
|
148 |
+
justify-content: center;
|
149 |
+
align-items: center;
|
150 |
+
width: 100%;
|
151 |
+
height: 512px;
|
152 |
+
overflow: hidden;
|
153 |
+
}
|
154 |
+
.gr-image img {
|
155 |
+
width: 100%;
|
156 |
+
height: 100%;
|
157 |
+
object-fit: cover;
|
158 |
+
object-position: center;
|
159 |
}
|
|
|
160 |
"""
|
161 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
162 |
#############################################################################
|
|
|
292 |
preprocess_start = time.time()
|
293 |
print("processing image")
|
294 |
preprocessor.load("NormalBae")
|
|
|
|
|
|
|
|
|
295 |
|
296 |
seed = random.randint(0, MAX_SEED)
|
297 |
generator = torch.cuda.manual_seed(seed)
|