alfredplpl commited on
Commit
5e70012
1 Parent(s): 3447cde

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -26
app.py CHANGED
@@ -1,12 +1,14 @@
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,7 +18,11 @@ pipe = StableDiffusionPipeline.from_pretrained(
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,21 +32,55 @@ def error_str(error, title="Error"):
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,8 +92,10 @@ def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator):
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,8 +105,8 @@ def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height
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,14 +118,23 @@ with gr.Blocks(css=css) as demo:
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,11 +153,11 @@ with gr.Blocks(css=css) as demo:
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)
@@ -118,10 +169,9 @@ with gr.Blocks(css=css) as demo:
118
  with gr.Group():
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)
 
1
+ # Thank AK. https://huggingface.co/spaces/akhaliq/cool-japan-diffusion-2-1-0/blob/main/app.py
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-1-beta'
9
+
10
+ scheduler = EulerAncestralDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
11
+ feature_extractor = CLIPFeatureExtractor.from_pretrained(model_id)
12
 
13
  pipe = StableDiffusionPipeline.from_pretrained(
14
  model_id,
 
18
  pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(
19
  model_id,
20
  torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
21
+ scheduler=scheduler,
22
+ requires_safety_checker=False,
23
+ safety_checker=None,
24
+ feature_extractor=feature_extractor
25
+ )
26
 
27
  if torch.cuda.is_available():
28
  pipe = pipe.to("cuda")
 
32
  return f"""#### {title}
33
  {error}""" if error else ""
34
 
 
35
 
36
+ def inference(prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt="", disable_auto_prompt_correction=False):
 
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, disable_auto_prompt_correction), None
43
  else:
44
+ return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator, disable_auto_prompt_correction), None
45
  except Exception as e:
46
  return None, error_str(e)
47
+ def auto_prompt_correction(prompt_ui,neg_prompt_ui):
48
+ # auto prompt correction
49
+ prompt=str(prompt_ui)
50
+ neg_prompt=str(neg_prompt_ui)
51
+ prompt=prompt.lower()
52
+ neg_prompt=neg_prompt.lower()
53
+ if(prompt=="" and neg_prompt==""):
54
+ prompt="anime, a portrait of a girl, 4k, detailed"
55
+ 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"
56
+
57
+ splited_prompt=prompt.replace(","," ").replace("_"," ").split(" ")
58
+ splited_prompt=["girl" if p=="1girl" or p=="solo" else p for p in splited_prompt]
59
+ splited_prompt=["boy" if p=="1boy" else p for p in splited_prompt]
60
+ human_words=["girl","maid","female","woman","boy","male","man","guy"]
61
+ for word in human_words:
62
+ if( word in splited_prompt):
63
+ prompt=f"anime, {prompt}, 4k, detailed"
64
+ 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"
65
+
66
+ animal_words=["cat","dog","bird"]
67
+ for word in animal_words:
68
+ if( word in splited_prompt):
69
+ prompt=f"anime, a {word}, 4k, detailed"
70
+ 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"
71
+
72
+ background_words=["mount fuji","mt. fuji","building", "buildings", "tokyo"]
73
+ for word in background_words:
74
+ if( word in splited_prompt):
75
+ prompt=f"anime, shinkai makoto, {word}, 4k, 8k, highly detailed"
76
+ neg_prompt=" (((deformed))), photo, people, low quality, ui, error, lowres, jpeg artifacts, 2d, 3d, cg, text"
77
+
78
+ return prompt,neg_prompt
79
+
80
+ def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator,disable_auto_prompt_correction):
81
+ if(not disable_auto_prompt_correction):
82
+ prompt,neg_prompt=auto_prompt_correction(prompt,neg_prompt)
83
+
84
  result = pipe(
85
  prompt,
86
  negative_prompt = neg_prompt,
 
92
 
93
  return result.images[0]
94
 
95
+ def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator,disable_auto_prompt_correction):
96
+ if(not disable_auto_prompt_correction):
97
+ prompt,neg_prompt=auto_prompt_correction(prompt,neg_prompt)
98
+
99
  ratio = min(height / img.height, width / img.width)
100
  img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
101
  result = pipe_i2i(
 
105
  num_inference_steps = int(steps),
106
  strength = strength,
107
  guidance_scale = guidance,
108
+ #width = width,
109
+ #height = height,
110
  generator = generator)
111
 
112
  return result.images[0]
 
118
  f"""
119
  <div class="main-div">
120
  <div>
121
+ <h1>Cool Japan Diffusion 2.1.1 Beta</h1>
122
  </div>
123
  <p>
124
+ Demo for <a href="https://huggingface.co/aipicasso/cool-japan-diffusion-2-1-0">Cool Japan Diffusion 2 1 0</a> Stable Diffusion model.<br>
125
  {"Add the following tokens to your prompts for the model to work properly: <b>prefix</b>" if prefix else ""}
126
  </p>
127
+ <p>
128
+ sample prompt1 : girl
129
+ </p>
130
+ <p>
131
+ sample prompt2 : boy
132
+ </p>
133
+ <p>
134
+ <a href="https://alfredplpl.hatenablog.com/entry/2022/12/30/102636">日本語の取扱説明書</a>.
135
+ </p>
136
+ 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>
137
+ <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>
138
  </div>
139
  """
140
  )
 
153
  with gr.Tab("Options"):
154
  with gr.Group():
155
  neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
156
+ disable_auto_prompt_correction = gr.Checkbox(label="Disable auto prompt corretion.")
157
 
158
  with gr.Row():
159
  guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
160
+ steps = gr.Slider(label="Steps", value=20, minimum=2, maximum=75, step=1)
161
 
162
  with gr.Row():
163
  width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)
 
169
  with gr.Group():
170
  image = gr.Image(label="Image", height=256, tool="editor", type="pil")
171
  strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
172
+
173
+ inputs = [prompt, guidance, steps, width, height, seed, image, strength, neg_prompt, disable_auto_prompt_correction]
174
 
 
 
 
175
  outputs = [image_out, error_output]
176
  prompt.submit(inference, inputs=inputs, outputs=outputs)
177
  generate.click(inference, inputs=inputs, outputs=outputs)