alfredplpl
commited on
Commit
•
9992492
1
Parent(s):
cc439fc
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -1,15 +1,12 @@
|
|
1 |
-
|
2 |
-
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, EulerAncestralDiscreteScheduler
|
3 |
-
from transformers import CLIPFeatureExtractor
|
4 |
import gradio as gr
|
5 |
import torch
|
6 |
from PIL import Image
|
7 |
|
8 |
-
model_id = 'aipicasso/cool-japan-diffusion-2-1-
|
9 |
prefix = ''
|
10 |
-
|
11 |
-
scheduler =
|
12 |
-
feature_extractor = CLIPFeatureExtractor.from_pretrained(model_id)
|
13 |
|
14 |
pipe = StableDiffusionPipeline.from_pretrained(
|
15 |
model_id,
|
@@ -19,11 +16,7 @@ pipe = StableDiffusionPipeline.from_pretrained(
|
|
19 |
pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(
|
20 |
model_id,
|
21 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
22 |
-
scheduler=scheduler
|
23 |
-
requires_safety_checker=False,
|
24 |
-
safety_checker=None,
|
25 |
-
feature_extractor=feature_extractor
|
26 |
-
)
|
27 |
|
28 |
if torch.cuda.is_available():
|
29 |
pipe = pipe.to("cuda")
|
@@ -33,55 +26,21 @@ def error_str(error, title="Error"):
|
|
33 |
return f"""#### {title}
|
34 |
{error}""" if error else ""
|
35 |
|
36 |
-
def inference(prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt="",
|
37 |
|
38 |
generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
|
|
|
39 |
|
40 |
try:
|
41 |
if img is not None:
|
42 |
-
return img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator
|
43 |
else:
|
44 |
-
return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator
|
45 |
except Exception as e:
|
46 |
return None, error_str(e)
|
47 |
|
48 |
-
def
|
49 |
-
|
50 |
-
prompt=str(prompt_ui)
|
51 |
-
neg_prompt=str(neg_prompt_ui)
|
52 |
-
prompt=prompt.lower()
|
53 |
-
neg_prompt=neg_prompt.lower()
|
54 |
-
if(prompt=="" and neg_prompt==""):
|
55 |
-
prompt="anime, a portrait of a girl, 4k, detailed"
|
56 |
-
neg_prompt=" (((deformed))), blurry, ((((bad anatomy)))), bad pupil, disfigured, poorly drawn face, mutation, mutated, (extra_limb), (ugly), (poorly drawn hands), bad hands, fused fingers, messy drawing, broken legs censor, low quality, ((mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), ((bad eyes)), ui, error, missing fingers, fused fingers, one hand with more than 5 fingers, one hand with less than 5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, long body, uncoordinated body, unnatural body, lowres, jpeg artifacts, 2d, 3d, cg, text"
|
57 |
-
|
58 |
-
splited_prompt=prompt.replace(","," ").replace("_"," ").split(" ")
|
59 |
-
splited_prompt=["girl" if p=="1girl" or p=="solo" else p for p in splited_prompt]
|
60 |
-
splited_prompt=["boy" if p=="1boy" else p for p in splited_prompt]
|
61 |
-
human_words=["girl","maid","female","woman","boy","male","man","guy"]
|
62 |
-
for word in human_words:
|
63 |
-
if( word in splited_prompt):
|
64 |
-
prompt=f"anime, {prompt}, 4k, detailed"
|
65 |
-
neg_prompt=" (((deformed))), blurry, ((((bad anatomy)))), bad pupil, disfigured, poorly drawn face, mutation, mutated, (extra_limb), (ugly), (poorly drawn hands), bad hands, fused fingers, messy drawing, broken legs censor, low quality, ((mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), ((bad eyes)), ui, error, missing fingers, fused fingers, one hand with more than 5 fingers, one hand with less than 5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, long body, uncoordinated body, unnatural body, lowres, jpeg artifacts, 2d, 3d, cg, text"
|
66 |
-
|
67 |
-
animal_words=["cat","dog","bird"]
|
68 |
-
for word in animal_words:
|
69 |
-
if( word in splited_prompt):
|
70 |
-
prompt=f"anime, a {word}, 4k, detailed"
|
71 |
-
neg_prompt=" (((deformed))), blurry, ((((bad anatomy)))), bad pupil, disfigured, poorly drawn face, mutation, mutated, (extra_limb), (ugly), (poorly drawn hands), bad hands, fused fingers, messy drawing, broken legs censor, low quality, ((mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), ((bad eyes)), ui, error, missing fingers, fused fingers, one hand with more than 5 fingers, one hand with less than 5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, long body, uncoordinated body, unnatural body, lowres, jpeg artifacts, 2d, 3d, cg, text"
|
72 |
-
|
73 |
-
background_words=["mount fuji","mt. fuji","building", "buildings", "tokyo"]
|
74 |
-
for word in background_words:
|
75 |
-
if( word in splited_prompt):
|
76 |
-
prompt=f"anime, shinkai makoto, {word}, 4k, 8k, highly detailed"
|
77 |
-
neg_prompt=" (((deformed))), photo, people, low quality, ui, error, lowres, jpeg artifacts, 2d, 3d, cg, text"
|
78 |
-
|
79 |
-
return prompt,neg_prompt
|
80 |
-
|
81 |
-
def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator,disable_auto_prompt_correction):
|
82 |
-
if(not disable_auto_prompt_correction):
|
83 |
-
prompt,neg_prompt=auto_prompt_correction(prompt,neg_prompt)
|
84 |
-
|
85 |
result = pipe(
|
86 |
prompt,
|
87 |
negative_prompt = neg_prompt,
|
@@ -93,10 +52,8 @@ def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator,dis
|
|
93 |
|
94 |
return result.images[0]
|
95 |
|
96 |
-
def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator
|
97 |
-
|
98 |
-
prompt,neg_prompt=auto_prompt_correction(prompt,neg_prompt)
|
99 |
-
|
100 |
ratio = min(height / img.height, width / img.width)
|
101 |
img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
|
102 |
result = pipe_i2i(
|
@@ -106,8 +63,8 @@ def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height
|
|
106 |
num_inference_steps = int(steps),
|
107 |
strength = strength,
|
108 |
guidance_scale = guidance,
|
109 |
-
|
110 |
-
|
111 |
generator = generator)
|
112 |
|
113 |
return result.images[0]
|
@@ -119,23 +76,14 @@ with gr.Blocks(css=css) as demo:
|
|
119 |
f"""
|
120 |
<div class="main-div">
|
121 |
<div>
|
122 |
-
<h1>Cool Japan Diffusion 2
|
123 |
</div>
|
124 |
<p>
|
125 |
-
Demo for <a href="https://huggingface.co/aipicasso/cool-japan-diffusion-2-1-
|
126 |
{"Add the following tokens to your prompts for the model to work properly: <b>prefix</b>" if prefix else ""}
|
127 |
</p>
|
128 |
-
<
|
129 |
-
|
130 |
-
</p>
|
131 |
-
<p>
|
132 |
-
sample prompt2 : boy
|
133 |
-
</p>
|
134 |
-
<p>
|
135 |
-
<a href="https://alfredplpl.hatenablog.com/entry/2022/12/30/102636">日本語の取扱説明書</a>.
|
136 |
-
</p>
|
137 |
-
Running on {"<b>GPU 🔥</b>" if torch.cuda.is_available() else f"<b>CPU 🥶</b>. For faster inference it is recommended to <b>upgrade to GPU in <a href='https://huggingface.co/spaces/akhaliq/cool-japan-diffusion-2-1-0/settings'>Settings</a></b>"} after duplicating the space<br><br>
|
138 |
-
<a style="display:inline-block" href="https://huggingface.co/spaces/akhaliq/cool-japan-diffusion-2-1-0?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
|
139 |
</div>
|
140 |
"""
|
141 |
)
|
@@ -154,11 +102,11 @@ with gr.Blocks(css=css) as demo:
|
|
154 |
with gr.Tab("Options"):
|
155 |
with gr.Group():
|
156 |
neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
|
157 |
-
|
158 |
|
159 |
with gr.Row():
|
160 |
guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
|
161 |
-
steps = gr.Slider(label="Steps", value=
|
162 |
|
163 |
with gr.Row():
|
164 |
width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)
|
@@ -171,7 +119,9 @@ with gr.Blocks(css=css) as demo:
|
|
171 |
image = gr.Image(label="Image", height=256, tool="editor", type="pil")
|
172 |
strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
|
173 |
|
174 |
-
|
|
|
|
|
175 |
outputs = [image_out, error_output]
|
176 |
prompt.submit(inference, inputs=inputs, outputs=outputs)
|
177 |
generate.click(inference, inputs=inputs, outputs=outputs)
|
@@ -184,4 +134,4 @@ with gr.Blocks(css=css) as demo:
|
|
184 |
""")
|
185 |
|
186 |
demo.queue(concurrency_count=1)
|
187 |
-
demo.launch()
|
|
|
1 |
+
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, DPMSolverMultistepScheduler
|
|
|
|
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
from PIL import Image
|
5 |
|
6 |
+
model_id = 'aipicasso/cool-japan-diffusion-2-1-1-beta'
|
7 |
prefix = ''
|
8 |
+
|
9 |
+
scheduler = DPMSolverMultistepScheduler.from_pretrained(model_id, subfolder="scheduler")
|
|
|
10 |
|
11 |
pipe = StableDiffusionPipeline.from_pretrained(
|
12 |
model_id,
|
|
|
16 |
pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(
|
17 |
model_id,
|
18 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
19 |
+
scheduler=scheduler)
|
|
|
|
|
|
|
|
|
20 |
|
21 |
if torch.cuda.is_available():
|
22 |
pipe = pipe.to("cuda")
|
|
|
26 |
return f"""#### {title}
|
27 |
{error}""" if error else ""
|
28 |
|
29 |
+
def inference(prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt="", auto_prefix=False):
|
30 |
|
31 |
generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
|
32 |
+
prompt = f"{prefix} {prompt}" if auto_prefix else prompt
|
33 |
|
34 |
try:
|
35 |
if img is not None:
|
36 |
+
return img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator), None
|
37 |
else:
|
38 |
+
return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator), None
|
39 |
except Exception as e:
|
40 |
return None, error_str(e)
|
41 |
|
42 |
+
def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator):
|
43 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
result = pipe(
|
45 |
prompt,
|
46 |
negative_prompt = neg_prompt,
|
|
|
52 |
|
53 |
return result.images[0]
|
54 |
|
55 |
+
def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator):
|
56 |
+
|
|
|
|
|
57 |
ratio = min(height / img.height, width / img.width)
|
58 |
img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
|
59 |
result = pipe_i2i(
|
|
|
63 |
num_inference_steps = int(steps),
|
64 |
strength = strength,
|
65 |
guidance_scale = guidance,
|
66 |
+
width = width,
|
67 |
+
height = height,
|
68 |
generator = generator)
|
69 |
|
70 |
return result.images[0]
|
|
|
76 |
f"""
|
77 |
<div class="main-div">
|
78 |
<div>
|
79 |
+
<h1>Cool Japan Diffusion 2 1 1 Beta</h1>
|
80 |
</div>
|
81 |
<p>
|
82 |
+
Demo for <a href="https://huggingface.co/aipicasso/cool-japan-diffusion-2-1-1-beta">Cool Japan Diffusion 2 1 1 Beta</a> Stable Diffusion model.<br>
|
83 |
{"Add the following tokens to your prompts for the model to work properly: <b>prefix</b>" if prefix else ""}
|
84 |
</p>
|
85 |
+
Running on {"<b>GPU 🔥</b>" if torch.cuda.is_available() else f"<b>CPU 🥶</b>. For faster inference it is recommended to <b>upgrade to GPU in <a href='https://huggingface.co/spaces/alfredplpl/cool-japan-diffusion-latest-demo/settings'>Settings</a></b>"} after duplicating the space<br><br>
|
86 |
+
<a style="display:inline-block" href="https://huggingface.co/spaces/alfredplpl/cool-japan-diffusion-latest-demo?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
</div>
|
88 |
"""
|
89 |
)
|
|
|
102 |
with gr.Tab("Options"):
|
103 |
with gr.Group():
|
104 |
neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
|
105 |
+
auto_prefix = gr.Checkbox(label="Prefix styling tokens automatically ()", value=prefix, visible=prefix)
|
106 |
|
107 |
with gr.Row():
|
108 |
guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
|
109 |
+
steps = gr.Slider(label="Steps", value=25, minimum=2, maximum=75, step=1)
|
110 |
|
111 |
with gr.Row():
|
112 |
width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)
|
|
|
119 |
image = gr.Image(label="Image", height=256, tool="editor", type="pil")
|
120 |
strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
|
121 |
|
122 |
+
auto_prefix.change(lambda x: gr.update(placeholder=f"{prefix} [your prompt]" if x else "[Your prompt]"), inputs=auto_prefix, outputs=prompt, queue=False)
|
123 |
+
|
124 |
+
inputs = [prompt, guidance, steps, width, height, seed, image, strength, neg_prompt, auto_prefix]
|
125 |
outputs = [image_out, error_output]
|
126 |
prompt.submit(inference, inputs=inputs, outputs=outputs)
|
127 |
generate.click(inference, inputs=inputs, outputs=outputs)
|
|
|
134 |
""")
|
135 |
|
136 |
demo.queue(concurrency_count=1)
|
137 |
+
demo.launch()
|