Himanshu-AT commited on
Commit
5193654
·
1 Parent(s): 0712200
Files changed (1) hide show
  1. app.py +161 -114
app.py CHANGED
@@ -1,124 +1,171 @@
1
  import gradio as gr
2
- import os
3
- from PIL import Image
 
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 huggingface_hub
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
- MARKDOWN = """
31
- # FLUX.1-dev-Inpainting
32
-
33
- #### VERSION: 0.0.3_beta
34
-
35
- Original Model: Flux.1-dev
36
- FluxControlNet: alimama-creative
37
 
38
- """
39
 
40
- @spaces.GPU()
41
- def process(input_image_editor,
42
- prompt,
43
- negative_prompt,
44
- controlnet_conditioning_scale,
45
- guidance_scale,
46
- seed,
47
- num_inference_steps,
48
- true_guidance_scale
49
- ):
50
- image = input_image_editor['background']
51
- mask = input_image_editor['layers'][0]
52
- size = (768, 768)
53
- image_or = image.copy()
54
-
55
- image = image.convert("RGB").resize(size)
56
- mask = mask.convert("RGB").resize(size)
57
- generator = torch.Generator(device="cuda").manual_seed(seed)
58
- result = pipe(
59
- prompt=prompt,
60
- height=size[1],
61
- width=size[0],
62
- control_image=image,
63
- control_mask=mask,
64
- num_inference_steps=num_inference_steps,
65
- generator=generator,
66
- controlnet_conditioning_scale=controlnet_conditioning_scale,
67
- guidance_scale=guidance_scale,
68
- negative_prompt=negative_prompt,
69
- true_guidance_scale=true_guidance_scale
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  ).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
- return result.resize((image_or.size[:2]))
73
-
74
- with gr.Blocks() as demo:
75
- gr.Markdown(MARKDOWN)
76
- with gr.Row():
77
- with gr.Column():
78
- input_image_editor_component = gr.ImageEditor(
79
- label='Image',
80
- type='pil',
81
- sources=["upload"],
82
- image_mode='RGB',
83
- layers=False,
84
- brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"))
85
-
86
-
87
- prompt = gr.Textbox(lines=2, placeholder="Enter prompt here...")
88
- negative_prompt = gr.Textbox(lines=2, placeholder="Enter negative_prompt here...")
89
- controlnet_conditioning_scale = gr.Slider(minimum=0, step=0.01, maximum=1, value=0.8, label="controlnet_conditioning_scale") # Adjusted value
90
- guidance_scale = gr.Slider(minimum=1, step=0.5, maximum=10, value=5.0, label="Image to generate") # Adjusted value
91
- seed = gr.Slider(minimum=0, step=1, maximum=10000000, value=124, label="Seed Value")
92
- num_inference_steps = gr.Slider(minimum=1, step=1, maximum=50, value=40, label="num_inference_steps") # Adjusted value
93
- true_guidance_scale = gr.Slider(minimum=1, step=1, maximum=10, value=7.0, label="true_guidance_scale") # Adjusted value
94
-
95
-
96
-
97
- submit_button_component = gr.Button(
98
- value='Submit', variant='primary', scale=0)
99
-
100
- with gr.Column():
101
- output_image_component = gr.Image(
102
- type='pil', image_mode='RGB', label='Generated image', format="png")
103
-
104
- submit_button_component.click(
105
- fn=process,
106
- inputs=[
107
- input_image_editor_component,
108
- prompt,
109
- negative_prompt,
110
- controlnet_conditioning_scale,
111
- guidance_scale,
112
- seed,
113
- num_inference_steps,
114
- true_guidance_scale
115
-
116
- ],
117
- outputs=[
118
- output_image_component,
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()