multimodalart HF staff commited on
Commit
0c9f948
·
verified ·
1 Parent(s): abdfac8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -3
app.py CHANGED
@@ -13,10 +13,47 @@ MAX_IMAGE_SIZE = 2048
13
 
14
  pipe = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16, revision="refs/pr/4").to("cuda")
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  @spaces.GPU
17
  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)):
18
- print(edit_images)
19
  image = edit_images["background"]
 
20
  mask = edit_images["layers"][0]
21
  if randomize_seed:
22
  seed = random.randint(0, MAX_SEED)
@@ -41,7 +78,7 @@ examples = [
41
  css="""
42
  #col-container {
43
  margin: 0 auto;
44
- max-width: 960px;
45
  }
46
  """
47
 
@@ -70,7 +107,7 @@ with gr.Blocks(css=css) as demo:
70
  placeholder="Enter your prompt",
71
  container=False,
72
  )
73
- run_button = gr.Button("Run", scale=0)
74
 
75
  result = gr.Image(label="Result", show_label=False)
76
 
@@ -94,6 +131,7 @@ with gr.Blocks(css=css) as demo:
94
  maximum=MAX_IMAGE_SIZE,
95
  step=32,
96
  value=1024,
 
97
  )
98
 
99
  height = gr.Slider(
@@ -102,6 +140,7 @@ with gr.Blocks(css=css) as demo:
102
  maximum=MAX_IMAGE_SIZE,
103
  step=32,
104
  value=1024,
 
105
  )
106
 
107
  with gr.Row():
 
13
 
14
  pipe = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16, revision="refs/pr/4").to("cuda")
15
 
16
+ def calculate_optimal_dimensions(image: Image.Image):
17
+ # Extract the original dimensions
18
+ original_width, original_height = image.size
19
+
20
+ # Set constants
21
+ MIN_ASPECT_RATIO = 9 / 16
22
+ MAX_ASPECT_RATIO = 16 / 9
23
+ FIXED_DIMENSION = 1024
24
+
25
+ # Calculate the aspect ratio of the original image
26
+ original_aspect_ratio = original_width / original_height
27
+
28
+ # Determine which dimension to fix
29
+ if original_aspect_ratio > 1: # Wider than tall
30
+ width = FIXED_DIMENSION
31
+ height = round(FIXED_DIMENSION / original_aspect_ratio)
32
+ else: # Taller than wide
33
+ height = FIXED_DIMENSION
34
+ width = round(FIXED_DIMENSION * original_aspect_ratio)
35
+
36
+ # Ensure dimensions are multiples of 8
37
+ width = (width // 8) * 8
38
+ height = (height // 8) * 8
39
+
40
+ # Enforce aspect ratio limits
41
+ calculated_aspect_ratio = width / height
42
+ if calculated_aspect_ratio > MAX_ASPECT_RATIO:
43
+ width = (height * MAX_ASPECT_RATIO // 8) * 8
44
+ elif calculated_aspect_ratio < MIN_ASPECT_RATIO:
45
+ height = (width / MIN_ASPECT_RATIO // 8) * 8
46
+
47
+ # Ensure width and height remain above the minimum dimensions
48
+ width = max(width, 576) if width == FIXED_DIMENSION else width
49
+ height = max(height, 576) if height == FIXED_DIMENSION else height
50
+
51
+ return width, height
52
+
53
  @spaces.GPU
54
  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)):
 
55
  image = edit_images["background"]
56
+ width, height = calculate_optimal_dimensions(image)
57
  mask = edit_images["layers"][0]
58
  if randomize_seed:
59
  seed = random.randint(0, MAX_SEED)
 
78
  css="""
79
  #col-container {
80
  margin: 0 auto;
81
+ max-width: 1000px;
82
  }
83
  """
84
 
 
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
 
 
131
  maximum=MAX_IMAGE_SIZE,
132
  step=32,
133
  value=1024,
134
+ visible=False
135
  )
136
 
137
  height = gr.Slider(
 
140
  maximum=MAX_IMAGE_SIZE,
141
  step=32,
142
  value=1024,
143
+ visible=False
144
  )
145
 
146
  with gr.Row():