Rename evoukiyoe_v1.py and update app.py

#1
Files changed (4) hide show
  1. README.md +4 -4
  2. app.py +10 -26
  3. evo_ukiyoe_v1.py +3 -22
  4. requirements.txt +8 -7
README.md CHANGED
@@ -1,12 +1,12 @@
1
  ---
2
- title: Evo-Ukiyoe
3
- emoji: 🐠
4
  colorFrom: purple
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 4.38.1
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Evo Ukiyoe
3
+ emoji: 🏆
4
  colorFrom: purple
5
  colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 4.37.2
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,21 +1,16 @@
1
  import random
2
 
3
  from PIL import Image
4
- from diffusers import EulerDiscreteScheduler
5
  import gradio as gr
6
  import numpy as np
7
  import spaces
8
  import torch
9
 
10
- # torch._inductor.config.conv_1x1_as_mm = True
11
- # torch._inductor.config.coordinate_descent_tuning = True
12
- # torch._inductor.config.epilogue_fusion = False
13
- # torch._inductor.config.coordinate_descent_check_all_directions = True
14
  from evo_ukiyoe_v1 import load_evo_ukiyoe
15
 
16
 
17
  DESCRIPTION = """# 🐟 Evo-Ukiyoe
18
- 🤗 [モデル一覧](https://huggingface.co/SakanaAI) | 📝 [ブログ](https://sakana.ai/evo-ukiyoe/) | 🐦 [Twitter](https://twitter.com/SakanaAILabs)
19
 
20
  [Evo-Ukiyoe](https://huggingface.co/SakanaAI/Evo-Ukiyoe-v1)は[Sakana AI](https://sakana.ai/)が教育目的で開発した浮世絵に特化した画像生成モデルです。
21
  入力した日本語プロンプトに沿った浮世絵風の画像を生成することができます。より詳しくは、上記のブログをご参照ください。
@@ -51,16 +46,7 @@ if SAFETY_CHECKER:
51
  return images, has_nsfw_concepts
52
 
53
 
54
- pipe = load_evo_ukiyoe(device)
55
- pipe.scheduler = EulerDiscreteScheduler.from_config(
56
- pipe.scheduler.config, use_karras_sigmas=True,
57
- )
58
- pipe.to(device=device, dtype=torch.float16)
59
- # pipe.unet.to(memory_format=torch.channels_last)
60
- # pipe.vae.to(memory_format=torch.channels_last)
61
- # # Compile the UNet and VAE.
62
- # pipe.unet = torch.compile(pipe.unet, mode="max-autotune", fullgraph=True)
63
- # pipe.vae.decode = torch.compile(pipe.vae.decode, mode="max-autotune", fullgraph=True)
64
 
65
 
66
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
@@ -73,21 +59,21 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
73
  @torch.inference_mode()
74
  def generate(
75
  prompt: str,
76
- negative_prompt: str = "",
77
  seed: int = 0,
78
  randomize_seed: bool = False,
79
  progress=gr.Progress(track_tqdm=True),
80
  ):
 
81
  seed = int(randomize_seed_fn(seed, randomize_seed))
82
  generator = torch.Generator().manual_seed(seed)
83
 
84
  images = pipe(
85
- prompt=prompt + "最高品質の輻の浮世絵。超詳細。",
86
- negative_prompt=negative_prompt,
87
  width=1024,
88
  height=1024,
89
  guidance_scale=8.0,
90
- num_inference_steps=40,
91
  generator=generator,
92
  num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
93
  output_type="pil",
@@ -102,9 +88,9 @@ def generate(
102
 
103
 
104
  examples = [
105
- "植物と花があります。蝶が飛んでいます。",
106
- "鶴が庭に立っています。雪が降っています。",
107
- "着物を着ている猫が庭でお茶を飲んでいます。",
108
  ]
109
 
110
  css = """
@@ -119,10 +105,9 @@ with gr.Blocks(css=css) as demo:
119
  submit = gr.Button(scale=0)
120
  result = gr.Image(label="Evo-Ukiyoeからの生成結果", type="pil", show_label=False)
121
  with gr.Accordion("詳細設定", open=False):
122
- negative_prompt = gr.Textbox(placeholder="日本語でネガティブプロンプトを入力してください。(空白可)", show_label=False)
123
  seed = gr.Slider(label="シード値", minimum=0, maximum=MAX_SEED, step=1, value=0)
124
  randomize_seed = gr.Checkbox(label="ランダムにシード値を決定", value=True)
125
- gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=generate)
126
  gr.on(
127
  triggers=[
128
  prompt.submit,
@@ -131,7 +116,6 @@ with gr.Blocks(css=css) as demo:
131
  fn=generate,
132
  inputs=[
133
  prompt,
134
- negative_prompt,
135
  seed,
136
  randomize_seed,
137
  ],
 
1
  import random
2
 
3
  from PIL import Image
 
4
  import gradio as gr
5
  import numpy as np
6
  import spaces
7
  import torch
8
 
 
 
 
 
9
  from evo_ukiyoe_v1 import load_evo_ukiyoe
10
 
11
 
12
  DESCRIPTION = """# 🐟 Evo-Ukiyoe
13
+ 🤗 [モデル一覧](https://huggingface.co/SakanaAI) | 📚 [技術レポート](https://arxiv.org/abs/2403.13187) | 📝 [ブログ](https://sakana.ai/evosdxl-jp/) | 🐦 [Twitter](https://twitter.com/SakanaAILabs)
14
 
15
  [Evo-Ukiyoe](https://huggingface.co/SakanaAI/Evo-Ukiyoe-v1)は[Sakana AI](https://sakana.ai/)が教育目的で開発した浮世絵に特化した画像生成モデルです。
16
  入力した日本語プロンプトに沿った浮世絵風の画像を生成することができます。より詳しくは、上記のブログをご参照ください。
 
46
  return images, has_nsfw_concepts
47
 
48
 
49
+ pipe = load_evo_ukiyoe("cpu").to(device)
 
 
 
 
 
 
 
 
 
50
 
51
 
52
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
 
59
  @torch.inference_mode()
60
  def generate(
61
  prompt: str,
 
62
  seed: int = 0,
63
  randomize_seed: bool = False,
64
  progress=gr.Progress(track_tqdm=True),
65
  ):
66
+ pipe.to(device)
67
  seed = int(randomize_seed_fn(seed, randomize_seed))
68
  generator = torch.Generator().manual_seed(seed)
69
 
70
  images = pipe(
71
+ prompt=prompt + "最高品質の輻の浮世絵。",
72
+ negative_prompt="",
73
  width=1024,
74
  height=1024,
75
  guidance_scale=8.0,
76
+ num_inference_steps=50,
77
  generator=generator,
78
  num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
79
  output_type="pil",
 
88
 
89
 
90
  examples = [
91
+ ["鶴が庭に立っている。雪が降っている。"],
92
+ ["富士山、桜の木、川と人々の風景。"],
93
+ ["熊が本を読んでいる。"],
94
  ]
95
 
96
  css = """
 
105
  submit = gr.Button(scale=0)
106
  result = gr.Image(label="Evo-Ukiyoeからの生成結果", type="pil", show_label=False)
107
  with gr.Accordion("詳細設定", open=False):
 
108
  seed = gr.Slider(label="シード値", minimum=0, maximum=MAX_SEED, step=1, value=0)
109
  randomize_seed = gr.Checkbox(label="ランダムにシード値を決定", value=True)
110
+ gr.Examples(examples=examples, inputs=prompt, outputs=[result, seed], fn=generate)
111
  gr.on(
112
  triggers=[
113
  prompt.submit,
 
116
  fn=generate,
117
  inputs=[
118
  prompt,
 
119
  seed,
120
  randomize_seed,
121
  ],
evo_ukiyoe_v1.py CHANGED
@@ -12,8 +12,7 @@ import torch
12
  from tqdm import tqdm
13
  from transformers import AutoTokenizer, CLIPTextModelWithProjection
14
 
15
-
16
- # Base models
17
  SDXL_REPO = "stabilityai/stable-diffusion-xl-base-1.0"
18
  DPO_REPO = "mhdang/dpo-sdxl-text2image-v1"
19
  JN_REPO = "RunDiffusion/Juggernaut-XL-v9"
@@ -117,7 +116,6 @@ def load_evo_ukiyoe(device="cuda") -> StableDiffusionXLPipeline:
117
  )
118
  jn_weights = split_conv_attn(load_from_pretrained(JN_REPO, device=device))
119
  jsdxl_weights = split_conv_attn(load_from_pretrained(JSDXL_REPO, device=device))
120
-
121
  # Merge base models
122
  tensors = [sdxl_weights, dpo_weights, jn_weights, jsdxl_weights]
123
  new_conv = merge_models(
@@ -138,14 +136,11 @@ def load_evo_ukiyoe(device="cuda") -> StableDiffusionXLPipeline:
138
  0.2198623756106564,
139
  ],
140
  )
141
-
142
- # Delete no longer needed variables to free
143
  del sdxl_weights, dpo_weights, jn_weights, jsdxl_weights
144
  gc.collect()
145
  if "cuda" in device:
146
  torch.cuda.empty_cache()
147
 
148
- # Instantiate UNet
149
  unet_config = UNet2DConditionModel.load_config(SDXL_REPO, subfolder="unet")
150
  unet = UNet2DConditionModel.from_config(unet_config).to(device=device)
151
  unet.load_state_dict({**new_conv, **new_attn})
@@ -167,23 +162,9 @@ def load_evo_ukiyoe(device="cuda") -> StableDiffusionXLPipeline:
167
  torch_dtype=torch.float16,
168
  variant="fp16",
169
  )
 
 
170
  # Load Evo-Ukiyoe weights
171
  pipe.load_lora_weights(UKIYOE_REPO)
172
  pipe.fuse_lora(lora_scale=1.0)
173
- pipe = pipe.to(device=torch.device(device), dtype=torch.float16)
174
-
175
  return pipe
176
-
177
-
178
- if __name__ == "__main__":
179
- pipe = load_evo_ukiyoe()
180
- images = pipe(
181
- prompt="着物を着ている猫が庭でお茶を飲んでいる。最高品質の輻の浮世絵。超詳細。",
182
- negative_prompt="",
183
- guidance_scale=8.0,
184
- num_inference_steps=50,
185
- generator=torch.Generator().manual_seed(0),
186
- num_images_per_prompt=1,
187
- output_type="pil",
188
- ).images
189
- images[0].save("out.png")
 
12
  from tqdm import tqdm
13
  from transformers import AutoTokenizer, CLIPTextModelWithProjection
14
 
15
+ # Base models (fine-tuned from SDXL-1.0)
 
16
  SDXL_REPO = "stabilityai/stable-diffusion-xl-base-1.0"
17
  DPO_REPO = "mhdang/dpo-sdxl-text2image-v1"
18
  JN_REPO = "RunDiffusion/Juggernaut-XL-v9"
 
116
  )
117
  jn_weights = split_conv_attn(load_from_pretrained(JN_REPO, device=device))
118
  jsdxl_weights = split_conv_attn(load_from_pretrained(JSDXL_REPO, device=device))
 
119
  # Merge base models
120
  tensors = [sdxl_weights, dpo_weights, jn_weights, jsdxl_weights]
121
  new_conv = merge_models(
 
136
  0.2198623756106564,
137
  ],
138
  )
 
 
139
  del sdxl_weights, dpo_weights, jn_weights, jsdxl_weights
140
  gc.collect()
141
  if "cuda" in device:
142
  torch.cuda.empty_cache()
143
 
 
144
  unet_config = UNet2DConditionModel.load_config(SDXL_REPO, subfolder="unet")
145
  unet = UNet2DConditionModel.from_config(unet_config).to(device=device)
146
  unet.load_state_dict({**new_conv, **new_attn})
 
162
  torch_dtype=torch.float16,
163
  variant="fp16",
164
  )
165
+ pipe = pipe.to(device, dtype=torch.float16)
166
+
167
  # Load Evo-Ukiyoe weights
168
  pipe.load_lora_weights(UKIYOE_REPO)
169
  pipe.fuse_lora(lora_scale=1.0)
 
 
170
  return pipe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,9 +1,10 @@
1
- torch==2.2.0
2
- torchvision==0.17.0
 
3
 
4
- accelerate==0.32.0
5
- diffusers==0.29.2
6
- gradio==4.38.1
 
7
  sentencepiece==0.2.0
8
- transformers==4.42.3
9
- peft==0.11.1
 
1
+ --extra-index-url https://download.pytorch.org/whl/cu121
2
+ torch==2.3.1+cu121
3
+ torchvision==0.18.1+cu121
4
 
5
+ accelerate==0.31.0
6
+ controlnet-aux==0.0.9
7
+ diffusers==0.26.0
8
+ gradio==4.37.2
9
  sentencepiece==0.2.0
10
+ transformers==4.42.3