John6666 commited on
Commit
d47d03d
·
verified ·
1 Parent(s): 4f34640

Upload 9 files

Browse files
README.md CHANGED
@@ -7,6 +7,7 @@ sdk: gradio
7
  sdk_version: 4.44.0
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
7
  sdk_version: 4.44.0
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
app.py CHANGED
@@ -15,9 +15,11 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
15
  is_upload = gr.Checkbox(label="Upload safetensors to HF Repo", info="Fast download, but files will be public.", value=False)
16
  with gr.Accordion("Advanced", open=False):
17
  dtype = gr.Radio(label="Output data type", choices=["fp16", "fp32", "bf16", "default"], value="fp16")
18
- hf_token = gr.Textbox(label="Your HF write token", placeholder="hf_...", value="", max_lines=1)
19
  with gr.Row():
20
- newrepo_id = gr.Textbox(label="Upload repo ID", placeholder="author/model", value="", max_lines=1)
 
 
 
21
  newrepo_type = gr.Radio(label="Upload repo type", choices=["model", "dataset"], value="model")
22
  is_private = gr.Checkbox(label="Create / Use private repo", value=True)
23
  uploaded_urls = gr.CheckboxGroup(visible=False, choices=[], value=None) # hidden
 
15
  is_upload = gr.Checkbox(label="Upload safetensors to HF Repo", info="Fast download, but files will be public.", value=False)
16
  with gr.Accordion("Advanced", open=False):
17
  dtype = gr.Radio(label="Output data type", choices=["fp16", "fp32", "bf16", "default"], value="fp16")
 
18
  with gr.Row():
19
+ hf_token = gr.Textbox(label="Your HF write token", placeholder="hf_...", value="", max_lines=1)
20
+ gr.Markdown("Your token is available at [hf.co/settings/tokens](https://huggingface.co/settings/tokens).")
21
+ with gr.Row():
22
+ newrepo_id = gr.Textbox(label="Upload repo ID", placeholder="yourid/newrepo", value="", max_lines=1)
23
  newrepo_type = gr.Radio(label="Upload repo type", choices=["model", "dataset"], value="model")
24
  is_private = gr.Checkbox(label="Create / Use private repo", value=True)
25
  uploaded_urls = gr.CheckboxGroup(visible=False, choices=[], value=None) # hidden
convert_repo_to_safetensors_gr.py CHANGED
@@ -15,23 +15,8 @@ import os
15
  from pathlib import Path
16
  import shutil
17
  import gc
 
18
 
19
- def get_token():
20
- try:
21
- token = HfFolder.get_token()
22
- except Exception:
23
- token = ""
24
- return token
25
-
26
- def is_repo_exists(repo_id: str, repo_type: str="model"):
27
- hf_token = get_token()
28
- api = HfApi(token=hf_token)
29
- try:
30
- if api.repo_exists(repo_id=repo_id, repo_type=repo_type, token=hf_token): return True
31
- else: return False
32
- except Exception as e:
33
- print(f"Error: Failed to connect {repo_id} ({repo_type}). {e}")
34
- return True # for safe
35
 
36
  # =================#
37
  # UNet Conversion #
@@ -292,7 +277,7 @@ def convert_openai_text_enc_state_dict(text_enc_dict):
292
  return text_enc_dict
293
 
294
 
295
- def convert_diffusers_to_safetensors(model_path, checkpoint_path, dtype="fp16"):
296
  # Path for safetensors
297
  unet_path = osp.join(model_path, "unet", "diffusion_pytorch_model.safetensors")
298
  vae_path = osp.join(model_path, "vae", "diffusion_pytorch_model.safetensors")
@@ -355,10 +340,12 @@ def convert_diffusers_to_safetensors(model_path, checkpoint_path, dtype="fp16"):
355
  save_file(state_dict, checkpoint_path)
356
 
357
 
358
- def download_repo(repo_id, dir_path):
 
359
  hf_token = get_token()
360
  try:
361
- snapshot_download(repo_id=repo_id, local_dir=dir_path, token=hf_token)
 
362
  except Exception as e:
363
  print(f"Error: Failed to download {repo_id}. {e}")
364
  gr.Warning(f"Error: Failed to download {repo_id}. {e}")
@@ -370,11 +357,11 @@ def upload_safetensors_to_repo(filename, repo_id, repo_type, is_private, progres
370
  hf_token = get_token()
371
  api = HfApi(token=hf_token)
372
  try:
373
- if not is_repo_exists(repo_id, repo_type): api.create_repo(repo_id, repo_type=repo_type, token=hf_token, private=is_private)
374
  progress(0, desc="Start uploading...")
375
- api.upload_file(path_or_fileobj=filename, path_in_repo=output_filename, repo_type=repo_type, token=hf_token, repo_id=repo_id)
376
  progress(1, desc="Uploaded.")
377
- url = hf_hub_url(repo_id=repo_id, repo_type=repo_type, filename=output_filename, token=hf_token)
378
  except Exception as e:
379
  print(f"Error: Failed to upload to {repo_id}. {e}")
380
  gr.Warning(f"Error: Failed to upload to {repo_id}. {e}")
@@ -396,9 +383,9 @@ def convert_repo_to_safetensors(repo_id, dtype="fp16", progress=gr.Progress(trac
396
 
397
  def convert_repo_to_safetensors_multi(repo_id, hf_token, files, urls, dtype="fp16", is_upload=False,
398
  newrepo_id="", repo_type="model", is_private=True, progress=gr.Progress(track_tqdm=True)):
399
- if is_upload and newrepo_id and not hf_token: raise gr.Error("Invalid token.")
400
- if hf_token: HfFolder.save_token(hf_token)
401
- else: HfFolder.save_token(os.environ.get("HF_TOKEN"))
402
  if not newrepo_id: newrepo_id = os.environ.get("HF_OUTPUT_REPO")
403
  file = convert_repo_to_safetensors(repo_id, dtype)
404
  if not urls: urls = []
 
15
  from pathlib import Path
16
  import shutil
17
  import gc
18
+ from utils import get_token, set_token, is_repo_exists
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # =================#
22
  # UNet Conversion #
 
277
  return text_enc_dict
278
 
279
 
280
+ def convert_diffusers_to_safetensors(model_path, checkpoint_path, dtype="fp16", progress=gr.Progress(track_tqdm=True)):
281
  # Path for safetensors
282
  unet_path = osp.join(model_path, "unet", "diffusion_pytorch_model.safetensors")
283
  vae_path = osp.join(model_path, "vae", "diffusion_pytorch_model.safetensors")
 
340
  save_file(state_dict, checkpoint_path)
341
 
342
 
343
+ # https://huggingface.co/docs/huggingface_hub/v0.25.1/en/package_reference/file_download#huggingface_hub.snapshot_download
344
+ def download_repo(repo_id, dir_path, progress=gr.Progress(track_tqdm=True)):
345
  hf_token = get_token()
346
  try:
347
+ snapshot_download(repo_id=repo_id, local_dir=dir_path, token=hf_token, allow_patterns=["*.safetensors", "*.bin"],
348
+ ignore_patterns=["*.fp16.*", "/*.safetensors", "/*.bin"], force_download=True)
349
  except Exception as e:
350
  print(f"Error: Failed to download {repo_id}. {e}")
351
  gr.Warning(f"Error: Failed to download {repo_id}. {e}")
 
357
  hf_token = get_token()
358
  api = HfApi(token=hf_token)
359
  try:
360
+ if not is_repo_exists(repo_id, repo_type): api.create_repo(repo_id=repo_id, repo_type=repo_type, token=hf_token, private=is_private)
361
  progress(0, desc="Start uploading...")
362
+ api.upload_file(path_or_fileobj=filename, path_in_repo=output_filename, repo_type=repo_type, revision="main", token=hf_token, repo_id=repo_id)
363
  progress(1, desc="Uploaded.")
364
+ url = hf_hub_url(repo_id=repo_id, repo_type=repo_type, filename=output_filename)
365
  except Exception as e:
366
  print(f"Error: Failed to upload to {repo_id}. {e}")
367
  gr.Warning(f"Error: Failed to upload to {repo_id}. {e}")
 
383
 
384
  def convert_repo_to_safetensors_multi(repo_id, hf_token, files, urls, dtype="fp16", is_upload=False,
385
  newrepo_id="", repo_type="model", is_private=True, progress=gr.Progress(track_tqdm=True)):
386
+ if hf_token: set_token(hf_token)
387
+ else: set_token(os.environ.get("HF_TOKEN"))
388
+ if is_upload and newrepo_id and not hf_token: raise gr.Error("HF write token is required for this process.")
389
  if not newrepo_id: newrepo_id = os.environ.get("HF_OUTPUT_REPO")
390
  file = convert_repo_to_safetensors(repo_id, dtype)
391
  if not urls: urls = []
local/convert_repo_to_safetensors.py CHANGED
@@ -269,7 +269,7 @@ def convert_openai_text_enc_state_dict(text_enc_dict):
269
  return text_enc_dict
270
 
271
 
272
- def convert_diffusers_to_safetensors(model_path, checkpoint_path, half = True):
273
  # Path for safetensors
274
  unet_path = osp.join(model_path, "unet", "diffusion_pytorch_model.safetensors")
275
  vae_path = osp.join(model_path, "vae", "diffusion_pytorch_model.safetensors")
@@ -325,8 +325,9 @@ def convert_diffusers_to_safetensors(model_path, checkpoint_path, half = True):
325
  # Put together new checkpoint
326
  state_dict = {**unet_state_dict, **vae_state_dict, **text_enc_dict, **text_enc_2_dict}
327
 
328
- if half:
329
- state_dict = {k: v.half() for k, v in state_dict.items()}
 
330
 
331
  save_file(state_dict, checkpoint_path)
332
 
@@ -336,15 +337,15 @@ def download_repo(repo_id, dir_path):
336
  try:
337
  snapshot_download(repo_id=repo_id, local_dir=dir_path)
338
  except Exception as e:
339
- print(f"Error: Failed to download {repo_id}. ")
340
  return
341
 
342
 
343
- def convert_repo_to_safetensors(repo_id, half=True):
344
  download_dir = f"{repo_id.split('/')[0]}_{repo_id.split('/')[-1]}"
345
  output_filename = f"{repo_id.split('/')[0]}_{repo_id.split('/')[-1]}.safetensors"
346
  download_repo(repo_id, download_dir)
347
- convert_diffusers_to_safetensors(download_dir, output_filename, half)
348
  return output_filename
349
 
350
 
@@ -352,12 +353,12 @@ if __name__ == "__main__":
352
  parser = argparse.ArgumentParser()
353
 
354
  parser.add_argument("--repo_id", default=None, type=str, required=True, help="HF Repo ID of the model to convert.")
355
- parser.add_argument("--half", default=True, help="Save weights in half precision.")
356
 
357
  args = parser.parse_args()
358
  assert args.repo_id is not None, "Must provide a Repo ID!"
359
 
360
- convert_repo_to_safetensors(args.repo_id, args.half)
361
 
362
 
363
  # Usage: python convert_repo_to_safetensors.py --repo_id GraydientPlatformAPI/goodfit-pony41-xl
 
269
  return text_enc_dict
270
 
271
 
272
+ def convert_diffusers_to_safetensors(model_path, checkpoint_path, dtype="fp16"):
273
  # Path for safetensors
274
  unet_path = osp.join(model_path, "unet", "diffusion_pytorch_model.safetensors")
275
  vae_path = osp.join(model_path, "vae", "diffusion_pytorch_model.safetensors")
 
325
  # Put together new checkpoint
326
  state_dict = {**unet_state_dict, **vae_state_dict, **text_enc_dict, **text_enc_2_dict}
327
 
328
+ if dtype == "fp16": state_dict = {k: v.half() for k, v in state_dict.items()}
329
+ elif dtype == "fp32": state_dict = {k: v.to(torch.float32) for k, v in state_dict.items()}
330
+ elif dtype == "bf16": state_dict = {k: v.to(torch.bfloat16) for k, v in state_dict.items()}
331
 
332
  save_file(state_dict, checkpoint_path)
333
 
 
337
  try:
338
  snapshot_download(repo_id=repo_id, local_dir=dir_path)
339
  except Exception as e:
340
+ print(f"Error: Failed to download {repo_id}. {e}")
341
  return
342
 
343
 
344
+ def convert_repo_to_safetensors(repo_id, dtype="fp16"):
345
  download_dir = f"{repo_id.split('/')[0]}_{repo_id.split('/')[-1]}"
346
  output_filename = f"{repo_id.split('/')[0]}_{repo_id.split('/')[-1]}.safetensors"
347
  download_repo(repo_id, download_dir)
348
+ convert_diffusers_to_safetensors(download_dir, output_filename, dtype)
349
  return output_filename
350
 
351
 
 
353
  parser = argparse.ArgumentParser()
354
 
355
  parser.add_argument("--repo_id", default=None, type=str, required=True, help="HF Repo ID of the model to convert.")
356
+ parser.add_argument("--dtype", default="fp16", type=str, choices=["fp16", "fp32", "bf16", "default"], help='Output data type. (Default: "fp16")')
357
 
358
  args = parser.parse_args()
359
  assert args.repo_id is not None, "Must provide a Repo ID!"
360
 
361
+ convert_repo_to_safetensors(args.repo_id, args.dtype)
362
 
363
 
364
  # Usage: python convert_repo_to_safetensors.py --repo_id GraydientPlatformAPI/goodfit-pony41-xl
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ git-lfs aria2
utils.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import HfApi, HfFolder, hf_hub_download
3
+ import os
4
+ from pathlib import Path
5
+ import shutil
6
+ import gc
7
+ import re
8
+ import urllib.parse
9
+
10
+
11
+ def get_token():
12
+ try:
13
+ token = HfFolder.get_token()
14
+ except Exception:
15
+ token = ""
16
+ return token
17
+
18
+
19
+ def set_token(token):
20
+ try:
21
+ HfFolder.save_token(token)
22
+ except Exception:
23
+ print(f"Error: Failed to save token.")
24
+
25
+
26
+ def is_repo_exists(repo_id: str, repo_type: str="model"):
27
+ hf_token = get_token()
28
+ api = HfApi(token=hf_token)
29
+ try:
30
+ if api.repo_exists(repo_id=repo_id, repo_type=repo_type, token=hf_token): return True
31
+ else: return False
32
+ except Exception as e:
33
+ print(f"Error: Failed to connect {repo_id} ({repo_type}). {e}")
34
+ return True # for safe
35
+
36
+
37
+ def list_sub(a, b):
38
+ return [e for e in a if e not in b]
39
+
40
+
41
+ def is_repo_name(s):
42
+ return re.fullmatch(r'^[^/,\s\"\']+/[^/,\s\"\']+$', s)
43
+
44
+
45
+ def split_hf_url(url: str):
46
+ try:
47
+ s = list(re.findall(r'^(?:https?://huggingface.co/)(?:(datasets)/)?(.+?/.+?)/\w+?/.+?/(?:(.+)/)?(.+?.safetensors)(?:\?download=true)?$', url)[0])
48
+ if len(s) < 4: return "", "", "", ""
49
+ repo_id = s[1]
50
+ repo_type = "dataset" if s[0] == "datasets" else "model"
51
+ subfolder = urllib.parse.unquote(s[2]) if s[2] else None
52
+ filename = urllib.parse.unquote(s[3])
53
+ return repo_id, filename, subfolder, repo_type
54
+ except Exception as e:
55
+ print(e)
56
+
57
+
58
+ def download_hf_file(directory, url, progress=gr.Progress(track_tqdm=True)):
59
+ hf_token = get_token()
60
+ repo_id, filename, subfolder, repo_type = split_hf_url(url)
61
+ try:
62
+ if subfolder is not None: hf_hub_download(repo_id=repo_id, filename=filename, subfolder=subfolder, repo_type=repo_type, local_dir=directory, token=hf_token)
63
+ else: hf_hub_download(repo_id=repo_id, filename=filename, repo_type=repo_type, local_dir=directory, token=hf_token)
64
+ except Exception as e:
65
+ print(f"Failed to download: {e}")
66
+
67
+
68
+ def download_thing(directory, url, civitai_api_key="", progress=gr.Progress(track_tqdm=True)): # requires aria2
69
+ hf_token = get_token()
70
+ url = url.strip()
71
+ if "drive.google.com" in url:
72
+ original_dir = os.getcwd()
73
+ os.chdir(directory)
74
+ os.system(f"gdown --fuzzy {url}")
75
+ os.chdir(original_dir)
76
+ elif "huggingface.co" in url:
77
+ url = url.replace("?download=true", "")
78
+ if "/blob/" in url:
79
+ url = url.replace("/blob/", "/resolve/")
80
+ #user_header = f'"Authorization: Bearer {hf_token}"'
81
+ if hf_token:
82
+ download_hf_file(directory, url)
83
+ #os.system(f"aria2c --console-log-level=error --summary-interval=10 --header={user_header} -c -x 16 -k 1M -s 16 {url} -d {directory} -o {url.split('/')[-1]}")
84
+ else:
85
+ os.system(f"aria2c --optimize-concurrent-downloads --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 {url} -d {directory} -o {url.split('/')[-1]}")
86
+ elif "civitai.com" in url:
87
+ if "?" in url:
88
+ url = url.split("?")[0]
89
+ if civitai_api_key:
90
+ url = url + f"?token={civitai_api_key}"
91
+ os.system(f"aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -d {directory} {url}")
92
+ else:
93
+ print("You need an API key to download Civitai models.")
94
+ else:
95
+ os.system(f"aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -d {directory} {url}")
96
+
97
+
98
+ def get_local_model_list(dir_path):
99
+ model_list = []
100
+ valid_extensions = ('.safetensors')
101
+ for file in Path(dir_path).glob("**/*.*"):
102
+ if file.is_file() and file.suffix in valid_extensions:
103
+ file_path = str(file)
104
+ model_list.append(file_path)
105
+ return model_list
106
+
107
+
108
+ def get_download_file(temp_dir, url, civitai_key, progress=gr.Progress(track_tqdm=True)):
109
+ if not "http" in url and is_repo_name(url) and not Path(url).exists():
110
+ print(f"Use HF Repo: {url}")
111
+ new_file = url
112
+ elif not "http" in url and Path(url).exists():
113
+ print(f"Use local file: {url}")
114
+ new_file = url
115
+ elif Path(f"{temp_dir}/{url.split('/')[-1]}").exists():
116
+ print(f"File to download alreday exists: {url}")
117
+ new_file = f"{temp_dir}/{url.split('/')[-1]}"
118
+ else:
119
+ print(f"Start downloading: {url}")
120
+ before = get_local_model_list(temp_dir)
121
+ try:
122
+ download_thing(temp_dir, url.strip(), civitai_key)
123
+ except Exception:
124
+ print(f"Download failed: {url}")
125
+ return ""
126
+ after = get_local_model_list(temp_dir)
127
+ new_file = list_sub(after, before)[0] if list_sub(after, before) else ""
128
+ if not new_file:
129
+ print(f"Download failed: {url}")
130
+ return ""
131
+ print(f"Download completed: {url}")
132
+ return new_file