Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from diffusers import DiffusionPipeline
|
| 3 |
+
from huggingface_hub import export_folder_as_dduf, create_repo, upload_file
|
| 4 |
+
import tempfile
|
| 5 |
+
import torch
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
_DTYPE_MAP = {"fp32": torch.float32, "fp16": torch.float16, "bf16": torch.bfloat16}
|
| 9 |
+
article = """
|
| 10 |
+
## DDUF
|
| 11 |
+
|
| 12 |
+
* [DDUF](https://huggingface.co/docs/diffusers/main/en/using-diffusers/other-formats#dduf)
|
| 13 |
+
|
| 14 |
+
Currently, we require a `repo_id` to have all the pipeline components in the Diffusers format. Examples include:
|
| 15 |
+
[black-forest-labs/FLUX.1-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev), [stabilityai/stable-video-diffusion-img2vid-xt](https://huggingface.co/stabilityai/stable-video-diffusion-img2vid-xt), etc.
|
| 16 |
+
|
| 17 |
+
Partial components will be supported in the future.
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def make_dduf(repo_id: str, destination_repo_id: str, dduf_filename: str, token: str, torch_dtype: str):
|
| 22 |
+
if destination_repo_id == "":
|
| 23 |
+
destination_repo_id = repo_id
|
| 24 |
+
|
| 25 |
+
destination_repo_id = create_repo(repo_id=destination_repo_id, exist_ok=True).repo_id
|
| 26 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 27 |
+
pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=_DTYPE_MAP[torch_dtype])
|
| 28 |
+
if dduf_filename == "":
|
| 29 |
+
dduf_filename = f"{pipe.__class__.__name__.lower()}"
|
| 30 |
+
if torch_dtype != "fp32":
|
| 31 |
+
dduf_filename += f"_{torch_dtype}"
|
| 32 |
+
dduf_filename += ".dduf"
|
| 33 |
+
dduf_filename = os.path.join(tmpdir, dduf_filename)
|
| 34 |
+
|
| 35 |
+
pipe.save_pretrained(tmpdir, safe_serialization=True)
|
| 36 |
+
|
| 37 |
+
return_message = ""
|
| 38 |
+
try:
|
| 39 |
+
export_folder_as_dduf(dduf_filename, folder_path=tmpdir)
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return_message += f"β Got the following error while exporting: \n{e}"
|
| 42 |
+
try:
|
| 43 |
+
commit_url = upload_file(
|
| 44 |
+
repo_id=destination_repo_id,
|
| 45 |
+
path_in_repo=dduf_filename.split("/")[-1],
|
| 46 |
+
path_or_fileobj=dduf_filename,
|
| 47 |
+
token=token,
|
| 48 |
+
).commit_url
|
| 49 |
+
return_message += f"Success π₯. Find the DDUF in [this commit]({commit_url})."
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return_message += f"β Got the following error while pushing: \n{e}"
|
| 52 |
+
return str(return_message)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
demo = gr.Interface(
|
| 56 |
+
title="DDUF my repo π€",
|
| 57 |
+
article=article,
|
| 58 |
+
fn=make_dduf,
|
| 59 |
+
inputs=[
|
| 60 |
+
gr.components.Textbox(lines=1, placeholder="Repo ID which should be DDUF'd."),
|
| 61 |
+
gr.components.Textbox(
|
| 62 |
+
lines=2,
|
| 63 |
+
value=None,
|
| 64 |
+
placeholder="Destination Repo ID that should be used to store the resultant DDUF. Leave it if you want to use the `repo_id` here.",
|
| 65 |
+
),
|
| 66 |
+
gr.components.Textbox(
|
| 67 |
+
lines=1,
|
| 68 |
+
value=None,
|
| 69 |
+
placeholder="Name of the DDUF file. If it's not provided we will infer it based on the pipeline class.",
|
| 70 |
+
),
|
| 71 |
+
gr.components.Textbox(lines=1, placeholder="HF token. You can obtain it from hf.co/settings/tokens."),
|
| 72 |
+
gr.Dropdown(
|
| 73 |
+
list(_DTYPE_MAP.keys()),
|
| 74 |
+
value="fp32",
|
| 75 |
+
multiselect=False,
|
| 76 |
+
label="dtype",
|
| 77 |
+
info="dtype to load the pipeline in.",
|
| 78 |
+
),
|
| 79 |
+
],
|
| 80 |
+
outputs="markdown",
|
| 81 |
+
examples=[
|
| 82 |
+
["stabilityai/stable-video-diffusion-img2vid-xt", "sayakpaul/svd-dduf", "svd.dduf", "hf_XXX", "fp32"],
|
| 83 |
+
],
|
| 84 |
+
allow_flagging="never",
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
demo.launch(debug=True)
|