Spaces:
Running
Running
controlnet
#12
by
pcuenq
HF staff
- opened
- app.py +1 -1
- convert.py +28 -13
app.py
CHANGED
@@ -24,7 +24,7 @@ demo = gr.Interface(
|
|
24 |
gr.Text(max_lines=1, label="your_hf_token"),
|
25 |
gr.Text(max_lines=1, label="model_id"),
|
26 |
gr.Text(max_lines=1, label="filename"),
|
27 |
-
gr.Radio(label="Model type", choices=["v1", "v2"]),
|
28 |
gr.Radio(label="Sample size (px)", choices=[512, 768]),
|
29 |
gr.Radio(label="Scheduler type", choices=["pndm", "heun", "euler", "dpm", "ddim"], value="dpm"),
|
30 |
gr.Radio(label="Extract EMA or non-EMA?", choices=["ema", "non-ema"], value="ema"),
|
|
|
24 |
gr.Text(max_lines=1, label="your_hf_token"),
|
25 |
gr.Text(max_lines=1, label="model_id"),
|
26 |
gr.Text(max_lines=1, label="filename"),
|
27 |
+
gr.Radio(label="Model type", choices=["v1", "v2", "ControlNet"]),
|
28 |
gr.Radio(label="Sample size (px)", choices=[512, 768]),
|
29 |
gr.Radio(label="Scheduler type", choices=["pndm", "heun", "euler", "dpm", "ddim"], value="dpm"),
|
30 |
gr.Radio(label="Extract EMA or non-EMA?", choices=["ema", "non-ema"], value="ema"),
|
convert.py
CHANGED
@@ -1,28 +1,29 @@
|
|
1 |
-
import
|
2 |
import requests
|
3 |
-
import json
|
4 |
import os
|
5 |
import shutil
|
6 |
-
from
|
7 |
-
from inspect import signature
|
8 |
from tempfile import TemporaryDirectory
|
9 |
-
from typing import
|
10 |
|
11 |
import torch
|
12 |
from io import BytesIO
|
13 |
|
14 |
from huggingface_hub import CommitInfo, Discussion, HfApi, hf_hub_download
|
15 |
from huggingface_hub.file_download import repo_folder_name
|
16 |
-
from diffusers.pipelines.stable_diffusion.convert_from_ckpt import
|
|
|
|
|
17 |
from transformers import CONFIG_MAPPING
|
18 |
|
19 |
|
20 |
-
COMMIT_MESSAGE = " This PR adds
|
21 |
|
22 |
|
23 |
-
def convert_single(model_id: str, filename: str, model_type: str, sample_size: int, scheduler_type: str, extract_ema: bool, folder: str):
|
24 |
from_safetensors = filename.endswith(".safetensors")
|
25 |
|
|
|
26 |
local_file = os.path.join(model_id, filename)
|
27 |
ckpt_file = local_file if os.path.isfile(local_file) else hf_hub_download(repo_id=model_id, filename=filename)
|
28 |
|
@@ -33,15 +34,25 @@ def convert_single(model_id: str, filename: str, model_type: str, sample_size: i
|
|
33 |
config_url = "https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/v2-inference.yaml"
|
34 |
else:
|
35 |
config_url = "https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/v2-inference-v.yaml"
|
|
|
|
|
|
|
36 |
|
37 |
config_file = BytesIO(requests.get(config_url).content)
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
pipeline.save_pretrained(folder)
|
42 |
pipeline.save_pretrained(folder, safe_serialization=True)
|
43 |
|
44 |
-
pipeline = pipeline.to(
|
45 |
pipeline.save_pretrained(folder, variant="fp16")
|
46 |
pipeline.save_pretrained(folder, safe_serialization=True, variant="fp16")
|
47 |
|
@@ -60,7 +71,7 @@ def previous_pr(api: "HfApi", model_id: str, pr_title: str) -> Optional["Discuss
|
|
60 |
return discussion
|
61 |
|
62 |
|
63 |
-
def convert(token: str, model_id: str, filename: str, model_type: str, sample_size: int = 512, scheduler_type: str = "pndm", extract_ema: bool = True):
|
64 |
api = HfApi()
|
65 |
|
66 |
pr_title = "Adding `diffusers` weights of this model"
|
@@ -70,10 +81,14 @@ def convert(token: str, model_id: str, filename: str, model_type: str, sample_si
|
|
70 |
os.makedirs(folder)
|
71 |
new_pr = None
|
72 |
try:
|
73 |
-
folder = convert_single(model_id, filename, model_type, sample_size, scheduler_type, extract_ema, folder)
|
74 |
-
|
|
|
75 |
pr_number = new_pr.split("%2F")[-1].split("/")[0]
|
76 |
link = f"Pr created at: {'https://huggingface.co/' + os.path.join(model_id, 'discussions', pr_number)}"
|
|
|
|
|
|
|
77 |
finally:
|
78 |
shutil.rmtree(folder)
|
79 |
|
|
|
1 |
+
import gradio as gr
|
2 |
import requests
|
|
|
3 |
import os
|
4 |
import shutil
|
5 |
+
from pathlib import Path
|
|
|
6 |
from tempfile import TemporaryDirectory
|
7 |
+
from typing import Optional
|
8 |
|
9 |
import torch
|
10 |
from io import BytesIO
|
11 |
|
12 |
from huggingface_hub import CommitInfo, Discussion, HfApi, hf_hub_download
|
13 |
from huggingface_hub.file_download import repo_folder_name
|
14 |
+
from diffusers.pipelines.stable_diffusion.convert_from_ckpt import (
|
15 |
+
download_from_original_stable_diffusion_ckpt, download_controlnet_from_original_ckpt
|
16 |
+
)
|
17 |
from transformers import CONFIG_MAPPING
|
18 |
|
19 |
|
20 |
+
COMMIT_MESSAGE = " This PR adds fp32 and fp16 weights in PyTorch and safetensors format to {}"
|
21 |
|
22 |
|
23 |
+
def convert_single(model_id: str, filename: str, model_type: str, sample_size: int, scheduler_type: str, extract_ema: bool, folder: str, progress):
|
24 |
from_safetensors = filename.endswith(".safetensors")
|
25 |
|
26 |
+
progress(0, desc="Downloading model")
|
27 |
local_file = os.path.join(model_id, filename)
|
28 |
ckpt_file = local_file if os.path.isfile(local_file) else hf_hub_download(repo_id=model_id, filename=filename)
|
29 |
|
|
|
34 |
config_url = "https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/v2-inference.yaml"
|
35 |
else:
|
36 |
config_url = "https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/v2-inference-v.yaml"
|
37 |
+
elif model_type == "ControlNet":
|
38 |
+
config_url = (Path(model_id)/"resolve/main"/filename).with_suffix(".yaml")
|
39 |
+
config_url = "https://huggingface.co/" + str(config_url)
|
40 |
|
41 |
config_file = BytesIO(requests.get(config_url).content)
|
42 |
|
43 |
+
if model_type == "ControlNet":
|
44 |
+
progress(0.2, desc="Converting ControlNet Model")
|
45 |
+
pipeline = download_controlnet_from_original_ckpt(ckpt_file, config_file, image_size=sample_size, from_safetensors=from_safetensors, extract_ema=extract_ema)
|
46 |
+
to_args = {"dtype": torch.float16}
|
47 |
+
else:
|
48 |
+
progress(0.1, desc="Converting Model")
|
49 |
+
pipeline = download_from_original_stable_diffusion_ckpt(ckpt_file, config_file, image_size=sample_size, scheduler_type=scheduler_type, from_safetensors=from_safetensors, extract_ema=extract_ema)
|
50 |
+
to_args = {"torch_dtype": torch.float16}
|
51 |
|
52 |
pipeline.save_pretrained(folder)
|
53 |
pipeline.save_pretrained(folder, safe_serialization=True)
|
54 |
|
55 |
+
pipeline = pipeline.to(**to_args)
|
56 |
pipeline.save_pretrained(folder, variant="fp16")
|
57 |
pipeline.save_pretrained(folder, safe_serialization=True, variant="fp16")
|
58 |
|
|
|
71 |
return discussion
|
72 |
|
73 |
|
74 |
+
def convert(token: str, model_id: str, filename: str, model_type: str, sample_size: int = 512, scheduler_type: str = "pndm", extract_ema: bool = True, progress=gr.Progress()):
|
75 |
api = HfApi()
|
76 |
|
77 |
pr_title = "Adding `diffusers` weights of this model"
|
|
|
81 |
os.makedirs(folder)
|
82 |
new_pr = None
|
83 |
try:
|
84 |
+
folder = convert_single(model_id, filename, model_type, sample_size, scheduler_type, extract_ema, folder, progress)
|
85 |
+
progress(0.7, desc="Uploading to Hub")
|
86 |
+
new_pr = api.upload_folder(folder_path=folder, path_in_repo="./", repo_id=model_id, repo_type="model", token=token, commit_message=pr_title, commit_description=COMMIT_MESSAGE.format(model_id), create_pr=True)
|
87 |
pr_number = new_pr.split("%2F")[-1].split("/")[0]
|
88 |
link = f"Pr created at: {'https://huggingface.co/' + os.path.join(model_id, 'discussions', pr_number)}"
|
89 |
+
progress(1, desc="Done")
|
90 |
+
except Exception as e:
|
91 |
+
raise gr.exceptions.Error(str(e))
|
92 |
finally:
|
93 |
shutil.rmtree(folder)
|
94 |
|