Spaces:
Running
Running
File size: 3,445 Bytes
1984062 f1d2081 da8050a 8da347e 1984062 f1d2081 1984062 8da347e 1984062 87c53b4 1984062 87c53b4 1984062 05282fa 1984062 87c53b4 1984062 87c53b4 1984062 098495b 1984062 8ef4215 03fbf82 1984062 8da347e 1984062 346b2e6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
from pathlib import Path
import gradio as gr
import pi_heif
import spaces
import requests
import base64
import io
import os
import json
from huggingface_hub import hf_hub_download
from PIL import Image
css = """
footer {display: none !important;}
"""
TITLE = """
<p>
Erase unwanted watermarks automatically with AI, no manual editing needed.
</p><p>
For premium-quality results,
<a href="https://nowatermark.cloud">try NoWatermark API</a>
— it's free to test!
</p>
"""
LIKE_BUTTON = """
<div style="text-align: left; margin: 5px 0 0 0;">
<a href="https://huggingface.co/spaces/abdul9999/NoWatermark" target="_blank"
style="text-decoration: none;">
<button style="
background-color: #ff4081;
color: white;
border: none;
padding: 4px 10px;
font-size: 13px;
border-radius: 6px;
cursor: pointer;
">
❤️ Like
</button>
</a>
</div>
"""
# HEADERS = os.getenv("HEADERS")
API_HOST = os.getenv("API_HOST")
def remove_watermark(img_bytes):
"""
headers = json.loads(HEADERS)
files = {
"original_preview_image": ("1.webp", img_bytes, "image/webp"),
}
data = {
"zoom_factor": 1,
"predict_mode": "old",
}
resp = requests.post(
API_HOST,
headers=headers,
files=files,
data=data,
)
"""
files = {"file": ("input_image.webp", img_bytes, "image/webp")}
resp = requests.post(
API_HOST,
files=files,
)
if resp.status_code != 200:
print(f"Error: {resp.status_code} - {resp.text}")
return None
result_img = Image.open(io.BytesIO(resp.content)).convert("RGB")
return result_img
def process(input_image):
"""
input_image: PIL.Image from Gradio
returns: PIL.Image (dewatermarked)
"""
# Convert PIL image to bytes (WebP, since your API expects webp)
img_bytes_io = io.BytesIO()
input_image.save(img_bytes_io, format="WEBP")
img_bytes = img_bytes_io.getvalue()
# Call your dewatermark function
result_img = remove_watermark(img_bytes)
if result_img is None:
return [input_image, input_image]
# return None # or return input_image as fallback
# Convert bytes back to PIL image
return [input_image, result_img]
with gr.Blocks(css=css) as demo:
gr.HTML(LIKE_BUTTON)
gr.HTML(TITLE)
with gr.Row():
with gr.Column():
input_image = gr.Image(type="pil", label="Input Image")
run_button = gr.ClearButton(components=None, value="Remove Watermark")
with gr.Column():
output_slider = gr.ImageSlider(label="Before / After", max_height=1200, show_fullscreen_button=False)
run_button.add(output_slider)
run_button.click(fn=process, inputs=input_image, outputs=output_slider, api_name=False)
gr.Examples(
examples=[
"examples/1.jpg",
"examples/2.jpg",
"examples/3.jpg",
"examples/4.jpg",
"examples/5.jpg",
"examples/6.jpg",
"examples/7.jpg",
"examples/8.jpg",
"examples/9.jpg",
"examples/10.jpg",
],
inputs=[input_image],
outputs=output_slider,
fn=process,
cache_examples=True,
cache_mode="lazy",
run_on_click=False,
)
demo.launch(share=False, ssr_mode=False, show_api=False) |