Favourites support.
Browse files
app.py
CHANGED
@@ -144,13 +144,43 @@ def get_files_by_model_id(model_id, api_key=None):
|
|
144 |
except requests.exceptions.RequestException as e:
|
145 |
raise gr.Error("Something went wrong in fetching CivitAI API")
|
146 |
|
147 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
if url.startswith("https://civitai.com/models/"):
|
149 |
model_id = url.split('/')[4]
|
150 |
(files, images) = get_files_by_model_id(model_id, api_key)
|
151 |
elif url.startswith("https://civitai.com/user/"):
|
152 |
username = url.split('/')[4]
|
153 |
-
(files, images) = get_files_by_username(username, api_key, nsfw, hidden)
|
|
|
|
|
154 |
else:
|
155 |
raise gr.Error("Unknown CivitAI URL format, please provide model URL or user profile URL")
|
156 |
|
@@ -268,7 +298,8 @@ def add_mirror(repo_id):
|
|
268 |
|
269 |
|
270 |
|
271 |
-
def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], oauth_token: gr.OAuthToken, url, destination_repo, civitai_api_key=None,
|
|
|
272 |
if not profile.name:
|
273 |
raise gr.Error("Are you sure you are logged in?")
|
274 |
|
@@ -279,6 +310,9 @@ def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], oauth_token: gr.OAuth
|
|
279 |
if not re.match(r'^[a-zA-Z0-9_-]+$', destination_repo):
|
280 |
raise gr.Error("Destination repository name must contain only alphanumeric characters, underscores, and hyphens")
|
281 |
|
|
|
|
|
|
|
282 |
# Convert bool flags.
|
283 |
nsfw = BOOLPARM[nsfw]
|
284 |
hidden = BOOLPARM[hidden]
|
@@ -299,7 +333,7 @@ def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], oauth_token: gr.OAuth
|
|
299 |
update_repo_visibility(repo_id=user_repo_id, private=False, token=oauth_token.token)
|
300 |
|
301 |
gr.Info(f"SBM get images upload {indimages}")
|
302 |
-
(files, images) = process_url(url, profile, user_repo_id, oauth_token.token, folder, civitai_api_key, nsfw, hidden, indimages)
|
303 |
if not files or len(files.keys()) == 0:
|
304 |
raise gr.Error("No files were copied. Something went wrong.")
|
305 |
|
@@ -380,6 +414,12 @@ Once uploaded, it will add this repository to CivitaiArchive.com as a mirror.
|
|
380 |
label="Hidden models",
|
381 |
info="Optional: Include, exclude or do not specify inclusion of hidden models.",
|
382 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
383 |
upload_images = gr.Checkbox(
|
384 |
value=True,
|
385 |
interactive=True, # Mandatory, otherwise disables the control.
|
@@ -395,7 +435,7 @@ Once uploaded, it will add this repository to CivitaiArchive.com as a mirror.
|
|
395 |
|
396 |
submit_button_civit.click(
|
397 |
fn=upload_civit_to_hf,
|
398 |
-
inputs=[submit_source_civit, destination_repo, civitai_api_key, include_nsfw, include_hidden, upload_images],
|
399 |
outputs=[output]
|
400 |
)
|
401 |
|
|
|
144 |
except requests.exceptions.RequestException as e:
|
145 |
raise gr.Error("Something went wrong in fetching CivitAI API")
|
146 |
|
147 |
+
def get_files_all(api_key=None, nsfw=None, hidden=None, favourites=None):
|
148 |
+
url = f"https://civitai.com/api/v1/models?limit=100"
|
149 |
+
output = {}
|
150 |
+
images = {}
|
151 |
+
headers = {}
|
152 |
+
if nsfw is not None:
|
153 |
+
url = url + f"&nsfw={BOOLPARM[nsfw]}"
|
154 |
+
if hidden is not None:
|
155 |
+
url = url + f"&hidden={BOOLPARM[hidden]}"
|
156 |
+
if favourites: # fav=false does nothing.
|
157 |
+
url = url + f"&favorites={BOOLPARM[favourites]}"
|
158 |
+
gr.Info(f"SBM url: {url}")
|
159 |
+
if api_key:
|
160 |
+
headers['Authorization'] = f'Bearer {api_key}'
|
161 |
+
|
162 |
+
while url:
|
163 |
+
response = requests.get(url, headers=headers, timeout=180)
|
164 |
+
data = response.json()
|
165 |
+
# Add current page items to the list
|
166 |
+
for model in data['items']:
|
167 |
+
(dfiles, dimages) = get_model_meta(model)
|
168 |
+
output.update(dfiles)
|
169 |
+
images.update(dimages)
|
170 |
+
|
171 |
+
metadata = data.get('metadata', {})
|
172 |
+
url = metadata.get('nextPage', None)
|
173 |
+
return (output, images)
|
174 |
+
|
175 |
+
def process_url(url, profile, user_repo_id, oauth_token, folder, api_key=None, nsfw=None, hidden=None, favourites=None, indimages = False):
|
176 |
if url.startswith("https://civitai.com/models/"):
|
177 |
model_id = url.split('/')[4]
|
178 |
(files, images) = get_files_by_model_id(model_id, api_key)
|
179 |
elif url.startswith("https://civitai.com/user/"):
|
180 |
username = url.split('/')[4]
|
181 |
+
(files, images) = get_files_by_username(username, api_key, nsfw, hidden, favourites)
|
182 |
+
elif favourites: # Can access all favourites if desired.
|
183 |
+
(files, images) = get_files_all(api_key, nsfw, hidden, favourites)
|
184 |
else:
|
185 |
raise gr.Error("Unknown CivitAI URL format, please provide model URL or user profile URL")
|
186 |
|
|
|
298 |
|
299 |
|
300 |
|
301 |
+
def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], oauth_token: gr.OAuthToken, url, destination_repo, civitai_api_key=None,
|
302 |
+
nsfw=None, hidden=None, favourites=None, indimages=False):
|
303 |
if not profile.name:
|
304 |
raise gr.Error("Are you sure you are logged in?")
|
305 |
|
|
|
310 |
if not re.match(r'^[a-zA-Z0-9_-]+$', destination_repo):
|
311 |
raise gr.Error("Destination repository name must contain only alphanumeric characters, underscores, and hyphens")
|
312 |
|
313 |
+
if favourites and not civitai_api_key:
|
314 |
+
raise gr.Error("You must provide an api key to access your favourites.")
|
315 |
+
|
316 |
# Convert bool flags.
|
317 |
nsfw = BOOLPARM[nsfw]
|
318 |
hidden = BOOLPARM[hidden]
|
|
|
333 |
update_repo_visibility(repo_id=user_repo_id, private=False, token=oauth_token.token)
|
334 |
|
335 |
gr.Info(f"SBM get images upload {indimages}")
|
336 |
+
(files, images) = process_url(url, profile, user_repo_id, oauth_token.token, folder, civitai_api_key, nsfw, hidden, favourites, indimages)
|
337 |
if not files or len(files.keys()) == 0:
|
338 |
raise gr.Error("No files were copied. Something went wrong.")
|
339 |
|
|
|
414 |
label="Hidden models",
|
415 |
info="Optional: Include, exclude or do not specify inclusion of hidden models.",
|
416 |
)
|
417 |
+
include_favorites = gr.Checkbox(
|
418 |
+
value=False,
|
419 |
+
interactive=True, # Mandatory, otherwise disables the control.
|
420 |
+
label="Favourite models",
|
421 |
+
info="Optional: Include only favourite (liked) models. This requires a hash key."
|
422 |
+
)
|
423 |
upload_images = gr.Checkbox(
|
424 |
value=True,
|
425 |
interactive=True, # Mandatory, otherwise disables the control.
|
|
|
435 |
|
436 |
submit_button_civit.click(
|
437 |
fn=upload_civit_to_hf,
|
438 |
+
inputs=[submit_source_civit, destination_repo, civitai_api_key, include_nsfw, include_hidden, include_favorites, upload_images],
|
439 |
outputs=[output]
|
440 |
)
|
441 |
|