OmPrakashSingh1704 commited on
Commit
368e60b
1 Parent(s): 10a04a8
app.py CHANGED
@@ -56,12 +56,17 @@ with gr.Blocks() as demo:
56
  )
57
 
58
  with gr.TabItem("Edit your Banner"):
59
- img = gr.ImageMask(sources=["upload"], layers=False, transforms=[], format="png", label="base image",
60
- show_label=True)
 
 
 
 
 
61
  prompt = gr.Textbox(label="Enter the text to get a good start")
62
  out_img=gr.Image()
63
  btn = gr.Button()
64
- btn.click(Banner.Image2Image, [prompt,img], out_img)
65
 
66
  with gr.TabItem("Upgrade your Banner"):
67
  img = gr.Image()
 
56
  )
57
 
58
  with gr.TabItem("Edit your Banner"):
59
+ input_image_editor_component = gr.ImageEditor(
60
+ label='Image',
61
+ type='pil',
62
+ sources=["upload", "webcam"],
63
+ image_mode='RGB',
64
+ layers=False,
65
+ brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"))
66
  prompt = gr.Textbox(label="Enter the text to get a good start")
67
  out_img=gr.Image()
68
  btn = gr.Button()
69
+ btn.click(Banner.Image2Image, [prompt,input_image_editor_component], out_img)
70
 
71
  with gr.TabItem("Upgrade your Banner"):
72
  img = gr.Image()
options/Banner_Model/Image2Image.py CHANGED
@@ -7,66 +7,150 @@ from .transformer_flux import FluxTransformer2DModel
7
  from .pipeline_flux_controlnet_inpaint import FluxControlNetInpaintingPipeline
8
 
9
 
10
- device = "cuda" if torch.cuda.is_available() else "cpu"
11
- print(f"Using device for I2I: {device}")
12
 
13
- # Load the inpainting pipeline
14
 
15
- def resize_image(image, height, width):
16
- """Resize image tensor to the desired height and width."""
17
- return torch.nn.functional.interpolate(image, size=(height, width), mode='nearest')
18
 
19
 
20
- def dummy(img):
21
- """Save the composite image and generate a mask from the alpha channel."""
22
- imageio.imwrite("output_image.png", img["composite"])
23
 
24
- # Extract alpha channel from the first layer to create the mask
25
- alpha_channel = img["layers"][0][:, :, 3]
26
- mask = np.where(alpha_channel == 0, 0, 255).astype(np.uint8)
27
 
28
- return img["background"], mask
29
 
 
 
 
 
 
30
 
31
- def I2I(prompt, image, width=1024, height=1024, guidance_scale=8.0, num_inference_steps=20, strength=0.99):
32
-
33
- controlnet = FluxControlNetModel.from_pretrained("alimama-creative/FLUX.1-dev-Controlnet-Inpainting-Alpha", torch_dtype=torch.bfloat16)
34
- transformer = FluxTransformer2DModel.from_pretrained(
35
- "black-forest-labs/FLUX.1-dev", subfolder='transformer', torch_dytpe=torch.bfloat16
36
- )
37
- pipe = FluxControlNetInpaintingPipeline.from_pretrained(
38
- "black-forest-labs/FLUX.1-dev",
39
- controlnet=controlnet,
40
- transformer=transformer,
41
- torch_dtype=torch.bfloat16
42
- ).to(device)
43
- pipe.transformer.to(torch.bfloat16)
44
- pipe.controlnet.to(torch.bfloat16)
45
- pipe.set_attn_processor(FluxAttnProcessor2_0())
46
-
47
-
48
- img_url, mask = dummy(image)
49
-
50
- # Resize image and mask to the target dimensions (height x width)
51
- img_url = Image.fromarray(img_url, mode="RGB").resize((width, height))
52
- mask_url = Image.fromarray(mask,mode="L").resize((width, height))
53
-
54
- # Make sure both image and mask are converted into correct tensors
55
- generator = torch.Generator(device=device).manual_seed(0)
56
-
57
- # Generate the inpainted image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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=28,
65
  generator=generator,
66
- controlnet_conditioning_scale=0.9,
67
- guidance_scale=3.5,
68
- negative_prompt="",
69
- true_guidance_scale=3.5
70
- ).images[0]
71
-
72
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  from .pipeline_flux_controlnet_inpaint import FluxControlNetInpaintingPipeline
8
 
9
 
10
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
11
+ print(f"Using device for I2I: {DEVICE}")
12
 
13
+ # # Load the inpainting pipeline
14
 
15
+ # def resize_image(image, height, width):
16
+ # """Resize image tensor to the desired height and width."""
17
+ # return torch.nn.functional.interpolate(image, size=(height, width), mode='nearest')
18
 
19
 
20
+ # def dummy(img):
21
+ # """Save the composite image and generate a mask from the alpha channel."""
22
+ # imageio.imwrite("output_image.png", img["composite"])
23
 
24
+ # # Extract alpha channel from the first layer to create the mask
25
+ # alpha_channel = img["layers"][0][:, :, 3]
26
+ # mask = np.where(alpha_channel == 0, 0, 255).astype(np.uint8)
27
 
28
+ # return img["background"], mask
29
 
30
+ def resize_image_dimensions(
31
+ original_resolution_wh: Tuple[int, int],
32
+ maximum_dimension: int = IMAGE_SIZE
33
+ ) -> Tuple[int, int]:
34
+ width, height = original_resolution_wh
35
 
36
+ # if width <= maximum_dimension and height <= maximum_dimension:
37
+ # width = width - (width % 32)
38
+ # height = height - (height % 32)
39
+ # return width, height
40
+
41
+ if width > height:
42
+ scaling_factor = maximum_dimension / width
43
+ else:
44
+ scaling_factor = maximum_dimension / height
45
+
46
+ new_width = int(width * scaling_factor)
47
+ new_height = int(height * scaling_factor)
48
+
49
+ new_width = new_width - (new_width % 32)
50
+ new_height = new_height - (new_height % 32)
51
+
52
+ return new_width, new_height
53
+
54
+
55
+ # @spaces.GPU(duration=100)
56
+ def I2I(
57
+ input_image_editor: dict,
58
+ input_text: str,
59
+ seed_slicer: int=42,
60
+ randomize_seed_checkbox: bool=True,
61
+ strength_slider: float=0.85,
62
+ num_inference_steps_slider: int=20,
63
+ progress=gr.Progress(track_tqdm=True)):
64
+ pipe = FluxInpaintPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16).to(DEVICE)
65
+ if not input_text:
66
+ gr.Info("Please enter a text prompt.")
67
+ return None, None
68
+
69
+ image = input_image_editor['background']
70
+ mask = input_image_editor['layers'][0]
71
+
72
+ if not image:
73
+ gr.Info("Please upload an image.")
74
+ return None, None
75
+
76
+ if not mask:
77
+ gr.Info("Please draw a mask on the image.")
78
+ return None, None
79
+
80
+ width, height = resize_image_dimensions(original_resolution_wh=image.size)
81
+ resized_image = image.resize((width, height), Image.LANCZOS)
82
+ resized_mask = mask.resize((width, height), Image.LANCZOS)
83
+
84
+ if randomize_seed_checkbox:
85
+ seed_slicer = random.randint(0, MAX_SEED)
86
+ generator = torch.Generator().manual_seed(seed_slicer)
87
  result = pipe(
88
+ prompt=input_text,
89
+ image=resized_image,
90
+ mask_image=resized_mask,
91
+ width=width,
92
+ height=height,
93
+ strength=strength_slider,
94
  generator=generator,
95
+ num_inference_steps=num_inference_steps_slider
96
+ ).images[0]
97
+ print('INFERENCE DONE')
98
+ return result
99
+
100
+ def remove_background(image: Image.Image, threshold: int = 50) -> Image.Image:
101
+ image = image.convert("RGBA")
102
+ data = image.getdata()
103
+ new_data = []
104
+ for item in data:
105
+ avg = sum(item[:3]) / 3
106
+ if avg < threshold:
107
+ new_data.append((0, 0, 0, 0))
108
+ else:
109
+ new_data.append(item)
110
+
111
+ image.putdata(new_data)
112
+ return image
113
+
114
+
115
+ # def I2I(prompt, image, width=1024, height=1024, guidance_scale=8.0, num_inference_steps=20, strength=0.99):
116
+
117
+ # controlnet = FluxControlNetModel.from_pretrained("alimama-creative/FLUX.1-dev-Controlnet-Inpainting-Alpha", torch_dtype=torch.bfloat16)
118
+ # transformer = FluxTransformer2DModel.from_pretrained(
119
+ # "black-forest-labs/FLUX.1-dev", subfolder='transformer', torch_dytpe=torch.bfloat16
120
+ # )
121
+ # pipe = FluxControlNetInpaintingPipeline.from_pretrained(
122
+ # "black-forest-labs/FLUX.1-dev",
123
+ # controlnet=controlnet,
124
+ # transformer=transformer,
125
+ # torch_dtype=torch.bfloat16
126
+ # ).to(device)
127
+ # pipe.transformer.to(torch.bfloat16)
128
+ # pipe.controlnet.to(torch.bfloat16)
129
+ # pipe.set_attn_processor(FluxAttnProcessor2_0())
130
+
131
+
132
+ # img_url, mask = dummy(image)
133
+
134
+ # # Resize image and mask to the target dimensions (height x width)
135
+ # img_url = Image.fromarray(img_url, mode="RGB").resize((width, height))
136
+ # mask_url = Image.fromarray(mask,mode="L").resize((width, height))
137
+
138
+ # # Make sure both image and mask are converted into correct tensors
139
+ # generator = torch.Generator(device=device).manual_seed(0)
140
+
141
+ # # Generate the inpainted image
142
+ # result = pipe(
143
+ # prompt=prompt,
144
+ # height=size[1],
145
+ # width=size[0],
146
+ # control_image=image,
147
+ # control_mask=mask,
148
+ # num_inference_steps=28,
149
+ # generator=generator,
150
+ # controlnet_conditioning_scale=0.9,
151
+ # guidance_scale=3.5,
152
+ # negative_prompt="",
153
+ # true_guidance_scale=3.5
154
+ # ).images[0]
155
+
156
+ # return result
options/Banner_Model/Text2Banner.py CHANGED
@@ -1,6 +1,6 @@
1
  from huggingface_hub import InferenceClient
2
  import torch
3
- device="cuda" torch.cuda.is_available() else "cpu"
4
  def T2I(prompt, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28):
5
  # Initialize the model client
6
  model = InferenceClient(model="black-forest-labs/FLUX.1-dev").to(device)
 
1
  from huggingface_hub import InferenceClient
2
  import torch
3
+ device="cuda" if torch.cuda.is_available() else "cpu"
4
  def T2I(prompt, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28):
5
  # Initialize the model client
6
  model = InferenceClient(model="black-forest-labs/FLUX.1-dev").to(device)
options/Banner_Model/__pycache__/__init__.cpython-310.pyc CHANGED
Binary files a/options/Banner_Model/__pycache__/__init__.cpython-310.pyc and b/options/Banner_Model/__pycache__/__init__.cpython-310.pyc differ
 
options/__pycache__/Banner.cpython-310.pyc CHANGED
Binary files a/options/__pycache__/Banner.cpython-310.pyc and b/options/__pycache__/Banner.cpython-310.pyc differ