Spaces:
Running
Running
Upload 2 files
Browse files- app.py +1 -1
- civitai_to_hf.py +13 -10
app.py
CHANGED
|
@@ -79,7 +79,7 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
|
|
| 79 |
fn=search_civitai,
|
| 80 |
inputs=[search_civitai_query, search_civitai_type, search_civitai_basemodel, search_civitai_sort,
|
| 81 |
search_civitai_period, search_civitai_tag, search_civitai_user, search_civitai_limit,
|
| 82 |
-
search_civitai_page, search_civitai_filetype, search_civitai_gallery, state],
|
| 83 |
outputs=[search_civitai_result, search_civitai_desc, search_civitai_submit, search_civitai_query, search_civitai_gallery,
|
| 84 |
search_civitai_result_checkbox, search_civitai_result_info, state],
|
| 85 |
queue=False,
|
|
|
|
| 79 |
fn=search_civitai,
|
| 80 |
inputs=[search_civitai_query, search_civitai_type, search_civitai_basemodel, search_civitai_sort,
|
| 81 |
search_civitai_period, search_civitai_tag, search_civitai_user, search_civitai_limit,
|
| 82 |
+
search_civitai_page, search_civitai_filetype, civitai_key, search_civitai_gallery, state],
|
| 83 |
outputs=[search_civitai_result, search_civitai_desc, search_civitai_submit, search_civitai_query, search_civitai_gallery,
|
| 84 |
search_civitai_result_checkbox, search_civitai_result_info, state],
|
| 85 |
queue=False,
|
civitai_to_hf.py
CHANGED
|
@@ -108,8 +108,8 @@ def download_file(dl_url, civitai_key, progress=gr.Progress(track_tqdm=True)):
|
|
| 108 |
return output_filename
|
| 109 |
|
| 110 |
|
| 111 |
-
def save_civitai_info(dl_url, filename, progress=gr.Progress(track_tqdm=True)):
|
| 112 |
-
json_str, html_str, image_path = get_civitai_json(dl_url, True, filename)
|
| 113 |
if not json_str: return "", "", ""
|
| 114 |
json_path = str(Path(TEMP_DIR, Path(filename).stem + ".json"))
|
| 115 |
html_path = str(Path(TEMP_DIR, Path(filename).stem + ".html"))
|
|
@@ -124,7 +124,7 @@ def save_civitai_info(dl_url, filename, progress=gr.Progress(track_tqdm=True)):
|
|
| 124 |
return "", "", ""
|
| 125 |
|
| 126 |
|
| 127 |
-
def upload_info_to_repo(dl_url, filename, repo_id, repo_type, is_private, progress=gr.Progress(track_tqdm=True)):
|
| 128 |
def upload_file(api, filename, repo_id, repo_type, hf_token):
|
| 129 |
if not Path(filename).exists(): return
|
| 130 |
api.upload_file(path_or_fileobj=filename, path_in_repo=Path(filename).name, repo_type=repo_type, revision="main", token=hf_token, repo_id=repo_id)
|
|
@@ -135,7 +135,7 @@ def upload_info_to_repo(dl_url, filename, repo_id, repo_type, is_private, progre
|
|
| 135 |
try:
|
| 136 |
if not is_repo_exists(repo_id, repo_type): api.create_repo(repo_id=repo_id, repo_type=repo_type, token=hf_token, private=is_private)
|
| 137 |
progress(0, desc=f"Downloading info... {filename}")
|
| 138 |
-
json_path, html_path, image_path = save_civitai_info(dl_url, filename)
|
| 139 |
progress(0, desc=f"Start uploading info... {filename} to {repo_id}")
|
| 140 |
if not json_path: return
|
| 141 |
else: upload_file(api, json_path, repo_id, repo_type, hf_token)
|
|
@@ -167,7 +167,7 @@ def download_civitai(dl_url, civitai_key, hf_token, urls,
|
|
| 167 |
if is_rename: file = get_safe_filename(file, newrepo_id, repo_type)
|
| 168 |
url = upload_safetensors_to_repo(file, newrepo_id, repo_type, is_private)
|
| 169 |
if url:
|
| 170 |
-
if is_info: upload_info_to_repo(u, file, newrepo_id, repo_type, is_private)
|
| 171 |
urls.append(url)
|
| 172 |
remain_urls.remove(u)
|
| 173 |
md += f"- Uploaded [{str(u)}]({str(u)})\n"
|
|
@@ -187,15 +187,17 @@ CIVITAI_TYPE = ["Checkpoint", "TextualInversion", "Hypernetwork", "AestheticGrad
|
|
| 187 |
"Controlnet", "Upscaler", "MotionModule", "VAE", "Poses", "Wildcards", "Workflows", "Other"]
|
| 188 |
CIVITAI_FILETYPE = ["Model", "VAE", "Config", "Training Data"]
|
| 189 |
CIVITAI_BASEMODEL = ["Pony", "Illustrious", "SDXL 1.0", "SD 1.5", "Flux.1 D", "Flux.1 S", "SD 3.5"]
|
| 190 |
-
CIVITAI_SORT = ["Highest Rated", "Most Downloaded", "Newest"]
|
|
|
|
| 191 |
CIVITAI_PERIOD = ["AllTime", "Year", "Month", "Week", "Day"]
|
| 192 |
|
| 193 |
|
| 194 |
def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [], limit: int = 100,
|
| 195 |
sort: str = "Highest Rated", period: str = "AllTime", tag: str = "", user: str = "", page: int = 1,
|
| 196 |
-
filetype: list[str] = [], progress=gr.Progress(track_tqdm=True)):
|
| 197 |
user_agent = get_user_agent()
|
| 198 |
headers = {'User-Agent': user_agent, 'content-type': 'application/json'}
|
|
|
|
| 199 |
base_url = 'https://civitai.com/api/v1/models'
|
| 200 |
params = {'sort': sort, 'period': period, 'limit': int(limit), 'nsfw': 'true'}
|
| 201 |
if len(types) != 0: params["types"] = types
|
|
@@ -271,13 +273,13 @@ def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [],
|
|
| 271 |
|
| 272 |
|
| 273 |
def search_civitai(query, types, base_model=[], sort=CIVITAI_SORT[0], period=CIVITAI_PERIOD[0], tag="", user="", limit=100, page=1,
|
| 274 |
-
filetype=[], gallery=[], state={}, progress=gr.Progress(track_tqdm=True)):
|
| 275 |
civitai_last_results = {}
|
| 276 |
set_state(state, "civitai_last_choices", [("", "")])
|
| 277 |
set_state(state, "civitai_last_gallery", [])
|
| 278 |
set_state(state, "civitai_last_results", civitai_last_results)
|
| 279 |
results_info = "No item found."
|
| 280 |
-
items = search_on_civitai(query, types, base_model, int(limit), sort, period, tag, user, int(page), filetype)
|
| 281 |
if not items: return gr.update(choices=[("", "")], value=[], visible=True),\
|
| 282 |
gr.update(value="", visible=False), gr.update(), gr.update(), gr.update(), gr.update(), results_info, state
|
| 283 |
choices = []
|
|
@@ -299,12 +301,13 @@ def search_civitai(query, types, base_model=[], sort=CIVITAI_SORT[0], period=CIV
|
|
| 299 |
gr.update(), gr.update(), gr.update(value=gallery), gr.update(choices=choices, value=[]), results_info, state
|
| 300 |
|
| 301 |
|
| 302 |
-
def get_civitai_json(dl_url: str, is_html: bool=False, image_baseurl: str=""):
|
| 303 |
if not image_baseurl: image_baseurl = dl_url
|
| 304 |
default = ("", "", "") if is_html else ""
|
| 305 |
if "https://civitai.com/api/download/models/" not in dl_url: return default
|
| 306 |
user_agent = get_user_agent()
|
| 307 |
headers = {'User-Agent': user_agent, 'content-type': 'application/json'}
|
|
|
|
| 308 |
base_url = 'https://civitai.com/api/v1/model-versions/'
|
| 309 |
params = {}
|
| 310 |
session = requests.Session()
|
|
|
|
| 108 |
return output_filename
|
| 109 |
|
| 110 |
|
| 111 |
+
def save_civitai_info(dl_url, filename, civitai_key="", progress=gr.Progress(track_tqdm=True)):
|
| 112 |
+
json_str, html_str, image_path = get_civitai_json(dl_url, True, filename, civitai_key)
|
| 113 |
if not json_str: return "", "", ""
|
| 114 |
json_path = str(Path(TEMP_DIR, Path(filename).stem + ".json"))
|
| 115 |
html_path = str(Path(TEMP_DIR, Path(filename).stem + ".html"))
|
|
|
|
| 124 |
return "", "", ""
|
| 125 |
|
| 126 |
|
| 127 |
+
def upload_info_to_repo(dl_url, filename, repo_id, repo_type, is_private, civitai_key="", progress=gr.Progress(track_tqdm=True)):
|
| 128 |
def upload_file(api, filename, repo_id, repo_type, hf_token):
|
| 129 |
if not Path(filename).exists(): return
|
| 130 |
api.upload_file(path_or_fileobj=filename, path_in_repo=Path(filename).name, repo_type=repo_type, revision="main", token=hf_token, repo_id=repo_id)
|
|
|
|
| 135 |
try:
|
| 136 |
if not is_repo_exists(repo_id, repo_type): api.create_repo(repo_id=repo_id, repo_type=repo_type, token=hf_token, private=is_private)
|
| 137 |
progress(0, desc=f"Downloading info... {filename}")
|
| 138 |
+
json_path, html_path, image_path = save_civitai_info(dl_url, filename, civitai_key)
|
| 139 |
progress(0, desc=f"Start uploading info... {filename} to {repo_id}")
|
| 140 |
if not json_path: return
|
| 141 |
else: upload_file(api, json_path, repo_id, repo_type, hf_token)
|
|
|
|
| 167 |
if is_rename: file = get_safe_filename(file, newrepo_id, repo_type)
|
| 168 |
url = upload_safetensors_to_repo(file, newrepo_id, repo_type, is_private)
|
| 169 |
if url:
|
| 170 |
+
if is_info: upload_info_to_repo(u, file, newrepo_id, repo_type, is_private, civitai_key)
|
| 171 |
urls.append(url)
|
| 172 |
remain_urls.remove(u)
|
| 173 |
md += f"- Uploaded [{str(u)}]({str(u)})\n"
|
|
|
|
| 187 |
"Controlnet", "Upscaler", "MotionModule", "VAE", "Poses", "Wildcards", "Workflows", "Other"]
|
| 188 |
CIVITAI_FILETYPE = ["Model", "VAE", "Config", "Training Data"]
|
| 189 |
CIVITAI_BASEMODEL = ["Pony", "Illustrious", "SDXL 1.0", "SD 1.5", "Flux.1 D", "Flux.1 S", "SD 3.5"]
|
| 190 |
+
#CIVITAI_SORT = ["Highest Rated", "Most Downloaded", "Newest"]
|
| 191 |
+
CIVITAI_SORT = ["Highest Rated", "Most Downloaded", "Most Liked", "Most Discussed", "Most Collected", "Most Buzz", "Newest"]
|
| 192 |
CIVITAI_PERIOD = ["AllTime", "Year", "Month", "Week", "Day"]
|
| 193 |
|
| 194 |
|
| 195 |
def search_on_civitai(query: str, types: list[str], allow_model: list[str] = [], limit: int = 100,
|
| 196 |
sort: str = "Highest Rated", period: str = "AllTime", tag: str = "", user: str = "", page: int = 1,
|
| 197 |
+
filetype: list[str] = [], api_key: str = "", progress=gr.Progress(track_tqdm=True)):
|
| 198 |
user_agent = get_user_agent()
|
| 199 |
headers = {'User-Agent': user_agent, 'content-type': 'application/json'}
|
| 200 |
+
if api_key: headers['Authorization'] = f'Bearer {{{api_key}}}'
|
| 201 |
base_url = 'https://civitai.com/api/v1/models'
|
| 202 |
params = {'sort': sort, 'period': period, 'limit': int(limit), 'nsfw': 'true'}
|
| 203 |
if len(types) != 0: params["types"] = types
|
|
|
|
| 273 |
|
| 274 |
|
| 275 |
def search_civitai(query, types, base_model=[], sort=CIVITAI_SORT[0], period=CIVITAI_PERIOD[0], tag="", user="", limit=100, page=1,
|
| 276 |
+
filetype=[], api_key="", gallery=[], state={}, progress=gr.Progress(track_tqdm=True)):
|
| 277 |
civitai_last_results = {}
|
| 278 |
set_state(state, "civitai_last_choices", [("", "")])
|
| 279 |
set_state(state, "civitai_last_gallery", [])
|
| 280 |
set_state(state, "civitai_last_results", civitai_last_results)
|
| 281 |
results_info = "No item found."
|
| 282 |
+
items = search_on_civitai(query, types, base_model, int(limit), sort, period, tag, user, int(page), filetype, api_key)
|
| 283 |
if not items: return gr.update(choices=[("", "")], value=[], visible=True),\
|
| 284 |
gr.update(value="", visible=False), gr.update(), gr.update(), gr.update(), gr.update(), results_info, state
|
| 285 |
choices = []
|
|
|
|
| 301 |
gr.update(), gr.update(), gr.update(value=gallery), gr.update(choices=choices, value=[]), results_info, state
|
| 302 |
|
| 303 |
|
| 304 |
+
def get_civitai_json(dl_url: str, is_html: bool=False, image_baseurl: str="", api_key=""):
|
| 305 |
if not image_baseurl: image_baseurl = dl_url
|
| 306 |
default = ("", "", "") if is_html else ""
|
| 307 |
if "https://civitai.com/api/download/models/" not in dl_url: return default
|
| 308 |
user_agent = get_user_agent()
|
| 309 |
headers = {'User-Agent': user_agent, 'content-type': 'application/json'}
|
| 310 |
+
if api_key: headers['Authorization'] = f'Bearer {{{api_key}}}'
|
| 311 |
base_url = 'https://civitai.com/api/v1/model-versions/'
|
| 312 |
params = {}
|
| 313 |
session = requests.Session()
|