Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,101 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
"""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from loadimg import load_img
|
| 3 |
+
import spaces
|
| 4 |
+
from transformers import AutoModelForImageSegmentation
|
| 5 |
+
import torch
|
| 6 |
+
from torchvision import transforms
|
| 7 |
+
from typing import Union, Tuple
|
| 8 |
+
from PIL import Image
|
| 9 |
|
| 10 |
+
torch.set_float32_matmul_precision(["high", "highest"][0])
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
| 13 |
+
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
| 14 |
+
)
|
| 15 |
+
birefnet.to("cuda")
|
| 16 |
|
| 17 |
+
transform_image = transforms.Compose(
|
| 18 |
+
[
|
| 19 |
+
transforms.Resize((1024, 1024)),
|
| 20 |
+
transforms.ToTensor(),
|
| 21 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
| 22 |
+
]
|
| 23 |
+
)
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
def fn(image: Union[Image.Image, str]) -> Tuple[Image.Image, Image.Image]:
|
| 26 |
+
"""
|
| 27 |
+
Remove the background from an image and return both the transparent version and the original.
|
| 28 |
+
This function performs background removal using a BiRefNet segmentation model. It is intended for use
|
| 29 |
+
with image input (either uploaded or from a URL). The function returns a transparent PNG version of the image
|
| 30 |
+
with the background removed, along with the original RGB version for comparison.
|
| 31 |
+
Args:
|
| 32 |
+
image (PIL.Image or str): The input image, either as a PIL object or a filepath/URL string.
|
| 33 |
+
Returns:
|
| 34 |
+
tuple:
|
| 35 |
+
- processed_image (PIL.Image): The input image with the background removed and transparency applied.
|
| 36 |
+
- origin (PIL.Image): The original RGB image, unchanged.
|
| 37 |
+
"""
|
| 38 |
+
im = load_img(image, output_type="pil")
|
| 39 |
+
im = im.convert("RGB")
|
| 40 |
+
origin = im.copy()
|
| 41 |
+
processed_image = process(im)
|
| 42 |
+
return (processed_image, origin)
|
| 43 |
|
| 44 |
+
@spaces.GPU
|
| 45 |
+
def process(image: Image.Image) -> Image.Image:
|
| 46 |
+
"""
|
| 47 |
+
Apply BiRefNet-based image segmentation to remove the background.
|
| 48 |
+
This function preprocesses the input image, runs it through a BiRefNet segmentation model to obtain a mask,
|
| 49 |
+
and applies the mask as an alpha (transparency) channel to the original image.
|
| 50 |
+
Args:
|
| 51 |
+
image (PIL.Image): The input RGB image.
|
| 52 |
+
Returns:
|
| 53 |
+
PIL.Image: The image with the background removed, using the segmentation mask as transparency.
|
| 54 |
+
"""
|
| 55 |
+
image_size = image.size
|
| 56 |
+
input_images = transform_image(image).unsqueeze(0).to("cuda")
|
| 57 |
+
# Prediction
|
| 58 |
+
with torch.no_grad():
|
| 59 |
+
preds = birefnet(input_images)[-1].sigmoid().cpu()
|
| 60 |
+
pred = preds[0].squeeze()
|
| 61 |
+
pred_pil = transforms.ToPILImage()(pred)
|
| 62 |
+
mask = pred_pil.resize(image_size)
|
| 63 |
+
image.putalpha(mask)
|
| 64 |
+
return image
|
| 65 |
|
| 66 |
+
def process_file(f: str) -> str:
|
| 67 |
+
"""
|
| 68 |
+
Load an image file from disk, remove the background, and save the output as a transparent PNG.
|
| 69 |
+
Args:
|
| 70 |
+
f (str): Filepath of the image to process.
|
| 71 |
+
Returns:
|
| 72 |
+
str: Path to the saved PNG image with background removed.
|
| 73 |
+
"""
|
| 74 |
+
name_path = f.rsplit(".", 1)[0] + ".png"
|
| 75 |
+
im = load_img(f, output_type="pil")
|
| 76 |
+
im = im.convert("RGB")
|
| 77 |
+
transparent = process(im)
|
| 78 |
+
transparent.save(name_path)
|
| 79 |
+
return name_path
|
| 80 |
|
| 81 |
+
slider1 = gr.ImageSlider(label="Processed Image", type="pil", format="png")
|
| 82 |
+
slider2 = gr.ImageSlider(label="Processed Image from URL", type="pil", format="png")
|
| 83 |
+
image_upload = gr.Image(label="Upload an image")
|
| 84 |
+
image_file_upload = gr.Image(label="Upload an image", type="filepath")
|
| 85 |
+
url_input = gr.Textbox(label="Paste an image URL")
|
| 86 |
+
output_file = gr.File(label="Output PNG File")
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
# Example images
|
| 89 |
+
chameleon = load_img("butterfly.jpg", output_type="pil")
|
| 90 |
+
url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
|
| 91 |
|
| 92 |
+
tab1 = gr.Interface(fn, inputs=image_upload, outputs=slider1, examples=[chameleon], api_name="image")
|
| 93 |
+
tab2 = gr.Interface(fn, inputs=url_input, outputs=slider2, examples=[url_example], api_name="text")
|
| 94 |
+
tab3 = gr.Interface(process_file, inputs=image_file_upload, outputs=output_file, examples=["butterfly.jpg"], api_name="png")
|
| 95 |
|
| 96 |
+
demo = gr.TabbedInterface(
|
| 97 |
+
[tab1, tab2, tab3], ["Image Upload", "URL Input", "File Output"], title="Background Removal Tool"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
)
|
| 99 |
|
|
|
|
| 100 |
if __name__ == "__main__":
|
| 101 |
+
demo.launch(show_error=True, mcp_server=True)
|