Commit
·
9a029ca
1
Parent(s):
f0bc74b
Add civitai-to-hf-uploader
Browse files
app.py
CHANGED
|
@@ -17,19 +17,20 @@ def restart_space():
|
|
| 17 |
api.restart_space(repo_id="civitaiarchive/civitai-to-hf-uploader", token=os.environ["HF_TOKEN"])
|
| 18 |
|
| 19 |
|
| 20 |
-
def download_file(url, file_path, folder):
|
| 21 |
headers = {}
|
| 22 |
full_path = os.path.join(folder, file_path)
|
| 23 |
os.makedirs(os.path.dirname(full_path), exist_ok=True)
|
| 24 |
|
| 25 |
-
curl_cmd = ['curl', '-L', '-o', full_path, url]
|
| 26 |
|
| 27 |
try:
|
| 28 |
result = subprocess.run(curl_cmd, check=True, capture_output=True, text=True)
|
| 29 |
except subprocess.CalledProcessError as e:
|
| 30 |
-
if b'401' in e.stderr:
|
| 31 |
# Try again with authorization
|
| 32 |
-
|
|
|
|
| 33 |
try:
|
| 34 |
result = subprocess.run(curl_cmd, check=True, capture_output=True, text=True)
|
| 35 |
except subprocess.CalledProcessError as e:
|
|
@@ -87,7 +88,7 @@ def get_files_by_model_id(model_id):
|
|
| 87 |
except requests.exceptions.RequestException as e:
|
| 88 |
raise gr.Error("Something went wrong in fetching CivitAI API")
|
| 89 |
|
| 90 |
-
def process_url(url, profile, user_repo_id, oauth_token, folder):
|
| 91 |
if url.startswith("https://civitai.com/models/"):
|
| 92 |
model_id = url.split('/')[4]
|
| 93 |
files = get_files_by_model_id(model_id)
|
|
@@ -108,7 +109,7 @@ def process_url(url, profile, user_repo_id, oauth_token, folder):
|
|
| 108 |
download_url = data['downloadUrl']
|
| 109 |
filename = dl_path.split('/')[-1]
|
| 110 |
gr.Info(f"Downloading {filename} ({current_file}/{total_files})")
|
| 111 |
-
download_file(download_url, dl_path, folder)
|
| 112 |
# Upload the model and card
|
| 113 |
gr.Info(f"Uploading {filename} ({current_file}/{total_files})")
|
| 114 |
base_folder = os.path.join(folder, os.path.dirname(dl_path))
|
|
@@ -158,7 +159,7 @@ def add_mirror(repo_id):
|
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
-
def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], oauth_token: gr.OAuthToken, url, destination_repo):
|
| 162 |
if not profile.name:
|
| 163 |
raise gr.Error("Are you sure you are logged in?")
|
| 164 |
|
|
@@ -184,7 +185,7 @@ def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], oauth_token: gr.OAuth
|
|
| 184 |
gr.Info(f"Repository {user_repo_id} already exists, will update it")
|
| 185 |
update_repo_visibility(repo_id=user_repo_id, private=False, token=oauth_token.token)
|
| 186 |
|
| 187 |
-
files = process_url(url, profile, user_repo_id, oauth_token.token, folder)
|
| 188 |
if not files or len(files.keys()) == 0:
|
| 189 |
raise gr.Error("No files were copied. Something went wrong.")
|
| 190 |
|
|
@@ -246,6 +247,11 @@ Once uploaded, it will add this repository to CivitaiArchive.com as a mirror.
|
|
| 246 |
label="HF Repo Name",
|
| 247 |
info="Name for the HuggingFace repository (a new one will be created if it doesn't exist)",
|
| 248 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
instructions = gr.HTML("")
|
| 251 |
submit_button_civit = gr.Button("Upload to Hugging Face", interactive=True)
|
|
@@ -255,7 +261,7 @@ Once uploaded, it will add this repository to CivitaiArchive.com as a mirror.
|
|
| 255 |
|
| 256 |
submit_button_civit.click(
|
| 257 |
fn=upload_civit_to_hf,
|
| 258 |
-
inputs=[submit_source_civit, destination_repo],
|
| 259 |
outputs=[output]
|
| 260 |
)
|
| 261 |
|
|
|
|
| 17 |
api.restart_space(repo_id="civitaiarchive/civitai-to-hf-uploader", token=os.environ["HF_TOKEN"])
|
| 18 |
|
| 19 |
|
| 20 |
+
def download_file(url, file_path, folder, api_key=None):
|
| 21 |
headers = {}
|
| 22 |
full_path = os.path.join(folder, file_path)
|
| 23 |
os.makedirs(os.path.dirname(full_path), exist_ok=True)
|
| 24 |
|
| 25 |
+
curl_cmd = ['curl', '--fail', '-L', '-o', full_path, url]
|
| 26 |
|
| 27 |
try:
|
| 28 |
result = subprocess.run(curl_cmd, check=True, capture_output=True, text=True)
|
| 29 |
except subprocess.CalledProcessError as e:
|
| 30 |
+
if b'401' in e.stderr or b'403' in e.stderr:
|
| 31 |
# Try again with authorization
|
| 32 |
+
auth_key = api_key or os.environ.get("CIVITAI_API_KEY")
|
| 33 |
+
curl_cmd.extend(['-H', f'Authorization: Bearer {auth_key}'])
|
| 34 |
try:
|
| 35 |
result = subprocess.run(curl_cmd, check=True, capture_output=True, text=True)
|
| 36 |
except subprocess.CalledProcessError as e:
|
|
|
|
| 88 |
except requests.exceptions.RequestException as e:
|
| 89 |
raise gr.Error("Something went wrong in fetching CivitAI API")
|
| 90 |
|
| 91 |
+
def process_url(url, profile, user_repo_id, oauth_token, folder, api_key=None):
|
| 92 |
if url.startswith("https://civitai.com/models/"):
|
| 93 |
model_id = url.split('/')[4]
|
| 94 |
files = get_files_by_model_id(model_id)
|
|
|
|
| 109 |
download_url = data['downloadUrl']
|
| 110 |
filename = dl_path.split('/')[-1]
|
| 111 |
gr.Info(f"Downloading {filename} ({current_file}/{total_files})")
|
| 112 |
+
download_file(download_url, dl_path, folder, api_key)
|
| 113 |
# Upload the model and card
|
| 114 |
gr.Info(f"Uploading {filename} ({current_file}/{total_files})")
|
| 115 |
base_folder = os.path.join(folder, os.path.dirname(dl_path))
|
|
|
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
+
def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], oauth_token: gr.OAuthToken, url, destination_repo, civitai_api_key=None):
|
| 163 |
if not profile.name:
|
| 164 |
raise gr.Error("Are you sure you are logged in?")
|
| 165 |
|
|
|
|
| 185 |
gr.Info(f"Repository {user_repo_id} already exists, will update it")
|
| 186 |
update_repo_visibility(repo_id=user_repo_id, private=False, token=oauth_token.token)
|
| 187 |
|
| 188 |
+
files = process_url(url, profile, user_repo_id, oauth_token.token, folder, civitai_api_key)
|
| 189 |
if not files or len(files.keys()) == 0:
|
| 190 |
raise gr.Error("No files were copied. Something went wrong.")
|
| 191 |
|
|
|
|
| 247 |
label="HF Repo Name",
|
| 248 |
info="Name for the HuggingFace repository (a new one will be created if it doesn't exist)",
|
| 249 |
)
|
| 250 |
+
civitai_api_key = gr.Textbox(
|
| 251 |
+
placeholder="Your CivitAI API key (optional)",
|
| 252 |
+
label="CivitAI API Key",
|
| 253 |
+
info="Optional: Provide your own CivitAI API key to avoid rate limits. If not provided, a default key will be used.",
|
| 254 |
+
)
|
| 255 |
|
| 256 |
instructions = gr.HTML("")
|
| 257 |
submit_button_civit = gr.Button("Upload to Hugging Face", interactive=True)
|
|
|
|
| 261 |
|
| 262 |
submit_button_civit.click(
|
| 263 |
fn=upload_civit_to_hf,
|
| 264 |
+
inputs=[submit_source_civit, destination_repo, civitai_api_key],
|
| 265 |
outputs=[output]
|
| 266 |
)
|
| 267 |
|