Spaces:
				
			
			
	
			
			
		Running
		
			on 
			
			Zero
	
	
	
			
			
	
	
	
	
		
		
		Running
		
			on 
			
			Zero
	提前加载lora
Browse files
    	
        app.py
    CHANGED
    
    | @@ -32,14 +32,12 @@ pipe = FluxPipeline.from_pretrained( | |
| 32 | 
             
            set_single_lora(pipe.transformer, omni_consistency_path,
         | 
| 33 | 
             
                            lora_weights=[1], cond_size=512)
         | 
| 34 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 35 | 
             
            def download_all_loras():
         | 
| 36 | 
            -
                lora_names = [
         | 
| 37 | 
            -
                    "3D_Chibi", "American_Cartoon", "Chinese_Ink", "Clay_Toy",
         | 
| 38 | 
            -
                    "Fabric", "Ghibli", "Irasutoya", "Jojo", "LEGO", "Line",
         | 
| 39 | 
            -
                    "Macaron", "Oil_Painting", "Origami", "Paper_Cutting",
         | 
| 40 | 
            -
                    "Picasso", "Pixel", "Poly", "Pop_Art", "Rick_Morty",
         | 
| 41 | 
            -
                    "Snoopy", "Van_Gogh", "Vector"
         | 
| 42 | 
            -
                ]
         | 
| 43 | 
             
                for name in lora_names:
         | 
| 44 | 
             
                    hf_hub_download(
         | 
| 45 | 
             
                        repo_id="showlab/OmniConsistency",
         | 
| @@ -48,15 +46,23 @@ def download_all_loras(): | |
| 48 | 
             
                    )
         | 
| 49 | 
             
            download_all_loras()
         | 
| 50 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 51 | 
             
            def clear_cache(transformer):
         | 
| 52 | 
             
                for _, attn_processor in transformer.attn_processors.items():
         | 
| 53 | 
             
                    attn_processor.bank_kv.clear()
         | 
| 54 |  | 
| 55 | 
             
            @spaces.GPU()
         | 
| 56 | 
             
            def generate_image(
         | 
| 57 | 
            -
                lora_name, | 
| 58 | 
            -
                custom_repo_id,  
         | 
| 59 | 
            -
                custom_weight_name, 
         | 
| 60 | 
             
                prompt,
         | 
| 61 | 
             
                uploaded_image,
         | 
| 62 | 
             
                width, height,
         | 
| @@ -65,31 +71,9 @@ def generate_image( | |
| 65 | 
             
                seed
         | 
| 66 | 
             
            ):
         | 
| 67 | 
             
                width, height = int(width), int(height)
         | 
| 68 | 
            -
                generator = torch.Generator("cpu").manual_seed(seed)
         | 
| 69 | 
            -
             | 
| 70 | 
            -
                if custom_repo_id and custom_repo_id.strip():
         | 
| 71 | 
            -
                    repo_id = custom_repo_id.strip()
         | 
| 72 | 
            -
                    try:
         | 
| 73 | 
            -
                        lora_path = hf_hub_download(
         | 
| 74 | 
            -
                            repo_id=repo_id,
         | 
| 75 | 
            -
                            filename=custom_weight_name,
         | 
| 76 | 
            -
                            local_dir=CUSTOM_LORA_DIR,
         | 
| 77 | 
            -
                        )
         | 
| 78 | 
            -
                    except Exception as e:
         | 
| 79 | 
            -
                        raise gr.Error(f"Load custom LoRA failed: {e}")
         | 
| 80 | 
            -
                else:
         | 
| 81 | 
            -
                    lora_path = os.path.join(
         | 
| 82 | 
            -
                        f"{LOCAL_LORA_DIR}/LoRAs", f"{lora_name}_rank128_bf16.safetensors"
         | 
| 83 | 
            -
                    )
         | 
| 84 |  | 
| 85 | 
            -
                pipe. | 
| 86 | 
            -
                try:
         | 
| 87 | 
            -
                    pipe.load_lora_weights(
         | 
| 88 | 
            -
                        os.path.dirname(lora_path),
         | 
| 89 | 
            -
                        weight_name=os.path.basename(lora_path)
         | 
| 90 | 
            -
                    )
         | 
| 91 | 
            -
                except Exception as e:
         | 
| 92 | 
            -
                    raise gr.Error(f"Load LoRA failed: {e}")
         | 
| 93 |  | 
| 94 | 
             
                spatial_image  = [uploaded_image.convert("RGB")]
         | 
| 95 | 
             
                subject_images = []
         | 
| @@ -113,16 +97,9 @@ def generate_image( | |
| 113 |  | 
| 114 | 
             
            # =============== Gradio UI ===============
         | 
| 115 | 
             
            def create_interface():
         | 
| 116 | 
            -
                demo_lora_names = [
         | 
| 117 | 
            -
                    "3D_Chibi", "American_Cartoon", "Chinese_Ink", "Clay_Toy",
         | 
| 118 | 
            -
                    "Fabric", "Ghibli", "Irasutoya", "Jojo", "LEGO", "Line",
         | 
| 119 | 
            -
                    "Macaron", "Oil_Painting", "Origami", "Paper_Cutting",
         | 
| 120 | 
            -
                    "Picasso", "Pixel", "Poly", "Pop_Art", "Rick_Morty",
         | 
| 121 | 
            -
                    "Snoopy", "Van_Gogh", "Vector"
         | 
| 122 | 
            -
                ]
         | 
| 123 |  | 
| 124 | 
             
                def update_trigger_word(lora_name, prompt):
         | 
| 125 | 
            -
                  for name in  | 
| 126 | 
             
                    trigger = " ".join(name.split("_")) + " style,"
         | 
| 127 | 
             
                    prompt = prompt.replace(trigger, "")
         | 
| 128 | 
             
                  new_trigger = " ".join(lora_name.split("_"))+ " style,"
         | 
|  | |
| 32 | 
             
            set_single_lora(pipe.transformer, omni_consistency_path,
         | 
| 33 | 
             
                            lora_weights=[1], cond_size=512)
         | 
| 34 |  | 
| 35 | 
            +
            lora_names = [
         | 
| 36 | 
            +
                "3D_Chibi", "American_Cartoon", "Macaron",
         | 
| 37 | 
            +
                "Pixel", "Poly", "Van_Gogh"
         | 
| 38 | 
            +
            ]
         | 
| 39 | 
            +
             | 
| 40 | 
             
            def download_all_loras():
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 41 | 
             
                for name in lora_names:
         | 
| 42 | 
             
                    hf_hub_download(
         | 
| 43 | 
             
                        repo_id="showlab/OmniConsistency",
         | 
|  | |
| 46 | 
             
                    )
         | 
| 47 | 
             
            download_all_loras()
         | 
| 48 |  | 
| 49 | 
            +
            def reload_all_loras():
         | 
| 50 | 
            +
                pipe.unload_lora_weights()
         | 
| 51 | 
            +
                for name in lora_names:
         | 
| 52 | 
            +
                    pipe.load_lora_weights(
         | 
| 53 | 
            +
                        "./LoRAs",
         | 
| 54 | 
            +
                        weight_name=f"{name}_rank128_bf16.safetensors",
         | 
| 55 | 
            +
                        adapter_name=name,
         | 
| 56 | 
            +
                    )
         | 
| 57 | 
            +
            reload_all_loras()
         | 
| 58 | 
            +
                    
         | 
| 59 | 
             
            def clear_cache(transformer):
         | 
| 60 | 
             
                for _, attn_processor in transformer.attn_processors.items():
         | 
| 61 | 
             
                    attn_processor.bank_kv.clear()
         | 
| 62 |  | 
| 63 | 
             
            @spaces.GPU()
         | 
| 64 | 
             
            def generate_image(
         | 
| 65 | 
            +
                lora_name,
         | 
|  | |
|  | |
| 66 | 
             
                prompt,
         | 
| 67 | 
             
                uploaded_image,
         | 
| 68 | 
             
                width, height,
         | 
|  | |
| 71 | 
             
                seed
         | 
| 72 | 
             
            ):
         | 
| 73 | 
             
                width, height = int(width), int(height)
         | 
| 74 | 
            +
                generator = torch.Generator("cpu").manual_seed(seed) 
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 75 |  | 
| 76 | 
            +
                pipe.set_adapters(lora_name)
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 77 |  | 
| 78 | 
             
                spatial_image  = [uploaded_image.convert("RGB")]
         | 
| 79 | 
             
                subject_images = []
         | 
|  | |
| 97 |  | 
| 98 | 
             
            # =============== Gradio UI ===============
         | 
| 99 | 
             
            def create_interface():
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 100 |  | 
| 101 | 
             
                def update_trigger_word(lora_name, prompt):
         | 
| 102 | 
            +
                  for name in lora_names:
         | 
| 103 | 
             
                    trigger = " ".join(name.split("_")) + " style,"
         | 
| 104 | 
             
                    prompt = prompt.replace(trigger, "")
         | 
| 105 | 
             
                  new_trigger = " ".join(lora_name.split("_"))+ " style,"
         |