SkalskiP commited on
Commit
d981a02
1 Parent(s): 16d967d

test new `masking_prompt_text` mode

Browse files
Files changed (1) hide show
  1. app.py +51 -18
app.py CHANGED
@@ -1,13 +1,15 @@
1
  from typing import Tuple
2
 
 
3
  import requests
4
  import random
5
  import numpy as np
6
  import gradio as gr
7
  import spaces
8
  import torch
9
- from PIL import Image
10
  from diffusers import FluxInpaintPipeline
 
11
 
12
  MARKDOWN = """
13
  # FLUX.1 Inpainting 🔥
@@ -21,6 +23,9 @@ MAX_SEED = np.iinfo(np.int32).max
21
  IMAGE_SIZE = 1024
22
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
23
 
 
 
 
24
 
25
  def remove_background(image: Image.Image, threshold: int = 50) -> Image.Image:
26
  image = image.convert("RGBA")
@@ -45,6 +50,7 @@ EXAMPLES = [
45
  "composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-2.png", stream=True).raw),
46
  },
47
  "little lion",
 
48
  42,
49
  False,
50
  0.85,
@@ -57,6 +63,7 @@ EXAMPLES = [
57
  "composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-3.png", stream=True).raw),
58
  },
59
  "tattoos",
 
60
  42,
61
  False,
62
  0.85,
@@ -74,11 +81,6 @@ def resize_image_dimensions(
74
  ) -> Tuple[int, int]:
75
  width, height = original_resolution_wh
76
 
77
- # if width <= maximum_dimension and height <= maximum_dimension:
78
- # width = width - (width % 32)
79
- # height = height - (height % 32)
80
- # return width, height
81
-
82
  if width > height:
83
  scaling_factor = maximum_dimension / width
84
  else:
@@ -93,31 +95,53 @@ def resize_image_dimensions(
93
  return new_width, new_height
94
 
95
 
 
 
 
 
 
 
96
  @spaces.GPU(duration=100)
97
  def process(
98
  input_image_editor: dict,
99
- input_text: str,
 
100
  seed_slicer: int,
101
  randomize_seed_checkbox: bool,
102
  strength_slider: float,
103
  num_inference_steps_slider: int,
104
  progress=gr.Progress(track_tqdm=True)
105
  ):
106
- if not input_text:
107
  gr.Info("Please enter a text prompt.")
108
  return None, None
109
 
110
- image = input_image_editor['background']
111
- mask = input_image_editor['layers'][0]
 
 
 
112
 
113
  if not image:
114
  gr.Info("Please upload an image.")
115
  return None, None
116
 
117
- if not mask:
118
- gr.Info("Please draw a mask on the image.")
 
 
 
 
119
  return None, None
120
 
 
 
 
 
 
 
 
 
121
  width, height = resize_image_dimensions(original_resolution_wh=image.size)
122
  resized_image = image.resize((width, height), Image.LANCZOS)
123
  resized_mask = mask.resize((width, height), Image.LANCZOS)
@@ -126,7 +150,7 @@ def process(
126
  seed_slicer = random.randint(0, MAX_SEED)
127
  generator = torch.Generator().manual_seed(seed_slicer)
128
  result = pipe(
129
- prompt=input_text,
130
  image=resized_image,
131
  mask_image=resized_mask,
132
  width=width,
@@ -145,24 +169,31 @@ with gr.Blocks() as demo:
145
  with gr.Column():
146
  input_image_editor_component = gr.ImageEditor(
147
  label='Image',
148
- type='pil',
149
  sources=["upload", "webcam"],
150
  image_mode='RGB',
151
  layers=False,
152
  brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"))
153
 
154
  with gr.Row():
155
- input_text_component = gr.Text(
156
  label="Prompt",
157
  show_label=False,
158
  max_lines=1,
159
- placeholder="Enter your prompt",
160
  container=False,
161
  )
162
  submit_button_component = gr.Button(
163
  value='Submit', variant='primary', scale=0)
164
 
165
  with gr.Accordion("Advanced Settings", open=False):
 
 
 
 
 
 
 
166
  seed_slicer_component = gr.Slider(
167
  label="Seed",
168
  minimum=0,
@@ -207,7 +238,8 @@ with gr.Blocks() as demo:
207
  examples=EXAMPLES,
208
  inputs=[
209
  input_image_editor_component,
210
- input_text_component,
 
211
  seed_slicer_component,
212
  randomize_seed_checkbox_component,
213
  strength_slider_component,
@@ -225,7 +257,8 @@ with gr.Blocks() as demo:
225
  fn=process,
226
  inputs=[
227
  input_image_editor_component,
228
- input_text_component,
 
229
  seed_slicer_component,
230
  randomize_seed_checkbox_component,
231
  strength_slider_component,
 
1
  from typing import Tuple
2
 
3
+ import os
4
  import requests
5
  import random
6
  import numpy as np
7
  import gradio as gr
8
  import spaces
9
  import torch
10
+ from PIL import Image, ImageFilter
11
  from diffusers import FluxInpaintPipeline
12
+ from gradio_client import Client, handle_file
13
 
14
  MARKDOWN = """
15
  # FLUX.1 Inpainting 🔥
 
23
  IMAGE_SIZE = 1024
24
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
25
 
26
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
27
+ client = Client("SkalskiP/florence-sam-masking", hf_token=HF_TOKEN)
28
+
29
 
30
  def remove_background(image: Image.Image, threshold: int = 50) -> Image.Image:
31
  image = image.convert("RGBA")
 
50
  "composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-2.png", stream=True).raw),
51
  },
52
  "little lion",
53
+ None,
54
  42,
55
  False,
56
  0.85,
 
63
  "composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-3.png", stream=True).raw),
64
  },
65
  "tattoos",
66
+ None,
67
  42,
68
  False,
69
  0.85,
 
81
  ) -> Tuple[int, int]:
82
  width, height = original_resolution_wh
83
 
 
 
 
 
 
84
  if width > height:
85
  scaling_factor = maximum_dimension / width
86
  else:
 
95
  return new_width, new_height
96
 
97
 
98
+ def is_image_empty(image: Image.Image) -> bool:
99
+ gray_img = image.convert("L")
100
+ pixels = list(gray_img.getdata())
101
+ return all(pixel == 0 for pixel in pixels)
102
+
103
+
104
  @spaces.GPU(duration=100)
105
  def process(
106
  input_image_editor: dict,
107
+ inpainting_prompt_text: str,
108
+ masking_prompt_text: str,
109
  seed_slicer: int,
110
  randomize_seed_checkbox: bool,
111
  strength_slider: float,
112
  num_inference_steps_slider: int,
113
  progress=gr.Progress(track_tqdm=True)
114
  ):
115
+ if not inpainting_prompt_text:
116
  gr.Info("Please enter a text prompt.")
117
  return None, None
118
 
119
+ image_path = input_image_editor['background']
120
+ mask_path = input_image_editor['layers'][0]
121
+
122
+ image = Image.open(image_path)
123
+ mask = Image.open(mask_path)
124
 
125
  if not image:
126
  gr.Info("Please upload an image.")
127
  return None, None
128
 
129
+ if is_image_empty(mask) and not masking_prompt_text:
130
+ gr.Info("Please draw a mask or enter a masking prompt.")
131
+ return None, None
132
+
133
+ if not is_image_empty(mask) and masking_prompt_text:
134
+ gr.Info("Both mask and masking prompt are provided. Please provide only one.")
135
  return None, None
136
 
137
+ if is_image_empty(mask):
138
+ mask = client.predict(
139
+ image_input=handle_file(image_path),
140
+ text_input=masking_prompt_text,
141
+ api_name="/process_image")
142
+ mask = Image.open(mask)
143
+
144
+ mask = mask.filter(ImageFilter.GaussianBlur(radius=5))
145
  width, height = resize_image_dimensions(original_resolution_wh=image.size)
146
  resized_image = image.resize((width, height), Image.LANCZOS)
147
  resized_mask = mask.resize((width, height), Image.LANCZOS)
 
150
  seed_slicer = random.randint(0, MAX_SEED)
151
  generator = torch.Generator().manual_seed(seed_slicer)
152
  result = pipe(
153
+ prompt=inpainting_prompt_text,
154
  image=resized_image,
155
  mask_image=resized_mask,
156
  width=width,
 
169
  with gr.Column():
170
  input_image_editor_component = gr.ImageEditor(
171
  label='Image',
172
+ type='filepath',
173
  sources=["upload", "webcam"],
174
  image_mode='RGB',
175
  layers=False,
176
  brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"))
177
 
178
  with gr.Row():
179
+ inpainting_prompt_text_component = gr.Text(
180
  label="Prompt",
181
  show_label=False,
182
  max_lines=1,
183
+ placeholder="Enter text to generate inpainting",
184
  container=False,
185
  )
186
  submit_button_component = gr.Button(
187
  value='Submit', variant='primary', scale=0)
188
 
189
  with gr.Accordion("Advanced Settings", open=False):
190
+ masking_prompt_text_component = gr.Text(
191
+ label="Prompt",
192
+ show_label=False,
193
+ max_lines=1,
194
+ placeholder="Enter text to generate masking",
195
+ container=False,
196
+ )
197
  seed_slicer_component = gr.Slider(
198
  label="Seed",
199
  minimum=0,
 
238
  examples=EXAMPLES,
239
  inputs=[
240
  input_image_editor_component,
241
+ inpainting_prompt_text_component,
242
+ masking_prompt_text_component,
243
  seed_slicer_component,
244
  randomize_seed_checkbox_component,
245
  strength_slider_component,
 
257
  fn=process,
258
  inputs=[
259
  input_image_editor_component,
260
+ inpainting_prompt_text_component,
261
+ masking_prompt_text_component,
262
  seed_slicer_component,
263
  randomize_seed_checkbox_component,
264
  strength_slider_component,