Spaces:
Running
on
Zero
Running
on
Zero
Create evo_nishikie_v1.py, app.py, safety_checker.py and requirements.txt
#1
by
yuki-imajuku
- opened
- .gitattributes +0 -4
- README.md +4 -4
- app.py +30 -56
- evo_nishikie_v1.py +7 -10
- requirements.txt +6 -7
- safety_checker.py +1 -1
- sample1.jpg +0 -3
- sample2.jpg +0 -3
- sample3.jpg +0 -3
- sample4.jpg +0 -3
.gitattributes
CHANGED
@@ -33,7 +33,3 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
-
sample1.jpg filter=lfs diff=lfs merge=lfs -text
|
37 |
-
sample2.jpg filter=lfs diff=lfs merge=lfs -text
|
38 |
-
sample3.jpg filter=lfs diff=lfs merge=lfs -text
|
39 |
-
sample4.jpg filter=lfs diff=lfs merge=lfs -text
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1 |
---
|
2 |
-
title: Evo
|
3 |
-
emoji:
|
4 |
colorFrom: red
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
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 Nishikie
|
3 |
+
emoji: 🌍
|
4 |
colorFrom: red
|
5 |
colorTo: indigo
|
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,25 +1,20 @@
|
|
1 |
import random
|
2 |
|
3 |
-
from PIL import Image
|
4 |
-
from controlnet_aux import
|
5 |
-
from diffusers import EulerDiscreteScheduler
|
6 |
import gradio as gr
|
7 |
import numpy as np
|
8 |
import spaces
|
9 |
import torch
|
10 |
|
11 |
-
# torch._inductor.config.conv_1x1_as_mm = True
|
12 |
-
# torch._inductor.config.coordinate_descent_tuning = True
|
13 |
-
# torch._inductor.config.epilogue_fusion = False
|
14 |
-
# torch._inductor.config.coordinate_descent_check_all_directions = True
|
15 |
from evo_nishikie_v1 import load_evo_nishikie
|
16 |
|
17 |
|
18 |
DESCRIPTION = """# 🐟 Evo-Nishikie
|
19 |
-
🤗 [モデル一覧](https://huggingface.co/SakanaAI) | 📝 [ブログ](https://sakana.ai/
|
20 |
|
21 |
[Evo-Nishikie](https://huggingface.co/SakanaAI/Evo-Nishikie-v1)は[Sakana AI](https://sakana.ai/)が教育目的で開発した浮世絵に特化した画像生成モデルです。
|
22 |
-
|
23 |
"""
|
24 |
if not torch.cuda.is_available():
|
25 |
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
|
@@ -52,22 +47,8 @@ if SAFETY_CHECKER:
|
|
52 |
return images, has_nsfw_concepts
|
53 |
|
54 |
|
55 |
-
pipe = load_evo_nishikie(device)
|
56 |
-
|
57 |
-
pipe.scheduler.config, use_karras_sigmas=True,
|
58 |
-
)
|
59 |
-
pipe.to(device=device, dtype=torch.float16)
|
60 |
-
# pipe.unet.to(memory_format=torch.channels_last)
|
61 |
-
# pipe.controlnet.to(memory_format=torch.channels_last)
|
62 |
-
# pipe.vae.to(memory_format=torch.channels_last)
|
63 |
-
# # Compile the UNet, ControlNet and VAE.
|
64 |
-
# pipe.unet = torch.compile(pipe.unet, mode="max-autotune", fullgraph=True)
|
65 |
-
# pipe.controlnet = torch.compile(pipe.controlnet, mode="max-autotune", fullgraph=True)
|
66 |
-
# pipe.vae.decode = torch.compile(pipe.vae.decode, mode="max-autotune", fullgraph=True)
|
67 |
-
|
68 |
-
lineart_detector = LineartDetector.from_pretrained("lllyasviel/Annotators")
|
69 |
-
image_filter = ImageFilter.MedianFilter(size=3)
|
70 |
-
BINARY_THRESHOLD = 40
|
71 |
|
72 |
|
73 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
@@ -79,26 +60,24 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
79 |
@spaces.GPU
|
80 |
@torch.inference_mode()
|
81 |
def generate(
|
82 |
-
input_image: Image.Image,
|
83 |
prompt: str,
|
|
|
84 |
seed: int = 0,
|
85 |
randomize_seed: bool = False,
|
86 |
progress=gr.Progress(track_tqdm=True),
|
87 |
):
|
88 |
-
|
89 |
-
|
90 |
-
conditioning_image = lineart_image_filtered.point(lambda p: 255 if p > BINARY_THRESHOLD else 0).convert("L")
|
91 |
-
|
92 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
93 |
generator = torch.Generator().manual_seed(seed)
|
94 |
|
95 |
images = pipe(
|
96 |
-
prompt=prompt + "
|
97 |
-
negative_prompt="
|
98 |
-
image=
|
99 |
-
guidance_scale=
|
100 |
-
controlnet_conditioning_scale=0.
|
101 |
-
num_inference_steps=
|
102 |
generator=generator,
|
103 |
num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
|
104 |
output_type="pil",
|
@@ -113,37 +92,34 @@ def generate(
|
|
113 |
|
114 |
|
115 |
examples = [
|
116 |
-
["
|
117 |
-
["./sample2.jpg", "着物を着た女性が、赤ん坊を抱え、もう一人の子どもが手押し車を引いています。背景には木があります。"],
|
118 |
-
["./sample3.jpg", "女性が花柄の着物を着ており、他の人物たちが座りながら会話しています。背景には家の内部があります。"],
|
119 |
-
["./sample4.jpg", "花柄や模様入りの着物を着た男女が室内で集まり、煎茶の準備をしています。背景に木材の装飾があります。"],
|
120 |
]
|
121 |
|
122 |
css = """
|
123 |
-
.gradio-container{max-width:
|
124 |
h1{text-align:center}
|
125 |
"""
|
126 |
with gr.Blocks(css=css) as demo:
|
127 |
gr.Markdown(DESCRIPTION)
|
128 |
-
with gr.
|
129 |
-
with gr.
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
gr.Examples(examples=examples, inputs=[
|
139 |
gr.on(
|
140 |
triggers=[
|
141 |
submit.click,
|
142 |
],
|
143 |
fn=generate,
|
144 |
inputs=[
|
145 |
-
input_image,
|
146 |
prompt,
|
|
|
147 |
seed,
|
148 |
randomize_seed,
|
149 |
],
|
@@ -154,8 +130,6 @@ with gr.Blocks(css=css) as demo:
|
|
154 |
本モデルの使用は、利用者の自己責任で行われ、その性能や結果については何ら保証されません。
|
155 |
Sakana AIは、本モデルの使用によって生じた直接的または間接的な損失に対して、結果に関わらず、一切の責任を負いません。
|
156 |
利用者は、本モデルの使用に伴うリスクを十分に理解し、自身の判断で使用することが必要です。
|
157 |
-
アップロードされた画像は画像生成のみに使用され、サーバー上に保存されることはありません。
|
158 |
-
|
159 |
-
出典:サンプル画像はすべて[日本古典籍データセット(国文学研究資料館蔵)『絵本玉かつら』](http://codh.rois.ac.jp/pmjt/book/200013861/)から引用しました。""")
|
160 |
|
161 |
demo.queue().launch()
|
|
|
1 |
import random
|
2 |
|
3 |
+
from PIL import Image
|
4 |
+
from controlnet_aux import CannyDetector
|
|
|
5 |
import gradio as gr
|
6 |
import numpy as np
|
7 |
import spaces
|
8 |
import torch
|
9 |
|
|
|
|
|
|
|
|
|
10 |
from evo_nishikie_v1 import load_evo_nishikie
|
11 |
|
12 |
|
13 |
DESCRIPTION = """# 🐟 Evo-Nishikie
|
14 |
+
🤗 [モデル一覧](https://huggingface.co/SakanaAI) | 📚 [技術レポート](https://arxiv.org/abs/2403.13187) | 📝 [ブログ](https://sakana.ai/evosdxl-jp/) | 🐦 [Twitter](https://twitter.com/SakanaAILabs)
|
15 |
|
16 |
[Evo-Nishikie](https://huggingface.co/SakanaAI/Evo-Nishikie-v1)は[Sakana AI](https://sakana.ai/)が教育目的で開発した浮世絵に特化した画像生成モデルです。
|
17 |
+
入力した画像を日本語プロンプトに沿って浮世絵風に変換した画像を生成することができます。より詳しくは、上記のブログをご参照ください。
|
18 |
"""
|
19 |
if not torch.cuda.is_available():
|
20 |
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
|
|
|
47 |
return images, has_nsfw_concepts
|
48 |
|
49 |
|
50 |
+
pipe = load_evo_nishikie("cpu").to(device)
|
51 |
+
canny_detector = CannyDetector()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
|
54 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
|
60 |
@spaces.GPU
|
61 |
@torch.inference_mode()
|
62 |
def generate(
|
|
|
63 |
prompt: str,
|
64 |
+
input_image: Image.Image,
|
65 |
seed: int = 0,
|
66 |
randomize_seed: bool = False,
|
67 |
progress=gr.Progress(track_tqdm=True),
|
68 |
):
|
69 |
+
pipe.to(device)
|
70 |
+
canny_image = canny_detector(input_image, image_resolution=1024)
|
|
|
|
|
71 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
72 |
generator = torch.Generator().manual_seed(seed)
|
73 |
|
74 |
images = pipe(
|
75 |
+
prompt=prompt + "最高品質の輻の浮世絵。",
|
76 |
+
negative_prompt="暗い。",
|
77 |
+
image=canny_image,
|
78 |
+
guidance_scale=8.0,
|
79 |
+
controlnet_conditioning_scale=0.6,
|
80 |
+
num_inference_steps=50,
|
81 |
generator=generator,
|
82 |
num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
|
83 |
output_type="pil",
|
|
|
92 |
|
93 |
|
94 |
examples = [
|
95 |
+
["銀杏が色づく。草木が生えた地面と青空の富士山。", "https://sakana.ai/assets/nedo-grant/nedo_grant.jpeg"],
|
|
|
|
|
|
|
96 |
]
|
97 |
|
98 |
css = """
|
99 |
+
.gradio-container{max-width: 690px !important}
|
100 |
h1{text-align:center}
|
101 |
"""
|
102 |
with gr.Blocks(css=css) as demo:
|
103 |
gr.Markdown(DESCRIPTION)
|
104 |
+
with gr.Group():
|
105 |
+
with gr.Row():
|
106 |
+
with gr.Column(scale=8.0):
|
107 |
+
prompt = gr.Textbox(placeholder="日本語でプロンプトを入力してください。", show_label=False)
|
108 |
+
input_image = gr.Image(image_mode="RGB", type="pil", show_label=False)
|
109 |
+
submit = gr.Button(scale=0)
|
110 |
+
result = gr.Image(label="Evo-Nishikieからの生成結果", type="pil", show_label=False)
|
111 |
+
with gr.Accordion("詳細設定", open=False):
|
112 |
+
seed = gr.Slider(label="シード値", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
113 |
+
randomize_seed = gr.Checkbox(label="ランダムにシード値を決定", value=True)
|
114 |
+
gr.Examples(examples=examples, inputs=[prompt, input_image], outputs=[result, seed], fn=generate)
|
115 |
gr.on(
|
116 |
triggers=[
|
117 |
submit.click,
|
118 |
],
|
119 |
fn=generate,
|
120 |
inputs=[
|
|
|
121 |
prompt,
|
122 |
+
input_image,
|
123 |
seed,
|
124 |
randomize_seed,
|
125 |
],
|
|
|
130 |
本モデルの使用は、利用者の自己責任で行われ、その性能や結果については何ら保証されません。
|
131 |
Sakana AIは、本モデルの使用によって生じた直接的または間接的な損失に対して、結果に関わらず、一切の責任を負いません。
|
132 |
利用者は、本モデルの使用に伴うリスクを十分に理解し、自身の判断で使用することが必要です。
|
133 |
+
アップロードされた画像は画像生成のみに使用され、サーバー上に保存されることはありません。""")
|
|
|
|
|
134 |
|
135 |
demo.queue().launch()
|
evo_nishikie_v1.py
CHANGED
@@ -1,20 +1,23 @@
|
|
1 |
import gc
|
|
|
2 |
import os
|
3 |
from typing import Dict, List, Union
|
4 |
|
|
|
|
|
5 |
from diffusers import (
|
6 |
ControlNetModel,
|
7 |
StableDiffusionXLControlNetPipeline,
|
8 |
UNet2DConditionModel,
|
9 |
)
|
10 |
from huggingface_hub import hf_hub_download
|
|
|
11 |
import safetensors
|
12 |
import torch
|
13 |
from tqdm import tqdm
|
14 |
from transformers import AutoTokenizer, CLIPTextModelWithProjection
|
15 |
|
16 |
-
|
17 |
-
# Base models
|
18 |
SDXL_REPO = "stabilityai/stable-diffusion-xl-base-1.0"
|
19 |
DPO_REPO = "mhdang/dpo-sdxl-text2image-v1"
|
20 |
JN_REPO = "RunDiffusion/Juggernaut-XL-v9"
|
@@ -121,7 +124,6 @@ def load_evo_nishikie(device="cuda") -> StableDiffusionXLControlNetPipeline:
|
|
121 |
)
|
122 |
jn_weights = split_conv_attn(load_from_pretrained(JN_REPO, device=device))
|
123 |
jsdxl_weights = split_conv_attn(load_from_pretrained(JSDXL_REPO, device=device))
|
124 |
-
|
125 |
# Merge base models
|
126 |
tensors = [sdxl_weights, dpo_weights, jn_weights, jsdxl_weights]
|
127 |
new_conv = merge_models(
|
@@ -142,14 +144,11 @@ def load_evo_nishikie(device="cuda") -> StableDiffusionXLControlNetPipeline:
|
|
142 |
0.2198623756106564,
|
143 |
],
|
144 |
)
|
145 |
-
|
146 |
-
# Delete no longer needed variables to free
|
147 |
del sdxl_weights, dpo_weights, jn_weights, jsdxl_weights
|
148 |
gc.collect()
|
149 |
if "cuda" in device:
|
150 |
torch.cuda.empty_cache()
|
151 |
|
152 |
-
# Instantiate UNet
|
153 |
unet_config = UNet2DConditionModel.load_config(SDXL_REPO, subfolder="unet")
|
154 |
unet = UNet2DConditionModel.from_config(unet_config).to(device=device)
|
155 |
unet.load_state_dict({**new_conv, **new_attn})
|
@@ -177,11 +176,9 @@ def load_evo_nishikie(device="cuda") -> StableDiffusionXLControlNetPipeline:
|
|
177 |
torch_dtype=torch.float16,
|
178 |
variant="fp16",
|
179 |
)
|
|
|
180 |
|
181 |
# Load Evo-Ukiyoe weights
|
182 |
pipe.load_lora_weights(UKIYOE_REPO)
|
183 |
pipe.fuse_lora(lora_scale=1.0)
|
184 |
-
|
185 |
-
pipe = pipe.to(device, dtype=torch.float16)
|
186 |
-
|
187 |
-
return pipe
|
|
|
1 |
import gc
|
2 |
+
from io import BytesIO
|
3 |
import os
|
4 |
from typing import Dict, List, Union
|
5 |
|
6 |
+
from PIL import Image
|
7 |
+
from controlnet_aux import CannyDetector
|
8 |
from diffusers import (
|
9 |
ControlNetModel,
|
10 |
StableDiffusionXLControlNetPipeline,
|
11 |
UNet2DConditionModel,
|
12 |
)
|
13 |
from huggingface_hub import hf_hub_download
|
14 |
+
import requests
|
15 |
import safetensors
|
16 |
import torch
|
17 |
from tqdm import tqdm
|
18 |
from transformers import AutoTokenizer, CLIPTextModelWithProjection
|
19 |
|
20 |
+
# Base models (fine-tuned from SDXL-1.0)
|
|
|
21 |
SDXL_REPO = "stabilityai/stable-diffusion-xl-base-1.0"
|
22 |
DPO_REPO = "mhdang/dpo-sdxl-text2image-v1"
|
23 |
JN_REPO = "RunDiffusion/Juggernaut-XL-v9"
|
|
|
124 |
)
|
125 |
jn_weights = split_conv_attn(load_from_pretrained(JN_REPO, device=device))
|
126 |
jsdxl_weights = split_conv_attn(load_from_pretrained(JSDXL_REPO, device=device))
|
|
|
127 |
# Merge base models
|
128 |
tensors = [sdxl_weights, dpo_weights, jn_weights, jsdxl_weights]
|
129 |
new_conv = merge_models(
|
|
|
144 |
0.2198623756106564,
|
145 |
],
|
146 |
)
|
|
|
|
|
147 |
del sdxl_weights, dpo_weights, jn_weights, jsdxl_weights
|
148 |
gc.collect()
|
149 |
if "cuda" in device:
|
150 |
torch.cuda.empty_cache()
|
151 |
|
|
|
152 |
unet_config = UNet2DConditionModel.load_config(SDXL_REPO, subfolder="unet")
|
153 |
unet = UNet2DConditionModel.from_config(unet_config).to(device=device)
|
154 |
unet.load_state_dict({**new_conv, **new_attn})
|
|
|
176 |
torch_dtype=torch.float16,
|
177 |
variant="fp16",
|
178 |
)
|
179 |
+
pipe = pipe.to(device, dtype=torch.float16)
|
180 |
|
181 |
# Load Evo-Ukiyoe weights
|
182 |
pipe.load_lora_weights(UKIYOE_REPO)
|
183 |
pipe.fuse_lora(lora_scale=1.0)
|
184 |
+
return pipe
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
-
|
2 |
-
|
|
|
3 |
|
4 |
-
accelerate==0.
|
5 |
-
|
6 |
-
|
7 |
-
gradio==4.38.1
|
8 |
sentencepiece==0.2.0
|
9 |
transformers==4.42.3
|
10 |
-
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 |
+
diffusers==0.26.0
|
7 |
+
gradio==4.37.2
|
|
|
8 |
sentencepiece==0.2.0
|
9 |
transformers==4.42.3
|
|
safety_checker.py
CHANGED
@@ -134,4 +134,4 @@ class StableDiffusionSafetyChecker(PreTrainedModel):
|
|
134 |
|
135 |
images[has_nsfw_concepts] = 0.0 # black image
|
136 |
|
137 |
-
return images, has_nsfw_concepts
|
|
|
134 |
|
135 |
images[has_nsfw_concepts] = 0.0 # black image
|
136 |
|
137 |
+
return images, has_nsfw_concepts
|
sample1.jpg
DELETED
Git LFS Details
|
sample2.jpg
DELETED
Git LFS Details
|
sample3.jpg
DELETED
Git LFS Details
|
sample4.jpg
DELETED
Git LFS Details
|