Spaces:
Runtime error
Runtime error
Synced repo using 'sync_with_huggingface' Github Action
Browse files- app.py +1 -1
- gradio_app.py +53 -10
- requirements.txt +5 -2
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import subprocess
|
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
|
| 7 |
REPO_URL = "https://github.com/facebookresearch/watermark-anything.git"
|
| 8 |
-
REPO_BRANCH = '
|
| 9 |
LOCAL_PATH = "./watermark-anything"
|
| 10 |
MODEL_ID = "facebook/watermark-anything"
|
| 11 |
|
|
|
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
|
| 7 |
REPO_URL = "https://github.com/facebookresearch/watermark-anything.git"
|
| 8 |
+
REPO_BRANCH = 'bbec07bca82a416e5a6ff9d75a295cae5c166aaf'
|
| 9 |
LOCAL_PATH = "./watermark-anything"
|
| 10 |
MODEL_ID = "facebook/watermark-anything"
|
| 11 |
|
gradio_app.py
CHANGED
|
@@ -137,6 +137,21 @@ if '__main__' == __name__:
|
|
| 137 |
wam = load_wam()
|
| 138 |
|
| 139 |
def detect_watermark(image, multi, timeout):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
if image is None:
|
| 141 |
return None, None, None, {"status": "error", "messages": [], "error": "No image provided"}
|
| 142 |
|
|
@@ -198,6 +213,26 @@ if '__main__' == __name__:
|
|
| 198 |
return pred_mask, cluster_viz, message_json, "\n".join(color_md)
|
| 199 |
|
| 200 |
def embed_watermark(image, wm_num, wm_type, wm_str, wm_loc):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
if image is None:
|
| 202 |
return None, None, {
|
| 203 |
"status": "failure",
|
|
@@ -327,6 +362,7 @@ if '__main__' == __name__:
|
|
| 327 |
with gr.Blocks(title="Watermark Anything Demo") as demo:
|
| 328 |
gr.Markdown("""
|
| 329 |
# Watermark Anything Demo
|
|
|
|
| 330 |
This app demonstrates watermark detection and embedding using the Watermark Anything model.
|
| 331 |
Find the project [here](https://github.com/facebookresearch/watermark-anything).
|
| 332 |
""")
|
|
@@ -360,11 +396,13 @@ if '__main__' == __name__:
|
|
| 360 |
embedding_img.select(
|
| 361 |
fn=get_select_coordinates,
|
| 362 |
inputs=[embedding_img, embedding_num],
|
| 363 |
-
outputs=embedding_box
|
|
|
|
| 364 |
embedding_box.select(
|
| 365 |
fn=del_select_coordinates,
|
| 366 |
inputs=embedding_box,
|
| 367 |
-
outputs=embedding_box
|
|
|
|
| 368 |
)
|
| 369 |
|
| 370 |
# The inability to dynamically render `AnnotatedImage` is because,
|
|
@@ -397,7 +435,8 @@ if '__main__' == __name__:
|
|
| 397 |
embedding_loc.change(
|
| 398 |
fn=visible_box_image,
|
| 399 |
inputs=[embedding_img, embedding_loc],
|
| 400 |
-
outputs=[embedding_box]
|
|
|
|
| 401 |
)
|
| 402 |
|
| 403 |
def visible_text_label(embedding_type, embedding_num):
|
|
@@ -418,17 +457,20 @@ if '__main__' == __name__:
|
|
| 418 |
embedding_num.change(
|
| 419 |
fn=visible_text_label,
|
| 420 |
inputs=[embedding_type, embedding_num],
|
| 421 |
-
outputs=[embedding_str]
|
|
|
|
| 422 |
)
|
| 423 |
embedding_type.change(
|
| 424 |
fn=visible_text_label,
|
| 425 |
inputs=[embedding_type, embedding_num],
|
| 426 |
-
outputs=[embedding_str]
|
|
|
|
| 427 |
)
|
| 428 |
embedding_str.change(
|
| 429 |
fn=check_embedding_str,
|
| 430 |
inputs=[embedding_str, embedding_num],
|
| 431 |
-
outputs=[embedding_btn]
|
|
|
|
| 432 |
)
|
| 433 |
|
| 434 |
embedding_btn.click(
|
|
@@ -460,12 +502,14 @@ if '__main__' == __name__:
|
|
| 460 |
detecting_img.change(
|
| 461 |
fn=lambda x: gr.update(value=False),
|
| 462 |
inputs=detecting_img,
|
| 463 |
-
outputs=multi_ckb
|
|
|
|
| 464 |
)
|
| 465 |
multi_ckb.change(
|
| 466 |
fn=lambda x: gr.update(visible=x),
|
| 467 |
inputs=multi_ckb,
|
| 468 |
-
outputs=timeout_sli
|
|
|
|
| 469 |
)
|
| 470 |
detecting_btn.click(
|
| 471 |
fn=detect_watermark,
|
|
@@ -473,6 +517,5 @@ if '__main__' == __name__:
|
|
| 473 |
outputs=[predicted_mask, predicted_cluster, predicted_messages, color_cluster]
|
| 474 |
)
|
| 475 |
|
| 476 |
-
|
| 477 |
if __name__ == '__main__':
|
| 478 |
-
demo.launch()
|
|
|
|
| 137 |
wam = load_wam()
|
| 138 |
|
| 139 |
def detect_watermark(image, multi, timeout):
|
| 140 |
+
"""
|
| 141 |
+
Detects watermark(s) in the given image.
|
| 142 |
+
|
| 143 |
+
Args:
|
| 144 |
+
image (Union[PIL.Image.Image, str]): Input image, either a PIL Image object or a URL pointing to the image.
|
| 145 |
+
multi (bool): If True, enables detection of multiple watermarks in the image.
|
| 146 |
+
timeout (float): Timeout in seconds for image loading or processing; used to handle unresponsive or invalid images.
|
| 147 |
+
|
| 148 |
+
Returns:
|
| 149 |
+
tuple: A 4-element tuple containing:
|
| 150 |
+
- dict: An image dict of the predicted watermark positions.
|
| 151 |
+
- dict: An image dict of watermark clusters.
|
| 152 |
+
- Union[str, float, bool, list, dict]: Metadata or messages related to the detection.
|
| 153 |
+
- str: A markdown string summarizing the detection result (e.g., confidence level).
|
| 154 |
+
"""
|
| 155 |
if image is None:
|
| 156 |
return None, None, None, {"status": "error", "messages": [], "error": "No image provided"}
|
| 157 |
|
|
|
|
| 213 |
return pred_mask, cluster_viz, message_json, "\n".join(color_md)
|
| 214 |
|
| 215 |
def embed_watermark(image, wm_num, wm_type, wm_str, wm_loc):
|
| 216 |
+
"""
|
| 217 |
+
Embeds one or more watermarks into the input image.
|
| 218 |
+
|
| 219 |
+
Args:
|
| 220 |
+
image (Union[PIL.Image.Image, str]): The input image, either as a PIL Image object or a URL string.
|
| 221 |
+
wm_num (float): The number of watermarks to embed.
|
| 222 |
+
wm_type (Literal['random', 'input']): The type of watermark.
|
| 223 |
+
- 'random': Use randomly generated watermark content.
|
| 224 |
+
- 'input': Use the content from `wm_str` as the watermark.
|
| 225 |
+
wm_str (str): The text content of the watermark when `wm_type='input'`.
|
| 226 |
+
wm_loc (Literal['random', 'bounding']): The placement mode of the watermark.
|
| 227 |
+
- 'random': Randomly place the watermark(s).
|
| 228 |
+
- 'bounding': Place within a bounding box or defined area.
|
| 229 |
+
|
| 230 |
+
Returns:
|
| 231 |
+
tuple: A 3-element tuple containing:
|
| 232 |
+
- dict: Watermarked image output (shown in "Watermarked Image" component).
|
| 233 |
+
- dict: Image showing the watermark position (shown in "Position of the watermark").
|
| 234 |
+
- Union[str, float, bool, list, dict]: Metadata or message related to the embedding process (shown in "Marked Messages").
|
| 235 |
+
"""
|
| 236 |
if image is None:
|
| 237 |
return None, None, {
|
| 238 |
"status": "failure",
|
|
|
|
| 362 |
with gr.Blocks(title="Watermark Anything Demo") as demo:
|
| 363 |
gr.Markdown("""
|
| 364 |
# Watermark Anything Demo
|
| 365 |
+

|
| 366 |
This app demonstrates watermark detection and embedding using the Watermark Anything model.
|
| 367 |
Find the project [here](https://github.com/facebookresearch/watermark-anything).
|
| 368 |
""")
|
|
|
|
| 396 |
embedding_img.select(
|
| 397 |
fn=get_select_coordinates,
|
| 398 |
inputs=[embedding_img, embedding_num],
|
| 399 |
+
outputs=embedding_box,
|
| 400 |
+
api_name=False)
|
| 401 |
embedding_box.select(
|
| 402 |
fn=del_select_coordinates,
|
| 403 |
inputs=embedding_box,
|
| 404 |
+
outputs=embedding_box,
|
| 405 |
+
api_name=False
|
| 406 |
)
|
| 407 |
|
| 408 |
# The inability to dynamically render `AnnotatedImage` is because,
|
|
|
|
| 435 |
embedding_loc.change(
|
| 436 |
fn=visible_box_image,
|
| 437 |
inputs=[embedding_img, embedding_loc],
|
| 438 |
+
outputs=[embedding_box],
|
| 439 |
+
api_name=False
|
| 440 |
)
|
| 441 |
|
| 442 |
def visible_text_label(embedding_type, embedding_num):
|
|
|
|
| 457 |
embedding_num.change(
|
| 458 |
fn=visible_text_label,
|
| 459 |
inputs=[embedding_type, embedding_num],
|
| 460 |
+
outputs=[embedding_str],
|
| 461 |
+
api_name=False
|
| 462 |
)
|
| 463 |
embedding_type.change(
|
| 464 |
fn=visible_text_label,
|
| 465 |
inputs=[embedding_type, embedding_num],
|
| 466 |
+
outputs=[embedding_str],
|
| 467 |
+
api_name=False
|
| 468 |
)
|
| 469 |
embedding_str.change(
|
| 470 |
fn=check_embedding_str,
|
| 471 |
inputs=[embedding_str, embedding_num],
|
| 472 |
+
outputs=[embedding_btn],
|
| 473 |
+
api_name=False
|
| 474 |
)
|
| 475 |
|
| 476 |
embedding_btn.click(
|
|
|
|
| 502 |
detecting_img.change(
|
| 503 |
fn=lambda x: gr.update(value=False),
|
| 504 |
inputs=detecting_img,
|
| 505 |
+
outputs=multi_ckb,
|
| 506 |
+
api_name=False
|
| 507 |
)
|
| 508 |
multi_ckb.change(
|
| 509 |
fn=lambda x: gr.update(visible=x),
|
| 510 |
inputs=multi_ckb,
|
| 511 |
+
outputs=timeout_sli,
|
| 512 |
+
api_name=False
|
| 513 |
)
|
| 514 |
detecting_btn.click(
|
| 515 |
fn=detect_watermark,
|
|
|
|
| 517 |
outputs=[predicted_mask, predicted_cluster, predicted_messages, color_cluster]
|
| 518 |
)
|
| 519 |
|
|
|
|
| 520 |
if __name__ == '__main__':
|
| 521 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, mcp_server=True, ssr_mode=False)
|
requirements.txt
CHANGED
|
@@ -1,5 +1,8 @@
|
|
| 1 |
torch==2.5.1
|
| 2 |
GitPython==3.1.43
|
| 3 |
-
gradio==5.
|
| 4 |
huggingface-hub==0.26.3
|
| 5 |
-
wrapt-timeout-decorator==1.5.1
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
torch==2.5.1
|
| 2 |
GitPython==3.1.43
|
| 3 |
+
gradio[mcp]==5.28.0
|
| 4 |
huggingface-hub==0.26.3
|
| 5 |
+
wrapt-timeout-decorator==1.5.1
|
| 6 |
+
|
| 7 |
+
# gradio[mcp] 5.28.0 depends on pydantic>=2.11
|
| 8 |
+
pydantic==2.11.4
|