John6666 commited on
Commit
7a355b6
1 Parent(s): 622a489

Upload 6 files

Browse files
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🎨➡️🧨
4
  colorFrom: indigo
5
  colorTo: purple
6
  sdk: gradio
7
- sdk_version: 4.38.1
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
4
  colorFrom: indigo
5
  colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 4.42.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
app.py CHANGED
@@ -61,6 +61,7 @@ It saves you the trouble of typing them in.<br>
61
  hf_token = gr.Textbox(label="Your HF write token", placeholder="hf_...", value="", max_lines=1)
62
  civitai_key = gr.Textbox(label="Your Civitai API Key (Optional)", info="If you download model from Civitai...", placeholder="", value="", max_lines=1)
63
  is_upload_sf = gr.Checkbox(label="Upload single safetensors file into new repo", value=False)
 
64
  presets = gr.Radio(label="Presets", choices=list(preset_dict.keys()), value="Default")
65
  with gr.Accordion("Advanced settings", open=False):
66
  is_half = gr.Checkbox(label="Half precision", value=True)
@@ -87,7 +88,7 @@ It saves you the trouble of typing them in.<br>
87
  gr.on(
88
  triggers=[run_button.click],
89
  fn=convert_url_to_diffusers_repo_sd,
90
- inputs=[dl_url, hf_user, hf_repo, hf_token, civitai_key, is_upload_sf, repo_urls, is_half, vae, scheduler,
91
  lora1, lora1s, lora2, lora2s, lora3, lora3s, lora4, lora4s, lora5, lora5s,
92
  model_type, sample_size, ema],
93
  outputs=[repo_urls, output_md],
 
61
  hf_token = gr.Textbox(label="Your HF write token", placeholder="hf_...", value="", max_lines=1)
62
  civitai_key = gr.Textbox(label="Your Civitai API Key (Optional)", info="If you download model from Civitai...", placeholder="", value="", max_lines=1)
63
  is_upload_sf = gr.Checkbox(label="Upload single safetensors file into new repo", value=False)
64
+ is_private = gr.Checkbox(label="Create private repo", value=True)
65
  presets = gr.Radio(label="Presets", choices=list(preset_dict.keys()), value="Default")
66
  with gr.Accordion("Advanced settings", open=False):
67
  is_half = gr.Checkbox(label="Half precision", value=True)
 
88
  gr.on(
89
  triggers=[run_button.click],
90
  fn=convert_url_to_diffusers_repo_sd,
91
+ inputs=[dl_url, hf_user, hf_repo, hf_token, civitai_key, is_private, is_upload_sf, repo_urls, is_half, vae, scheduler,
92
  lora1, lora1s, lora2, lora2s, lora3, lora3s, lora4, lora4s, lora5, lora5s,
93
  model_type, sample_size, ema],
94
  outputs=[repo_urls, output_md],
convert_url_to_diffusers_sd.py CHANGED
@@ -12,7 +12,7 @@ def list_sub(a, b):
12
 
13
  def is_repo_name(s):
14
  import re
15
- return re.fullmatch(r'^[^/,\s]+?/[^/,\s]+?$', s)
16
 
17
 
18
  def download_thing(directory, url, civitai_api_key=""):
 
12
 
13
  def is_repo_name(s):
14
  import re
15
+ return re.fullmatch(r'^[^/,\s\"\']+/[^/,\s\"\']+$', s)
16
 
17
 
18
  def download_thing(directory, url, civitai_api_key=""):
convert_url_to_diffusers_sd_gr.py CHANGED
@@ -13,7 +13,7 @@ def list_sub(a, b):
13
 
14
  def is_repo_name(s):
15
  import re
16
- return re.fullmatch(r'^[^/,\s]+?/[^/,\s]+?$', s)
17
 
18
 
19
  def download_thing(directory, url, civitai_api_key="", progress=gr.Progress(track_tqdm=True)):
@@ -216,7 +216,7 @@ def fuse_loras(pipe, lora_dict={}, temp_dir=".", civitai_key=""):
216
 
217
 
218
  def convert_url_to_diffusers_sd(url, civitai_key="", is_upload_sf=False, half=True, vae=None, scheduler="Euler", lora_dict={},
219
- model_type="v1", sample_size=768, ema="ema", progress=gr.Progress(track_tqdm=True)):
220
  progress(0, desc="Start converting...")
221
  temp_dir = "."
222
  new_file = get_download_file(temp_dir, url, civitai_key)
@@ -277,6 +277,7 @@ def convert_url_to_diffusers_sd(url, civitai_key="", is_upload_sf=False, half=Tr
277
  if not is_repo_name(new_file) and is_upload_sf:
278
  import shutil
279
  shutil.move(str(Path(new_file).resolve()), str(Path(new_repo_name, Path(new_file).name).resolve()))
 
280
 
281
  progress(1, desc="Converted.")
282
  return new_repo_name
@@ -293,14 +294,14 @@ def is_repo_exists(repo_id):
293
  return True # for safe
294
 
295
 
296
- def create_diffusers_repo(new_repo_id, diffusers_folder, progress=gr.Progress(track_tqdm=True)):
297
  from huggingface_hub import HfApi
298
  import os
299
  hf_token = os.environ.get("HF_TOKEN")
300
  api = HfApi()
301
  try:
302
  progress(0, desc="Start uploading...")
303
- api.create_repo(repo_id=new_repo_id, token=hf_token)
304
  for path in Path(diffusers_folder).glob("*"):
305
  if path.is_dir():
306
  api.upload_folder(repo_id=new_repo_id, folder_path=str(path), path_in_repo=path.name, token=hf_token)
@@ -315,10 +316,11 @@ def create_diffusers_repo(new_repo_id, diffusers_folder, progress=gr.Progress(tr
315
  return url
316
 
317
 
318
- def convert_url_to_diffusers_repo_sd(dl_url, hf_user, hf_repo, hf_token, civitai_key="", is_upload_sf=False, repo_urls=[], half=True, vae=None,
319
  scheduler="Euler", lora1=None, lora1s=1.0, lora2=None, lora2s=1.0, lora3=None, lora3s=1.0,
320
  lora4=None, lora4s=1.0, lora5=None, lora5s=1.0,
321
  model_type="v1", sample_size=768, ema="ema", progress=gr.Progress(track_tqdm=True)):
 
322
  if not hf_user:
323
  print(f"Invalid user name: {hf_user}")
324
  progress(1, desc=f"Invalid user name: {hf_user}")
@@ -327,7 +329,7 @@ def convert_url_to_diffusers_repo_sd(dl_url, hf_user, hf_repo, hf_token, civitai
327
  if not civitai_key and os.environ.get("CIVITAI_API_KEY"): civitai_key = os.environ.get("CIVITAI_API_KEY")
328
  lora_dict = {lora1: lora1s, lora2: lora2s, lora3: lora3s, lora4: lora4s, lora5: lora5s}
329
  new_path = convert_url_to_diffusers_sd(dl_url, civitai_key, is_upload_sf, half, vae, scheduler, lora_dict,
330
- model_type, sample_size, ema)
331
  if not new_path: return gr.update(value=repo_urls, choices=repo_urls), gr.update(value="")
332
  new_repo_id = f"{hf_user}/{Path(new_path).stem}"
333
  if hf_repo != "": new_repo_id = f"{hf_user}/{hf_repo}"
@@ -339,7 +341,8 @@ def convert_url_to_diffusers_repo_sd(dl_url, hf_user, hf_repo, hf_token, civitai
339
  print(f"Repo already exists: {new_repo_id}")
340
  progress(1, desc=f"Repo already exists: {new_repo_id}")
341
  return gr.update(value=repo_urls, choices=repo_urls), gr.update(value="")
342
- repo_url = create_diffusers_repo(new_repo_id, new_path)
 
343
  if not repo_urls: repo_urls = []
344
  repo_urls.append(repo_url)
345
  md = "Your new repo:<br>"
@@ -381,4 +384,4 @@ if __name__ == "__main__":
381
  lora_dict[str(p)] = 1.0
382
 
383
  convert_url_to_diffusers_sd(args.url, args.civitai_key, args.half, args.vae, args.scheduler, lora_dict,
384
- args.model_type, args.sample_size, args.ema)
 
13
 
14
  def is_repo_name(s):
15
  import re
16
+ return re.fullmatch(r'^[^/,\s\"\']+/[^/,\s\"\']+$', s)
17
 
18
 
19
  def download_thing(directory, url, civitai_api_key="", progress=gr.Progress(track_tqdm=True)):
 
216
 
217
 
218
  def convert_url_to_diffusers_sd(url, civitai_key="", is_upload_sf=False, half=True, vae=None, scheduler="Euler", lora_dict={},
219
+ model_type="v1", sample_size=768, ema="ema", is_local=True, progress=gr.Progress(track_tqdm=True)):
220
  progress(0, desc="Start converting...")
221
  temp_dir = "."
222
  new_file = get_download_file(temp_dir, url, civitai_key)
 
277
  if not is_repo_name(new_file) and is_upload_sf:
278
  import shutil
279
  shutil.move(str(Path(new_file).resolve()), str(Path(new_repo_name, Path(new_file).name).resolve()))
280
+ elif not is_local: os.remove(new_file)
281
 
282
  progress(1, desc="Converted.")
283
  return new_repo_name
 
294
  return True # for safe
295
 
296
 
297
+ def create_diffusers_repo(new_repo_id, diffusers_folder, is_private, progress=gr.Progress(track_tqdm=True)):
298
  from huggingface_hub import HfApi
299
  import os
300
  hf_token = os.environ.get("HF_TOKEN")
301
  api = HfApi()
302
  try:
303
  progress(0, desc="Start uploading...")
304
+ api.create_repo(repo_id=new_repo_id, token=hf_token, private=is_private)
305
  for path in Path(diffusers_folder).glob("*"):
306
  if path.is_dir():
307
  api.upload_folder(repo_id=new_repo_id, folder_path=str(path), path_in_repo=path.name, token=hf_token)
 
316
  return url
317
 
318
 
319
+ def convert_url_to_diffusers_repo_sd(dl_url, hf_user, hf_repo, hf_token, civitai_key="", is_private=True, is_upload_sf=False, repo_urls=[], half=True, vae=None,
320
  scheduler="Euler", lora1=None, lora1s=1.0, lora2=None, lora2s=1.0, lora3=None, lora3s=1.0,
321
  lora4=None, lora4s=1.0, lora5=None, lora5s=1.0,
322
  model_type="v1", sample_size=768, ema="ema", progress=gr.Progress(track_tqdm=True)):
323
+ import shutil
324
  if not hf_user:
325
  print(f"Invalid user name: {hf_user}")
326
  progress(1, desc=f"Invalid user name: {hf_user}")
 
329
  if not civitai_key and os.environ.get("CIVITAI_API_KEY"): civitai_key = os.environ.get("CIVITAI_API_KEY")
330
  lora_dict = {lora1: lora1s, lora2: lora2s, lora3: lora3s, lora4: lora4s, lora5: lora5s}
331
  new_path = convert_url_to_diffusers_sd(dl_url, civitai_key, is_upload_sf, half, vae, scheduler, lora_dict,
332
+ model_type, sample_size, ema, False)
333
  if not new_path: return gr.update(value=repo_urls, choices=repo_urls), gr.update(value="")
334
  new_repo_id = f"{hf_user}/{Path(new_path).stem}"
335
  if hf_repo != "": new_repo_id = f"{hf_user}/{hf_repo}"
 
341
  print(f"Repo already exists: {new_repo_id}")
342
  progress(1, desc=f"Repo already exists: {new_repo_id}")
343
  return gr.update(value=repo_urls, choices=repo_urls), gr.update(value="")
344
+ repo_url = create_diffusers_repo(new_repo_id, new_path, is_private)
345
+ shutil.rmtree(new_path)
346
  if not repo_urls: repo_urls = []
347
  repo_urls.append(repo_url)
348
  md = "Your new repo:<br>"
 
384
  lora_dict[str(p)] = 1.0
385
 
386
  convert_url_to_diffusers_sd(args.url, args.civitai_key, args.half, args.vae, args.scheduler, lora_dict,
387
+ args.model_type, args.sample_size, args.ema, True)
local/convert_url_to_diffusers_sd.py CHANGED
@@ -12,7 +12,7 @@ def list_sub(a, b):
12
 
13
  def is_repo_name(s):
14
  import re
15
- return re.fullmatch(r'^[^/,\s]+?/[^/,\s]+?$', s)
16
 
17
 
18
  def download_thing(directory, url, civitai_api_key=""):
 
12
 
13
  def is_repo_name(s):
14
  import re
15
+ return re.fullmatch(r'^[^/,\s\"\']+/[^/,\s\"\']+$', s)
16
 
17
 
18
  def download_thing(directory, url, civitai_api_key=""):