John6666 commited on
Commit
420d105
·
verified ·
1 Parent(s): 211d865

Upload civitai_to_hf.py

Browse files
Files changed (1) hide show
  1. civitai_to_hf.py +8 -3
civitai_to_hf.py CHANGED
@@ -251,7 +251,8 @@ CIVITAI_BASEMODEL = ["Aura Flow", "CogVideoX", "Flux.1 D", "Flux.1 S", "HiDream"
251
  "SDXL 1.0 LCM", "SDXL Distilled", "SDXL Hyper", "SDXL Lightning", "SDXL Turbo", "SVD", "SVD XT", "Stable Cascade",
252
  "Wan Video", "VPRED"]
253
  #CIVITAI_SORT = ["Highest Rated", "Most Downloaded", "Newest"]
254
- CIVITAI_SORT = ["Highest Rated", "Most Downloaded", "Most Liked", "Most Discussed", "Most Collected", "Most Buzz", "Newest"]
 
255
  CIVITAI_PERIOD = ["AllTime", "Year", "Month", "Week", "Day"]
256
 
257
 
@@ -262,7 +263,7 @@ def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [],
262
  headers = {'User-Agent': user_agent, 'content-type': 'application/json'}
263
  if api_key: headers['Authorization'] = f'Bearer {{{api_key}}}'
264
  base_url = 'https://civitai.com/api/v1/models'
265
- params = {'sort': sort, 'period': period, 'limit': int(limit), 'nsfw': 'true'}
266
  if len(types) != 0: params["types"] = types
267
  if query: params["query"] = query
268
  if tag: params["tag"] = tag
@@ -327,11 +328,15 @@ def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [],
327
  for f in model['files']:
328
  i = item.copy()
329
  i['dl_url'] = f['downloadUrl']
 
330
  if len(filetype) != 0 and f['type'] not in set(filetype): continue
331
  items.append(i)
332
  else:
333
  item['dl_url'] = model['downloadUrl']
334
  items.append(item)
 
 
 
335
  return items if len(items) > 0 else None
336
 
337
 
@@ -349,7 +354,7 @@ def search_civitai(query, types, base_model=[], sort=CIVITAI_SORT[0], period=CIV
349
  gallery = []
350
  for item in items:
351
  base_model_name = "Pony🐴" if item['base_model'] == "Pony" else item['base_model']
352
- name = f"{item['name']} (for {base_model_name} / By: {item['creator']})"
353
  value = item['dl_url']
354
  choices.append((name, value))
355
  gallery.append((item['img_url'], name))
 
251
  "SDXL 1.0 LCM", "SDXL Distilled", "SDXL Hyper", "SDXL Lightning", "SDXL Turbo", "SVD", "SVD XT", "Stable Cascade",
252
  "Wan Video", "VPRED"]
253
  #CIVITAI_SORT = ["Highest Rated", "Most Downloaded", "Newest"]
254
+ CIVITAI_SORT_EXT = ["Size", "Size (from smallest)"]
255
+ CIVITAI_SORT = ["Highest Rated", "Most Downloaded", "Most Liked", "Most Discussed", "Most Collected", "Most Buzz", "Newest"] + CIVITAI_SORT_EXT
256
  CIVITAI_PERIOD = ["AllTime", "Year", "Month", "Week", "Day"]
257
 
258
 
 
263
  headers = {'User-Agent': user_agent, 'content-type': 'application/json'}
264
  if api_key: headers['Authorization'] = f'Bearer {{{api_key}}}'
265
  base_url = 'https://civitai.com/api/v1/models'
266
+ params = {'sort': CIVITAI_SORT[0] if sort in CIVITAI_SORT_EXT else sort, 'period': period, 'limit': int(limit), 'nsfw': 'true'}
267
  if len(types) != 0: params["types"] = types
268
  if query: params["query"] = query
269
  if tag: params["tag"] = tag
 
328
  for f in model['files']:
329
  i = item.copy()
330
  i['dl_url'] = f['downloadUrl']
331
+ i['size_kb'] = f['sizeKB']
332
  if len(filetype) != 0 and f['type'] not in set(filetype): continue
333
  items.append(i)
334
  else:
335
  item['dl_url'] = model['downloadUrl']
336
  items.append(item)
337
+ if sort in CIVITAI_SORT_EXT:
338
+ if sort == "Size": items = sorted(items, key=lambda x: x.get('size_kb', 0.), reverse=True)
339
+ elif sort == "Size (from smallest)": items = sorted(items, key=lambda x: x.get('size_kb', 0.))
340
  return items if len(items) > 0 else None
341
 
342
 
 
354
  gallery = []
355
  for item in items:
356
  base_model_name = "Pony🐴" if item['base_model'] == "Pony" else item['base_model']
357
+ name = f"{item['name']} (for {base_model_name} / By: {item['creator']}) ({round(item['size_kb'] / 1000., 2)}MB)" if "size_kb" in item.keys() else f"{item['name']} (for {base_model_name} / By: {item['creator']})"
358
  value = item['dl_url']
359
  choices.append((name, value))
360
  gallery.append((item['img_url'], name))