hideosnes commited on
Commit
5c041f6
·
verified ·
1 Parent(s): 46ea192

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -1
app.py CHANGED
@@ -16,6 +16,9 @@ import gradio as gr
16
  from huggingface_hub import hf_hub_download, snapshot_download
17
  from ip_adapter import IPAdapterXL
18
  from safetensors.torch import load_file
 
 
 
19
 
20
  snapshot_download(
21
  repo_id="h94/IP-Adapter", allow_patterns="sdxl_models/*", local_dir="."
@@ -100,8 +103,30 @@ def resize_img(
100
  np.array(input_image)
101
  )
102
  input_image = Image.fromarray(res)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  return input_image
104
 
 
105
 
106
  examples = [
107
  [
@@ -138,7 +163,7 @@ def run_for_examples(style_image, source_image, prompt, scale, control_scale):
138
  )
139
 
140
 
141
- @spaces.GPU(enable_queue=True)
142
  def create_image(
143
  image_pil,
144
  input_image,
@@ -257,6 +282,7 @@ with block:
257
  with gr.Row():
258
  with gr.Column():
259
  image_pil = gr.Image(label="Style Image", type="pil")
 
260
  with gr.Column():
261
  prompt = gr.Textbox(
262
  label="Prompt",
 
16
  from huggingface_hub import hf_hub_download, snapshot_download
17
  from ip_adapter import IPAdapterXL
18
  from safetensors.torch import load_file
19
+ # remove the background
20
+ from tsr.system import TSR
21
+ from tsr.utils import remove_background, resize_foreground
22
 
23
  snapshot_download(
24
  repo_id="h94/IP-Adapter", allow_patterns="sdxl_models/*", local_dir="."
 
103
  np.array(input_image)
104
  )
105
  input_image = Image.fromarray(res)
106
+ # return input_image
107
+ return resized_image
108
+
109
+ #added
110
+
111
+ def preprocess(resized_image, do_remove_background, foreground_ratio):
112
+ def fill_background(image):
113
+ image = np.array(image).astype(np.float32) / 255.0
114
+ image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
115
+ image = Image.fromarray((image * 255.0).astype(np.uint8))
116
+ return image
117
+
118
+ if do_remove_background:
119
+ image = input_image.convert("RGB")
120
+ image = remove_background(image, rembg_session)
121
+ image = resize_foreground(image, foreground_ratio)
122
+ image = fill_background(image)
123
+ else:
124
+ image = input_image
125
+ if image.mode == "RGBA":
126
+ image = fill_background(image)
127
  return input_image
128
 
129
+ #/added
130
 
131
  examples = [
132
  [
 
163
  )
164
 
165
 
166
+ @spaces.GPU
167
  def create_image(
168
  image_pil,
169
  input_image,
 
282
  with gr.Row():
283
  with gr.Column():
284
  image_pil = gr.Image(label="Style Image", type="pil")
285
+ processed_image = gr.Image(label="Preprocess uWu", interactive=False)
286
  with gr.Column():
287
  prompt = gr.Textbox(
288
  label="Prompt",