Spaces:
Sleeping
Sleeping
Commit
·
f4242db
verified
·
0
Parent(s):
Super-squash branch 'main' using huggingface_hub
Browse filesCo-authored-by: sayakpaul <[email protected]>
Co-authored-by: idlebg <[email protected]>
Co-authored-by: patrickvonplaten <[email protected]>
- .gitattributes +34 -0
- README.md +13 -0
- __pycache__/app.cpython-310.pyc +0 -0
- __pycache__/convert.cpython-310.pyc +0 -0
- app.py +40 -0
- convert.py +70 -0
- hf_utils.py +50 -0
- requirements.txt +7 -0
- utils.py +6 -0
.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,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: SD-XL To Diffusers (fp16)
|
3 |
+
emoji: 🎨➡️🧨
|
4 |
+
colorFrom: indigo
|
5 |
+
colorTo: purple
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 4.36.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
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
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from convert import convert
|
4 |
+
|
5 |
+
DESCRIPTION = """
|
6 |
+
|
7 |
+
[Here is the full version of the converter by Huggingface official.](https://huggingface.co/spaces/diffusers/sdxl-to-diffusers)
|
8 |
+
|
9 |
+
The steps are the following:
|
10 |
+
|
11 |
+
- 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.
|
12 |
+
- Input a model id from the Hub
|
13 |
+
- Input the filename from the root dir of the repo that you would like to convert, e.g. 'xl-pruned.safetensors'
|
14 |
+
- Click "Submit"
|
15 |
+
- 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 🔥
|
16 |
+
|
17 |
+
⚠️ 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.
|
18 |
+
|
19 |
+
⚠️⚠️ Note that if you want your model to work with Huggingface's Inference API, you will need to create a model card (README.md) after the conversion.
|
20 |
+
|
21 |
+
If you have a good PC, you can use the following script in your local environment. [kiyohken2000 /
|
22 |
+
convert-safetensors-to-diffusers](https://github.com/kiyohken2000/convert-safetensors-to-diffusers)
|
23 |
+
|
24 |
+
"""
|
25 |
+
|
26 |
+
demo = gr.Interface(
|
27 |
+
title="Convert any Stable Diffusion XL checkpoint to Diffusers and open a PR",
|
28 |
+
description=DESCRIPTION,
|
29 |
+
allow_flagging="never",
|
30 |
+
article="Check out the [Diffusers repo on GitHub](https://github.com/huggingface/diffusers)",
|
31 |
+
inputs=[
|
32 |
+
gr.Text(max_lines=1, label="your_hf_token"),
|
33 |
+
gr.Text(max_lines=1, label="model_id"),
|
34 |
+
gr.Text(max_lines=1, label="filename"),
|
35 |
+
],
|
36 |
+
outputs=[gr.Markdown(label="output")],
|
37 |
+
fn=convert,
|
38 |
+
).queue(max_size=1)
|
39 |
+
|
40 |
+
demo.launch(show_api=True)
|
convert.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 fp16 weights in safetensors format to {}"
|
20 |
+
|
21 |
+
|
22 |
+
def convert_single(model_id: str, filename: str, folder: str, progress: Any, token: str):
|
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, token=token)
|
26 |
+
|
27 |
+
pipeline = StableDiffusionXLPipeline.from_single_file(ckpt_file, torch_dtype=torch.float16)
|
28 |
+
|
29 |
+
from diffusers import EulerAncestralDiscreteScheduler
|
30 |
+
pipeline.scheduler = EulerAncestralDiscreteScheduler.from_config(pipeline.scheduler.config)
|
31 |
+
|
32 |
+
pipeline.save_pretrained(folder, safe_serialization=True)
|
33 |
+
|
34 |
+
return folder
|
35 |
+
|
36 |
+
|
37 |
+
def previous_pr(api: "HfApi", model_id: str, pr_title: str) -> Optional["Discussion"]:
|
38 |
+
try:
|
39 |
+
discussions = api.get_repo_discussions(repo_id=model_id)
|
40 |
+
except Exception:
|
41 |
+
return None
|
42 |
+
for discussion in discussions:
|
43 |
+
if discussion.status == "open" and discussion.is_pull_request and discussion.title == pr_title:
|
44 |
+
details = api.get_discussion_details(repo_id=model_id, discussion_num=discussion.num)
|
45 |
+
if details.target_branch == "refs/heads/main":
|
46 |
+
return discussion
|
47 |
+
|
48 |
+
|
49 |
+
def convert(token: str, model_id: str, filename: str, progress=gr.Progress()):
|
50 |
+
api = HfApi()
|
51 |
+
|
52 |
+
pr_title = "Adding `diffusers` weights of this model"
|
53 |
+
|
54 |
+
with TemporaryDirectory() as d:
|
55 |
+
folder = os.path.join(d, repo_folder_name(repo_id=model_id, repo_type="models"))
|
56 |
+
os.makedirs(folder)
|
57 |
+
new_pr = None
|
58 |
+
try:
|
59 |
+
folder = convert_single(model_id, filename, folder, progress, token)
|
60 |
+
progress(0.7, desc="Uploading to Hub")
|
61 |
+
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)
|
62 |
+
pr_number = new_pr.split("%2F")[-1].split("/")[0]
|
63 |
+
link = f"Pr created at: {'https://huggingface.co/' + os.path.join(model_id, 'discussions', pr_number)}"
|
64 |
+
progress(1, desc="Done")
|
65 |
+
except Exception as e:
|
66 |
+
raise gr.exceptions.Error(str(e))
|
67 |
+
finally:
|
68 |
+
shutil.rmtree(folder)
|
69 |
+
|
70 |
+
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
|