Spaces:
Running on Zero
Running on Zero
Update utils.py
Browse files274 unmodified lines2752762772782792802812822832842857 unmodified lines29329429529629729829930030130230330430530630730830931031131231331431531631731831934 unmodified lines3543553563573583593603613623636 unmodified lines37037137237337437537616 unmodified lines393394395396397398399274 unmodified lineselif os.path.exists(os.path.join(dir_, filename_base)):return os.path.join(dir_, filename_base), filename_basearia2_command = (f'aria2c --console-log-level=error --summary-interval=10 -c -x 16 'f'-k 1M -s 16 -d "{dir_}" -o "{filename_base}" "{redirect_url}"')r_code = os.system(aria2_command) # noqa# if r_code != 0:# raise RuntimeError(f"Failed to download file: {filename_base}. Error code: {r_code}")7 unmodified linesdef civ_api_down(url, dir_, civitai_api_key, civ_filename):"""This method is susceptible to being blocked because it generates a lot of temp redirect links with aria2c.If an API key limit is reached, generating a new API key and using it can fix the issue."""output_path = Noneurl_dl = url + f"?token={civitai_api_key}"if not civ_filename:aria2_command = f'aria2c -c -x 1 -s 1 -d "{dir_}" "{url_dl}"'os.system(aria2_command)else:output_path = os.path.join(dir_, civ_filename)if not os.path.exists(output_path):aria2_command = (f'aria2c --console-log-level=error --summary-interval=10 -c -x 16 'f'-k 1M -s 16 -d "{dir_}" -o "{civ_filename}" "{url_dl}"')os.system(aria2_command)return output_pathdef drive_down(url, dir_):import gdown34 unmodified linesurl = url.replace("/blob/", "/resolve/")if hf_token:user_header = f'"Authorization: Bearer {hf_token}"'os.system(f"aria2c --console-log-level=error --summary-interval=10 --header={user_header} -c -x 16 -k 1M -s 16 {url} -d {dir_} -o {filename}")else:os.system(f"aria2c --optimize-concurrent-downloads --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 {url} -d {dir_} -o {filename}")return output_path6 unmodified linesdownloaded_file_path = drive_down(url, directory)elif "huggingface.co" in url:downloaded_file_path = hf_down(url, directory, hf_token, romanize)elif "civitai.com" in url:if not civitai_api_key:msg = "You need an API key to download Civitai models."print(f"\033[91m{msg}\033[0m")16 unmodified linesgr.Warning(msg)downloaded_file_path = civ_api_down(url, directory, civitai_api_key, civ_filename)else:os.system(f"aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -d {directory} {url}")return downloaded_file_path274 unmodified lines2752762772782792802812822832842857 unmodified lines29329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332434 unmodified lines3593603613623633643653663673683693703713723733746 unmodified lines38138238338438538638738816 unmodified lines405406407408409410411412413414274 unmodified lineselif os.path.exists(os.path.join(dir_, filename_base)):return os.path.join(dir_, filename_base), filename_basewget_command = (f'wget -c -nv 'f'-O "{os.path.join(dir_, filename_base)}" "{redirect_url}"')r_code = os.system(wget_command) # noqa# if r_code != 0:# raise RuntimeError(f"Failed to download file: {filename_base}. Error code: {r_code}")7 unmodified linesdef civ_api_down(url, dir_, civitai_api_key, civ_filename):"""This method is susceptible to being blocked because it generates a lot of temp redirect links with wget.If an API key limit is reached, generating a new API key and using it can fix the issue. no"""output_path = Noneurl_dl = url + f"?token={civitai_api_key}"if not civ_filename:wget_command = (f'wget -c -nv 'f'-P "{dir_}" "{url_dl}"')os.system(wget_command)else:output_path = os.path.join(dir_, civ_filename)if not os.path.exists(output_path):wget_command = (f'wget -c -nv 'f'-O "{output_path}" "{url_dl}"')os.system(wget_command)return output_pathdef drive_down(url, dir_):import gdown34 unmodified linesurl = url.replace("/blob/", "/resolve/")if hf_token:os.system(f'wget -c -nv 'f'--header="Authorization: Bearer {hf_token}" 'f'-O "{os.path.join(dir_, filename)}" "{url}"')else:os.system(f'wget -c -nv 'f'-O "{os.path.join(dir_, filename)}" "{url}"')return output_path6 unmodified linesdownloaded_file_path = drive_down(url, directory)elif "huggingface.co" in url:downloaded_file_path = hf_down(url, directory, hf_token, romanize)elif "civitai." in url:url = url.replace("civitai.red", "civitai.com")if not civitai_api_key:msg = "You need an API key to download Civitai models."print(f"\033[91m{msg}\033[0m")16 unmodified linesgr.Warning(msg)downloaded_file_path = civ_api_down(url, directory, civitai_api_key, civ_filename)else:os.system(f'wget -c -nv 'f'-P "{directory}" "{url}"')return downloaded_file_path
utils.py+35-20