patrickvonplaten commited on
Commit
876d6e2
1 Parent(s): 9a6c912
README.md CHANGED
@@ -1,14 +1,13 @@
1
  ---
2
- title: SD To Diffusers
3
  emoji: 🎨➡️🧨
4
  colorFrom: indigo
5
- colorTo: blue
6
  sdk: gradio
7
  sdk_version: 3.31.0
8
  app_file: app.py
9
  pinned: true
10
  license: mit
11
- duplicated_from: diffusers/sd-to-diffusers
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: SD-XL To Diffusers
3
  emoji: 🎨➡️🧨
4
  colorFrom: indigo
5
+ colorTo: purple
6
  sdk: gradio
7
  sdk_version: 3.31.0
8
  app_file: app.py
9
  pinned: true
10
  license: mit
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
__pycache__/app.cpython-310.pyc ADDED
Binary file (1.31 kB). View file
 
__pycache__/convert.cpython-310.pyc ADDED
Binary file (2.75 kB). View file
 
app.py CHANGED
@@ -7,8 +7,7 @@ The steps are the following:
7
 
8
  - Paste a read-access token from hf.co/settings/tokens. Read access is enough given that we will open a PR against the source repo.
9
  - Input a model id from the Hub
10
- - Input the filename from the root dir of the repo that you would like to convert, e.g. 'v2-1_768-ema-pruned.ckpt' or 'v1-5-pruned.safetensors'
11
- - Chose which Stable Diffusion version, image size, scheduler type the model has and whether you want the "ema", or "non-ema" weights.
12
  - Click "Submit"
13
  - That's it! You'll get feedback if it works or not, and if it worked, you'll get the URL of the opened PR 🔥
14
 
@@ -16,7 +15,7 @@ The steps are the following:
16
  """
17
 
18
  demo = gr.Interface(
19
- title="Convert any Stable Diffusion checkpoint to Diffusers and open a PR",
20
  description=DESCRIPTION,
21
  allow_flagging="never",
22
  article="Check out the [Diffusers repo on GitHub](https://github.com/huggingface/diffusers)",
@@ -24,10 +23,6 @@ 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", "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"),
31
  ],
32
  outputs=[gr.Markdown(label="output")],
33
  fn=convert,
 
7
 
8
  - Paste a read-access token from hf.co/settings/tokens. Read access is enough given that we will open a PR against the source repo.
9
  - Input a model id from the Hub
10
+ - Input the filename from the root dir of the repo that you would like to convert, e.g. 'xl-pruned.safetensors'
 
11
  - Click "Submit"
12
  - That's it! You'll get feedback if it works or not, and if it worked, you'll get the URL of the opened PR 🔥
13
 
 
15
  """
16
 
17
  demo = gr.Interface(
18
+ title="Convert any Stable Diffusion XL checkpoint to Diffusers and open a PR",
19
  description=DESCRIPTION,
20
  allow_flagging="never",
21
  article="Check out the [Diffusers repo on GitHub](https://github.com/huggingface/diffusers)",
 
23
  gr.Text(max_lines=1, label="your_hf_token"),
24
  gr.Text(max_lines=1, label="model_id"),
25
  gr.Text(max_lines=1, label="filename"),
 
 
 
 
26
  ],
27
  outputs=[gr.Markdown(label="output")],
28
  fn=convert,
convert.py CHANGED
@@ -3,6 +3,7 @@ import requests
3
  import os
4
  import shutil
5
  from pathlib import Path
 
6
  from tempfile import TemporaryDirectory
7
  from typing import Optional
8
 
@@ -11,49 +12,22 @@ 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
 
30
- if model_type == "v1":
31
- config_url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml"
32
- elif model_type == "v2":
33
- if sample_size == 512:
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
 
59
  return folder
@@ -71,7 +45,7 @@ def previous_pr(api: "HfApi", model_id: str, pr_title: str) -> Optional["Discuss
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,7 +55,7 @@ def convert(token: str, model_id: str, filename: str, model_type: str, sample_si
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]
 
3
  import os
4
  import shutil
5
  from pathlib import Path
6
+ from typing import Any
7
  from tempfile import TemporaryDirectory
8
  from typing import Optional
9
 
 
12
 
13
  from huggingface_hub import CommitInfo, Discussion, HfApi, hf_hub_download
14
  from huggingface_hub.file_download import repo_folder_name
15
+ from diffusers import StableDiffusionXLPipeline
 
 
16
  from transformers import CONFIG_MAPPING
17
 
18
 
19
+ COMMIT_MESSAGE = " This PR adds fp32 and fp16 weights in safetensors format to {}"
20
 
21
 
22
+ def convert_single(model_id: str, filename: str, folder: str, progress: Any):
 
 
23
  progress(0, desc="Downloading model")
24
  local_file = os.path.join(model_id, filename)
25
  ckpt_file = local_file if os.path.isfile(local_file) else hf_hub_download(repo_id=model_id, filename=filename)
26
 
27
+ pipeline = StableDiffusionXLPipeline.from_single_file(ckpt_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
+ pipeline.save_pretrained(folder, safe_serialization=True)
30
+ pipeline = pipeline.to(torch_dtype=torch.float16)
31
  pipeline.save_pretrained(folder, safe_serialization=True, variant="fp16")
32
 
33
  return folder
 
45
  return discussion
46
 
47
 
48
+ def convert(token: str, model_id: str, filename: str, progress=gr.Progress()):
49
  api = HfApi()
50
 
51
  pr_title = "Adding `diffusers` weights of this model"
 
55
  os.makedirs(folder)
56
  new_pr = None
57
  try:
58
+ folder = convert_single(model_id, filename, folder, progress)
59
  progress(0.7, desc="Uploading to Hub")
60
  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)
61
  pr_number = new_pr.split("%2F")[-1].split("/")[0]