John6666 commited on
Commit
4326a08
1 Parent(s): 0045deb

Upload convert_url_to_diffusers_sdxl_gr.py

Browse files
Files changed (1) hide show
  1. convert_url_to_diffusers_sdxl_gr.py +34 -10
convert_url_to_diffusers_sdxl_gr.py CHANGED
@@ -16,6 +16,31 @@ def is_repo_name(s):
16
  return re.fullmatch(r'^[^/,\s\"\']+/[^/,\s\"\']+$', s)
17
 
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def download_thing(directory, url, civitai_api_key="", hf_token="", progress=gr.Progress(track_tqdm=True)):
20
  url = url.strip()
21
  if "drive.google.com" in url:
@@ -29,7 +54,8 @@ def download_thing(directory, url, civitai_api_key="", hf_token="", progress=gr.
29
  url = url.replace("/blob/", "/resolve/")
30
  user_header = f'"Authorization: Bearer {hf_token}"'
31
  if hf_token:
32
- 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]}")
 
33
  else:
34
  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]}")
35
  elif "civitai.com" in url:
@@ -47,9 +73,9 @@ def download_thing(directory, url, civitai_api_key="", hf_token="", progress=gr.
47
  def get_local_model_list(dir_path):
48
  model_list = []
49
  valid_extensions = ('.safetensors')
50
- for file in Path(dir_path).glob("*"):
51
- if file.suffix in valid_extensions:
52
- file_path = str(Path(f"{dir_path}/{file.name}"))
53
  model_list.append(file_path)
54
  return model_list
55
 
@@ -287,11 +313,9 @@ def is_repo_exists(repo_id, hf_token):
287
  return True # for safe
288
 
289
 
290
- def create_diffusers_repo(new_repo_id, diffusers_folder, is_private, progress=gr.Progress(track_tqdm=True)):
291
  from huggingface_hub import HfApi
292
- import os
293
- hf_token = os.environ.get("HF_TOKEN")
294
- api = HfApi()
295
  try:
296
  progress(0, desc="Start uploading...")
297
  api.create_repo(repo_id=new_repo_id, token=hf_token, private=is_private)
@@ -303,7 +327,7 @@ def create_diffusers_repo(new_repo_id, diffusers_folder, is_private, progress=gr
303
  progress(1, desc="Uploaded.")
304
  url = f"https://huggingface.co/{new_repo_id}"
305
  except Exception as e:
306
- print(f"Error: Failed to upload to {new_repo_id}. ")
307
  print(e)
308
  return ""
309
  return url
@@ -331,7 +355,7 @@ def convert_url_to_diffusers_repo(dl_url, hf_user, hf_repo, hf_token, civitai_ke
331
  print(f"Repo already exists: {new_repo_id}")
332
  progress(1, desc=f"Repo already exists: {new_repo_id}")
333
  return gr.update(value=repo_urls, choices=repo_urls), gr.update(value="")
334
- repo_url = create_diffusers_repo(new_repo_id, new_path, is_private)
335
  shutil.rmtree(new_path)
336
  if not repo_urls: repo_urls = []
337
  repo_urls.append(repo_url)
 
16
  return re.fullmatch(r'^[^/,\s\"\']+/[^/,\s\"\']+$', s)
17
 
18
 
19
+ def split_hf_url(url: str):
20
+ import re
21
+ import urllib.parse
22
+ try:
23
+ s = list(re.findall(r'^(?:https?://huggingface.co/)(?:(datasets)/)?(.+?/.+?)/\w+?/.+?/(?:(.+)/)?(.+?.safetensors)(?:\?download=true)?$', url)[0])
24
+ if len(s) < 4: return "", "", "", ""
25
+ repo_id = s[1]
26
+ repo_type = "dataset" if s[0] == "datasets" else "model"
27
+ subfolder = urllib.parse.unquote(s[2]) if s[2] else None
28
+ filename = urllib.parse.unquote(s[3])
29
+ return repo_id, filename, subfolder, repo_type
30
+ except Exception as e:
31
+ print(e)
32
+
33
+
34
+ def download_hf_file(directory, url, hf_token="", progress=gr.Progress(track_tqdm=True)):
35
+ from huggingface_hub import hf_hub_download
36
+ repo_id, filename, subfolder, repo_type = split_hf_url(url)
37
+ try:
38
+ 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)
39
+ else: hf_hub_download(repo_id=repo_id, filename=filename, repo_type=repo_type, local_dir=directory, token=hf_token)
40
+ except Exception as e:
41
+ print(f"Failed to download: {e}")
42
+
43
+
44
  def download_thing(directory, url, civitai_api_key="", hf_token="", progress=gr.Progress(track_tqdm=True)):
45
  url = url.strip()
46
  if "drive.google.com" in url:
 
54
  url = url.replace("/blob/", "/resolve/")
55
  user_header = f'"Authorization: Bearer {hf_token}"'
56
  if hf_token:
57
+ download_hf_file(directory, url, hf_token)
58
+ #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]}")
59
  else:
60
  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]}")
61
  elif "civitai.com" in url:
 
73
  def get_local_model_list(dir_path):
74
  model_list = []
75
  valid_extensions = ('.safetensors')
76
+ for file in Path(dir_path).glob("**/*.*"):
77
+ if file.is_file() and file.suffix in valid_extensions:
78
+ file_path = str(file)
79
  model_list.append(file_path)
80
  return model_list
81
 
 
313
  return True # for safe
314
 
315
 
316
+ def create_diffusers_repo(new_repo_id, diffusers_folder, is_private, hf_token, progress=gr.Progress(track_tqdm=True)):
317
  from huggingface_hub import HfApi
318
+ api = HfApi(token=hf_token)
 
 
319
  try:
320
  progress(0, desc="Start uploading...")
321
  api.create_repo(repo_id=new_repo_id, token=hf_token, private=is_private)
 
327
  progress(1, desc="Uploaded.")
328
  url = f"https://huggingface.co/{new_repo_id}"
329
  except Exception as e:
330
+ print(f"Error: Failed to upload to {new_repo_id}.")
331
  print(e)
332
  return ""
333
  return url
 
355
  print(f"Repo already exists: {new_repo_id}")
356
  progress(1, desc=f"Repo already exists: {new_repo_id}")
357
  return gr.update(value=repo_urls, choices=repo_urls), gr.update(value="")
358
+ repo_url = create_diffusers_repo(new_repo_id, new_path, is_private, hf_token)
359
  shutil.rmtree(new_path)
360
  if not repo_urls: repo_urls = []
361
  repo_urls.append(repo_url)