Upload 8 files
Browse files- requirements.txt +5 -2
- utils.py +184 -56
requirements.txt
CHANGED
@@ -3,5 +3,8 @@ safetensors
|
|
3 |
huggingface-hub
|
4 |
accelerate
|
5 |
diffusers
|
6 |
-
transformers
|
7 |
-
peft
|
|
|
|
|
|
|
|
3 |
huggingface-hub
|
4 |
accelerate
|
5 |
diffusers
|
6 |
+
transformers<=4.49.0
|
7 |
+
peft
|
8 |
+
hf_xet
|
9 |
+
hf_transfer
|
10 |
+
pydantic==2.10.6
|
utils.py
CHANGED
@@ -1,11 +1,14 @@
|
|
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():
|
@@ -23,6 +26,17 @@ def set_token(token):
|
|
23 |
print(f"Error: Failed to save token.")
|
24 |
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
def get_user_agent():
|
27 |
return 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0'
|
28 |
|
@@ -63,20 +77,33 @@ def get_model_type(repo_id: str):
|
|
63 |
return default
|
64 |
|
65 |
|
|
|
|
|
|
|
|
|
66 |
def list_sub(a, b):
|
67 |
return [e for e in a if e not in b]
|
68 |
|
69 |
|
70 |
def is_repo_name(s):
|
71 |
-
return re.fullmatch(r'^[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
|
74 |
def split_hf_url(url: str):
|
75 |
try:
|
76 |
-
s = list(re.findall(r'^(?:https?://huggingface.co/)(?:(datasets)/)?(.+?/.+?)/\w+?/.+?/(?:(.+)/)?(
|
77 |
if len(s) < 4: return "", "", "", ""
|
78 |
repo_id = s[1]
|
79 |
-
|
|
|
|
|
80 |
subfolder = urllib.parse.unquote(s[2]) if s[2] else None
|
81 |
filename = urllib.parse.unquote(s[3])
|
82 |
return repo_id, filename, subfolder, repo_type
|
@@ -88,74 +115,175 @@ def download_hf_file(directory, url, progress=gr.Progress(track_tqdm=True)):
|
|
88 |
hf_token = get_token()
|
89 |
repo_id, filename, subfolder, repo_type = split_hf_url(url)
|
90 |
try:
|
91 |
-
|
92 |
-
|
|
|
|
|
93 |
except Exception as e:
|
94 |
print(f"Failed to download: {e}")
|
|
|
95 |
|
96 |
|
97 |
def download_thing(directory, url, civitai_api_key="", progress=gr.Progress(track_tqdm=True)): # requires aria2, gdown
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
url = url.replace("/blob/", "/resolve/")
|
109 |
-
#user_header = f'"Authorization: Bearer {hf_token}"'
|
110 |
-
if hf_token:
|
111 |
download_hf_file(directory, url)
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
else:
|
114 |
-
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]}")
|
115 |
-
elif "civitai.com" in url:
|
116 |
-
if "?" in url:
|
117 |
-
url = url.split("?")[0]
|
118 |
-
if civitai_api_key:
|
119 |
-
url = url + f"?token={civitai_api_key}"
|
120 |
os.system(f"aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -d {directory} {url}")
|
121 |
-
|
122 |
-
|
123 |
-
else:
|
124 |
-
os.system(f"aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -d {directory} {url}")
|
125 |
|
126 |
|
127 |
-
def
|
128 |
-
|
129 |
-
valid_extensions = ('.safetensors')
|
130 |
for file in Path(dir_path).glob("**/*.*"):
|
131 |
-
if file.is_file()
|
132 |
file_path = str(file)
|
133 |
-
|
134 |
-
return
|
135 |
|
136 |
|
137 |
def get_download_file(temp_dir, url, civitai_key, progress=gr.Progress(track_tqdm=True)):
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
download_thing(temp_dir, url.strip(), civitai_key)
|
152 |
-
|
|
|
|
|
153 |
print(f"Download failed: {url}")
|
154 |
return ""
|
155 |
-
|
156 |
-
new_file
|
157 |
-
|
158 |
-
print(f"Download failed: {url}")
|
159 |
return ""
|
160 |
-
|
161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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():
|
|
|
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 |
|
|
|
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
|
|
|
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')
|