israelweiss commited on
Commit
ebb5e31
·
1 Parent(s): ee4c270

initial template of tabs with api call to int

Browse files
Files changed (1) hide show
  1. app.py +86 -36
app.py CHANGED
@@ -23,33 +23,64 @@ def download_image(url):
23
  img_bytes = BytesIO(response.content)
24
  return Image.open(img_bytes).convert("RGB")
25
 
26
- def gen_fill_api_call(image_base64_file, mask_base64_file, prompt):
27
 
28
- url = "http://engine.prod.bria-api.com/v1/gen_fill"
 
29
 
30
  payload = {
31
  "file": image_base64_file,
32
- "mask_file": mask_base64_file,
33
- "prompt": prompt,
34
- "steps_num": 12,
35
- "sync": True
 
36
  }
37
  response = requests.post(url, json=payload, headers=auth_headers)
38
  response = response.json()
39
- res_image = download_image(response["urls"][0])
40
 
41
  return res_image
42
 
43
 
44
- def predict(dict, prompt):
45
 
46
- init_image = Image.fromarray(dict['background'][:, :, :3], 'RGB') #dict['background'].convert("RGB")#.resize((1024, 1024))
47
- mask = Image.fromarray(dict['layers'][0][:,:,3], 'L') #dict['layers'].convert("RGB")#.resize((1024, 1024))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  image_base64_file = convert_mask_image_to_base64_string(init_image)
50
- mask_base64_file = convert_mask_image_to_base64_string(mask)
51
 
52
- gen_img = gen_fill_api_call(image_base64_file, mask_base64_file, prompt)
53
 
54
  return gen_img
55
 
@@ -103,37 +134,56 @@ div#share-btn-container > div {flex-direction: row;background: black;align-items
103
 
104
  image_blocks = gr.Blocks(css=css, elem_id="total-container")
105
  with image_blocks as demo:
106
- with gr.Column(elem_id="col-container"):
107
- gr.Markdown("## BRIA Generative Fill API")
108
- gr.HTML('''
109
  <p style="margin-bottom: 10px; font-size: 94%">
110
- This demo showcases the BRIA Generative Fill capability, which allows users to add and modify elements or objects from images, guided by a mask and a prompt.<br>
111
  The pipeline comprises multiple components, including <a href="https://huggingface.co/briaai/BRIA-2.3" target="_blank">briaai/BRIA-2.3</a>,
112
- <a href="https://huggingface.co/briaai/BRIA-2.3-ControlNet-Generative-Fill" target="_blank">briaai/BRIA-2.3-ControlNet-Generative-Fill</a>,
113
- and <a href="https://huggingface.co/briaai/BRIA-2.3-FAST-LORA" target="_blank">briaai/BRIA-2.3-FAST-LORA</a>, all trained on licensed data.<br>
114
  This ensures full legal liability coverage for copyright and privacy infringement.<br>
115
  Notes:<br>
116
  - High-resolution images may take longer to process.<br>
117
- - For best results use blobby masks.<br>
118
- - The Generative Fill ControlNet's weights are publicily available.<br>
119
  </p>
120
  ''')
121
-
122
- with gr.Row():
123
- with gr.Column():
124
- image = gr.ImageEditor(sources=["upload"], layers=False, transforms=[],
125
- brush=gr.Brush(colors=["#000000"], color_mode="fixed"),
126
- )
127
- prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
128
- with gr.Row(elem_id="prompt-container", equal_height=True):
129
- with gr.Column():
130
- btn = gr.Button("Fill!", elem_id="run_button")
131
-
132
- with gr.Column():
133
- image_out = gr.Image(label="Output", elem_id="output-img")
134
-
135
- # Button click will trigger the inpainting function (now with prompt included)
136
- btn.click(fn=predict, inputs=[image, prompt], outputs=[image_out], api_name='run')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
  gr.HTML(
139
  """
 
23
  img_bytes = BytesIO(response.content)
24
  return Image.open(img_bytes).convert("RGB")
25
 
26
+ def lifestyle_shot_by_text_api_call(image_base64_file, prompt):
27
 
28
+ # url = "http://engine.prod.bria-api.com/v1/product/lifestyle_shot_by_text" # TODO: use this line!
29
+ url = "http://engine.int.bria-api.com/v1/product/lifestyle_shot_by_text" # TODO: use line above!
30
 
31
  payload = {
32
  "file": image_base64_file,
33
+ "scene_description": prompt,
34
+ "num_results": 1,
35
+ "sync": True,
36
+ "original_quality": True,
37
+ "optimize_description": True,
38
  }
39
  response = requests.post(url, json=payload, headers=auth_headers)
40
  response = response.json()
41
+ res_image = download_image(response['result'][0][0])
42
 
43
  return res_image
44
 
45
 
46
+ def predict_ref_by_text(input_image, prompt):
47
 
48
+ # init_image = Image.fromarray(dict['background'][:, :, :3], 'RGB') #dict['background'].convert("RGB")#.resize((1024, 1024))
49
+ # mask = Image.fromarray(dict['layers'][0][:,:,3], 'L') #dict['layers'].convert("RGB")#.resize((1024, 1024))
50
+
51
+ image_base64_file = convert_mask_image_to_base64_string(input_image)
52
+
53
+ gen_img = lifestyle_shot_by_text_api_call(image_base64_file, prompt)
54
+
55
+ return gen_img
56
+
57
+
58
+ def lifestyle_shot_by_image_api_call(image_base64_file, ref_image_base64_file):
59
+
60
+ # url = "http://engine.prod.bria-api.com/v1/product/lifestyle_shot_by_image" # TODO: use this line!
61
+ url = "http://engine.int.bria-api.com/v1/product/lifestyle_shot_by_image" # TODO: use line above!
62
+
63
+ payload = {
64
+ "file": image_base64_file,
65
+ "ref_image_file": ref_image_base64_file,
66
+ "num_results": 1,
67
+ "sync": True,
68
+ "original_quality": True,
69
+ "optimize_description": True,
70
+ }
71
+ response = requests.post(url, json=payload, headers=auth_headers)
72
+ response = response.json()
73
+ res_image = download_image(response['result'][0][0])
74
+
75
+ return res_image
76
+
77
+
78
+ def predict_ref_by_image(init_image, ref_image):
79
 
80
  image_base64_file = convert_mask_image_to_base64_string(init_image)
81
+ ref_base64_file = convert_mask_image_to_base64_string(ref_image)
82
 
83
+ gen_img = lifestyle_shot_by_image_api_call(image_base64_file, ref_base64_file)
84
 
85
  return gen_img
86
 
 
134
 
135
  image_blocks = gr.Blocks(css=css, elem_id="total-container")
136
  with image_blocks as demo:
137
+ # with gr.Column(elem_id="col-container"):
138
+ gr.Markdown("## BRIA Replace Background By Reference API")
139
+ gr.HTML('''
140
  <p style="margin-bottom: 10px; font-size: 94%">
141
+ This demo showcases the BRIA Replace Background By Reference capability, which allows users to replace background for an image, guided by either a textual prompt or a reference image which is used for inpiring the generated background.<br>
142
  The pipeline comprises multiple components, including <a href="https://huggingface.co/briaai/BRIA-2.3" target="_blank">briaai/BRIA-2.3</a>,
143
+ <a href="https://huggingface.co/briaai/RMBG-2.0" target="_blank">briaai/RMBG-2.0</a>, all trained on licensed data.<br>
 
144
  This ensures full legal liability coverage for copyright and privacy infringement.<br>
145
  Notes:<br>
146
  - High-resolution images may take longer to process.<br>
147
+ - For best results in reference by image: make sure the foreground in the image is already located in the wanted position and scale, relative to the elements in the reference image.<br>
 
148
  </p>
149
  ''')
150
+ with gr.Tab(elem_id="By prompt"):
151
+
152
+ with gr.Row():
153
+ with gr.Column():
154
+ image = gr.ImageEditor(sources=["upload"], layers=False, transforms=[],
155
+ brush=gr.Brush(colors=["#000000"], color_mode="fixed"),
156
+ )
157
+ prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
158
+ with gr.Row(elem_id="prompt-container", equal_height=True):
159
+ with gr.Column():
160
+ btn = gr.Button("Replace Background!", elem_id="run_button")
161
+
162
+ with gr.Column():
163
+ image_out = gr.Image(label="Output", elem_id="output-img")
164
+
165
+ # Button click will trigger the inpainting function (now with prompt included)
166
+ btn.click(fn=predict_ref_by_text, inputs=[image, prompt], outputs=[image_out], api_name='run')
167
+
168
+ with gr.Tab(elem_id="By image reference"):
169
+
170
+ with gr.Row():
171
+ with gr.Column():
172
+ image = gr.ImageEditor(sources=["upload"], layers=False, transforms=[],
173
+ brush=gr.Brush(colors=["#000000"], color_mode="fixed"),
174
+ )
175
+ ref_image = gr.ImageEditor(sources=["upload"], layers=False, transforms=[],
176
+ brush=gr.Brush(colors=["#000000"], color_mode="fixed"),
177
+ )
178
+ with gr.Row(elem_id="prompt-container", equal_height=True):
179
+ with gr.Column():
180
+ btn = gr.Button("Replace Background!", elem_id="run_button")
181
+
182
+ with gr.Column():
183
+ image_out = gr.Image(label="Output", elem_id="output-img")
184
+
185
+ # Button click will trigger the inpainting function (now with prompt included)
186
+ btn.click(fn=predict_ref_by_image, inputs=[image, ref_image], outputs=[image_out], api_name='run')
187
 
188
  gr.HTML(
189
  """