John6666 commited on
Commit
4780ff3
·
verified ·
1 Parent(s): 3b2fac4

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +7 -3
  3. civitai_to_hf.py +5 -2
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🤗
4
  colorFrom: yellow
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 5.47.2
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
4
  colorFrom: yellow
5
  colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 5.48.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
app.py CHANGED
@@ -62,19 +62,23 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
62
  is_private = gr.Checkbox(label="Create private repo", value=True)
63
  is_info = gr.Checkbox(label="Upload Civitai information files", value=False)
64
  is_rename = gr.Checkbox(label="Auto rename", value=True)
65
- run_button = gr.Button(value="Download and Upload", variant="primary")
 
 
66
  uploaded_urls = gr.CheckboxGroup(visible=False, choices=[], value=None) # hidden
67
  urls_md = gr.Markdown("<br><br>", elem_classes="result")
68
- urls_remain = gr.Textbox("Remaining URLs", value="", show_copy_button=True, visible=False)
69
  gr.DuplicateButton(value="Duplicate Space")
70
 
71
- gr.on(
72
  triggers=[run_button.click],
73
  fn=download_civitai,
74
  inputs=[dl_url, civitai_key, hf_token, uploaded_urls, newrepo_id, newrepo_type, is_private, is_info, is_rename],
75
  outputs=[uploaded_urls, urls_md, urls_remain],
76
  queue=True,
77
  )
 
 
78
  gr.on(
79
  triggers=[search_civitai_submit.click, search_civitai_query.submit, search_civitai_user.submit],
80
  fn=search_civitai,
 
62
  is_private = gr.Checkbox(label="Create private repo", value=True)
63
  is_info = gr.Checkbox(label="Upload Civitai information files", value=False)
64
  is_rename = gr.Checkbox(label="Auto rename", value=True)
65
+ with gr.Row():
66
+ run_button = gr.Button(value="Download and Upload", variant="primary")
67
+ stop_button = gr.Button(value="Cancel", variant="stop")
68
  uploaded_urls = gr.CheckboxGroup(visible=False, choices=[], value=None) # hidden
69
  urls_md = gr.Markdown("<br><br>", elem_classes="result")
70
+ urls_remain = gr.Textbox(label="Remaining URLs", value="", show_copy_button=True, visible=False)
71
  gr.DuplicateButton(value="Duplicate Space")
72
 
73
+ download_evt = gr.on(
74
  triggers=[run_button.click],
75
  fn=download_civitai,
76
  inputs=[dl_url, civitai_key, hf_token, uploaded_urls, newrepo_id, newrepo_type, is_private, is_info, is_rename],
77
  outputs=[uploaded_urls, urls_md, urls_remain],
78
  queue=True,
79
  )
80
+ stop_button.click(fn=None, cancels=[download_evt], queue=False, show_api=False)
81
+ stop_button.click(fn=lambda: gr.update(visible=True), inputs=None, outputs=[urls_remain], queue=False, show_api=False)
82
  gr.on(
83
  triggers=[search_civitai_submit.click, search_civitai_query.submit, search_civitai_user.submit],
84
  fn=search_civitai,
civitai_to_hf.py CHANGED
@@ -214,11 +214,13 @@ def download_civitai(dl_url, civitai_key, hf_token, urls,
214
  hashes = set(get_repo_hashes(newrepo_id, repo_type))
215
  try:
216
  md = f'### Your repo: [{newrepo_id}]({"https://huggingface.co/datasets/" if repo_type == "dataset" else "https://huggingface.co/"}{newrepo_id})\n'
 
217
  for u in dl_urls:
218
  if get_civitai_sha256(u, civitai_key) in hashes:
219
  print(f"{u} is already exitsts. skipping.")
220
  remain_urls.remove(u)
221
  md += f"- Skipped [{str(u)}]({str(u)})\n"
 
222
  continue
223
  file = download_file(u, civitai_key)
224
  if not Path(file).exists() or not Path(file).is_file(): continue
@@ -229,14 +231,15 @@ def download_civitai(dl_url, civitai_key, hf_token, urls,
229
  urls.append(url)
230
  remain_urls.remove(u)
231
  md += f"- Uploaded [{str(u)}]({str(u)})\n"
 
232
  dp_repos = parse_repos(dl_url)
233
  for r in dp_repos:
234
  url = duplicate_hf_repo(r, newrepo_id, "model", repo_type, is_private, HF_SUBFOLDER_NAME[1])
235
  if url: urls.append(url)
236
- return gr.update(value=urls, choices=urls), gr.update(value=md), gr.update(value="\n".join(remain_urls), visible=False)
237
  except Exception as e:
238
  gr.Info(f"Error occured: {e}")
239
- return gr.update(value=urls, choices=urls), gr.update(value=md), gr.update(value="\n".join(remain_urls), visible=True)
240
  finally:
241
  gc.collect()
242
 
 
214
  hashes = set(get_repo_hashes(newrepo_id, repo_type))
215
  try:
216
  md = f'### Your repo: [{newrepo_id}]({"https://huggingface.co/datasets/" if repo_type == "dataset" else "https://huggingface.co/"}{newrepo_id})\n'
217
+ yield gr.update(value=urls, choices=urls), gr.update(value=md), gr.update(value="\n".join(remain_urls))
218
  for u in dl_urls:
219
  if get_civitai_sha256(u, civitai_key) in hashes:
220
  print(f"{u} is already exitsts. skipping.")
221
  remain_urls.remove(u)
222
  md += f"- Skipped [{str(u)}]({str(u)})\n"
223
+ yield gr.update(value=urls, choices=urls), gr.update(value=md), gr.update(value="\n".join(remain_urls))
224
  continue
225
  file = download_file(u, civitai_key)
226
  if not Path(file).exists() or not Path(file).is_file(): continue
 
231
  urls.append(url)
232
  remain_urls.remove(u)
233
  md += f"- Uploaded [{str(u)}]({str(u)})\n"
234
+ yield gr.update(value=urls, choices=urls), gr.update(value=md), gr.update(value="\n".join(remain_urls))
235
  dp_repos = parse_repos(dl_url)
236
  for r in dp_repos:
237
  url = duplicate_hf_repo(r, newrepo_id, "model", repo_type, is_private, HF_SUBFOLDER_NAME[1])
238
  if url: urls.append(url)
239
+ yield gr.update(value=urls, choices=urls), gr.update(value=md), gr.update(value="\n".join(remain_urls), visible=False)
240
  except Exception as e:
241
  gr.Info(f"Error occured: {e}")
242
+ yield gr.update(value=urls, choices=urls), gr.update(value=md), gr.update(value="\n".join(remain_urls), visible=True)
243
  finally:
244
  gc.collect()
245