John6666 commited on
Commit
c7c9dd3
·
verified ·
1 Parent(s): e9d9ca7

Upload 15 files

Browse files
Files changed (3) hide show
  1. app.py +5 -5
  2. stkey_gr.py +2 -2
  3. utils.py +33 -1
app.py CHANGED
@@ -26,17 +26,17 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
26
  civitai_key = gr.Textbox(label="Your Civitai Key (Optional)", value="", max_lines=1)
27
  gr.Markdown("Your Civitai API key is available at [https://civitai.com/user/account](https://civitai.com/user/account).", elem_classes="info")
28
  run_button = gr.Button(value="Check", variant="primary")
29
- uploaded_urls = gr.CheckboxGroup(visible=False, choices=[], value=[]) # hidden
30
- urls_md = gr.Markdown("<br><br>", elem_classes="result", visible=False)
31
- out_files = gr.Files(label="Output", interactive=False, value=[])
32
  with gr.Group():
 
 
 
33
  with gr.Row():
34
  missing = gr.JSON(value=[], label="Missing keys")
35
  added = gr.JSON(value=[], label="Added keys")
36
  with gr.Row():
37
  keys = gr.JSON(value=[], label="All keys")
38
  metadata = gr.JSON(value={}, label="Metadata")
39
- clear_button = gr.Button(value="Clear Output", variant="secondary")
40
  gr.DuplicateButton(value="Duplicate Space")
41
 
42
  gr.on(
@@ -45,7 +45,7 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_ca
45
  inputs=[dl_url, civitai_key, hf_token, uploaded_urls, out_files, is_validate, rfile],
46
  outputs=[uploaded_urls, out_files, urls_md, metadata, keys, missing, added],
47
  )
48
- clear_button.click(lambda: ([], [], "", {}, [], [], []), None, [uploaded_urls, out_files, urls_md, metadata, keys, missing, added], queue=False, show_api=False)
49
  upload_rfile.upload(upload_keys_file, [upload_rfile], [rfile], queue=False, show_api=False)
50
 
51
  demo.queue()
 
26
  civitai_key = gr.Textbox(label="Your Civitai Key (Optional)", value="", max_lines=1)
27
  gr.Markdown("Your Civitai API key is available at [https://civitai.com/user/account](https://civitai.com/user/account).", elem_classes="info")
28
  run_button = gr.Button(value="Check", variant="primary")
 
 
 
29
  with gr.Group():
30
+ uploaded_urls = gr.CheckboxGroup(visible=False, choices=[], value=[]) # hidden
31
+ urls_md = gr.Markdown("<br><br>", elem_classes="result", visible=False)
32
+ out_files = gr.Files(label="Output", interactive=False, value=[])
33
  with gr.Row():
34
  missing = gr.JSON(value=[], label="Missing keys")
35
  added = gr.JSON(value=[], label="Added keys")
36
  with gr.Row():
37
  keys = gr.JSON(value=[], label="All keys")
38
  metadata = gr.JSON(value={}, label="Metadata")
39
+ clear_button = gr.Button(value="Clear Output", variant="secondary")
40
  gr.DuplicateButton(value="Duplicate Space")
41
 
42
  gr.on(
 
45
  inputs=[dl_url, civitai_key, hf_token, uploaded_urls, out_files, is_validate, rfile],
46
  outputs=[uploaded_urls, out_files, urls_md, metadata, keys, missing, added],
47
  )
48
+ clear_button.click(lambda: ([], [], "<br><br>", {}, [], [], []), None, [uploaded_urls, out_files, urls_md, metadata, keys, missing, added], queue=False, show_api=False)
49
  upload_rfile.upload(upload_keys_file, [upload_rfile], [rfile], queue=False, show_api=False)
50
 
51
  demo.queue()
stkey_gr.py CHANGED
@@ -84,8 +84,8 @@ def get_stkey(filename: str, is_validate: bool=True, rfile: str=KEYS_FILES[0], p
84
 
85
  def stkey_gr(dl_url: str, civitai_key: str, hf_token: str, urls: list[str], files: list[str],
86
  is_validate=True, rfile=KEYS_FILES[0], progress=gr.Progress(track_tqdm=True)):
87
- if hf_token: set_token(hf_token)
88
- else: set_token(os.environ.get("HF_TOKEN")) # default huggingface token
89
  if not civitai_key: civitai_key = os.environ.get("CIVITAI_API_KEY") # default Civitai API key
90
  dl_urls = parse_urls(dl_url)
91
  if not urls: urls = []
 
84
 
85
  def stkey_gr(dl_url: str, civitai_key: str, hf_token: str, urls: list[str], files: list[str],
86
  is_validate=True, rfile=KEYS_FILES[0], progress=gr.Progress(track_tqdm=True)):
87
+ if not hf_token: hf_token = os.environ.get("HF_TOKEN") # default huggingface token
88
+ set_token(hf_token)
89
  if not civitai_key: civitai_key = os.environ.get("CIVITAI_API_KEY") # default Civitai API key
90
  dl_urls = parse_urls(dl_url)
91
  if not urls: urls = []
utils.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from huggingface_hub import HfApi, HfFolder, hf_hub_download
3
  import os
4
  from pathlib import Path
5
  import shutil
@@ -163,3 +163,35 @@ def get_download_file(temp_dir, url, civitai_key, progress=gr.Progress(track_tqd
163
  return ""
164
  print(f"Download completed: {url}")
165
  return new_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from huggingface_hub import HfApi, HfFolder, hf_hub_download, snapshot_download
3
  import os
4
  from pathlib import Path
5
  import shutil
 
163
  return ""
164
  print(f"Download completed: {url}")
165
  return new_file
166
+
167
+
168
+ # https://huggingface.co/docs/huggingface_hub/v0.25.1/en/package_reference/file_download#huggingface_hub.snapshot_download
169
+ def download_repo(repo_id, dir_path, progress=gr.Progress(track_tqdm=True)):
170
+ hf_token = get_token()
171
+ try:
172
+ snapshot_download(repo_id=repo_id, local_dir=dir_path, token=hf_token, allow_patterns=["*.safetensors", "*.bin"],
173
+ ignore_patterns=["*.fp16.*", "/*.safetensors", "/*.bin"], force_download=True)
174
+ return True
175
+ except Exception as e:
176
+ print(f"Error: Failed to download {repo_id}. {e}")
177
+ gr.Warning(f"Error: Failed to download {repo_id}. {e}")
178
+ return False
179
+
180
+
181
+ def upload_repo(new_repo_id, dir_path, is_private, progress=gr.Progress(track_tqdm=True)):
182
+ hf_token = get_token()
183
+ api = HfApi(token=hf_token)
184
+ try:
185
+ progress(0, desc="Start uploading...")
186
+ api.create_repo(repo_id=new_repo_id, token=hf_token, private=is_private, exist_ok=True)
187
+ for path in Path(dir_path).glob("*"):
188
+ if path.is_dir():
189
+ api.upload_folder(repo_id=new_repo_id, folder_path=str(path), path_in_repo=path.name, token=hf_token)
190
+ elif path.is_file():
191
+ api.upload_file(repo_id=new_repo_id, path_or_fileobj=str(path), path_in_repo=path.name, token=hf_token)
192
+ progress(1, desc="Uploaded.")
193
+ url = f"https://huggingface.co/{new_repo_id}"
194
+ except Exception as e:
195
+ print(f"Error: Failed to upload to {new_repo_id}. {e}")
196
+ return ""
197
+ return url