John6666 commited on
Commit
d85bcb0
1 Parent(s): f1658df

Upload 9 files

Browse files
Files changed (3) hide show
  1. app.py +6 -6
  2. convert_url_to_diffusers_multi_gr.py +5 -2
  3. utils.py +12 -0
app.py CHANGED
@@ -55,6 +55,7 @@ In particular, conversion of FLUX.1 or SD 3.5 is almost impossible in CPU space.
55
  with gr.Row():
56
  is_upload_sf = gr.Checkbox(label="Upload single safetensors file into new repo", value=False)
57
  is_private = gr.Checkbox(label="Create private repo", value=True)
 
58
  with gr.Row():
59
  is_overwrite = gr.Checkbox(label="Overwrite repo", value=HF_OW)
60
  is_pr = gr.Checkbox(label="Create PR", value=HF_PR)
@@ -178,8 +179,7 @@ In particular, conversion of FLUX.1 or SD 3.5 is almost impossible in CPU space.
178
  sd35_run_button = gr.Button(value="Submit", variant="primary")
179
  adv_args = gr.Textbox(label="Advanced arguments", value="", visible=False)
180
  with gr.Group():
181
- with gr.Row():
182
- repo_urls = gr.CheckboxGroup(visible=False, choices=[], value=[])
183
  output_md = gr.Markdown(label="Output", value="<br><br>", elem_classes="result")
184
  clear_button = gr.Button(value="Clear Output", variant="secondary")
185
  gr.DuplicateButton(value="Duplicate Space")
@@ -188,7 +188,7 @@ In particular, conversion of FLUX.1 or SD 3.5 is almost impossible in CPU space.
188
  gr.on(
189
  triggers=[sdxl_run_button.click],
190
  fn=convert_url_to_diffusers_repo,
191
- inputs=[dl_url, hf_user, hf_repo, hf_token, civitai_key, is_private, is_overwrite, is_pr, is_upload_sf, repo_urls,
192
  sdxl_dtype, sdxl_vae, sdxl_clip, flux_t5, sdxl_scheduler, sd15_ema, sd15_isize, sd15_sc, flux_base_repo, sdxl_mtype,
193
  sdxl_lora1, sdxl_lora1s, sdxl_lora2, sdxl_lora2s, sdxl_lora3, sdxl_lora3s, sdxl_lora4, sdxl_lora4s, sdxl_lora5, sdxl_lora5s, adv_args],
194
  outputs=[repo_urls, output_md],
@@ -203,7 +203,7 @@ In particular, conversion of FLUX.1 or SD 3.5 is almost impossible in CPU space.
203
  gr.on(
204
  triggers=[sd15_run_button.click],
205
  fn=convert_url_to_diffusers_repo,
206
- inputs=[dl_url, hf_user, hf_repo, hf_token, civitai_key, is_private, is_overwrite, is_pr, is_upload_sf, repo_urls,
207
  sd15_dtype, sd15_vae, sd15_clip, flux_t5, sd15_scheduler, sd15_ema, sd15_isize, sd15_sc, flux_base_repo, sd15_mtype,
208
  sd15_lora1, sd15_lora1s, sd15_lora2, sd15_lora2s, sd15_lora3, sd15_lora3s, sd15_lora4, sd15_lora4s, sd15_lora5, sd15_lora5s, adv_args],
209
  outputs=[repo_urls, output_md],
@@ -218,7 +218,7 @@ In particular, conversion of FLUX.1 or SD 3.5 is almost impossible in CPU space.
218
  gr.on(
219
  triggers=[flux_run_button.click],
220
  fn=convert_url_to_diffusers_repo,
221
- inputs=[dl_url, hf_user, hf_repo, hf_token, civitai_key, is_private, is_overwrite, is_pr, is_upload_sf, repo_urls,
222
  flux_dtype, flux_vae, flux_clip, flux_t5, flux_scheduler, sd15_ema, sd15_isize, sd15_sc, flux_base_repo, flux_mtype,
223
  flux_lora1, flux_lora1s, flux_lora2, flux_lora2s, flux_lora3, flux_lora3s, flux_lora4, flux_lora4s, flux_lora5, flux_lora5s, adv_args],
224
  outputs=[repo_urls, output_md],
@@ -233,7 +233,7 @@ In particular, conversion of FLUX.1 or SD 3.5 is almost impossible in CPU space.
233
  gr.on(
234
  triggers=[sd35_run_button.click],
235
  fn=convert_url_to_diffusers_repo,
236
- inputs=[dl_url, hf_user, hf_repo, hf_token, civitai_key, is_private, is_overwrite, is_pr, is_upload_sf, repo_urls,
237
  sd35_dtype, sd35_vae, sd35_clip, sd35_t5, sd35_scheduler, sd15_ema, sd15_isize, sd15_sc, sd35_base_repo, sd35_mtype,
238
  sd35_lora1, sd35_lora1s, sd35_lora2, sd35_lora2s, sd35_lora3, sd35_lora3s, sd35_lora4, sd35_lora4s, sd35_lora5, sd35_lora5s, adv_args],
239
  outputs=[repo_urls, output_md],
 
55
  with gr.Row():
56
  is_upload_sf = gr.Checkbox(label="Upload single safetensors file into new repo", value=False)
57
  is_private = gr.Checkbox(label="Create private repo", value=True)
58
+ gated = gr.Radio(label="Create gated repo", info="Gated repo must be public", choices=["auto", "manual", "False"], value="False")
59
  with gr.Row():
60
  is_overwrite = gr.Checkbox(label="Overwrite repo", value=HF_OW)
61
  is_pr = gr.Checkbox(label="Create PR", value=HF_PR)
 
179
  sd35_run_button = gr.Button(value="Submit", variant="primary")
180
  adv_args = gr.Textbox(label="Advanced arguments", value="", visible=False)
181
  with gr.Group():
182
+ repo_urls = gr.CheckboxGroup(visible=False, choices=[], value=[])
 
183
  output_md = gr.Markdown(label="Output", value="<br><br>", elem_classes="result")
184
  clear_button = gr.Button(value="Clear Output", variant="secondary")
185
  gr.DuplicateButton(value="Duplicate Space")
 
188
  gr.on(
189
  triggers=[sdxl_run_button.click],
190
  fn=convert_url_to_diffusers_repo,
191
+ inputs=[dl_url, hf_user, hf_repo, hf_token, civitai_key, is_private, gated, is_overwrite, is_pr, is_upload_sf, repo_urls,
192
  sdxl_dtype, sdxl_vae, sdxl_clip, flux_t5, sdxl_scheduler, sd15_ema, sd15_isize, sd15_sc, flux_base_repo, sdxl_mtype,
193
  sdxl_lora1, sdxl_lora1s, sdxl_lora2, sdxl_lora2s, sdxl_lora3, sdxl_lora3s, sdxl_lora4, sdxl_lora4s, sdxl_lora5, sdxl_lora5s, adv_args],
194
  outputs=[repo_urls, output_md],
 
203
  gr.on(
204
  triggers=[sd15_run_button.click],
205
  fn=convert_url_to_diffusers_repo,
206
+ inputs=[dl_url, hf_user, hf_repo, hf_token, civitai_key, is_private, gated, is_overwrite, is_pr, is_upload_sf, repo_urls,
207
  sd15_dtype, sd15_vae, sd15_clip, flux_t5, sd15_scheduler, sd15_ema, sd15_isize, sd15_sc, flux_base_repo, sd15_mtype,
208
  sd15_lora1, sd15_lora1s, sd15_lora2, sd15_lora2s, sd15_lora3, sd15_lora3s, sd15_lora4, sd15_lora4s, sd15_lora5, sd15_lora5s, adv_args],
209
  outputs=[repo_urls, output_md],
 
218
  gr.on(
219
  triggers=[flux_run_button.click],
220
  fn=convert_url_to_diffusers_repo,
221
+ inputs=[dl_url, hf_user, hf_repo, hf_token, civitai_key, is_private, gated, is_overwrite, is_pr, is_upload_sf, repo_urls,
222
  flux_dtype, flux_vae, flux_clip, flux_t5, flux_scheduler, sd15_ema, sd15_isize, sd15_sc, flux_base_repo, flux_mtype,
223
  flux_lora1, flux_lora1s, flux_lora2, flux_lora2s, flux_lora3, flux_lora3s, flux_lora4, flux_lora4s, flux_lora5, flux_lora5s, adv_args],
224
  outputs=[repo_urls, output_md],
 
233
  gr.on(
234
  triggers=[sd35_run_button.click],
235
  fn=convert_url_to_diffusers_repo,
236
+ inputs=[dl_url, hf_user, hf_repo, hf_token, civitai_key, is_private, gated, is_overwrite, is_pr, is_upload_sf, repo_urls,
237
  sd35_dtype, sd35_vae, sd35_clip, sd35_t5, sd35_scheduler, sd15_ema, sd15_isize, sd15_sc, sd35_base_repo, sd35_mtype,
238
  sd35_lora1, sd35_lora1s, sd35_lora2, sd35_lora2s, sd35_lora3, sd35_lora3s, sd35_lora4, sd35_lora4s, sd35_lora5, sd35_lora5s, adv_args],
239
  outputs=[repo_urls, output_md],
convert_url_to_diffusers_multi_gr.py CHANGED
@@ -17,7 +17,7 @@ import shutil
17
  import gc
18
  import tempfile
19
  # also requires aria, gdown, peft, huggingface_hub, safetensors, transformers, accelerate, pytorch_lightning
20
- from utils import (get_token, set_token, is_repo_exists, is_repo_name, get_download_file, upload_repo)
21
  from sdutils import (SCHEDULER_CONFIG_MAP, get_scheduler_config, fuse_loras, DTYPE_DEFAULT, get_dtype, get_dtypes, get_model_type_from_key, get_process_dtype)
22
 
23
 
@@ -394,7 +394,8 @@ def convert_url_to_diffusers(url: str, civitai_key: str="", is_upload_sf: bool=F
394
  gc.collect()
395
 
396
 
397
- def convert_url_to_diffusers_repo(dl_url: str, hf_user: str, hf_repo: str, hf_token: str, civitai_key="", is_private: bool=True, is_overwrite: bool=False, is_pr: bool=False,
 
398
  is_upload_sf: bool=False, urls: list=[], dtype: str="fp16", vae: str="", clip: str="", t5: str="", scheduler: str="Euler a",
399
  ema: bool=True, image_size: str="768", safety_checker: bool=False,
400
  base_repo: str="", mtype: str="", lora1: str="", lora1s=1.0, lora2: str="", lora2s=1.0, lora3: str="", lora3s=1.0,
@@ -404,6 +405,7 @@ def convert_url_to_diffusers_repo(dl_url: str, hf_user: str, hf_repo: str, hf_to
404
  if not civitai_key and os.environ.get("CIVITAI_API_KEY"): civitai_key = os.environ.get("CIVITAI_API_KEY") # default Civitai API key
405
  if not hf_token and os.environ.get("HF_TOKEN"): hf_token = os.environ.get("HF_TOKEN") # default HF write token
406
  if not hf_user: raise gr.Error(f"Invalid user name: {hf_user}")
 
407
  set_token(hf_token)
408
  lora_dict = {lora1: lora1s, lora2: lora2s, lora3: lora3s, lora4: lora4s, lora5: lora5s}
409
  new_path = convert_url_to_diffusers(dl_url, civitai_key, is_upload_sf, dtype, vae, clip, t5, scheduler, ema, image_size, safety_checker, base_repo, mtype, lora_dict, is_local)
@@ -413,6 +415,7 @@ def convert_url_to_diffusers_repo(dl_url: str, hf_user: str, hf_repo: str, hf_to
413
  if not is_repo_name(new_repo_id): raise gr.Error(f"Invalid repo name: {new_repo_id}")
414
  if not is_overwrite and is_repo_exists(new_repo_id) and not is_pr: raise gr.Error(f"Repo already exists: {new_repo_id}")
415
  repo_url = upload_repo(new_repo_id, new_path, is_private, is_pr)
 
416
  safe_clean(new_path)
417
  if not urls: urls = []
418
  urls.append(repo_url)
 
17
  import gc
18
  import tempfile
19
  # also requires aria, gdown, peft, huggingface_hub, safetensors, transformers, accelerate, pytorch_lightning
20
+ from utils import (get_token, set_token, is_repo_exists, is_repo_name, get_download_file, upload_repo, gate_repo)
21
  from sdutils import (SCHEDULER_CONFIG_MAP, get_scheduler_config, fuse_loras, DTYPE_DEFAULT, get_dtype, get_dtypes, get_model_type_from_key, get_process_dtype)
22
 
23
 
 
394
  gc.collect()
395
 
396
 
397
+ def convert_url_to_diffusers_repo(dl_url: str, hf_user: str, hf_repo: str, hf_token: str, civitai_key="", is_private: bool=True,
398
+ gated: str="False", is_overwrite: bool=False, is_pr: bool=False,
399
  is_upload_sf: bool=False, urls: list=[], dtype: str="fp16", vae: str="", clip: str="", t5: str="", scheduler: str="Euler a",
400
  ema: bool=True, image_size: str="768", safety_checker: bool=False,
401
  base_repo: str="", mtype: str="", lora1: str="", lora1s=1.0, lora2: str="", lora2s=1.0, lora3: str="", lora3s=1.0,
 
405
  if not civitai_key and os.environ.get("CIVITAI_API_KEY"): civitai_key = os.environ.get("CIVITAI_API_KEY") # default Civitai API key
406
  if not hf_token and os.environ.get("HF_TOKEN"): hf_token = os.environ.get("HF_TOKEN") # default HF write token
407
  if not hf_user: raise gr.Error(f"Invalid user name: {hf_user}")
408
+ if gated != "False" and is_private: raise gr.Error(f"Gated repo must be public")
409
  set_token(hf_token)
410
  lora_dict = {lora1: lora1s, lora2: lora2s, lora3: lora3s, lora4: lora4s, lora5: lora5s}
411
  new_path = convert_url_to_diffusers(dl_url, civitai_key, is_upload_sf, dtype, vae, clip, t5, scheduler, ema, image_size, safety_checker, base_repo, mtype, lora_dict, is_local)
 
415
  if not is_repo_name(new_repo_id): raise gr.Error(f"Invalid repo name: {new_repo_id}")
416
  if not is_overwrite and is_repo_exists(new_repo_id) and not is_pr: raise gr.Error(f"Repo already exists: {new_repo_id}")
417
  repo_url = upload_repo(new_repo_id, new_path, is_private, is_pr)
418
+ gate_repo(new_repo_id, gated)
419
  safe_clean(new_path)
420
  if not urls: urls = []
421
  urls.append(repo_url)
utils.py CHANGED
@@ -211,6 +211,18 @@ def upload_repo(repo_id: str, dir_path: str, is_private: bool, is_pr: bool=False
211
  return ""
212
 
213
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  HF_SUBFOLDER_NAME = ["None", "user_repo"]
215
 
216
 
 
211
  return ""
212
 
213
 
214
+ def gate_repo(repo_id: str, gated_str: str, repo_type: str="model"):
215
+ hf_token = get_token()
216
+ api = HfApi(token=hf_token)
217
+ try:
218
+ if gated_str == "auto": gated = "auto"
219
+ elif gated_str == "manual": gated = "manual"
220
+ else: gated = False
221
+ api.update_repo_settings(repo_id=repo_id, gated=gated, repo_type=repo_type, token=hf_token)
222
+ except Exception as e:
223
+ print(f"Error: Failed to update settings {repo_id}. {e}")
224
+
225
+
226
  HF_SUBFOLDER_NAME = ["None", "user_repo"]
227
 
228