Spaces:
Running
Running
adapt code
Browse files
app.py
CHANGED
|
@@ -11,28 +11,18 @@ import base64
|
|
| 11 |
hf_token = os.environ.get("HF_TOKEN_API_DEMO") # we get it from a secret env variable, such that it's private
|
| 12 |
auth_headers = {"api_token": hf_token}
|
| 13 |
|
| 14 |
-
def convert_mask_image_to_base64_string(mask_image):
|
| 15 |
-
buffer = io.BytesIO()
|
| 16 |
-
mask_image.save(buffer, format="PNG") # You can choose the format (e.g., "JPEG", "PNG")
|
| 17 |
-
# Encode the buffer in base64
|
| 18 |
-
image_base64_string = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
| 19 |
-
return f",{image_base64_string}" # for some reason the funciton which downloads image from base64 expects prefix of "," which is redundant in the url
|
| 20 |
|
| 21 |
def download_image(url):
|
| 22 |
response = requests.get(url)
|
| 23 |
img_bytes = BytesIO(response.content)
|
| 24 |
return Image.open(img_bytes).convert("RGB")
|
| 25 |
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
url = "http://engine.prod.bria-api.com/v1/gen_fill"
|
| 29 |
|
| 30 |
payload = {
|
| 31 |
-
"
|
| 32 |
-
"
|
| 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()
|
|
@@ -44,12 +34,10 @@ def gen_fill_api_call(image_base64_file, mask_base64_file, prompt):
|
|
| 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 =
|
| 53 |
|
| 54 |
return gen_img
|
| 55 |
|
|
@@ -107,37 +95,24 @@ with image_blocks as demo:
|
|
| 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
|
| 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><br>
|
| 119 |
</p>
|
| 120 |
-
<p style="margin-bottom: 10px; font-size: 94%">
|
| 121 |
-
API Endpoint available on: <a href="https://fal.ai/models/fal-ai/bria/genfill" target="_blank">fal.ai</a><br>
|
| 122 |
-
ComfyUI node is available here: <a href="https://github.com/Bria-AI/ComfyUI-BRIA-API" target="_blank">ComfyUI Node</a>
|
| 123 |
-
</p>
|
| 124 |
''')
|
| 125 |
|
| 126 |
with gr.Row():
|
| 127 |
with gr.Column():
|
| 128 |
-
image = gr.ImageEditor(sources=["upload"], layers=False, transforms=[],
|
| 129 |
-
brush=gr.Brush(colors=["#000000"], color_mode="fixed"),
|
| 130 |
)
|
| 131 |
-
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
|
| 132 |
with gr.Row(elem_id="prompt-container", equal_height=True):
|
| 133 |
with gr.Column():
|
| 134 |
-
btn = gr.Button("
|
| 135 |
|
| 136 |
with gr.Column():
|
| 137 |
image_out = gr.Image(label="Output", elem_id="output-img")
|
| 138 |
|
| 139 |
# Button click will trigger the inpainting function (now with prompt included)
|
| 140 |
-
btn.click(fn=predict, inputs=[image
|
| 141 |
|
| 142 |
gr.HTML(
|
| 143 |
"""
|
|
|
|
| 11 |
hf_token = os.environ.get("HF_TOKEN_API_DEMO") # we get it from a secret env variable, such that it's private
|
| 12 |
auth_headers = {"api_token": hf_token}
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def download_image(url):
|
| 16 |
response = requests.get(url)
|
| 17 |
img_bytes = BytesIO(response.content)
|
| 18 |
return Image.open(img_bytes).convert("RGB")
|
| 19 |
|
| 20 |
+
def api_call(image_base64_file):
|
| 21 |
+
url = "https://engine.prod.bria-api.com/v1/tailored-gen/restyle_portrait"
|
|
|
|
| 22 |
|
| 23 |
payload = {
|
| 24 |
+
"id_image_file": image_base64_file,
|
| 25 |
+
"tailored_model_id": 598,
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
response = requests.post(url, json=payload, headers=auth_headers)
|
| 28 |
response = response.json()
|
|
|
|
| 34 |
def predict(dict, prompt):
|
| 35 |
|
| 36 |
init_image = Image.fromarray(dict['background'][:, :, :3], 'RGB') #dict['background'].convert("RGB")#.resize((1024, 1024))
|
|
|
|
| 37 |
|
| 38 |
image_base64_file = convert_mask_image_to_base64_string(init_image)
|
|
|
|
| 39 |
|
| 40 |
+
gen_img = api_call(image_base64_file, mask_base64_file, prompt)
|
| 41 |
|
| 42 |
return gen_img
|
| 43 |
|
|
|
|
| 95 |
gr.Markdown("## BRIA Generative Fill API")
|
| 96 |
gr.HTML('''
|
| 97 |
<p style="margin-bottom: 10px; font-size: 94%">
|
| 98 |
+
This demo showcases the BRIA Tailored Portrait capability
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
''')
|
| 101 |
|
| 102 |
with gr.Row():
|
| 103 |
with gr.Column():
|
| 104 |
+
image = gr.ImageEditor(sources=["upload"], layers=False, transforms=[], color_mode="fixed"),
|
|
|
|
| 105 |
)
|
| 106 |
+
# prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
|
| 107 |
with gr.Row(elem_id="prompt-container", equal_height=True):
|
| 108 |
with gr.Column():
|
| 109 |
+
btn = gr.Button("Restyle!", elem_id="run_button")
|
| 110 |
|
| 111 |
with gr.Column():
|
| 112 |
image_out = gr.Image(label="Output", elem_id="output-img")
|
| 113 |
|
| 114 |
# Button click will trigger the inpainting function (now with prompt included)
|
| 115 |
+
btn.click(fn=predict, inputs=[image], outputs=[image_out], api_name='run')
|
| 116 |
|
| 117 |
gr.HTML(
|
| 118 |
"""
|