Spaces:
Runtime error
Runtime error
Himanshu-AT commited on
Commit ·
5193654
1
Parent(s): 0712200
change ar
Browse files
app.py
CHANGED
|
@@ -1,124 +1,171 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
|
|
|
| 4 |
import torch
|
| 5 |
-
from diffusers.utils import load_image, check_min_version
|
| 6 |
-
from controlnet_flux import FluxControlNetModel
|
| 7 |
-
from transformer_flux import FluxTransformer2DModel
|
| 8 |
-
from pipeline_flux_controlnet_inpaint import FluxControlNetInpaintingPipeline
|
| 9 |
import spaces
|
| 10 |
-
import
|
| 11 |
-
huggingface_hub.login(os.getenv('HF_TOKEN'))
|
| 12 |
-
|
| 13 |
-
check_min_version("0.30.2")
|
| 14 |
-
transformer = FluxTransformer2DModel.from_pretrained(
|
| 15 |
-
"black-forest-labs/FLUX.1-dev", subfolder='transformer', torch_dytpe=torch.bfloat16
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
# Build pipeline
|
| 19 |
-
# controlnet = FluxControlNetModel.from_pretrained("alimama-creative/FLUX.1-dev-Controlnet-Inpainting-Beta", torch_dtype=torch.bfloat16)
|
| 20 |
-
pipe = FluxControlNetInpaintingPipeline.from_pretrained(
|
| 21 |
-
"black-forest-labs/FLUX.1-dev",
|
| 22 |
-
# controlnet=controlnet,
|
| 23 |
-
transformer=transformer,
|
| 24 |
-
torch_dtype=torch.bfloat16
|
| 25 |
-
).to("cuda")
|
| 26 |
-
pipe.transformer.to(torch.bfloat16)
|
| 27 |
-
pipe.controlnet.to(torch.bfloat16)
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
#### VERSION: 0.0.3_beta
|
| 34 |
-
|
| 35 |
-
Original Model: Flux.1-dev
|
| 36 |
-
FluxControlNet: alimama-creative
|
| 37 |
|
| 38 |
-
"""
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
with gr.
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
with gr.
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
)
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
demo.launch(debug=False, show_error=True,share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
import spaces
|
| 5 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import spaces
|
| 7 |
+
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
from diffusers import FluxFillPipeline
|
| 10 |
+
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
|
|
|
| 12 |
|
| 13 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 14 |
+
MAX_IMAGE_SIZE = 2048
|
| 15 |
+
|
| 16 |
+
pipe = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16).to("cuda")
|
| 17 |
+
|
| 18 |
+
def calculate_optimal_dimensions(image: Image.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 |
+
@spaces.GPU
|
| 56 |
+
def infer(edit_images, prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
|
| 57 |
+
image = edit_images["background"]
|
| 58 |
+
width, height = calculate_optimal_dimensions(image)
|
| 59 |
+
mask = edit_images["layers"][0]
|
| 60 |
+
if randomize_seed:
|
| 61 |
+
seed = random.randint(0, MAX_SEED)
|
| 62 |
+
image = pipe(
|
| 63 |
+
prompt=prompt,
|
| 64 |
+
image=image,
|
| 65 |
+
mask_image=mask,
|
| 66 |
+
height=height,
|
| 67 |
+
width=width,
|
| 68 |
+
guidance_scale=guidance_scale,
|
| 69 |
+
num_inference_steps=num_inference_steps,
|
| 70 |
+
generator=torch.Generator("cpu").manual_seed(seed)
|
| 71 |
).images[0]
|
| 72 |
+
return image, seed
|
| 73 |
+
|
| 74 |
+
examples = [
|
| 75 |
+
"a tiny astronaut hatching from an egg on the moon",
|
| 76 |
+
"a cat holding a sign that says hello world",
|
| 77 |
+
"an anime illustration of a wiener schnitzel",
|
| 78 |
+
]
|
| 79 |
+
|
| 80 |
+
css="""
|
| 81 |
+
#col-container {
|
| 82 |
+
margin: 0 auto;
|
| 83 |
+
max-width: 1000px;
|
| 84 |
+
}
|
| 85 |
+
"""
|
| 86 |
|
| 87 |
+
with gr.Blocks(css=css) as demo:
|
| 88 |
+
|
| 89 |
+
with gr.Column(elem_id="col-container"):
|
| 90 |
+
gr.Markdown(f"""# FLUX.1 [dev]
|
| 91 |
+
""")
|
| 92 |
+
with gr.Row():
|
| 93 |
+
with gr.Column():
|
| 94 |
+
edit_image = gr.ImageEditor(
|
| 95 |
+
label='Upload and draw mask for inpainting',
|
| 96 |
+
type='pil',
|
| 97 |
+
sources=["upload", "webcam"],
|
| 98 |
+
image_mode='RGB',
|
| 99 |
+
layers=False,
|
| 100 |
+
brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"),
|
| 101 |
+
height=600
|
| 102 |
+
)
|
| 103 |
+
prompt = gr.Text(
|
| 104 |
+
label="Prompt",
|
| 105 |
+
show_label=False,
|
| 106 |
+
max_lines=1,
|
| 107 |
+
placeholder="Enter your prompt",
|
| 108 |
+
container=False,
|
| 109 |
+
)
|
| 110 |
+
run_button = gr.Button("Run")
|
| 111 |
+
|
| 112 |
+
result = gr.Image(label="Result", show_label=False)
|
| 113 |
+
|
| 114 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 115 |
+
|
| 116 |
+
seed = gr.Slider(
|
| 117 |
+
label="Seed",
|
| 118 |
+
minimum=0,
|
| 119 |
+
maximum=MAX_SEED,
|
| 120 |
+
step=1,
|
| 121 |
+
value=0,
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 125 |
+
|
| 126 |
+
with gr.Row():
|
| 127 |
+
|
| 128 |
+
width = gr.Slider(
|
| 129 |
+
label="Width",
|
| 130 |
+
minimum=256,
|
| 131 |
+
maximum=MAX_IMAGE_SIZE,
|
| 132 |
+
step=32,
|
| 133 |
+
value=1024,
|
| 134 |
+
visible=False
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
height = gr.Slider(
|
| 138 |
+
label="Height",
|
| 139 |
+
minimum=256,
|
| 140 |
+
maximum=MAX_IMAGE_SIZE,
|
| 141 |
+
step=32,
|
| 142 |
+
value=1024,
|
| 143 |
+
visible=False
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
with gr.Row():
|
| 147 |
+
|
| 148 |
+
guidance_scale = gr.Slider(
|
| 149 |
+
label="Guidance Scale",
|
| 150 |
+
minimum=1,
|
| 151 |
+
maximum=30,
|
| 152 |
+
step=0.5,
|
| 153 |
+
value=50,
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
num_inference_steps = gr.Slider(
|
| 157 |
+
label="Number of inference steps",
|
| 158 |
+
minimum=1,
|
| 159 |
+
maximum=50,
|
| 160 |
+
step=1,
|
| 161 |
+
value=28,
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
gr.on(
|
| 165 |
+
triggers=[run_button.click, prompt.submit],
|
| 166 |
+
fn = infer,
|
| 167 |
+
inputs = [edit_image, prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 168 |
+
outputs = [result, seed]
|
| 169 |
)
|
| 170 |
|
| 171 |
+
demo.launch()
|
|
|
|
|
|