Bobby commited on
Commit
778d937
·
1 Parent(s): 26a9c47
Files changed (5) hide show
  1. .gitignore +1 -0
  2. anime_app.py +136 -135
  3. anime_model.py +157 -185
  4. preprocess_anime.py +49 -57
  5. requirements.txt +4 -4
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ venv/*
anime_app.py CHANGED
@@ -1,135 +1,136 @@
1
- import gradio as gr
2
- from anime_model import Model
3
- import spaces
4
-
5
- show_options = True
6
-
7
- from settings import (
8
- DEFAULT_IMAGE_RESOLUTION,
9
- MAX_NUM_IMAGES,
10
- MAX_SEED,
11
- )
12
- from utils import randomize_seed_fn
13
- # if gr.NO_RELOAD:
14
-
15
- @spaces.GPU
16
- def init():
17
- base_model = "nyxia/AAM-AnyLoRA-Anime-Mix"
18
- model = Model()
19
-
20
- def auto_process_image(image, prompt):
21
- a_prompt="anime style, cartoon, drawing, 2D anime, illustration, cartoon"
22
- n_prompt="realism, 3d, BadDream, (UnrealisticDream:1.2), split image, multiple views, text, cropped, out of frame, worst quality, low quality, jpeg artifacts, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, bad proportions, extra limbs, longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality"
23
- num_samples=2
24
- image_resolution=512
25
- preprocess_resolution=512
26
- num_steps=15
27
- guidance_scale=4.5
28
- seed=randomize_seed_fn(0, True)
29
- config = [
30
- image,
31
- prompt,
32
- a_prompt,
33
- n_prompt,
34
- num_samples,
35
- image_resolution,
36
- preprocess_resolution,
37
- num_steps,
38
- guidance_scale,
39
- seed,
40
- ]
41
- if image is None:
42
- return None
43
- print("processing image")
44
- config[0] = image
45
- config[1] = prompt
46
- # print(config)
47
- return model.process_normal(*config)
48
-
49
- with gr.Blocks(theme=gr.themes.Soft(), css="footer {visibility: hidden}") as demo:
50
- # with gr.Row():
51
- # # examples
52
- # gr.Text(label="Anime Style Examples", value="Weeb!")
53
- with gr.Row():
54
- with gr.Column():
55
- # input text
56
- prompt = gr.Textbox(label="Anime Style", placeholder="anime tittes")
57
- with gr.Row():
58
- with gr.Column():
59
- # input image
60
- image = gr.Image(label="Input", sources=['upload'], show_label=True, format="jpeg")
61
- with gr.Column():
62
- # output
63
- result = gr.Gallery(label="Anime", show_label=True, columns=2, scale=3, object_fit="contain", format="jpeg")
64
- with gr.Column():
65
- # run button
66
- run_button = gr.Button(size=["lg"])
67
- with gr.Row():
68
- with gr.Accordion("Advanced options", open=show_options, visible=show_options):
69
- num_samples = gr.Slider(
70
- label="Images", minimum=1, maximum=MAX_NUM_IMAGES, value=2, step=1
71
- )
72
- image_resolution = gr.Slider(
73
- label="Image resolution",
74
- minimum=256,
75
- maximum=1024,
76
- value=DEFAULT_IMAGE_RESOLUTION,
77
- step=256,
78
- )
79
- preprocess_resolution = gr.Slider(
80
- label="Preprocess resolution", minimum=128, maximum=1024, value=512, step=1
81
- )
82
- num_steps = gr.Slider(label="Number of steps", minimum=1, maximum=100, value=15, step=1) # 20/4.5 or 12 without lora, 4 with lora
83
- guidance_scale = gr.Slider(label="Guidance scale", minimum=0.1, maximum=30.0, value=4.5, step=0.1) #5 without lora, 2 with lora
84
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
85
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
86
- a_prompt = gr.Textbox(label="Additional prompt", value="anime style, cartoon, drawing, 2D anime, illustration, cartoon")
87
- n_prompt = gr.Textbox(
88
- label="Negative prompt",
89
- # value="BadDream, (UnrealisticDream:1.2), split image, multiple views, text, cropped, out of frame, worst quality, low quality, jpeg artifacts, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, bad proportions, extra limbs, longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality"
90
- value="(signature:1.2),(artist name:1.2),(watermark:1.2), (easynegative), (low quality, worst quality:1.4), (bad anatomy), (inaccurate limb:1.2),bad composition, inaccurate eyes, extra digit,fewer digits, (extra arms:1.2), badhandv4,clothes"
91
- )
92
- config = [
93
- image,
94
- prompt,
95
- a_prompt,
96
- n_prompt,
97
- num_samples,
98
- image_resolution,
99
- preprocess_resolution,
100
- num_steps,
101
- guidance_scale,
102
- seed,
103
- ]
104
- prompt.submit(
105
- fn=randomize_seed_fn,
106
- inputs=[seed, randomize_seed],
107
- outputs=seed,
108
- queue=False,
109
- api_name=False,
110
- show_progress="minimal",
111
- ).then(
112
- fn=Model.process_normal,
113
- inputs=config,
114
- outputs=result,
115
- api_name=False,
116
- show_progress="minimal"
117
- )
118
- run_button.click(
119
- fn=randomize_seed_fn,
120
- inputs=[seed, randomize_seed],
121
- outputs=seed,
122
- queue=False,
123
- api_name=False,
124
- show_progress="minimal"
125
- ).then(
126
- fn=Model.process_normal,
127
- inputs=config,
128
- outputs=result,
129
- show_progress="minimal"
130
- )
131
- # image.change(auto_process_image, inputs=[image, prompt], outputs=[result])
132
-
133
- if __name__ == "__main__":
134
- #init()
135
- demo.queue(max_size=1).launch()
 
 
1
+ import gradio as gr
2
+ from anime_model import Model
3
+ import spaces
4
+ prod = False
5
+ port = 8080
6
+ show_options = True
7
+ if prod:
8
+ port = 8081
9
+ show_options = False
10
+
11
+ from settings import (
12
+ DEFAULT_IMAGE_RESOLUTION,
13
+ MAX_NUM_IMAGES,
14
+ MAX_SEED,
15
+ )
16
+ from utils import randomize_seed_fn
17
+
18
+ base_model = "nyxia/AAM-AnyLoRA-Anime-Mix"
19
+ model = Model(base_model_id=base_model, task_name="NormalBae")
20
+
21
+ # note: for high res 1024x1024, set num steps to 9 and guidance to 6
22
+ def auto_process_image(image, prompt):
23
+ a_prompt="anime style, cartoon, drawing, 2D anime, illustration, cartoon"
24
+ n_prompt="realism, 3d, BadDream, (UnrealisticDream:1.2), split image, multiple views, text, cropped, out of frame, worst quality, low quality, jpeg artifacts, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, bad proportions, extra limbs, longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality"
25
+ num_samples=4
26
+ image_resolution=512
27
+ preprocess_resolution=512
28
+ num_steps=15
29
+ guidance_scale=4.5
30
+ seed=randomize_seed_fn(0, True)
31
+ config = [
32
+ image,
33
+ prompt,
34
+ a_prompt,
35
+ n_prompt,
36
+ num_samples,
37
+ image_resolution,
38
+ preprocess_resolution,
39
+ num_steps,
40
+ guidance_scale,
41
+ seed,
42
+ ]
43
+ if image is None:
44
+ return None
45
+ print("processing image")
46
+ config[0] = image
47
+ config[1] = prompt
48
+ # print(config)
49
+ return model.process_normal(*config)
50
+
51
+ with gr.Blocks(theme=gr.themes.Soft(), css="footer {visibility: hidden}") as demo:
52
+ with gr.Row():
53
+ # examples
54
+ gr.Text(label="Anime Style Examples", value="Weeb!")
55
+ with gr.Row():
56
+ with gr.Column():
57
+ # input text
58
+ prompt = gr.Textbox(label="Anime Style", placeholder="anime tittes")
59
+ with gr.Row():
60
+ with gr.Column():
61
+ # input image
62
+ image = gr.Image(label="Input", sources=['upload'], show_label=True, format="jpeg")
63
+ with gr.Column():
64
+ # output
65
+ result = gr.Gallery(label="Anime", show_label=True, columns=2, scale=3, object_fit="contain", format="jpeg")
66
+ with gr.Column():
67
+ # run button
68
+ run_button = gr.Button(size=["lg"])
69
+ with gr.Row():
70
+ with gr.Accordion("Advanced options", open=show_options, visible=show_options):
71
+ num_samples = gr.Slider(
72
+ label="Images", minimum=1, maximum=MAX_NUM_IMAGES, value=4, step=1
73
+ )
74
+ image_resolution = gr.Slider(
75
+ label="Image resolution",
76
+ minimum=256,
77
+ maximum=1024,
78
+ value=DEFAULT_IMAGE_RESOLUTION,
79
+ step=256,
80
+ )
81
+ preprocess_resolution = gr.Slider(
82
+ label="Preprocess resolution", minimum=128, maximum=1024, value=512, step=1
83
+ )
84
+ num_steps = gr.Slider(label="Number of steps", minimum=1, maximum=100, value=15, step=1) # 20/4.5 or 12 without lora, 4 with lora
85
+ guidance_scale = gr.Slider(label="Guidance scale", minimum=0.1, maximum=30.0, value=4.5, step=0.1) #5 without lora, 2 with lora
86
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
87
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
88
+ a_prompt = gr.Textbox(label="Additional prompt", value="anime style, cartoon, drawing, 2D anime, illustration, cartoon")
89
+ n_prompt = gr.Textbox(
90
+ label="Negative prompt",
91
+ # value="BadDream, (UnrealisticDream:1.2), split image, multiple views, text, cropped, out of frame, worst quality, low quality, jpeg artifacts, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, bad proportions, extra limbs, longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality"
92
+ value="(signature:1.2),(artist name:1.2),(watermark:1.2), (easynegative), (low quality, worst quality:1.4), (bad anatomy), (inaccurate limb:1.2),bad composition, inaccurate eyes, extra digit,fewer digits, (extra arms:1.2), badhandv4,clothes"
93
+ )
94
+ config = [
95
+ image,
96
+ prompt,
97
+ a_prompt,
98
+ n_prompt,
99
+ num_samples,
100
+ image_resolution,
101
+ preprocess_resolution,
102
+ num_steps,
103
+ guidance_scale,
104
+ seed,
105
+ ]
106
+ prompt.submit(
107
+ fn=randomize_seed_fn,
108
+ inputs=[seed, randomize_seed],
109
+ outputs=seed,
110
+ queue=False,
111
+ api_name=False,
112
+ show_progress="minimal",
113
+ ).then(
114
+ fn=model.process_normal,
115
+ inputs=config,
116
+ outputs=result,
117
+ api_name=False,
118
+ show_progress="minimal"
119
+ )
120
+ run_button.click(
121
+ fn=randomize_seed_fn,
122
+ inputs=[seed, randomize_seed],
123
+ outputs=seed,
124
+ queue=False,
125
+ api_name=False,
126
+ show_progress="minimal"
127
+ ).then(
128
+ fn=model.process_normal,
129
+ inputs=config,
130
+ outputs=result,
131
+ show_progress="minimal"
132
+ )
133
+ image.change(auto_process_image, inputs=[image, prompt], outputs=[result])
134
+
135
+ if __name__ == "__main__":
136
+ demo.queue(max_size=20).launch(server_name="localhost", server_port=port)
anime_model.py CHANGED
@@ -1,185 +1,157 @@
1
- from __future__ import annotations
2
- import gc
3
- import time
4
- import spaces
5
- import numpy as np
6
- import PIL.Image
7
- import torch
8
- from controlnet_aux import NormalBaeDetector
9
- from controlnet_aux.util import HWC3
10
- from cv_utils import resize_image
11
- from diffusers import (
12
- ControlNetModel,
13
- AutoencoderKL,
14
- StableDiffusionControlNetPipeline,
15
- DPMSolverMultistepScheduler,
16
- )
17
- from settings import MAX_IMAGE_RESOLUTION, MAX_NUM_IMAGES
18
-
19
- class Model:
20
-
21
- def __init__(self):
22
- print("Initializing base model: ", base_model_id, " with ", task_name)
23
- self.base_model_id = "nyxia/AAM-AnyLoRA-Anime-Mix"
24
- self.task_name = "NormalBae"
25
- self.pipe = self.load_pipe(base_model_id, task_name)
26
- self.model = None
27
- self.name = ""
28
-
29
- def load(self, name: str) -> None:
30
- if name == self.name:
31
- return
32
- elif name == "NormalBae":
33
- model = NormalBaeDetector.from_pretrained(self.MODEL_ID)#.to("cuda")
34
- else:
35
- raise ValueError
36
- torch.cuda.empty_cache()
37
- gc.collect()
38
- self.name = name
39
- self.model = model
40
-
41
-
42
- def preprocess(self, image: PIL.Image.Image, **kwargs) -> PIL.Image.Image:
43
- if self.name == "Canny":
44
- if "detect_resolution" in kwargs:
45
- detect_resolution = kwargs.pop("detect_resolution")
46
- image = np.array(image)
47
- image = HWC3(image)
48
- image = resize_image(image, resolution=detect_resolution)
49
- image = self.model(image, **kwargs)
50
- return PIL.Image.fromarray(image)
51
- elif self.name == "Midas":
52
- detect_resolution = kwargs.pop("detect_resolution", 512)
53
- image_resolution = kwargs.pop("image_resolution", 512)
54
- image = np.array(image)
55
- image = HWC3(image)
56
- image = resize_image(image, resolution=detect_resolution)
57
- image = self.model(image, **kwargs)
58
- image = HWC3(image)
59
- image = resize_image(image, resolution=image_resolution)
60
- return PIL.Image.fromarray(image)
61
- else:
62
- return self.model(image, **kwargs)
63
-
64
- @spaces.GPU
65
- def load_pipe(self, base_model_id, task_name):
66
- print("loading pipe")
67
- # Controlnet
68
- model_id = "lllyasviel/control_v11p_sd15_normalbae"
69
- print("initializing controlnet")
70
- controlnet = ControlNetModel.from_pretrained(
71
- model_id,
72
- torch_dtype=torch.float16,
73
- attn_implementation="flash_attention_2",
74
- ).to("cuda")
75
- controlnet.to(memory_format=torch.channels_last)
76
-
77
- # Scheduler
78
- scheduler = DPMSolverMultistepScheduler.from_pretrained(
79
- "stabilityai/stable-diffusion-xl-base-1.0",
80
- subfolder="scheduler",
81
- use_karras_sigmas=True,
82
- algorithm_type="sde-dpmsolver++",
83
- denoise_final=True,
84
- device_map="cuda",
85
- attn_implementation="flash_attention_2",
86
- )
87
-
88
- # VAE
89
- vae_url = "https://huggingface.co/stabilityai/sd-vae-ft-mse-original/blob/main/vae-ft-mse-840000-ema-pruned.safetensors"
90
- vae = AutoencoderKL.from_single_file(vae_url, torch_dtype=torch.float16).to("cuda")
91
- vae.to(memory_format=torch.channels_last)
92
-
93
- # Stable Diffusion Pipeline
94
- pipe = StableDiffusionControlNetPipeline.from_pretrained(
95
- base_model_id,
96
- safety_checker=None,
97
- controlnet=controlnet,
98
- scheduler=scheduler,
99
- vae=vae,
100
- torch_dtype=torch.float16,
101
- ).to("cuda")
102
-
103
- # Efficiency optimizations - DO NOT CHANGE ORDER
104
- pipe.enable_xformers_memory_efficient_attention()
105
-
106
- torch.cuda.empty_cache()
107
- gc.collect()
108
- self.base_model_id = base_model_id
109
- self.task_name = task_name
110
-
111
- return pipe
112
-
113
- def get_prompt(self, prompt: str, additional_prompt: str) -> str:
114
- if not prompt:
115
- prompt = additional_prompt
116
- else:
117
- prompt = f"{prompt}, {additional_prompt}"
118
- return prompt
119
-
120
- @torch.inference_mode()
121
- def run_pipe(
122
- prompt: str,
123
- negative_prompt: str,
124
- control_image: PIL.Image.Image,
125
- num_images: int,
126
- num_steps: int,
127
- guidance_scale: float,
128
- seed: int,
129
- ) -> list[PIL.Image.Image]:
130
- generator = torch.cuda.manual_seed(seed)
131
- torch.cuda.synchronize()
132
- start = time.time()
133
- results = self.pipe(
134
- prompt=prompt,
135
- negative_prompt=negative_prompt,
136
- guidance_scale=guidance_scale,
137
- num_images_per_prompt=num_images,
138
- num_inference_steps=num_steps,
139
- generator=generator,
140
- image=control_image,
141
- ).images
142
- print(f"Inference done in: {time.time() - start:.2f} seconds")
143
- print(f"Prompt {prompt}")
144
- torch.cuda.synchronize()
145
- torch.cuda.empty_cache()
146
- gc.collect()
147
-
148
- return results
149
-
150
- @spaces.GPU
151
- def process_normal(
152
- self,
153
- image: np.ndarray,
154
- prompt: str,
155
- additional_prompt: str,
156
- negative_prompt: str,
157
- num_images: int,
158
- image_resolution: int,
159
- preprocess_resolution: int,
160
- num_steps: int,
161
- guidance_scale: float,
162
- seed: int,
163
- ) -> list[PIL.Image.Image]:
164
- if image is None:
165
- raise ValueError
166
- #if image_resolution > MAX_IMAGE_RESOLUTION:
167
- # raise ValueError
168
- #if num_images > MAX_NUM_IMAGES:
169
- # raise ValueError
170
- #self.load("NormalBae")
171
- model = NormalBaeDetector.from_pretrained("lllyasviel/Annotators").to("cuda")
172
- torch.cuda.empty_cache()
173
- gc.collect()
174
- if prompt == "":
175
- prompt = "anime girl"
176
- print(prompt)
177
- return run_pipe(
178
- prompt=self.get_prompt("Hentai Nude Anime Titties of " + prompt, additional_prompt),
179
- negative_prompt=negative_prompt,
180
- control_image=image,
181
- num_images=num_images,
182
- num_steps=num_steps,
183
- guidance_scale=guidance_scale,
184
- seed=seed,
185
- )
 
1
+ from __future__ import annotations
2
+
3
+ import gc
4
+ import time
5
+ import numpy as np
6
+ import PIL.Image
7
+ import torch
8
+ import spaces
9
+ from diffusers import (
10
+ ControlNetModel,
11
+ AutoencoderKL,
12
+ StableDiffusionControlNetPipeline,
13
+ DPMSolverMultistepScheduler,
14
+ )
15
+ from preprocess_anime import Preprocessor
16
+ from settings import MAX_IMAGE_RESOLUTION, MAX_NUM_IMAGES
17
+
18
+ @spaces.GPU
19
+ class Model:
20
+ def __init__(self, base_model_id, task_name):
21
+ print("Initializing base model: ", base_model_id, " with ", task_name)
22
+ self.base_model_id = base_model_id
23
+ self.task_name = task_name
24
+ self.pipe = self.load_pipe(base_model_id, task_name)
25
+ self.preprocessor = Preprocessor()
26
+
27
+ def load_pipe(self, base_model_id, task_name):
28
+ print("loading pipe")
29
+ # Controlnet
30
+ model_id = "lllyasviel/control_v11p_sd15_normalbae"
31
+ print("initializing controlnet")
32
+ controlnet = ControlNetModel.from_pretrained(
33
+ model_id,
34
+ torch_dtype=torch.float16,
35
+ attn_implementation="flash_attention_2",
36
+ ).to("cuda")
37
+ controlnet.to(memory_format=torch.channels_last)
38
+
39
+ # Scheduler
40
+ scheduler = DPMSolverMultistepScheduler.from_pretrained(
41
+ "stabilityai/stable-diffusion-xl-base-1.0",
42
+ subfolder="scheduler",
43
+ use_karras_sigmas=True,
44
+ # final_sigmas_type="sigma_min",
45
+ algorithm_type="sde-dpmsolver++",
46
+ # prediction_type="epsilon",
47
+ # thresholding=False,
48
+ denoise_final=True,
49
+ device_map="cuda",
50
+ attn_implementation="flash_attention_2",
51
+ )
52
+
53
+ #vae
54
+ vae_url = "https://huggingface.co/stabilityai/sd-vae-ft-mse-original/blob/main/vae-ft-mse-840000-ema-pruned.safetensors"
55
+ vae = AutoencoderKL.from_single_file(vae_url, torch_dtype=torch.float16).to("cuda")
56
+ vae.to(memory_format=torch.channels_last)
57
+ # Stable Diffusion Pipeline
58
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
59
+ base_model_id,
60
+ safety_checker=None,
61
+ controlnet=controlnet,
62
+ scheduler=scheduler,
63
+ vae=vae,
64
+ torch_dtype=torch.float16,
65
+ ).to("cuda")
66
+
67
+ # efficiency optimizations - DO NOT CHANGE ORDER
68
+ pipe.enable_xformers_memory_efficient_attention()
69
+
70
+ # lora
71
+ # pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5")
72
+ # pipe.load_lora_weights("Lykon/AnyLoRA", weight_name="AnyLoRA_bakedVae_blessed_fp16.safetensors")
73
+ # pipe.load_lora_weights("Lykon/AnyLoRA", weight_name="AnyLoRA_noVae_fp16-pruned.safetensors")
74
+ # pipe.fuse_lora()
75
+ # pipe.unet.to(memory_format=torch.channels_last)
76
+
77
+ torch.cuda.empty_cache()
78
+ gc.collect()
79
+ self.base_model_id = base_model_id
80
+ self.task_name = task_name
81
+
82
+ return pipe
83
+
84
+ def get_prompt(self, prompt: str, additional_prompt: str) -> str:
85
+ if not prompt:
86
+ prompt = additional_prompt
87
+ else:
88
+ prompt = f"{prompt}, {additional_prompt}"
89
+ return prompt
90
+
91
+ @torch.inference_mode()
92
+ def run_pipe(
93
+ self,
94
+ prompt: str,
95
+ negative_prompt: str,
96
+ control_image: PIL.Image.Image,
97
+ num_images: int,
98
+ num_steps: int,
99
+ guidance_scale: float,
100
+ seed: int,
101
+ ) -> list[PIL.Image.Image]:
102
+ generator = torch.cuda.manual_seed(seed)
103
+ torch.cuda.synchronize()
104
+ start = time.time()
105
+ results = self.pipe(
106
+ prompt=prompt,
107
+ negative_prompt=negative_prompt,
108
+ guidance_scale=guidance_scale,
109
+ num_images_per_prompt=num_images,
110
+ num_inference_steps=num_steps,
111
+ generator=generator,
112
+ image=control_image,
113
+ ).images
114
+ print(f"Inference done in: {time.time() - start:.2f} seconds")
115
+ print(f"Prompt {prompt}")
116
+ torch.cuda.synchronize()
117
+ torch.cuda.empty_cache()
118
+ gc.collect()
119
+
120
+ return results
121
+ def process_normal(
122
+ self,
123
+ image: np.ndarray,
124
+ prompt: str,
125
+ additional_prompt: str,
126
+ negative_prompt: str,
127
+ num_images: int,
128
+ image_resolution: int,
129
+ preprocess_resolution: int,
130
+ num_steps: int,
131
+ guidance_scale: float,
132
+ seed: int,
133
+ ) -> list[PIL.Image.Image]:
134
+ if image is None:
135
+ raise ValueError
136
+ if image_resolution > MAX_IMAGE_RESOLUTION:
137
+ raise ValueError
138
+ if num_images > MAX_NUM_IMAGES:
139
+ raise ValueError
140
+ self.preprocessor.load("NormalBae")
141
+ control_image = self.preprocessor(
142
+ image=image,
143
+ image_resolution=image_resolution,
144
+ detect_resolution=preprocess_resolution,
145
+ )
146
+ if prompt == "":
147
+ prompt = "anime girl"
148
+ print(prompt)
149
+ return self.run_pipe(
150
+ prompt=self.get_prompt("Hentai Photo from imgur of " + prompt, additional_prompt),
151
+ negative_prompt=negative_prompt,
152
+ control_image=control_image,
153
+ num_images=num_images,
154
+ num_steps=num_steps,
155
+ guidance_scale=guidance_scale,
156
+ seed=seed,
157
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
preprocess_anime.py CHANGED
@@ -1,57 +1,49 @@
1
- import gc
2
- import spaces
3
- import numpy as np
4
- import PIL.Image
5
- import torch
6
- from controlnet_aux import (
7
-
8
- NormalBaeDetector,
9
-
10
- )
11
- from controlnet_aux.util import HWC3
12
-
13
- from cv_utils import resize_image
14
-
15
-
16
-
17
- class Preprocessor:
18
- MODEL_ID = "lllyasviel/Annotators"
19
-
20
- def __init__(self):
21
- self.model = None
22
- self.name = ""
23
-
24
- @spaces.GPU
25
- def load(self, name: str) -> None:
26
- if name == self.name:
27
- return
28
- elif name == "NormalBae":
29
- self.model = NormalBaeDetector.from_pretrained(self.MODEL_ID).to("cuda")
30
- else:
31
- raise ValueError
32
- torch.cuda.empty_cache()
33
- gc.collect()
34
- self.name = name
35
-
36
- @spaces.GPU
37
- def __call__(self, image: PIL.Image.Image, **kwargs) -> PIL.Image.Image:
38
- if self.name == "Canny":
39
- if "detect_resolution" in kwargs:
40
- detect_resolution = kwargs.pop("detect_resolution")
41
- image = np.array(image)
42
- image = HWC3(image)
43
- image = resize_image(image, resolution=detect_resolution)
44
- image = self.model(image, **kwargs)
45
- return PIL.Image.fromarray(image)
46
- elif self.name == "Midas":
47
- detect_resolution = kwargs.pop("detect_resolution", 512)
48
- image_resolution = kwargs.pop("image_resolution", 512)
49
- image = np.array(image)
50
- image = HWC3(image)
51
- image = resize_image(image, resolution=detect_resolution)
52
- image = self.model(image, **kwargs)
53
- image = HWC3(image)
54
- image = resize_image(image, resolution=image_resolution)
55
- return PIL.Image.fromarray(image)
56
- else:
57
- return self.model(image, **kwargs)
 
1
+ import gc
2
+
3
+ import numpy as np
4
+ import PIL.Image
5
+ import torch
6
+ from controlnet_aux import NormalBaeDetector
7
+
8
+ from controlnet_aux.util import HWC3
9
+ from cv_utils import resize_image
10
+
11
+ class Preprocessor:
12
+ MODEL_ID = "lllyasviel/Annotators"
13
+
14
+ def __init__(self):
15
+ self.model = None
16
+ self.name = ""
17
+
18
+ def load(self, name: str) -> None:
19
+ if name == self.name:
20
+ return
21
+ elif name == "NormalBae":
22
+ self.model = NormalBaeDetector.from_pretrained(self.MODEL_ID).to("cuda")
23
+ else:
24
+ raise ValueError
25
+ torch.cuda.empty_cache()
26
+ gc.collect()
27
+ self.name = name
28
+
29
+ def __call__(self, image: PIL.Image.Image, **kwargs) -> PIL.Image.Image:
30
+ if self.name == "Canny":
31
+ if "detect_resolution" in kwargs:
32
+ detect_resolution = kwargs.pop("detect_resolution")
33
+ image = np.array(image)
34
+ image = HWC3(image)
35
+ image = resize_image(image, resolution=detect_resolution)
36
+ image = self.model(image, **kwargs)
37
+ return PIL.Image.fromarray(image)
38
+ elif self.name == "Midas":
39
+ detect_resolution = kwargs.pop("detect_resolution", 512)
40
+ image_resolution = kwargs.pop("image_resolution", 512)
41
+ image = np.array(image)
42
+ image = HWC3(image)
43
+ image = resize_image(image, resolution=detect_resolution)
44
+ image = self.model(image, **kwargs)
45
+ image = HWC3(image)
46
+ image = resize_image(image, resolution=image_resolution)
47
+ return PIL.Image.fromarray(image)
48
+ else:
49
+ return self.model(image, **kwargs)
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,6 +1,6 @@
1
- torch==2.1.2
2
- torchvision
3
- torchaudio
4
  diffusers==0.27.2
5
  einops==0.6.1
6
  gradio==4.26.0
@@ -10,6 +10,6 @@ mediapipe==0.10.1
10
  opencv-python-headless==4.8.0.74
11
  safetensors==0.4.2
12
  transformers==4.39.3
13
- xformers==0.0.23.post1
14
  accelerate==0.29.1
15
  #controlnet_aux==0.0.7
 
1
+ #torch==2.1.2
2
+ #torchvision
3
+ #torchaudio
4
  diffusers==0.27.2
5
  einops==0.6.1
6
  gradio==4.26.0
 
10
  opencv-python-headless==4.8.0.74
11
  safetensors==0.4.2
12
  transformers==4.39.3
13
+ #xformers==0.0.23.post1
14
  accelerate==0.29.1
15
  #controlnet_aux==0.0.7