abdul9999 commited on
Commit
1984062
·
verified ·
1 Parent(s): 346b2e6

Update app.py

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