Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	Upload 6 files
Browse files- README.md +1 -1
- app.py +2 -1
- convert_url_to_diffusers_sdxl.py +1 -1
- convert_url_to_diffusers_sdxl_gr.py +13 -8
- local/convert_url_to_diffusers_sdxl.py +1 -1
    	
        README.md
    CHANGED
    
    | @@ -4,7 +4,7 @@ emoji: 🎨➡️🧨 | |
| 4 | 
             
            colorFrom: indigo
         | 
| 5 | 
             
            colorTo: purple
         | 
| 6 | 
             
            sdk: gradio
         | 
| 7 | 
            -
            sdk_version: 4. | 
| 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
    
    | @@ -78,6 +78,7 @@ It saves you the trouble of typing them in.<br> | |
| 78 | 
             
                    hf_token = gr.Textbox(label="Your HF write token", placeholder="hf_...", value="", max_lines=1)
         | 
| 79 | 
             
                    civitai_key = gr.Textbox(label="Your Civitai API Key (Optional)", info="If you download model from Civitai...", placeholder="", value="", max_lines=1)
         | 
| 80 | 
             
                    is_upload_sf = gr.Checkbox(label="Upload single safetensors file into new repo", value=False)
         | 
|  | |
| 81 | 
             
                    presets = gr.Radio(label="Presets", choices=list(preset_dict.keys()), value="Default")
         | 
| 82 | 
             
                    with gr.Accordion("Advanced settings", open=False):
         | 
| 83 | 
             
                        is_half = gr.Checkbox(label="Half precision", value=True)
         | 
| @@ -101,7 +102,7 @@ It saves you the trouble of typing them in.<br> | |
| 101 | 
             
                gr.on(
         | 
| 102 | 
             
                    triggers=[run_button.click],
         | 
| 103 | 
             
                    fn=convert_url_to_diffusers_repo,
         | 
| 104 | 
            -
                    inputs=[dl_url, hf_user, hf_repo, hf_token, civitai_key, is_upload_sf, repo_urls, is_half, vae, scheduler,
         | 
| 105 | 
             
                             lora1, lora1s, lora2, lora2s, lora3, lora3s, lora4, lora4s, lora5, lora5s],
         | 
| 106 | 
             
                    outputs=[repo_urls, output_md],
         | 
| 107 | 
             
                )
         | 
|  | |
| 78 | 
             
                    hf_token = gr.Textbox(label="Your HF write token", placeholder="hf_...", value="", max_lines=1)
         | 
| 79 | 
             
                    civitai_key = gr.Textbox(label="Your Civitai API Key (Optional)", info="If you download model from Civitai...", placeholder="", value="", max_lines=1)
         | 
| 80 | 
             
                    is_upload_sf = gr.Checkbox(label="Upload single safetensors file into new repo", value=False)
         | 
| 81 | 
            +
                    is_private = gr.Checkbox(label="Create private repo", value=True)
         | 
| 82 | 
             
                    presets = gr.Radio(label="Presets", choices=list(preset_dict.keys()), value="Default")
         | 
| 83 | 
             
                    with gr.Accordion("Advanced settings", open=False):
         | 
| 84 | 
             
                        is_half = gr.Checkbox(label="Half precision", value=True)
         | 
|  | |
| 102 | 
             
                gr.on(
         | 
| 103 | 
             
                    triggers=[run_button.click],
         | 
| 104 | 
             
                    fn=convert_url_to_diffusers_repo,
         | 
| 105 | 
            +
                    inputs=[dl_url, hf_user, hf_repo, hf_token, civitai_key, is_private, is_upload_sf, repo_urls, is_half, vae, scheduler,
         | 
| 106 | 
             
                             lora1, lora1s, lora2, lora2s, lora3, lora3s, lora4, lora4s, lora5, lora5s],
         | 
| 107 | 
             
                    outputs=[repo_urls, output_md],
         | 
| 108 | 
             
                )
         | 
    	
        convert_url_to_diffusers_sdxl.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] | 
| 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_sdxl_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] | 
| 17 |  | 
| 18 |  | 
| 19 | 
             
            def download_thing(directory, url, civitai_api_key="", progress=gr.Progress(track_tqdm=True)):
         | 
| @@ -215,7 +215,8 @@ def fuse_loras(pipe, lora_dict={}, temp_dir=".", civitai_key=""): | |
| 215 | 
             
                pipe.unload_lora_weights()
         | 
| 216 |  | 
| 217 |  | 
| 218 | 
            -
            def convert_url_to_diffusers_sdxl(url, civitai_key="", is_upload_sf=False, half=True, vae=None, | 
|  | |
| 219 | 
             
                progress(0, desc="Start converting...")
         | 
| 220 | 
             
                temp_dir = "."
         | 
| 221 | 
             
                new_file = get_download_file(temp_dir, url, civitai_key)
         | 
| @@ -266,6 +267,7 @@ def convert_url_to_diffusers_sdxl(url, civitai_key="", is_upload_sf=False, half= | |
| 266 | 
             
                if not is_repo_name(new_file) and is_upload_sf:
         | 
| 267 | 
             
                    import shutil
         | 
| 268 | 
             
                    shutil.move(str(Path(new_file).resolve()), str(Path(new_repo_name, Path(new_file).name).resolve()))
         | 
|  | |
| 269 |  | 
| 270 | 
             
                progress(1, desc="Converted.")
         | 
| 271 | 
             
                return new_repo_name
         | 
| @@ -278,18 +280,19 @@ def is_repo_exists(repo_id): | |
| 278 | 
             
                    if api.repo_exists(repo_id=repo_id): return True
         | 
| 279 | 
             
                    else: return False
         | 
| 280 | 
             
                except Exception as e:
         | 
|  | |
| 281 | 
             
                    print(f"Error: Failed to connect {repo_id}. ")
         | 
| 282 | 
             
                    return True # for safe
         | 
| 283 |  | 
| 284 |  | 
| 285 | 
            -
            def create_diffusers_repo(new_repo_id, diffusers_folder, progress=gr.Progress(track_tqdm=True)):
         | 
| 286 | 
             
                from huggingface_hub import HfApi
         | 
| 287 | 
             
                import os
         | 
| 288 | 
             
                hf_token = os.environ.get("HF_TOKEN")
         | 
| 289 | 
             
                api = HfApi()
         | 
| 290 | 
             
                try:
         | 
| 291 | 
             
                    progress(0, desc="Start uploading...")
         | 
| 292 | 
            -
                    api.create_repo(repo_id=new_repo_id, token=hf_token)
         | 
| 293 | 
             
                    for path in Path(diffusers_folder).glob("*"):
         | 
| 294 | 
             
                        if path.is_dir():
         | 
| 295 | 
             
                            api.upload_folder(repo_id=new_repo_id, folder_path=str(path), path_in_repo=path.name, token=hf_token)
         | 
| @@ -304,9 +307,10 @@ def create_diffusers_repo(new_repo_id, diffusers_folder, progress=gr.Progress(tr | |
| 304 | 
             
                return url
         | 
| 305 |  | 
| 306 |  | 
| 307 | 
            -
            def convert_url_to_diffusers_repo(dl_url, hf_user, hf_repo, hf_token, civitai_key="", is_upload_sf=False, repo_urls=[], half=True, vae=None,
         | 
| 308 | 
             
                                              scheduler="Euler a", lora1=None, lora1s=1.0, lora2=None, lora2s=1.0, lora3=None, lora3s=1.0,
         | 
| 309 | 
             
                                              lora4=None, lora4s=1.0, lora5=None, lora5s=1.0, progress=gr.Progress(track_tqdm=True)):
         | 
|  | |
| 310 | 
             
                if not hf_user:
         | 
| 311 | 
             
                    print(f"Invalid user name: {hf_user}")
         | 
| 312 | 
             
                    progress(1, desc=f"Invalid user name: {hf_user}")
         | 
| @@ -314,7 +318,7 @@ def convert_url_to_diffusers_repo(dl_url, hf_user, hf_repo, hf_token, civitai_ke | |
| 314 | 
             
                if hf_token and not os.environ.get("HF_TOKEN"): os.environ['HF_TOKEN'] = hf_token
         | 
| 315 | 
             
                if not civitai_key and os.environ.get("CIVITAI_API_KEY"): civitai_key = os.environ.get("CIVITAI_API_KEY")
         | 
| 316 | 
             
                lora_dict = {lora1: lora1s, lora2: lora2s, lora3: lora3s, lora4: lora4s, lora5: lora5s}
         | 
| 317 | 
            -
                new_path = convert_url_to_diffusers_sdxl(dl_url, civitai_key, is_upload_sf, half, vae, scheduler, lora_dict)
         | 
| 318 | 
             
                if not new_path: return ""
         | 
| 319 | 
             
                new_repo_id = f"{hf_user}/{Path(new_path).stem}"
         | 
| 320 | 
             
                if hf_repo != "": new_repo_id = f"{hf_user}/{hf_repo}"
         | 
| @@ -326,7 +330,8 @@ def convert_url_to_diffusers_repo(dl_url, hf_user, hf_repo, hf_token, civitai_ke | |
| 326 | 
             
                    print(f"Repo already exists: {new_repo_id}")
         | 
| 327 | 
             
                    progress(1, desc=f"Repo already exists: {new_repo_id}")
         | 
| 328 | 
             
                    return gr.update(value=repo_urls, choices=repo_urls), gr.update(value="")
         | 
| 329 | 
            -
                repo_url = create_diffusers_repo(new_repo_id, new_path)
         | 
|  | |
| 330 | 
             
                if not repo_urls: repo_urls = []
         | 
| 331 | 
             
                repo_urls.append(repo_url)
         | 
| 332 | 
             
                md = "Your new repo:<br>"
         | 
| @@ -364,4 +369,4 @@ if __name__ == "__main__": | |
| 364 | 
             
                    for p in Path(args.loras).glob('**/*.safetensors'):
         | 
| 365 | 
             
                        lora_dict[str(p)] = 1.0
         | 
| 366 |  | 
| 367 | 
            -
                convert_url_to_diffusers_sdxl(args.url, args.civitai_key, args.half, args.vae, args.scheduler, lora_dict)
         | 
|  | |
| 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)):
         | 
|  | |
| 215 | 
             
                pipe.unload_lora_weights()
         | 
| 216 |  | 
| 217 |  | 
| 218 | 
            +
            def convert_url_to_diffusers_sdxl(url, civitai_key="", is_upload_sf=False, half=True, vae=None,
         | 
| 219 | 
            +
                                              scheduler="Euler a", lora_dict={}, 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)
         | 
|  | |
| 267 | 
             
                if not is_repo_name(new_file) and is_upload_sf:
         | 
| 268 | 
             
                    import shutil
         | 
| 269 | 
             
                    shutil.move(str(Path(new_file).resolve()), str(Path(new_repo_name, Path(new_file).name).resolve()))
         | 
| 270 | 
            +
                elif not is_local: os.remove(new_file)
         | 
| 271 |  | 
| 272 | 
             
                progress(1, desc="Converted.")
         | 
| 273 | 
             
                return new_repo_name
         | 
|  | |
| 280 | 
             
                    if api.repo_exists(repo_id=repo_id): return True
         | 
| 281 | 
             
                    else: return False
         | 
| 282 | 
             
                except Exception as e:
         | 
| 283 | 
            +
                    print(e)
         | 
| 284 | 
             
                    print(f"Error: Failed to connect {repo_id}. ")
         | 
| 285 | 
             
                    return True # for safe
         | 
| 286 |  | 
| 287 |  | 
| 288 | 
            +
            def create_diffusers_repo(new_repo_id, diffusers_folder, is_private, progress=gr.Progress(track_tqdm=True)):
         | 
| 289 | 
             
                from huggingface_hub import HfApi
         | 
| 290 | 
             
                import os
         | 
| 291 | 
             
                hf_token = os.environ.get("HF_TOKEN")
         | 
| 292 | 
             
                api = HfApi()
         | 
| 293 | 
             
                try:
         | 
| 294 | 
             
                    progress(0, desc="Start uploading...")
         | 
| 295 | 
            +
                    api.create_repo(repo_id=new_repo_id, token=hf_token, private=is_private)
         | 
| 296 | 
             
                    for path in Path(diffusers_folder).glob("*"):
         | 
| 297 | 
             
                        if path.is_dir():
         | 
| 298 | 
             
                            api.upload_folder(repo_id=new_repo_id, folder_path=str(path), path_in_repo=path.name, token=hf_token)
         | 
|  | |
| 307 | 
             
                return url
         | 
| 308 |  | 
| 309 |  | 
| 310 | 
            +
            def convert_url_to_diffusers_repo(dl_url, hf_user, hf_repo, hf_token, civitai_key="", is_private=True, is_upload_sf=False, repo_urls=[], half=True, vae=None,
         | 
| 311 | 
             
                                              scheduler="Euler a", lora1=None, lora1s=1.0, lora2=None, lora2s=1.0, lora3=None, lora3s=1.0,
         | 
| 312 | 
             
                                              lora4=None, lora4s=1.0, lora5=None, lora5s=1.0, progress=gr.Progress(track_tqdm=True)):
         | 
| 313 | 
            +
                import shutil
         | 
| 314 | 
             
                if not hf_user:
         | 
| 315 | 
             
                    print(f"Invalid user name: {hf_user}")
         | 
| 316 | 
             
                    progress(1, desc=f"Invalid user name: {hf_user}")
         | 
|  | |
| 318 | 
             
                if hf_token and not os.environ.get("HF_TOKEN"): os.environ['HF_TOKEN'] = hf_token
         | 
| 319 | 
             
                if not civitai_key and os.environ.get("CIVITAI_API_KEY"): civitai_key = os.environ.get("CIVITAI_API_KEY")
         | 
| 320 | 
             
                lora_dict = {lora1: lora1s, lora2: lora2s, lora3: lora3s, lora4: lora4s, lora5: lora5s}
         | 
| 321 | 
            +
                new_path = convert_url_to_diffusers_sdxl(dl_url, civitai_key, is_upload_sf, half, vae, scheduler, lora_dict, False)
         | 
| 322 | 
             
                if not new_path: return ""
         | 
| 323 | 
             
                new_repo_id = f"{hf_user}/{Path(new_path).stem}"
         | 
| 324 | 
             
                if hf_repo != "": new_repo_id = f"{hf_user}/{hf_repo}"
         | 
|  | |
| 330 | 
             
                    print(f"Repo already exists: {new_repo_id}")
         | 
| 331 | 
             
                    progress(1, desc=f"Repo already exists: {new_repo_id}")
         | 
| 332 | 
             
                    return gr.update(value=repo_urls, choices=repo_urls), gr.update(value="")
         | 
| 333 | 
            +
                repo_url = create_diffusers_repo(new_repo_id, new_path, is_private)
         | 
| 334 | 
            +
                shutil.rmtree(new_path)
         | 
| 335 | 
             
                if not repo_urls: repo_urls = []
         | 
| 336 | 
             
                repo_urls.append(repo_url)
         | 
| 337 | 
             
                md = "Your new repo:<br>"
         | 
|  | |
| 369 | 
             
                    for p in Path(args.loras).glob('**/*.safetensors'):
         | 
| 370 | 
             
                        lora_dict[str(p)] = 1.0
         | 
| 371 |  | 
| 372 | 
            +
                convert_url_to_diffusers_sdxl(args.url, args.civitai_key, args.half, args.vae, args.scheduler, lora_dict, True)
         | 
    	
        local/convert_url_to_diffusers_sdxl.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] | 
| 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=""):
         | 
