Spaces:
				
			
			
	
			
			
		Running
		
			on 
			
			Zero
	
	
	
			
			
	
	
	
	
		
		
		Running
		
			on 
			
			Zero
	Upload 4 files
Browse files- .gitattributes +1 -0
 - app.py +116 -0
 - assets/Inpainting mask.png +0 -0
 - assets/rocket.png +3 -0
 - requirements.txt +7 -0
 
    	
        .gitattributes
    CHANGED
    
    | 
         @@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text 
     | 
|
| 33 | 
         
             
            *.zip filter=lfs diff=lfs merge=lfs -text
         
     | 
| 34 | 
         
             
            *.zst filter=lfs diff=lfs merge=lfs -text
         
     | 
| 35 | 
         
             
            *tfevents* filter=lfs diff=lfs merge=lfs -text
         
     | 
| 
         | 
| 
         | 
|
| 33 | 
         
             
            *.zip filter=lfs diff=lfs merge=lfs -text
         
     | 
| 34 | 
         
             
            *.zst filter=lfs diff=lfs merge=lfs -text
         
     | 
| 35 | 
         
             
            *tfevents* filter=lfs diff=lfs merge=lfs -text
         
     | 
| 36 | 
         
            +
            assets/rocket.png filter=lfs diff=lfs merge=lfs -text
         
     | 
    	
        app.py
    ADDED
    
    | 
         @@ -0,0 +1,116 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            import random
         
     | 
| 2 | 
         
            +
             
     | 
| 3 | 
         
            +
            import numpy as np
         
     | 
| 4 | 
         
            +
            import torch
         
     | 
| 5 | 
         
            +
            import spaces
         
     | 
| 6 | 
         
            +
            import gradio as gr
         
     | 
| 7 | 
         
            +
            from diffusers import FluxFillPipeline
         
     | 
| 8 | 
         
            +
             
     | 
| 9 | 
         
            +
            MAX_SEED = np.iinfo(np.int32).max
         
     | 
| 10 | 
         
            +
             
     | 
| 11 | 
         
            +
            pipe = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16)
         
     | 
| 12 | 
         
            +
            lora_repo = "strangerzonehf/Flux-Super-Realism-LoRA"
         
     | 
| 13 | 
         
            +
            trigger_word = "Super Realism"
         
     | 
| 14 | 
         
            +
            pipe.load_lora_weights(lora_repo)
         
     | 
| 15 | 
         
            +
            pipe.to("cuda")
         
     | 
| 16 | 
         
            +
             
     | 
| 17 | 
         
            +
            # reference https://huggingface.co/spaces/black-forest-labs/FLUX.1-Fill-dev/blob/main/app.py 
         
     | 
| 18 | 
         
            +
            def calculate_optimal_dimensions(image):
         
     | 
| 19 | 
         
            +
                # Extract the original dimensions
         
     | 
| 20 | 
         
            +
                original_width, original_height = image.size
         
     | 
| 21 | 
         
            +
                
         
     | 
| 22 | 
         
            +
                # Set constants
         
     | 
| 23 | 
         
            +
                MIN_ASPECT_RATIO = 9 / 16
         
     | 
| 24 | 
         
            +
                MAX_ASPECT_RATIO = 16 / 9
         
     | 
| 25 | 
         
            +
                FIXED_DIMENSION = 1024
         
     | 
| 26 | 
         
            +
             
     | 
| 27 | 
         
            +
                # Calculate the aspect ratio of the original image
         
     | 
| 28 | 
         
            +
                original_aspect_ratio = original_width / original_height
         
     | 
| 29 | 
         
            +
             
     | 
| 30 | 
         
            +
                # Determine which dimension to fix
         
     | 
| 31 | 
         
            +
                if original_aspect_ratio > 1:  # Wider than tall
         
     | 
| 32 | 
         
            +
                    width = FIXED_DIMENSION
         
     | 
| 33 | 
         
            +
                    height = round(FIXED_DIMENSION / original_aspect_ratio)
         
     | 
| 34 | 
         
            +
                else:  # Taller than wide
         
     | 
| 35 | 
         
            +
                    height = FIXED_DIMENSION
         
     | 
| 36 | 
         
            +
                    width = round(FIXED_DIMENSION * original_aspect_ratio)
         
     | 
| 37 | 
         
            +
             
     | 
| 38 | 
         
            +
                # Ensure dimensions are multiples of 8
         
     | 
| 39 | 
         
            +
                width = (width // 8) * 8
         
     | 
| 40 | 
         
            +
                height = (height // 8) * 8
         
     | 
| 41 | 
         
            +
             
     | 
| 42 | 
         
            +
                # Enforce aspect ratio limits
         
     | 
| 43 | 
         
            +
                calculated_aspect_ratio = width / height
         
     | 
| 44 | 
         
            +
                if calculated_aspect_ratio > MAX_ASPECT_RATIO:
         
     | 
| 45 | 
         
            +
                    width = (height * MAX_ASPECT_RATIO // 8) * 8
         
     | 
| 46 | 
         
            +
                elif calculated_aspect_ratio < MIN_ASPECT_RATIO:
         
     | 
| 47 | 
         
            +
                    height = (width / MIN_ASPECT_RATIO // 8) * 8
         
     | 
| 48 | 
         
            +
             
     | 
| 49 | 
         
            +
                # Ensure width and height remain above the minimum dimensions
         
     | 
| 50 | 
         
            +
                width = max(width, 576) if width == FIXED_DIMENSION else width
         
     | 
| 51 | 
         
            +
                height = max(height, 576) if height == FIXED_DIMENSION else height
         
     | 
| 52 | 
         
            +
             
     | 
| 53 | 
         
            +
                return width, height
         
     | 
| 54 | 
         
            +
             
     | 
| 55 | 
         
            +
             
     | 
| 56 | 
         
            +
            @spaces.GPU(duration=30)
         
     | 
| 57 | 
         
            +
            def inpaint(
         
     | 
| 58 | 
         
            +
                image,
         
     | 
| 59 | 
         
            +
                mask,
         
     | 
| 60 | 
         
            +
                prompt="",
         
     | 
| 61 | 
         
            +
                seed=0,
         
     | 
| 62 | 
         
            +
                num_inference_steps=28,
         
     | 
| 63 | 
         
            +
                guidance_scale=50,
         
     | 
| 64 | 
         
            +
            ):
         
     | 
| 65 | 
         
            +
                image = image.convert("RGB")
         
     | 
| 66 | 
         
            +
                mask = mask.convert("L")
         
     | 
| 67 | 
         
            +
                width, height = calculate_optimal_dimensions(image)
         
     | 
| 68 | 
         
            +
             
     | 
| 69 | 
         
            +
                if trigger_word:
         
     | 
| 70 | 
         
            +
                    prompt = f"{trigger_word} {prompt}"
         
     | 
| 71 | 
         
            +
                    
         
     | 
| 72 | 
         
            +
                if not isinstance(seed, int) or seed <= 0:        
         
     | 
| 73 | 
         
            +
                    seed = random.randint(0, MAX_SEED)
         
     | 
| 74 | 
         
            +
                    
         
     | 
| 75 | 
         
            +
                result = pipe(
         
     | 
| 76 | 
         
            +
                    image=image,
         
     | 
| 77 | 
         
            +
                    mask_image=mask,
         
     | 
| 78 | 
         
            +
                    prompt=prompt,
         
     | 
| 79 | 
         
            +
                    width=width,        
         
     | 
| 80 | 
         
            +
                    height=height,        
         
     | 
| 81 | 
         
            +
                    num_inference_steps=num_inference_steps,
         
     | 
| 82 | 
         
            +
                    guidance_scale=guidance_scale,
         
     | 
| 83 | 
         
            +
                    generator = torch.Generator().manual_seed(seed)
         
     | 
| 84 | 
         
            +
                ).images[0]
         
     | 
| 85 | 
         
            +
             
     | 
| 86 | 
         
            +
                result = result.convert("RGBA")
         
     | 
| 87 | 
         
            +
             
     | 
| 88 | 
         
            +
                return result, seed
         
     | 
| 89 | 
         
            +
             
     | 
| 90 | 
         
            +
            demo = gr.Interface(
         
     | 
| 91 | 
         
            +
                fn=inpaint,
         
     | 
| 92 | 
         
            +
                inputs=[
         
     | 
| 93 | 
         
            +
                    gr.Image(label="image", type="pil"),
         
     | 
| 94 | 
         
            +
                    gr.Image(label="mask", type="pil"),
         
     | 
| 95 | 
         
            +
                    gr.Text(label="prompt", lines=4),
         
     | 
| 96 | 
         
            +
                    gr.Slider(
         
     | 
| 97 | 
         
            +
                        label="Seed",
         
     | 
| 98 | 
         
            +
                        minimum=0,
         
     | 
| 99 | 
         
            +
                        maximum=MAX_SEED,
         
     | 
| 100 | 
         
            +
                        step=1,
         
     | 
| 101 | 
         
            +
                        value=0,
         
     | 
| 102 | 
         
            +
                        info="(0 = Random)"
         
     | 
| 103 | 
         
            +
                    ),
         
     | 
| 104 | 
         
            +
                    gr.Number(value=40, label="num_inference_steps"),
         
     | 
| 105 | 
         
            +
                    gr.Number(value=28, label="guidance_scale"),
         
     | 
| 106 | 
         
            +
                ],
         
     | 
| 107 | 
         
            +
                outputs=[
         
     | 
| 108 | 
         
            +
                    gr.Image(label="Result"),
         
     | 
| 109 | 
         
            +
                    gr.Number(label="Seed")
         
     | 
| 110 | 
         
            +
                ],
         
     | 
| 111 | 
         
            +
                api_name="inpaint",
         
     | 
| 112 | 
         
            +
                examples=[["./assets/rocket.png", "./assets/Inpainting mask.png"]],
         
     | 
| 113 | 
         
            +
                cache_examples=False
         
     | 
| 114 | 
         
            +
            )
         
     | 
| 115 | 
         
            +
             
     | 
| 116 | 
         
            +
            demo.launch()
         
     | 
    	
        assets/Inpainting mask.png
    ADDED
    
    
											 
									 | 
									
								
    	
        assets/rocket.png
    ADDED
    
    
											 
									 | 
									
								
											Git LFS Details
  | 
									
    	
        requirements.txt
    ADDED
    
    | 
         @@ -0,0 +1,7 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            diffusers
         
     | 
| 2 | 
         
            +
            transformers
         
     | 
| 3 | 
         
            +
            git+https://github.com/huggingface/peft.git
         
     | 
| 4 | 
         
            +
            git+https://github.com/huggingface/accelerate.git
         
     | 
| 5 | 
         
            +
            spaces
         
     | 
| 6 | 
         
            +
            sentencepiece
         
     | 
| 7 | 
         
            +
            accelerate
         
     |