Spaces:
Running
Running
Upload convert_url_to_diffusers_sdxl_gr.py
Browse files
convert_url_to_diffusers_sdxl_gr.py
CHANGED
@@ -16,7 +16,7 @@ def is_repo_name(s):
|
|
16 |
return re.fullmatch(r'^[^/,\s\"\']+/[^/,\s\"\']+$', s)
|
17 |
|
18 |
|
19 |
-
def download_thing(directory, url, civitai_api_key="", progress=gr.Progress(track_tqdm=True)):
|
20 |
url = url.strip()
|
21 |
if "drive.google.com" in url:
|
22 |
original_dir = os.getcwd()
|
@@ -27,9 +27,11 @@ def download_thing(directory, url, civitai_api_key="", progress=gr.Progress(trac
|
|
27 |
url = url.replace("?download=true", "")
|
28 |
if "/blob/" in url:
|
29 |
url = url.replace("/blob/", "/resolve/")
|
30 |
-
|
|
|
|
|
31 |
else:
|
32 |
-
os.system
|
33 |
elif "civitai.com" in url:
|
34 |
if "?" in url:
|
35 |
url = url.split("?")[0]
|
@@ -52,7 +54,7 @@ def get_local_model_list(dir_path):
|
|
52 |
return model_list
|
53 |
|
54 |
|
55 |
-
def get_download_file(temp_dir, url, civitai_key, progress=gr.Progress(track_tqdm=True)):
|
56 |
if not "http" in url and is_repo_name(url) and not Path(url).exists():
|
57 |
print(f"Use HF Repo: {url}")
|
58 |
new_file = url
|
@@ -66,7 +68,7 @@ def get_download_file(temp_dir, url, civitai_key, progress=gr.Progress(track_tqd
|
|
66 |
print(f"Start downloading: {url}")
|
67 |
before = get_local_model_list(temp_dir)
|
68 |
try:
|
69 |
-
download_thing(temp_dir, url.strip(), civitai_key)
|
70 |
except Exception:
|
71 |
print(f"Download failed: {url}")
|
72 |
return ""
|
@@ -215,11 +217,11 @@ def fuse_loras(pipe, lora_dict={}, temp_dir=".", civitai_key=""):
|
|
215 |
pipe.unload_lora_weights()
|
216 |
|
217 |
|
218 |
-
def convert_url_to_diffusers_sdxl(url, civitai_key="", is_upload_sf=False, half=True, vae=None,
|
219 |
scheduler="Euler a", lora_dict={}, is_local=True, progress=gr.Progress(track_tqdm=True)):
|
220 |
progress(0, desc="Start converting...")
|
221 |
temp_dir = "."
|
222 |
-
new_file = get_download_file(temp_dir, url, civitai_key)
|
223 |
if not new_file:
|
224 |
print(f"Not found: {url}")
|
225 |
return ""
|
@@ -273,15 +275,15 @@ def convert_url_to_diffusers_sdxl(url, civitai_key="", is_upload_sf=False, half=
|
|
273 |
return new_repo_name
|
274 |
|
275 |
|
276 |
-
def is_repo_exists(repo_id):
|
277 |
from huggingface_hub import HfApi
|
278 |
-
api = HfApi()
|
279 |
try:
|
280 |
if api.repo_exists(repo_id=repo_id): return True
|
281 |
else: return False
|
282 |
except Exception as e:
|
283 |
print(e)
|
284 |
-
print(f"Error: Failed to connect {repo_id}.
|
285 |
return True # for safe
|
286 |
|
287 |
|
@@ -315,10 +317,9 @@ def convert_url_to_diffusers_repo(dl_url, hf_user, hf_repo, hf_token, civitai_ke
|
|
315 |
print(f"Invalid user name: {hf_user}")
|
316 |
progress(1, desc=f"Invalid user name: {hf_user}")
|
317 |
return gr.update(value=repo_urls, choices=repo_urls), gr.update(value="")
|
318 |
-
if hf_token and not os.environ.get("HF_TOKEN"): os.environ['HF_TOKEN'] = hf_token
|
319 |
if not civitai_key and os.environ.get("CIVITAI_API_KEY"): civitai_key = os.environ.get("CIVITAI_API_KEY")
|
320 |
lora_dict = {lora1: lora1s, lora2: lora2s, lora3: lora3s, lora4: lora4s, lora5: lora5s}
|
321 |
-
new_path = convert_url_to_diffusers_sdxl(dl_url, civitai_key, is_upload_sf, half, vae, scheduler, lora_dict, False)
|
322 |
if not new_path: return ""
|
323 |
new_repo_id = f"{hf_user}/{Path(new_path).stem}"
|
324 |
if hf_repo != "": new_repo_id = f"{hf_user}/{hf_repo}"
|
@@ -326,7 +327,7 @@ def convert_url_to_diffusers_repo(dl_url, hf_user, hf_repo, hf_token, civitai_ke
|
|
326 |
print(f"Invalid repo name: {new_repo_id}")
|
327 |
progress(1, desc=f"Invalid repo name: {new_repo_id}")
|
328 |
return gr.update(value=repo_urls, choices=repo_urls), gr.update(value="")
|
329 |
-
if is_repo_exists(new_repo_id):
|
330 |
print(f"Repo already exists: {new_repo_id}")
|
331 |
progress(1, desc=f"Repo already exists: {new_repo_id}")
|
332 |
return gr.update(value=repo_urls, choices=repo_urls), gr.update(value="")
|
|
|
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:
|
22 |
original_dir = os.getcwd()
|
|
|
27 |
url = url.replace("?download=true", "")
|
28 |
if "/blob/" in url:
|
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:
|
36 |
if "?" in url:
|
37 |
url = url.split("?")[0]
|
|
|
54 |
return model_list
|
55 |
|
56 |
|
57 |
+
def get_download_file(temp_dir, url, civitai_key, hf_token, progress=gr.Progress(track_tqdm=True)):
|
58 |
if not "http" in url and is_repo_name(url) and not Path(url).exists():
|
59 |
print(f"Use HF Repo: {url}")
|
60 |
new_file = url
|
|
|
68 |
print(f"Start downloading: {url}")
|
69 |
before = get_local_model_list(temp_dir)
|
70 |
try:
|
71 |
+
download_thing(temp_dir, url.strip(), civitai_key, hf_token)
|
72 |
except Exception:
|
73 |
print(f"Download failed: {url}")
|
74 |
return ""
|
|
|
217 |
pipe.unload_lora_weights()
|
218 |
|
219 |
|
220 |
+
def convert_url_to_diffusers_sdxl(url, civitai_key="", hf_token="", is_upload_sf=False, half=True, vae=None,
|
221 |
scheduler="Euler a", lora_dict={}, is_local=True, progress=gr.Progress(track_tqdm=True)):
|
222 |
progress(0, desc="Start converting...")
|
223 |
temp_dir = "."
|
224 |
+
new_file = get_download_file(temp_dir, url, civitai_key, hf_token)
|
225 |
if not new_file:
|
226 |
print(f"Not found: {url}")
|
227 |
return ""
|
|
|
275 |
return new_repo_name
|
276 |
|
277 |
|
278 |
+
def is_repo_exists(repo_id, hf_token):
|
279 |
from huggingface_hub import HfApi
|
280 |
+
api = HfApi(token=hf_token)
|
281 |
try:
|
282 |
if api.repo_exists(repo_id=repo_id): return True
|
283 |
else: return False
|
284 |
except Exception as e:
|
285 |
print(e)
|
286 |
+
print(f"Error: Failed to connect {repo_id}.")
|
287 |
return True # for safe
|
288 |
|
289 |
|
|
|
317 |
print(f"Invalid user name: {hf_user}")
|
318 |
progress(1, desc=f"Invalid user name: {hf_user}")
|
319 |
return gr.update(value=repo_urls, choices=repo_urls), gr.update(value="")
|
|
|
320 |
if not civitai_key and os.environ.get("CIVITAI_API_KEY"): civitai_key = os.environ.get("CIVITAI_API_KEY")
|
321 |
lora_dict = {lora1: lora1s, lora2: lora2s, lora3: lora3s, lora4: lora4s, lora5: lora5s}
|
322 |
+
new_path = convert_url_to_diffusers_sdxl(dl_url, civitai_key, hf_token, is_upload_sf, half, vae, scheduler, lora_dict, False)
|
323 |
if not new_path: return ""
|
324 |
new_repo_id = f"{hf_user}/{Path(new_path).stem}"
|
325 |
if hf_repo != "": new_repo_id = f"{hf_user}/{hf_repo}"
|
|
|
327 |
print(f"Invalid repo name: {new_repo_id}")
|
328 |
progress(1, desc=f"Invalid repo name: {new_repo_id}")
|
329 |
return gr.update(value=repo_urls, choices=repo_urls), gr.update(value="")
|
330 |
+
if is_repo_exists(new_repo_id, hf_token):
|
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="")
|