Update app.py
Browse files
app.py
CHANGED
|
@@ -2,30 +2,127 @@ import os
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
print("Loading Z-Image-Turbo pipeline...")
|
| 8 |
-
|
|
|
|
| 9 |
"Tongyi-MAI/Z-Image-Turbo",
|
| 10 |
torch_dtype=torch.bfloat16,
|
| 11 |
low_cpu_mem_usage=False,
|
| 12 |
)
|
|
|
|
| 13 |
pipe.to("cuda")
|
|
|
|
| 14 |
print("Z-Image pipeline loaded!")
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
from watermark.video import get_video_info
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
@spaces.GPU(duration=60)
|
| 20 |
-
def generate_image(
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
if randomize_seed:
|
| 26 |
-
seed = torch.randint(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
seed = int(seed)
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
image = pipe(
|
| 30 |
prompt=prompt,
|
| 31 |
height=int(height),
|
|
@@ -34,173 +131,446 @@ def generate_image(prompt: str, height: int, width: int, steps: int,
|
|
| 34 |
guidance_scale=0.0,
|
| 35 |
generator=generator,
|
| 36 |
).images[0]
|
|
|
|
| 37 |
return image, seed
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
@spaces.GPU(duration=60)
|
| 40 |
-
def wm_preview(
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
@spaces.GPU(duration=90)
|
| 47 |
-
def wm_remove_image(
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
return remove_image(
|
| 53 |
-
image,
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
@spaces.GPU(duration=120)
|
| 58 |
-
def wm_remove_video(
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
if not video_path:
|
| 65 |
-
raise gr.Error(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
try:
|
| 67 |
-
duration, fps, width, height, frames = get_video_info(video_path)
|
| 68 |
-
max_seconds = float(os.getenv("WATERMARK_MAX_VIDEO_SECONDS", "30"))
|
| 69 |
-
if duration > max_seconds + 0.25:
|
| 70 |
-
raise gr.Error(
|
| 71 |
-
f"Video is {duration:.1f}s. "
|
| 72 |
-
f"Maximum is {max_seconds:.0f}s. "
|
| 73 |
-
"Please trim the video and try again."
|
| 74 |
-
)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
raise gr.Error("Unable to detect video FPS.")
|
| 79 |
-
|
| 80 |
-
if frames <= 0:
|
| 81 |
-
raise gr.Error("Unable to detect video frame count.")
|
| 82 |
-
|
| 83 |
-
result = process_video(
|
| 84 |
-
video_path,
|
| 85 |
-
detection_prompt or "watermark",
|
| 86 |
-
float(max_bbox_percent),
|
| 87 |
-
max(1, int(detection_skip)),
|
| 88 |
-
max(0, int(fade_in)),
|
| 89 |
-
max(0, int(fade_out)),
|
| 90 |
-
progress=progress,
|
| 91 |
)
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
except gr.Error:
|
| 96 |
raise
|
| 97 |
|
| 98 |
except Exception as e:
|
| 99 |
-
|
| 100 |
-
traceback.print_exc()
|
| 101 |
raise gr.Error(
|
| 102 |
-
f"
|
| 103 |
)
|
| 104 |
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
gr.Markdown(
|
| 107 |
-
"
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
)
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
with gr.Tabs():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
with gr.Tab("🎨 Z-Image"):
|
|
|
|
| 113 |
with gr.Row():
|
|
|
|
| 114 |
with gr.Column():
|
|
|
|
| 115 |
prompt = gr.Textbox(
|
| 116 |
label="Prompt",
|
| 117 |
-
placeholder=
|
|
|
|
|
|
|
| 118 |
lines=5,
|
| 119 |
)
|
|
|
|
| 120 |
with gr.Row():
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
with gr.Row():
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
with gr.Column():
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
generate_btn.click(
|
| 133 |
fn=generate_image,
|
| 134 |
-
inputs=[
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
api_name="generate_image",
|
| 137 |
)
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
with gr.Tab("🧹 图片去水印"):
|
|
|
|
| 140 |
with gr.Row():
|
|
|
|
| 141 |
with gr.Column():
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
with gr.Row():
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
with gr.Column():
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
detect_btn.click(
|
| 155 |
fn=wm_preview,
|
| 156 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
outputs=wm_preview_out,
|
| 158 |
api_name="watermark_preview",
|
| 159 |
)
|
|
|
|
| 160 |
remove_btn.click(
|
| 161 |
fn=wm_remove_image,
|
| 162 |
-
inputs=[
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
api_name="remove_image",
|
| 165 |
)
|
| 166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
with gr.Tab("🎬 视频去水印"):
|
| 168 |
-
gr.Markdown("""
|
| 169 |
-
### 🎬 视频去水印
|
| 170 |
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
- 单次 GPU 任务最长 120 秒
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
|
|
|
| 182 |
)
|
| 183 |
-
|
| 184 |
-
video_prompt = gr.Textbox(value="watermark", label="Detection prompt")
|
| 185 |
with gr.Row():
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
with gr.Row():
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
video_btn.click(
|
| 195 |
fn=wm_remove_video,
|
| 196 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
outputs=video_output,
|
| 198 |
api_name="remove_video",
|
| 199 |
-
|
| 200 |
)
|
| 201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
if __name__ == "__main__":
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
print(
|
| 206 |
f"CUDA available: "
|
|
@@ -208,17 +578,15 @@ if __name__ == "__main__":
|
|
| 208 |
)
|
| 209 |
|
| 210 |
if torch.cuda.is_available():
|
|
|
|
| 211 |
print(
|
| 212 |
"CUDA device:",
|
| 213 |
torch.cuda.get_device_name(
|
| 214 |
torch.cuda.current_device()
|
| 215 |
-
)
|
| 216 |
)
|
| 217 |
|
| 218 |
demo.launch(
|
| 219 |
server_name="0.0.0.0",
|
| 220 |
-
server_port=
|
| 221 |
-
os.getenv("PORT", "7860")
|
| 222 |
-
),
|
| 223 |
-
show_error=True,
|
| 224 |
)
|
|
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
+
import diffusers
|
| 6 |
+
|
| 7 |
+
from diffusers import ZImagePipeline
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# ============================================================
|
| 11 |
+
# VERSION CHECK
|
| 12 |
+
# ============================================================
|
| 13 |
+
|
| 14 |
+
print("=" * 60)
|
| 15 |
+
print("VERSION CHECK")
|
| 16 |
+
print("=" * 60)
|
| 17 |
+
print("Torch:", torch.__version__)
|
| 18 |
+
print("Diffusers:", diffusers.__version__)
|
| 19 |
+
print(
|
| 20 |
+
"Transformers:",
|
| 21 |
+
__import__("transformers").__version__,
|
| 22 |
+
)
|
| 23 |
+
print(
|
| 24 |
+
"HuggingFace Hub:",
|
| 25 |
+
__import__("huggingface_hub").__version__,
|
| 26 |
+
)
|
| 27 |
+
print(
|
| 28 |
+
"Gradio:",
|
| 29 |
+
gr.__version__,
|
| 30 |
+
)
|
| 31 |
+
print(
|
| 32 |
+
"CUDA available:",
|
| 33 |
+
torch.cuda.is_available(),
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
print(
|
| 37 |
+
"Has ZImagePipeline:",
|
| 38 |
+
hasattr(diffusers, "ZImagePipeline"),
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
print("=" * 60)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# ============================================================
|
| 45 |
+
# CHECK Z-IMAGE PIPELINE
|
| 46 |
+
# ============================================================
|
| 47 |
+
|
| 48 |
+
if not hasattr(diffusers, "ZImagePipeline"):
|
| 49 |
+
raise RuntimeError(
|
| 50 |
+
"ZImagePipeline is not available. "
|
| 51 |
+
"Please install diffusers==0.36.0 or newer."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# ============================================================
|
| 56 |
+
# LOAD Z-IMAGE
|
| 57 |
+
# ============================================================
|
| 58 |
|
| 59 |
print("Loading Z-Image-Turbo pipeline...")
|
| 60 |
+
|
| 61 |
+
pipe = ZImagePipeline.from_pretrained(
|
| 62 |
"Tongyi-MAI/Z-Image-Turbo",
|
| 63 |
torch_dtype=torch.bfloat16,
|
| 64 |
low_cpu_mem_usage=False,
|
| 65 |
)
|
| 66 |
+
|
| 67 |
pipe.to("cuda")
|
| 68 |
+
|
| 69 |
print("Z-Image pipeline loaded!")
|
| 70 |
|
| 71 |
+
|
| 72 |
+
# ============================================================
|
| 73 |
+
# WATERMARK MODULE
|
| 74 |
+
# ============================================================
|
| 75 |
+
|
| 76 |
+
from watermark import (
|
| 77 |
+
preview,
|
| 78 |
+
remove_image,
|
| 79 |
+
process_video,
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
from watermark.video import get_video_info
|
| 83 |
|
| 84 |
+
|
| 85 |
+
# ============================================================
|
| 86 |
+
# Z-IMAGE GENERATION
|
| 87 |
+
# ============================================================
|
| 88 |
+
|
| 89 |
@spaces.GPU(duration=60)
|
| 90 |
+
def generate_image(
|
| 91 |
+
prompt: str,
|
| 92 |
+
height: int,
|
| 93 |
+
width: int,
|
| 94 |
+
steps: int,
|
| 95 |
+
seed: int,
|
| 96 |
+
randomize_seed: bool,
|
| 97 |
+
progress=gr.Progress(track_tqdm=True),
|
| 98 |
+
):
|
| 99 |
+
"""
|
| 100 |
+
Generate an image with Z-Image-Turbo.
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
if not prompt or not prompt.strip():
|
| 104 |
+
raise gr.Error("Please enter a prompt.")
|
| 105 |
|
| 106 |
if randomize_seed:
|
| 107 |
+
seed = torch.randint(
|
| 108 |
+
0,
|
| 109 |
+
2**32 - 1,
|
| 110 |
+
(1,),
|
| 111 |
+
).item()
|
| 112 |
+
|
| 113 |
seed = int(seed)
|
| 114 |
+
|
| 115 |
+
generator = torch.Generator(
|
| 116 |
+
device="cuda"
|
| 117 |
+
).manual_seed(seed)
|
| 118 |
+
|
| 119 |
+
print(
|
| 120 |
+
f"Generating image: "
|
| 121 |
+
f"{width}x{height}, "
|
| 122 |
+
f"steps={steps}, "
|
| 123 |
+
f"seed={seed}"
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
image = pipe(
|
| 127 |
prompt=prompt,
|
| 128 |
height=int(height),
|
|
|
|
| 131 |
guidance_scale=0.0,
|
| 132 |
generator=generator,
|
| 133 |
).images[0]
|
| 134 |
+
|
| 135 |
return image, seed
|
| 136 |
|
| 137 |
+
|
| 138 |
+
# ============================================================
|
| 139 |
+
# WATERMARK PREVIEW
|
| 140 |
+
# ============================================================
|
| 141 |
+
|
| 142 |
@spaces.GPU(duration=60)
|
| 143 |
+
def wm_preview(
|
| 144 |
+
image,
|
| 145 |
+
detection_prompt: str = "watermark",
|
| 146 |
+
max_bbox_percent: float = 30,
|
| 147 |
+
):
|
| 148 |
+
"""
|
| 149 |
+
Detect likely watermark regions.
|
| 150 |
+
"""
|
| 151 |
+
|
| 152 |
+
if image is None:
|
| 153 |
+
raise gr.Error("Please upload an image.")
|
| 154 |
+
|
| 155 |
+
return preview(
|
| 156 |
+
image,
|
| 157 |
+
float(max_bbox_percent),
|
| 158 |
+
detection_prompt or "watermark",
|
| 159 |
+
)
|
| 160 |
|
| 161 |
+
|
| 162 |
+
# ============================================================
|
| 163 |
+
# WATERMARK IMAGE REMOVAL
|
| 164 |
+
# ============================================================
|
| 165 |
|
| 166 |
@spaces.GPU(duration=90)
|
| 167 |
+
def wm_remove_image(
|
| 168 |
+
image,
|
| 169 |
+
detection_prompt: str = "watermark",
|
| 170 |
+
max_bbox_percent: float = 30,
|
| 171 |
+
transparent: bool = False,
|
| 172 |
+
):
|
| 173 |
+
"""
|
| 174 |
+
Detect and remove watermark regions from an image.
|
| 175 |
+
"""
|
| 176 |
+
|
| 177 |
+
if image is None:
|
| 178 |
+
raise gr.Error("Please upload an image.")
|
| 179 |
|
| 180 |
return remove_image(
|
| 181 |
+
image,
|
| 182 |
+
float(max_bbox_percent),
|
| 183 |
+
detection_prompt or "watermark",
|
| 184 |
+
bool(transparent),
|
| 185 |
)
|
| 186 |
|
| 187 |
+
|
| 188 |
+
# ============================================================
|
| 189 |
+
# WATERMARK VIDEO REMOVAL
|
| 190 |
+
# ============================================================
|
| 191 |
+
|
| 192 |
@spaces.GPU(duration=120)
|
| 193 |
+
def wm_remove_video(
|
| 194 |
+
video_path: str,
|
| 195 |
+
detection_prompt: str = "watermark",
|
| 196 |
+
max_bbox_percent: float = 30,
|
| 197 |
+
detection_skip: int = 5,
|
| 198 |
+
fade_in: int = 2,
|
| 199 |
+
fade_out: int = 2,
|
| 200 |
+
progress=gr.Progress(),
|
| 201 |
+
):
|
| 202 |
+
"""
|
| 203 |
+
Detect and remove watermarks from a video.
|
| 204 |
+
"""
|
| 205 |
|
| 206 |
if not video_path:
|
| 207 |
+
raise gr.Error(
|
| 208 |
+
"Please upload a video."
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
# --------------------------------------------------------
|
| 212 |
+
# Inspect video
|
| 213 |
+
# --------------------------------------------------------
|
| 214 |
+
|
| 215 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
+
duration, _, _, _, _ = get_video_info(
|
| 218 |
+
video_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
)
|
| 220 |
|
| 221 |
+
max_seconds = float(
|
| 222 |
+
os.getenv(
|
| 223 |
+
"WATERMARK_MAX_VIDEO_SECONDS",
|
| 224 |
+
"30",
|
| 225 |
+
)
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
if duration > max_seconds + 0.25:
|
| 229 |
+
|
| 230 |
+
raise gr.Error(
|
| 231 |
+
f"Video is {duration:.1f}s. "
|
| 232 |
+
f"Maximum is "
|
| 233 |
+
f"{max_seconds:.0f}s "
|
| 234 |
+
f"for this ZeroGPU build. "
|
| 235 |
+
f"Please trim the video."
|
| 236 |
+
)
|
| 237 |
|
| 238 |
except gr.Error:
|
| 239 |
raise
|
| 240 |
|
| 241 |
except Exception as e:
|
| 242 |
+
|
|
|
|
| 243 |
raise gr.Error(
|
| 244 |
+
f"Unable to inspect video: {e}"
|
| 245 |
)
|
| 246 |
|
| 247 |
+
# --------------------------------------------------------
|
| 248 |
+
# Process video
|
| 249 |
+
# --------------------------------------------------------
|
| 250 |
+
|
| 251 |
+
return process_video(
|
| 252 |
+
video_path,
|
| 253 |
+
detection_prompt or "watermark",
|
| 254 |
+
float(max_bbox_percent),
|
| 255 |
+
max(1, int(detection_skip)),
|
| 256 |
+
max(0, int(fade_in)),
|
| 257 |
+
max(0, int(fade_out)),
|
| 258 |
+
progress=progress,
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
# ============================================================
|
| 263 |
+
# GRADIO UI
|
| 264 |
+
# ============================================================
|
| 265 |
+
|
| 266 |
+
with gr.Blocks(
|
| 267 |
+
title="Z-Image + Watermark Remover V5.2"
|
| 268 |
+
) as demo:
|
| 269 |
+
|
| 270 |
gr.Markdown(
|
| 271 |
+
"""
|
| 272 |
+
# 🎨 Z-Image + Watermark Remover V5.2
|
| 273 |
+
|
| 274 |
+
Z-Image-Turbo + Florence-2 + LaMa
|
| 275 |
+
|
| 276 |
+
支持:
|
| 277 |
+
|
| 278 |
+
- 🎨 AI 文生图
|
| 279 |
+
- 🧹 图片去水印
|
| 280 |
+
- 🎬 短视频去水印
|
| 281 |
+
- 🔌 Gradio API
|
| 282 |
+
"""
|
| 283 |
)
|
| 284 |
|
| 285 |
+
# ========================================================
|
| 286 |
+
# TABS
|
| 287 |
+
# ========================================================
|
| 288 |
+
|
| 289 |
with gr.Tabs():
|
| 290 |
+
|
| 291 |
+
# ====================================================
|
| 292 |
+
# Z-IMAGE
|
| 293 |
+
# ====================================================
|
| 294 |
+
|
| 295 |
with gr.Tab("🎨 Z-Image"):
|
| 296 |
+
|
| 297 |
with gr.Row():
|
| 298 |
+
|
| 299 |
with gr.Column():
|
| 300 |
+
|
| 301 |
prompt = gr.Textbox(
|
| 302 |
label="Prompt",
|
| 303 |
+
placeholder=(
|
| 304 |
+
"Describe the image..."
|
| 305 |
+
),
|
| 306 |
lines=5,
|
| 307 |
)
|
| 308 |
+
|
| 309 |
with gr.Row():
|
| 310 |
+
|
| 311 |
+
height = gr.Slider(
|
| 312 |
+
minimum=512,
|
| 313 |
+
maximum=2048,
|
| 314 |
+
value=1024,
|
| 315 |
+
step=64,
|
| 316 |
+
label="Height",
|
| 317 |
+
)
|
| 318 |
+
|
| 319 |
+
width = gr.Slider(
|
| 320 |
+
minimum=512,
|
| 321 |
+
maximum=2048,
|
| 322 |
+
value=1024,
|
| 323 |
+
step=64,
|
| 324 |
+
label="Width",
|
| 325 |
+
)
|
| 326 |
+
|
| 327 |
with gr.Row():
|
| 328 |
+
|
| 329 |
+
steps = gr.Slider(
|
| 330 |
+
minimum=1,
|
| 331 |
+
maximum=30,
|
| 332 |
+
value=9,
|
| 333 |
+
step=1,
|
| 334 |
+
label="Inference Steps",
|
| 335 |
+
)
|
| 336 |
+
|
| 337 |
+
seed = gr.Number(
|
| 338 |
+
value=42,
|
| 339 |
+
precision=0,
|
| 340 |
+
label="Seed",
|
| 341 |
+
)
|
| 342 |
+
|
| 343 |
+
randomize = gr.Checkbox(
|
| 344 |
+
value=True,
|
| 345 |
+
label="Randomize seed",
|
| 346 |
+
)
|
| 347 |
+
|
| 348 |
+
generate_btn = gr.Button(
|
| 349 |
+
"Generate",
|
| 350 |
+
variant="primary",
|
| 351 |
+
)
|
| 352 |
+
|
| 353 |
with gr.Column():
|
| 354 |
+
|
| 355 |
+
output_image = gr.Image(
|
| 356 |
+
label="Generated Image",
|
| 357 |
+
type="pil",
|
| 358 |
+
)
|
| 359 |
+
|
| 360 |
+
output_seed = gr.Number(
|
| 361 |
+
label="Seed used",
|
| 362 |
+
precision=0,
|
| 363 |
+
)
|
| 364 |
|
| 365 |
generate_btn.click(
|
| 366 |
fn=generate_image,
|
| 367 |
+
inputs=[
|
| 368 |
+
prompt,
|
| 369 |
+
height,
|
| 370 |
+
width,
|
| 371 |
+
steps,
|
| 372 |
+
seed,
|
| 373 |
+
randomize,
|
| 374 |
+
],
|
| 375 |
+
outputs=[
|
| 376 |
+
output_image,
|
| 377 |
+
output_seed,
|
| 378 |
+
],
|
| 379 |
api_name="generate_image",
|
| 380 |
)
|
| 381 |
|
| 382 |
+
# ====================================================
|
| 383 |
+
# IMAGE WATERMARK
|
| 384 |
+
# ====================================================
|
| 385 |
+
|
| 386 |
with gr.Tab("🧹 图片去水印"):
|
| 387 |
+
|
| 388 |
with gr.Row():
|
| 389 |
+
|
| 390 |
with gr.Column():
|
| 391 |
+
|
| 392 |
+
wm_input = gr.Image(
|
| 393 |
+
type="pil",
|
| 394 |
+
label="Input image",
|
| 395 |
+
)
|
| 396 |
+
|
| 397 |
+
wm_prompt = gr.Textbox(
|
| 398 |
+
value="watermark",
|
| 399 |
+
label="Detection prompt",
|
| 400 |
+
)
|
| 401 |
+
|
| 402 |
+
wm_max = gr.Slider(
|
| 403 |
+
minimum=1,
|
| 404 |
+
maximum=90,
|
| 405 |
+
value=30,
|
| 406 |
+
step=1,
|
| 407 |
+
label="Max bbox area (%)",
|
| 408 |
+
)
|
| 409 |
+
|
| 410 |
+
wm_transparent = gr.Checkbox(
|
| 411 |
+
value=False,
|
| 412 |
+
label="Transparent output",
|
| 413 |
+
)
|
| 414 |
+
|
| 415 |
with gr.Row():
|
| 416 |
+
|
| 417 |
+
detect_btn = gr.Button(
|
| 418 |
+
"Detect / Preview"
|
| 419 |
+
)
|
| 420 |
+
|
| 421 |
+
remove_btn = gr.Button(
|
| 422 |
+
"Remove watermark",
|
| 423 |
+
variant="primary",
|
| 424 |
+
)
|
| 425 |
+
|
| 426 |
with gr.Column():
|
| 427 |
+
|
| 428 |
+
wm_preview_out = gr.Image(
|
| 429 |
+
label="Detection preview",
|
| 430 |
+
type="pil",
|
| 431 |
+
)
|
| 432 |
+
|
| 433 |
+
wm_result = gr.Image(
|
| 434 |
+
label="Result",
|
| 435 |
+
type="pil",
|
| 436 |
+
)
|
| 437 |
+
|
| 438 |
+
wm_mask = gr.Image(
|
| 439 |
+
label="Mask",
|
| 440 |
+
type="pil",
|
| 441 |
+
)
|
| 442 |
|
| 443 |
detect_btn.click(
|
| 444 |
fn=wm_preview,
|
| 445 |
+
inputs=[
|
| 446 |
+
wm_input,
|
| 447 |
+
wm_prompt,
|
| 448 |
+
wm_max,
|
| 449 |
+
],
|
| 450 |
outputs=wm_preview_out,
|
| 451 |
api_name="watermark_preview",
|
| 452 |
)
|
| 453 |
+
|
| 454 |
remove_btn.click(
|
| 455 |
fn=wm_remove_image,
|
| 456 |
+
inputs=[
|
| 457 |
+
wm_input,
|
| 458 |
+
wm_prompt,
|
| 459 |
+
wm_max,
|
| 460 |
+
wm_transparent,
|
| 461 |
+
],
|
| 462 |
+
outputs=[
|
| 463 |
+
wm_result,
|
| 464 |
+
wm_mask,
|
| 465 |
+
],
|
| 466 |
api_name="remove_image",
|
| 467 |
)
|
| 468 |
|
| 469 |
+
# ====================================================
|
| 470 |
+
# VIDEO WATERMARK
|
| 471 |
+
# ====================================================
|
| 472 |
+
|
| 473 |
with gr.Tab("🎬 视频去水印"):
|
|
|
|
|
|
|
| 474 |
|
| 475 |
+
gr.Markdown(
|
| 476 |
+
"""
|
| 477 |
+
### 视频去水印
|
| 478 |
+
|
| 479 |
+
ZeroGPU 版本:
|
| 480 |
+
|
| 481 |
+
- 默认最多 30 秒
|
| 482 |
+
- 每 N 帧进行一次 Florence-2 检测
|
| 483 |
+
- LaMa 对检测到的水印区域进行局部修复
|
| 484 |
+
- 尽量保留原视频音频
|
| 485 |
+
"""
|
| 486 |
+
)
|
| 487 |
+
|
| 488 |
+
# ------------------------------------------------
|
| 489 |
+
# IMPORTANT:
|
| 490 |
+
# Gradio 5.23.1 does NOT use type="filepath"
|
| 491 |
+
# ------------------------------------------------
|
| 492 |
|
| 493 |
+
video_input = gr.Video(
|
| 494 |
+
label="Input video (max 30s)",
|
| 495 |
+
sources=["upload"],
|
| 496 |
+
format="mp4",
|
| 497 |
+
)
|
|
|
|
| 498 |
|
| 499 |
+
video_prompt = gr.Textbox(
|
| 500 |
+
value="watermark",
|
| 501 |
+
label="Detection prompt",
|
| 502 |
)
|
| 503 |
+
|
|
|
|
| 504 |
with gr.Row():
|
| 505 |
+
|
| 506 |
+
video_max = gr.Slider(
|
| 507 |
+
minimum=1,
|
| 508 |
+
maximum=90,
|
| 509 |
+
value=30,
|
| 510 |
+
step=1,
|
| 511 |
+
label="Max bbox area (%)",
|
| 512 |
+
)
|
| 513 |
+
|
| 514 |
+
video_skip = gr.Slider(
|
| 515 |
+
minimum=1,
|
| 516 |
+
maximum=30,
|
| 517 |
+
value=5,
|
| 518 |
+
step=1,
|
| 519 |
+
label="Detection interval",
|
| 520 |
+
)
|
| 521 |
+
|
| 522 |
with gr.Row():
|
| 523 |
+
|
| 524 |
+
fade_in = gr.Slider(
|
| 525 |
+
minimum=0,
|
| 526 |
+
maximum=30,
|
| 527 |
+
value=2,
|
| 528 |
+
step=1,
|
| 529 |
+
label="Fade in frames",
|
| 530 |
+
)
|
| 531 |
+
|
| 532 |
+
fade_out = gr.Slider(
|
| 533 |
+
minimum=0,
|
| 534 |
+
maximum=30,
|
| 535 |
+
value=2,
|
| 536 |
+
step=1,
|
| 537 |
+
label="Fade out frames",
|
| 538 |
+
)
|
| 539 |
+
|
| 540 |
+
video_btn = gr.Button(
|
| 541 |
+
"Remove watermark from video",
|
| 542 |
+
variant="primary",
|
| 543 |
+
)
|
| 544 |
+
|
| 545 |
+
video_output = gr.Video(
|
| 546 |
+
label="Output video",
|
| 547 |
+
format="mp4",
|
| 548 |
+
)
|
| 549 |
|
| 550 |
video_btn.click(
|
| 551 |
fn=wm_remove_video,
|
| 552 |
+
inputs=[
|
| 553 |
+
video_input,
|
| 554 |
+
video_prompt,
|
| 555 |
+
video_max,
|
| 556 |
+
video_skip,
|
| 557 |
+
fade_in,
|
| 558 |
+
fade_out,
|
| 559 |
+
],
|
| 560 |
outputs=video_output,
|
| 561 |
api_name="remove_video",
|
|
|
|
| 562 |
)
|
| 563 |
|
| 564 |
+
|
| 565 |
+
# ============================================================
|
| 566 |
+
# START
|
| 567 |
+
# ============================================================
|
| 568 |
+
|
| 569 |
if __name__ == "__main__":
|
| 570 |
+
|
| 571 |
+
print(
|
| 572 |
+
"Starting Gradio Space..."
|
| 573 |
+
)
|
| 574 |
|
| 575 |
print(
|
| 576 |
f"CUDA available: "
|
|
|
|
| 578 |
)
|
| 579 |
|
| 580 |
if torch.cuda.is_available():
|
| 581 |
+
|
| 582 |
print(
|
| 583 |
"CUDA device:",
|
| 584 |
torch.cuda.get_device_name(
|
| 585 |
torch.cuda.current_device()
|
| 586 |
+
),
|
| 587 |
)
|
| 588 |
|
| 589 |
demo.launch(
|
| 590 |
server_name="0.0.0.0",
|
| 591 |
+
server_port=7860,
|
|
|
|
|
|
|
|
|
|
| 592 |
)
|