not-lain commited on
Commit
ba1aaa1
Β·
1 Parent(s): 7c49730

update app

Browse files
Files changed (1) hide show
  1. app.py +44 -4
app.py CHANGED
@@ -5,6 +5,45 @@ from diffusers import FluxFillPipeline
5
 
6
  pipe = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16).to("cuda")
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  @spaces.GPU()
9
  def inpaint(
10
  image,
@@ -13,14 +52,15 @@ def inpaint(
13
  num_inference_steps=28,
14
  guidance_scale=50,
15
  ):
16
- background = image.convert("RGB")
17
  mask = mask.convert("L")
 
18
 
19
  result = pipe(
20
  prompt=prompt,
21
- height=background.height,
22
- width=background.width,
23
- image=background,
24
  mask_image=mask,
25
  num_inference_steps=num_inference_steps,
26
  guidance_scale=guidance_scale,
 
5
 
6
  pipe = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16).to("cuda")
7
 
8
+ # reference https://huggingface.co/spaces/black-forest-labs/FLUX.1-Fill-dev/blob/main/app.py
9
+ def calculate_optimal_dimensions(image):
10
+ # Extract the original dimensions
11
+ original_width, original_height = image.size
12
+
13
+ # Set constants
14
+ MIN_ASPECT_RATIO = 9 / 16
15
+ MAX_ASPECT_RATIO = 16 / 9
16
+ FIXED_DIMENSION = 1024
17
+
18
+ # Calculate the aspect ratio of the original image
19
+ original_aspect_ratio = original_width / original_height
20
+
21
+ # Determine which dimension to fix
22
+ if original_aspect_ratio > 1: # Wider than tall
23
+ width = FIXED_DIMENSION
24
+ height = round(FIXED_DIMENSION / original_aspect_ratio)
25
+ else: # Taller than wide
26
+ height = FIXED_DIMENSION
27
+ width = round(FIXED_DIMENSION * original_aspect_ratio)
28
+
29
+ # Ensure dimensions are multiples of 8
30
+ width = (width // 8) * 8
31
+ height = (height // 8) * 8
32
+
33
+ # Enforce aspect ratio limits
34
+ calculated_aspect_ratio = width / height
35
+ if calculated_aspect_ratio > MAX_ASPECT_RATIO:
36
+ width = (height * MAX_ASPECT_RATIO // 8) * 8
37
+ elif calculated_aspect_ratio < MIN_ASPECT_RATIO:
38
+ height = (width / MIN_ASPECT_RATIO // 8) * 8
39
+
40
+ # Ensure width and height remain above the minimum dimensions
41
+ width = max(width, 576) if width == FIXED_DIMENSION else width
42
+ height = max(height, 576) if height == FIXED_DIMENSION else height
43
+
44
+ return width, height
45
+
46
+
47
  @spaces.GPU()
48
  def inpaint(
49
  image,
 
52
  num_inference_steps=28,
53
  guidance_scale=50,
54
  ):
55
+ image = image.convert("RGB")
56
  mask = mask.convert("L")
57
+ width, height = calculate_optimal_dimensions(image)
58
 
59
  result = pipe(
60
  prompt=prompt,
61
+ height= height,
62
+ width= width,
63
+ image= image,
64
  mask_image=mask,
65
  num_inference_steps=num_inference_steps,
66
  guidance_scale=guidance_scale,