Yntec commited on
Commit
1736606
·
verified ·
1 Parent(s): 3910690

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +287 -4
utils.py CHANGED
@@ -1,6 +1,289 @@
1
- def is_google_colab():
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  try:
3
- import google.colab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  return True
5
- except:
6
- return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import HfApi, HfFolder, hf_hub_download, snapshot_download
3
+ import os
4
+ from pathlib import Path
5
+ import shutil
6
+ import gc
7
+ import re
8
+ import urllib.parse
9
+ import subprocess
10
+ import time
11
+ from typing import Any
12
+
13
+
14
+ def get_token():
15
  try:
16
+ token = HfFolder.get_token()
17
+ except Exception:
18
+ token = ""
19
+ return token
20
+
21
+
22
+ def set_token(token):
23
+ try:
24
+ HfFolder.save_token(token)
25
+ except Exception:
26
+ print(f"Error: Failed to save token.")
27
+
28
+
29
+ def get_state(state: dict, key: str):
30
+ if key in state.keys(): return state[key]
31
+ else:
32
+ print(f"State '{key}' not found.")
33
+ return None
34
+
35
+
36
+ def set_state(state: dict, key: str, value: Any):
37
+ state[key] = value
38
+
39
+
40
+ def get_user_agent():
41
+ return 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0'
42
+
43
+
44
+ def is_repo_exists(repo_id: str, repo_type: str="model"):
45
+ hf_token = get_token()
46
+ api = HfApi(token=hf_token)
47
+ try:
48
+ if api.repo_exists(repo_id=repo_id, repo_type=repo_type, token=hf_token): return True
49
+ else: return False
50
+ except Exception as e:
51
+ print(f"Error: Failed to connect {repo_id} ({repo_type}). {e}")
52
+ return True # for safe
53
+
54
+
55
+ MODEL_TYPE_CLASS = {
56
+ "diffusers:StableDiffusionPipeline": "SD 1.5",
57
+ "diffusers:StableDiffusionXLPipeline": "SDXL",
58
+ "diffusers:FluxPipeline": "FLUX",
59
+ }
60
+
61
+
62
+ def get_model_type(repo_id: str):
63
+ hf_token = get_token()
64
+ api = HfApi(token=hf_token)
65
+ lora_filename = "pytorch_lora_weights.safetensors"
66
+ diffusers_filename = "model_index.json"
67
+ default = "SDXL"
68
+ try:
69
+ if api.file_exists(repo_id=repo_id, filename=lora_filename, token=hf_token): return "LoRA"
70
+ if not api.file_exists(repo_id=repo_id, filename=diffusers_filename, token=hf_token): return "None"
71
+ model = api.model_info(repo_id=repo_id, token=hf_token)
72
+ tags = model.tags
73
+ for tag in tags:
74
+ if tag in MODEL_TYPE_CLASS.keys(): return MODEL_TYPE_CLASS.get(tag, default)
75
+ except Exception:
76
+ return default
77
+ return default
78
+
79
+
80
+ def list_uniq(l):
81
+ return sorted(set(l), key=l.index)
82
+
83
+
84
+ def list_sub(a, b):
85
+ return [e for e in a if e not in b]
86
+
87
+
88
+ def is_repo_name(s):
89
+ return re.fullmatch(r'^[\w_\-\.]+/[\w_\-\.]+$', s)
90
+
91
+
92
+ def get_hf_url(repo_id: str, repo_type: str="model"):
93
+ if repo_type == "dataset": url = f"https://huggingface.co/datasets/{repo_id}"
94
+ elif repo_type == "space": url = f"https://huggingface.co/spaces/{repo_id}"
95
+ else: url = f"https://huggingface.co/{repo_id}"
96
+ return url
97
+
98
+
99
+ def split_hf_url(url: str):
100
+ try:
101
+ s = list(re.findall(r'^(?:https?://huggingface.co/)(?:(datasets|spaces)/)?(.+?/.+?)/\w+?/.+?/(?:(.+)/)?(.+?.\w+)(?:\?download=true)?$', url)[0])
102
+ if len(s) < 4: return "", "", "", ""
103
+ repo_id = s[1]
104
+ if s[0] == "datasets": repo_type = "dataset"
105
+ elif s[0] == "spaces": repo_type = "space"
106
+ else: repo_type = "model"
107
+ subfolder = urllib.parse.unquote(s[2]) if s[2] else None
108
+ filename = urllib.parse.unquote(s[3])
109
+ return repo_id, filename, subfolder, repo_type
110
+ except Exception as e:
111
+ print(e)
112
+
113
+
114
+ def download_hf_file(directory, url, progress=gr.Progress(track_tqdm=True)):
115
+ hf_token = get_token()
116
+ repo_id, filename, subfolder, repo_type = split_hf_url(url)
117
+ try:
118
+ print(f"Downloading {url} to {directory}")
119
+ if subfolder is not None: path = hf_hub_download(repo_id=repo_id, filename=filename, subfolder=subfolder, repo_type=repo_type, local_dir=directory, token=hf_token)
120
+ else: path = hf_hub_download(repo_id=repo_id, filename=filename, repo_type=repo_type, local_dir=directory, token=hf_token)
121
+ return path
122
+ except Exception as e:
123
+ print(f"Failed to download: {e}")
124
+ return None
125
+
126
+
127
+ def download_thing(directory, url, civitai_api_key="", progress=gr.Progress(track_tqdm=True)): # requires aria2, gdown
128
+ try:
129
+ url = url.strip()
130
+ if "drive.google.com" in url:
131
+ original_dir = os.getcwd()
132
+ os.chdir(directory)
133
+ subprocess.run(f"gdown --fuzzy {url}", shell=True)
134
+ os.chdir(original_dir)
135
+ elif "huggingface.co" in url:
136
+ url = url.replace("?download=true", "")
137
+ if "/blob/" in url: url = url.replace("/blob/", "/resolve/")
138
+ download_hf_file(directory, url)
139
+ elif "civitai.com" in url:
140
+ if civitai_api_key:
141
+ url = f"'{url}&token={civitai_api_key}'" if "?" in url else f"{url}?token={civitai_api_key}"
142
+ print(f"Downloading {url}")
143
+ subprocess.run(f"aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -d {directory} {url}", shell=True)
144
+ else:
145
+ print("You need an API key to download Civitai models.")
146
+ else:
147
+ os.system(f"aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -d {directory} {url}")
148
+ except Exception as e:
149
+ print(f"Failed to download: {e}")
150
+
151
+
152
+ def get_local_file_list(dir_path):
153
+ file_list = []
154
+ for file in Path(dir_path).glob("**/*.*"):
155
+ if file.is_file():
156
+ file_path = str(file)
157
+ file_list.append(file_path)
158
+ return file_list
159
+
160
+
161
+ def get_download_file(temp_dir, url, civitai_key, progress=gr.Progress(track_tqdm=True)):
162
+ try:
163
+ if not "http" in url and is_repo_name(url) and not Path(url).exists():
164
+ print(f"Use HF Repo: {url}")
165
+ new_file = url
166
+ elif not "http" in url and Path(url).exists():
167
+ print(f"Use local file: {url}")
168
+ new_file = url
169
+ elif Path(f"{temp_dir}/{url.split('/')[-1]}").exists():
170
+ print(f"File to download alreday exists: {url}")
171
+ new_file = f"{temp_dir}/{url.split('/')[-1]}"
172
+ else:
173
+ print(f"Start downloading: {url}")
174
+ before = get_local_file_list(temp_dir)
175
+ download_thing(temp_dir, url.strip(), civitai_key)
176
+ after = get_local_file_list(temp_dir)
177
+ new_file = list_sub(after, before)[0] if list_sub(after, before) else ""
178
+ if not new_file:
179
+ print(f"Download failed: {url}")
180
+ return ""
181
+ print(f"Download completed: {url}")
182
+ return new_file
183
+ except Exception as e:
184
+ print(f"Download failed: {url} {e}")
185
+ return ""
186
+
187
+
188
+ def download_repo(repo_id: str, dir_path: str, progress=gr.Progress(track_tqdm=True)): # for diffusers repo
189
+ hf_token = get_token()
190
+ try:
191
+ snapshot_download(repo_id=repo_id, local_dir=dir_path, token=hf_token, allow_patterns=["*.safetensors", "*.bin"],
192
+ ignore_patterns=["*.fp16.*", "/*.safetensors", "/*.bin"], force_download=True)
193
  return True
194
+ except Exception as e:
195
+ print(f"Error: Failed to download {repo_id}. {e}")
196
+ gr.Warning(f"Error: Failed to download {repo_id}. {e}")
197
+ return False
198
+
199
+
200
+ def upload_repo(repo_id: str, dir_path: str, is_private: bool, progress=gr.Progress(track_tqdm=True)): # for diffusers repo
201
+ hf_token = get_token()
202
+ api = HfApi(token=hf_token)
203
+ try:
204
+ progress(0, desc="Start uploading...")
205
+ api.create_repo(repo_id=repo_id, token=hf_token, private=is_private, exist_ok=True)
206
+ for path in Path(dir_path).glob("*"):
207
+ if path.is_dir():
208
+ api.upload_folder(repo_id=repo_id, folder_path=str(path), path_in_repo=path.name, token=hf_token)
209
+ elif path.is_file():
210
+ api.upload_file(repo_id=repo_id, path_or_fileobj=str(path), path_in_repo=path.name, token=hf_token)
211
+ progress(1, desc="Uploaded.")
212
+ return get_hf_url(repo_id, "model")
213
+ except Exception as e:
214
+ print(f"Error: Failed to upload to {repo_id}. {e}")
215
+ return ""
216
+
217
+
218
+ HF_SUBFOLDER_NAME = ["None", "user_repo"]
219
+
220
+
221
+ def duplicate_hf_repo(src_repo: str, dst_repo: str, src_repo_type: str, dst_repo_type: str,
222
+ is_private: bool, subfolder_type: str=HF_SUBFOLDER_NAME[1], progress=gr.Progress(track_tqdm=True)):
223
+ hf_token = get_token()
224
+ api = HfApi(token=hf_token)
225
+ try:
226
+ if subfolder_type == "user_repo": subfolder = src_repo.replace("/", "_")
227
+ else: subfolder = ""
228
+ progress(0, desc="Start duplicating...")
229
+ api.create_repo(repo_id=dst_repo, repo_type=dst_repo_type, private=is_private, exist_ok=True, token=hf_token)
230
+ for path in api.list_repo_files(repo_id=src_repo, repo_type=src_repo_type, token=hf_token):
231
+ file = hf_hub_download(repo_id=src_repo, filename=path, repo_type=src_repo_type, token=hf_token)
232
+ if not Path(file).exists(): continue
233
+ if Path(file).is_dir(): # unused for now
234
+ api.upload_folder(repo_id=dst_repo, folder_path=file, path_in_repo=f"{subfolder}/{path}" if subfolder else path,
235
+ repo_type=dst_repo_type, token=hf_token)
236
+ elif Path(file).is_file():
237
+ api.upload_file(repo_id=dst_repo, path_or_fileobj=file, path_in_repo=f"{subfolder}/{path}" if subfolder else path,
238
+ repo_type=dst_repo_type, token=hf_token)
239
+ if Path(file).exists(): Path(file).unlink()
240
+ progress(1, desc="Duplicated.")
241
+ return f"{get_hf_url(dst_repo, dst_repo_type)}/tree/main/{subfolder}" if subfolder else get_hf_url(dst_repo, dst_repo_type)
242
+ except Exception as e:
243
+ print(f"Error: Failed to duplicate repo {src_repo} to {dst_repo}. {e}")
244
+ return ""
245
+
246
+
247
+ BASE_DIR = str(Path(__file__).resolve().parent.resolve())
248
+ CIVITAI_API_KEY = os.environ.get("CIVITAI_API_KEY")
249
+
250
+
251
+ def get_file(url: str, path: str): # requires aria2, gdown
252
+ print(f"Downloading {url} to {path}...")
253
+ get_download_file(path, url, CIVITAI_API_KEY)
254
+
255
+
256
+ def git_clone(url: str, path: str, pip: bool=False, addcmd: str=""): # requires git
257
+ os.makedirs(str(Path(BASE_DIR, path)), exist_ok=True)
258
+ os.chdir(Path(BASE_DIR, path))
259
+ print(f"Cloning {url} to {path}...")
260
+ cmd = f'git clone {url}'
261
+ print(f'Running {cmd} at {Path.cwd()}')
262
+ i = subprocess.run(cmd, shell=True).returncode
263
+ if i != 0: print(f'Error occured at running {cmd}')
264
+ p = url.split("/")[-1]
265
+ if not Path(p).exists: return
266
+ if pip:
267
+ os.chdir(Path(BASE_DIR, path, p))
268
+ cmd = f'pip install -r requirements.txt'
269
+ print(f'Running {cmd} at {Path.cwd()}')
270
+ i = subprocess.run(cmd, shell=True).returncode
271
+ if i != 0: print(f'Error occured at running {cmd}')
272
+ if addcmd:
273
+ os.chdir(Path(BASE_DIR, path, p))
274
+ cmd = addcmd
275
+ print(f'Running {cmd} at {Path.cwd()}')
276
+ i = subprocess.run(cmd, shell=True).returncode
277
+ if i != 0: print(f'Error occured at running {cmd}')
278
+
279
+
280
+ def run(cmd: str, timeout: float=0):
281
+ print(f'Running {cmd} at {Path.cwd()}')
282
+ if timeout == 0:
283
+ i = subprocess.run(cmd, shell=True).returncode
284
+ if i != 0: print(f'Error occured at running {cmd}')
285
+ else:
286
+ p = subprocess.Popen(cmd, shell=True)
287
+ time.sleep(timeout)
288
+ p.terminate()
289
+ print(f'Terminated in {timeout} seconds')