idlebg patrickvonplaten commited on
Commit
5440daa
0 Parent(s):

Duplicate from diffusers/sdxl-to-diffusers

Browse files

Co-authored-by: Patrick von Platen <[email protected]>

.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ duplicated_from: diffusers/sdxl-to-diffusers
12
+ ---
13
+
14
+ 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 ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from convert import convert
4
+
5
+ DESCRIPTION = """
6
+ 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. '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
+
14
+ ⚠️ If you encounter weird error messages, please have a look into the Logs and feel free to open a PR to correct the error messages.
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)",
22
+ inputs=[
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,
29
+ ).queue(max_size=10, concurrency_count=1)
30
+
31
+ demo.launch(show_api=True)
convert.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
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
+
10
+ import torch
11
+ from io import BytesIO
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
34
+
35
+
36
+ def previous_pr(api: "HfApi", model_id: str, pr_title: str) -> Optional["Discussion"]:
37
+ try:
38
+ discussions = api.get_repo_discussions(repo_id=model_id)
39
+ except Exception:
40
+ return None
41
+ for discussion in discussions:
42
+ if discussion.status == "open" and discussion.is_pull_request and discussion.title == pr_title:
43
+ details = api.get_discussion_details(repo_id=model_id, discussion_num=discussion.num)
44
+ if details.target_branch == "refs/heads/main":
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"
52
+
53
+ with TemporaryDirectory() as d:
54
+ folder = os.path.join(d, repo_folder_name(repo_id=model_id, repo_type="models"))
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]
62
+ link = f"Pr created at: {'https://huggingface.co/' + os.path.join(model_id, 'discussions', pr_number)}"
63
+ progress(1, desc="Done")
64
+ except Exception as e:
65
+ raise gr.exceptions.Error(str(e))
66
+ finally:
67
+ shutil.rmtree(folder)
68
+
69
+ return link
hf_utils.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import get_hf_file_metadata, hf_hub_url, hf_hub_download, scan_cache_dir, whoami, list_models
2
+
3
+
4
+ def get_my_model_names(token):
5
+
6
+ try:
7
+ author = whoami(token=token)
8
+ model_infos = list_models(author=author["name"], use_auth_token=token)
9
+ return [model.modelId for model in model_infos], None
10
+
11
+ except Exception as e:
12
+ return [], e
13
+
14
+ def download_file(repo_id: str, filename: str, token: str):
15
+ """Download a file from a repo on the Hugging Face Hub.
16
+
17
+ Returns:
18
+ file_path (:obj:`str`): The path to the downloaded file.
19
+ revision (:obj:`str`): The commit hash of the file.
20
+ """
21
+
22
+ md = get_hf_file_metadata(hf_hub_url(repo_id=repo_id, filename=filename), token=token)
23
+ revision = md.commit_hash
24
+
25
+ file_path = hf_hub_download(repo_id=repo_id, filename=filename, revision=revision, token=token)
26
+
27
+ return file_path, revision
28
+
29
+ def delete_file(revision: str):
30
+ """Delete a file from local cache.
31
+
32
+ Args:
33
+ revision (:obj:`str`): The commit hash of the file.
34
+ Returns:
35
+ None
36
+ """
37
+ scan_cache_dir().delete_revisions(revision).execute()
38
+
39
+ def get_pr_url(api, repo_id, title):
40
+ try:
41
+ discussions = api.get_repo_discussions(repo_id=repo_id)
42
+ except Exception:
43
+ return None
44
+ for discussion in discussions:
45
+ if (
46
+ discussion.status == "open"
47
+ and discussion.is_pull_request
48
+ and discussion.title == title
49
+ ):
50
+ return f"https://huggingface.co/{repo_id}/discussions/{discussion.num}"
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ huggingface_hub
2
+ safetensors
3
+ transformers
4
+ accelerate
5
+ git+https://github.com/huggingface/diffusers
6
+ omegaconf
7
+ pytorch_lightning
utils.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ def is_google_colab():
2
+ try:
3
+ import google.colab
4
+ return True
5
+ except:
6
+ return False