Spaces:
Running on Zero
Running on Zero
add mcp support
#8
by linoyts HF Staff - opened
app.py
CHANGED
|
@@ -54,13 +54,33 @@ def imagelist_to_pptx(img_files):
|
|
| 54 |
return tmp.name
|
| 55 |
|
| 56 |
def export_gallery(images):
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
images = [e[0] for e in images]
|
| 59 |
pptx_path = imagelist_to_pptx(images)
|
| 60 |
return pptx_path
|
| 61 |
|
| 62 |
def export_gallery_zip(images):
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
images = [e[0] for e in images]
|
| 65 |
|
| 66 |
with tempfile.NamedTemporaryFile(suffix=".zip", delete=False) as tmp:
|
|
@@ -73,20 +93,65 @@ def export_gallery_zip(images):
|
|
| 73 |
return tmp.name
|
| 74 |
|
| 75 |
@spaces.GPU(duration=180)
|
| 76 |
-
def infer(
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
if randomize_seed:
|
| 88 |
seed = random.randint(0, MAX_SEED)
|
| 89 |
|
|
|
|
| 90 |
if isinstance(input_image, list):
|
| 91 |
input_image = input_image[0]
|
| 92 |
|
|
@@ -251,5 +316,7 @@ with gr.Blocks() as demo:
|
|
| 251 |
outputs=[gallery, export_file, export_zip_file],
|
| 252 |
)
|
| 253 |
|
|
|
|
|
|
|
| 254 |
if __name__ == "__main__":
|
| 255 |
-
demo.launch()
|
|
|
|
| 54 |
return tmp.name
|
| 55 |
|
| 56 |
def export_gallery(images):
|
| 57 |
+
"""
|
| 58 |
+
Export gallery images to a PowerPoint file.
|
| 59 |
+
|
| 60 |
+
Args:
|
| 61 |
+
images (list):
|
| 62 |
+
List of tuples containing (file_path, _) for each image.
|
| 63 |
+
|
| 64 |
+
Returns:
|
| 65 |
+
str:
|
| 66 |
+
Path to the generated PPTX file.
|
| 67 |
+
"""
|
| 68 |
images = [e[0] for e in images]
|
| 69 |
pptx_path = imagelist_to_pptx(images)
|
| 70 |
return pptx_path
|
| 71 |
|
| 72 |
def export_gallery_zip(images):
|
| 73 |
+
"""
|
| 74 |
+
Export gallery images to a ZIP archive.
|
| 75 |
+
|
| 76 |
+
Args:
|
| 77 |
+
images (list):
|
| 78 |
+
List of tuples containing (file_path, _) for each image.
|
| 79 |
+
|
| 80 |
+
Returns:
|
| 81 |
+
str:
|
| 82 |
+
Path to the generated ZIP file containing all images.
|
| 83 |
+
"""
|
| 84 |
images = [e[0] for e in images]
|
| 85 |
|
| 86 |
with tempfile.NamedTemporaryFile(suffix=".zip", delete=False) as tmp:
|
|
|
|
| 93 |
return tmp.name
|
| 94 |
|
| 95 |
@spaces.GPU(duration=180)
|
| 96 |
+
def infer(
|
| 97 |
+
input_image,
|
| 98 |
+
seed: int = 777,
|
| 99 |
+
randomize_seed: bool = False,
|
| 100 |
+
prompt: str = None,
|
| 101 |
+
neg_prompt: str = " ",
|
| 102 |
+
true_guidance_scale: float = 4.0,
|
| 103 |
+
num_inference_steps: int = 50,
|
| 104 |
+
layer: int = 4,
|
| 105 |
+
cfg_norm: bool = True,
|
| 106 |
+
use_en_prompt: bool = True
|
| 107 |
+
):
|
| 108 |
+
"""
|
| 109 |
+
Decompose an image into multiple layers using Qwen-Image-Layered.
|
| 110 |
+
|
| 111 |
+
Takes an input image and separates it into distinct visual layers,
|
| 112 |
+
useful for design, editing, or creating layered presentations.
|
| 113 |
+
|
| 114 |
+
Args:
|
| 115 |
+
input_image (PIL.Image.Image):
|
| 116 |
+
The input image to decompose. Can be a PIL Image, file path,
|
| 117 |
+
or numpy array.
|
| 118 |
+
seed (int, optional):
|
| 119 |
+
Random seed for reproducible generation. Ignored if
|
| 120 |
+
`randomize_seed=True`. Defaults to 777.
|
| 121 |
+
randomize_seed (bool, optional):
|
| 122 |
+
If True, a random seed is chosen per call. Defaults to False.
|
| 123 |
+
prompt (str, optional):
|
| 124 |
+
Text prompt describing the overall content of the image,
|
| 125 |
+
including elements that may be partially occluded.
|
| 126 |
+
Defaults to None.
|
| 127 |
+
neg_prompt (str, optional):
|
| 128 |
+
Negative prompt to guide what to avoid in generation.
|
| 129 |
+
Defaults to " ".
|
| 130 |
+
true_guidance_scale (float, optional):
|
| 131 |
+
Guidance scale controlling prompt adherence. Higher values
|
| 132 |
+
follow the prompt more strictly. Defaults to 4.0.
|
| 133 |
+
num_inference_steps (int, optional):
|
| 134 |
+
Number of denoising steps. More steps generally produce
|
| 135 |
+
better quality but take longer. Defaults to 50.
|
| 136 |
+
layer (int, optional):
|
| 137 |
+
Number of layers to decompose the image into (2-10).
|
| 138 |
+
Defaults to 4.
|
| 139 |
+
cfg_norm (bool, optional):
|
| 140 |
+
Whether to enable CFG normalization. Defaults to True.
|
| 141 |
+
use_en_prompt (bool, optional):
|
| 142 |
+
If True, uses English for automatic captioning when no prompt
|
| 143 |
+
is provided. If False, uses Chinese. Defaults to True.
|
| 144 |
+
|
| 145 |
+
Returns:
|
| 146 |
+
Tuple[List[PIL.Image.Image], str, str]:
|
| 147 |
+
- List of decomposed layer images (from background to foreground).
|
| 148 |
+
- Path to the generated PPTX file containing all layers.
|
| 149 |
+
- Path to the generated ZIP file containing all layer images.
|
| 150 |
+
"""
|
| 151 |
if randomize_seed:
|
| 152 |
seed = random.randint(0, MAX_SEED)
|
| 153 |
|
| 154 |
+
|
| 155 |
if isinstance(input_image, list):
|
| 156 |
input_image = input_image[0]
|
| 157 |
|
|
|
|
| 316 |
outputs=[gallery, export_file, export_zip_file],
|
| 317 |
)
|
| 318 |
|
| 319 |
+
gr.api(infer, api_name="decompose_image")
|
| 320 |
+
|
| 321 |
if __name__ == "__main__":
|
| 322 |
+
demo.launch(mcp_server=True, footer_links=["api", "gradio", "settings"])
|