Spaces:
Running
on
Zero
Running
on
Zero
Upload folder using huggingface_hub
Browse files- .gitignore +2 -0
- remove-backround-gradio.py +39 -6
- requirements.txt +1 -0
.gitignore
CHANGED
|
@@ -10,6 +10,8 @@ Output_images/*
|
|
| 10 |
|
| 11 |
processed_images.zip
|
| 12 |
|
|
|
|
|
|
|
| 13 |
# Other common Python ignores (if not already present)
|
| 14 |
__pycache__/
|
| 15 |
*.pyc
|
|
|
|
| 10 |
|
| 11 |
processed_images.zip
|
| 12 |
|
| 13 |
+
mcp_implemention
|
| 14 |
+
|
| 15 |
# Other common Python ignores (if not already present)
|
| 16 |
__pycache__/
|
| 17 |
*.pyc
|
remove-backround-gradio.py
CHANGED
|
@@ -4,12 +4,25 @@ from rembg import remove
|
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
import zipfile
|
| 7 |
-
from typing import List, Tuple
|
| 8 |
import spaces
|
| 9 |
|
| 10 |
@spaces.GPU
|
| 11 |
-
def remove_background_single(image_file) -> Image.Image:
|
| 12 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if image_file is None:
|
| 14 |
return None
|
| 15 |
|
|
@@ -28,8 +41,25 @@ def remove_background_single(image_file) -> Image.Image:
|
|
| 28 |
return output_image
|
| 29 |
|
| 30 |
@spaces.GPU
|
| 31 |
-
def remove_background_multiple(image_files) -> Tuple[str, List[Image.Image]]:
|
| 32 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
if not image_files:
|
| 34 |
return None, []
|
| 35 |
|
|
@@ -218,4 +248,7 @@ with gr.Blocks(title="Background Removal Tool", theme=gr.themes.Default()) as ap
|
|
| 218 |
)
|
| 219 |
|
| 220 |
if __name__ == "__main__":
|
| 221 |
-
app.launch(
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
import zipfile
|
| 7 |
+
from typing import List, Tuple, Union, IO, Optional
|
| 8 |
import spaces
|
| 9 |
|
| 10 |
@spaces.GPU
|
| 11 |
+
def remove_background_single(image_file: Union[str, IO[bytes]]) -> Optional[Image.Image]:
|
| 12 |
+
"""Removes the background from a single image.
|
| 13 |
+
|
| 14 |
+
This function takes an image file (either as a path or a file-like object),
|
| 15 |
+
processes it to remove the background, and returns the result as a
|
| 16 |
+
Pillow (PIL) Image object with a transparent background.
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
image_file: The input image. Can be a path to a
|
| 20 |
+
file on disk or a file-like object (e.g., from a file upload).
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
A Pillow Image object with the background removed, or None if
|
| 24 |
+
the input was None.
|
| 25 |
+
"""
|
| 26 |
if image_file is None:
|
| 27 |
return None
|
| 28 |
|
|
|
|
| 41 |
return output_image
|
| 42 |
|
| 43 |
@spaces.GPU
|
| 44 |
+
def remove_background_multiple(image_files: List[Union[str, IO[bytes]]]) -> Tuple[Optional[str], List[Image.Image]]:
|
| 45 |
+
"""Removes backgrounds from multiple images and provides a zip archive.
|
| 46 |
+
|
| 47 |
+
This function processes a list of image files. For each image, it removes
|
| 48 |
+
the background, then bundles all the processed images (in PNG format)
|
| 49 |
+
into a single zip file for download. It also returns a small number of
|
| 50 |
+
processed images as previews for display in the UI.
|
| 51 |
+
|
| 52 |
+
Args:
|
| 53 |
+
image_files: A list of input images. Each element can be a
|
| 54 |
+
path to a file or a file-like object.
|
| 55 |
+
|
| 56 |
+
Returns:
|
| 57 |
+
A tuple containing:
|
| 58 |
+
- A string with the file path to the generated zip archive. This
|
| 59 |
+
is `None` if the input list was empty.
|
| 60 |
+
- A list of up to 5 processed Pillow Image objects to be used
|
| 61 |
+
as a preview.
|
| 62 |
+
"""
|
| 63 |
if not image_files:
|
| 64 |
return None, []
|
| 65 |
|
|
|
|
| 248 |
)
|
| 249 |
|
| 250 |
if __name__ == "__main__":
|
| 251 |
+
app.launch(
|
| 252 |
+
enable_mcp=True, # Enable MCP server
|
| 253 |
+
mcp_functions=[remove_background_single, remove_background_multiple]
|
| 254 |
+
# Expose these functions)
|
requirements.txt
CHANGED
|
@@ -4,3 +4,4 @@ pillow>=9.0.0
|
|
| 4 |
numpy>=1.21.0
|
| 5 |
onnxruntime>=1.12.0
|
| 6 |
spaces>=0.19.4
|
|
|
|
|
|
| 4 |
numpy>=1.21.0
|
| 5 |
onnxruntime>=1.12.0
|
| 6 |
spaces>=0.19.4
|
| 7 |
+
fastmcp>=2.10.6
|