import gradio as gr import threading import os import shutil import tempfile import time from util import process_image_edit, OPENROUTER_API_KEY # Configuration parameters for watermark removal FREE_TRY_N = 15 # Free phase: first 8 tries without restrictions SLOW_TRY_N = 20 # Slow phase start: 20 tries RATE_LIMIT_20 = 25 # Full restriction: blocked after 20 tries # Time window configuration (minutes) PHASE_1_WINDOW = 5 # 8-20 tries: 5 minutes MAX_IMAGES_PER_WINDOW = 2 # Max images per time window IP_Dict = {} # IP generation statistics and time window tracking IP_Generation_Count = {} # Record total generation count for each IP IP_Rate_Limit_Track = {} # Record generation count and timestamp in current time window for each IP def get_ip_generation_count(client_ip): """ Get IP generation count """ if client_ip not in IP_Generation_Count: IP_Generation_Count[client_ip] = 0 return IP_Generation_Count[client_ip] def increment_ip_generation_count(client_ip): """ Increment IP generation count """ if client_ip not in IP_Generation_Count: IP_Generation_Count[client_ip] = 0 IP_Generation_Count[client_ip] += 1 return IP_Generation_Count[client_ip] def get_ip_phase(client_ip): """ Get current phase for IP Returns: str: 'free', 'rate_limit_1', 'blocked' """ count = get_ip_generation_count(client_ip) if count < FREE_TRY_N: # 0-7 tries return 'free' elif count < SLOW_TRY_N: # 8-19 tries return 'rate_limit_1' # 5 minutes 2 images else: # 20+ tries return 'blocked' # Generation blocked def check_rate_limit_for_phase(client_ip, phase): """ Check rate limit for specific phase Returns: tuple: (is_limited, wait_time_minutes, current_count) """ if phase not in ['rate_limit_1']: return False, 0, 0 # Determine time window window_minutes = PHASE_1_WINDOW # 5 minutes current_time = time.time() window_key = f"{client_ip}_{phase}" # Clean expired records if window_key in IP_Rate_Limit_Track: track_data = IP_Rate_Limit_Track[window_key] # Check if within current time window if current_time - track_data['start_time'] > window_minutes * 60: # Time window expired, reset IP_Rate_Limit_Track[window_key] = { 'count': 0, 'start_time': current_time, 'last_generation': current_time } else: # Initialize IP_Rate_Limit_Track[window_key] = { 'count': 0, 'start_time': current_time, 'last_generation': current_time } track_data = IP_Rate_Limit_Track[window_key] # Check if exceeded limit if track_data['count'] >= MAX_IMAGES_PER_WINDOW: # Calculate remaining wait time elapsed = current_time - track_data['start_time'] wait_time = (window_minutes * 60) - elapsed wait_minutes = max(0, wait_time / 60) return True, wait_minutes, track_data['count'] return False, 0, track_data['count'] def record_generation_attempt(client_ip, phase): """ Record generation attempt """ # Increment total count increment_ip_generation_count(client_ip) # Record time window count if phase in ['rate_limit_1']: window_key = f"{client_ip}_{phase}" current_time = time.time() if window_key in IP_Rate_Limit_Track: IP_Rate_Limit_Track[window_key]['count'] += 1 IP_Rate_Limit_Track[window_key]['last_generation'] = current_time else: IP_Rate_Limit_Track[window_key] = { 'count': 1, 'start_time': current_time, 'last_generation': current_time } # Load sample images from samples directory def load_sample_images(): """Load sample images from samples directory""" samples_dir = "samples" sample_images = [] if os.path.exists(samples_dir): for filename in os.listdir(samples_dir): if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')): if "rm" in filename: continue file_path = os.path.join(samples_dir, filename) sample_images.append((file_path, filename)) return sample_images # Pre-processed results for sample images (local files) SAMPLE_RESULTS = { "sample01.jpeg": "samples/sample01_rm.jpeg", "sample02.jpeg": "samples/sample02_rm.jpeg", "1.jpg": "samples/1_rm.jpg", "4.jpg": "samples/4_rm.jpg", "10.jpg": "samples/10_rm.jpg" } def generate_smart_watermark_prompt(image_url): """Generate smart watermark removal prompt using OpenRouter API""" import requests import json try: api_key = OPENROUTER_API_KEY if 'OPENROUTER_API_KEY' in globals() else "" if not api_key: print("Warning: OPENROUTER_API_KEY not found") return "remove watermark in the image" headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {api_key}' } data = { "model": "google/gemini-2.5-flash-lite", "messages": [ {"role": "user", "content": "现在用户输入了一张带水印的图片,需要生成修复prompt来引导图像生成模型去除水印。你需要找到水印,然后输出去除水印的英文prompt,指出水印的位置和特点,让模型知道即可,尽量简短。直接输出prompt即可。举例1: 'Remove the blue watermark in the lower right corner of the document'. 举例2: 'Remove the white watermark over the whole picture'"}, {"role": "assistant", "content": "好的"}, { "role": "user", "content": [ {"type": "text", "text": "Generate an English prompt to remove the watermark in this image."}, {"type": "image_url", "image_url": {"url": image_url}} ] } ] } response = requests.post( 'https://openrouter.ai/api/v1/chat/completions', headers=headers, json=data, timeout=30 ) if response.status_code == 200: result = response.json() if result.get("choices") and len(result["choices"]) > 0: prompt = result["choices"][0]["message"]["content"].strip() print(f"✅ Generated smart prompt: {prompt}") return prompt print(f"⚠️ OpenRouter API failed with status {response.status_code}") return "remove watermark in the image" except Exception as e: print(f"⚠️ Error generating smart prompt: {e}") return "remove watermark in the image" def remove_watermark_interface(input_image, force_removal, request: gr.Request, progress=gr.Progress()): """ Interface function for watermark removal with phase-based limitations """ try: # Extract user IP client_ip = request.client.host x_forwarded_for = dict(request.headers).get('x-forwarded-for') if x_forwarded_for: client_ip = x_forwarded_for if client_ip not in IP_Dict: IP_Dict[client_ip] = 0 IP_Dict[client_ip] += 1 if input_image is None: return None, "Please upload an image first", gr.update(visible=False) # Set initial prompt for watermark removal prompt = "remove watermark in the image" except Exception as e: print(f"⚠️ Request preprocessing error: {e}") return None, "❌ Request processing error", gr.update(visible=False) # Get user current phase current_phase = get_ip_phase(client_ip) current_count = get_ip_generation_count(client_ip) print(f"📊 User phase info - IP: {client_ip}, current phase: {current_phase}, generation count: {current_count}") # Check if completely blocked if current_phase == 'blocked': # Generate blocked limit button blocked_button_html = f"""
""" return None, f"❌ You have reached Hugging Face's free watermark removal limit. Please visit https://omnicreator.net/remove-watermark#generator for unlimited removal", gr.update(value=blocked_button_html, visible=True) # Check rate limit (applies to rate_limit phases) if current_phase in ['rate_limit_1']: is_limited, wait_minutes, window_count = check_rate_limit_for_phase(client_ip, current_phase) if is_limited: wait_minutes_int = int(wait_minutes) + 1 # Generate rate limit button rate_limit_button_html = f""" """ return None, f"❌ You have reached Hugging Face's free watermark removal limit. Please visit https://omnicreator.net/remove-watermark#generator for unlimited removal, or wait {wait_minutes_int} minutes before generating again", gr.update(value=rate_limit_button_html, visible=True) # No NSFW detection needed for watermark removal result_url = None status_message = "" def progress_callback(message): try: nonlocal status_message status_message = message # Add error handling to prevent progress update failure if progress is not None: progress(0.5, desc=message) except Exception as e: print(f"⚠️ Progress update failed: {e}") try: # Record generation attempt (before actual generation to ensure correct count) record_generation_attempt(client_ip, current_phase) updated_count = get_ip_generation_count(client_ip) print(f"✅ Processing started - IP: {client_ip}, phase: {current_phase}, total count: {updated_count}, prompt: {prompt.strip()}", flush=True) # Force removal mode: Generate smart prompt using OpenRouter API if force_removal: if progress is not None: progress(0.3, desc="💪 AI Force Mode: Analyzing watermark pattern...") try: # Upload image to get URL for OpenRouter API from util import upload_user_img_r2 time_id = int(time.time()) image_url = upload_user_img_r2(client_ip, time_id, input_image) if image_url: # Generate smart prompt using OpenRouter API smart_prompt = generate_smart_watermark_prompt(image_url) if smart_prompt and smart_prompt.strip() != "remove watermark in the image": prompt = smart_prompt print(f"🚀 Using AI Force Mode prompt: {prompt}") else: print("⚠️ Using fallback prompt") else: print("⚠️ Failed to upload image for AI analysis, using default prompt") except Exception as e: print(f"⚠️ Force Mode prompt generation failed: {e}, using default prompt") if progress is not None: progress(0.5, desc="Starting force watermark removal...") # Call image editing processing function result_url, message, task_uuid = process_image_edit(input_image, prompt.strip(), progress_callback) if result_url: print(f"✅ Watermark removal completed successfully - IP: {client_ip}, result_url: {result_url}, task_uuid: {task_uuid}", flush=True) # No NSFW detection needed for watermark removal final_result = result_url final_message = "✅ " + message try: if progress is not None: progress(1.0, desc="Processing completed") except Exception as e: print(f"⚠️ Final progress update failed: {e}") # Generate action buttons HTML like Trump AI Voice action_buttons_html = "" if task_uuid: task_detail_url = f"https://omnicreator.net/my-creations/task/{task_uuid}" action_buttons_html = f"""Not satisfied with the removal result?
💪 Try Force Mode on Websitepowered by omnicreator.net
Remove watermarks from any image instantly with advanced AI technology! Clean your photos and documents without compromising image quality using our professional watermark removal tool.
Join thousands of users who trust Omni Creator for professional watermark removal!
Clean your images effortlessly with our advanced AI watermark removal technology. Whether you're dealing with text watermarks, logo stamps, or transparent overlays - our intelligent algorithm removes them while preserving the original image quality and details.
Premium users enjoy unlimited watermark removal without daily limits or processing restrictions. Clean as many images as you need, whenever you need them, with no quality loss.
AI automatically identifies and removes various types of watermarks including text, logos, stamps, and transparent overlays without affecting the main image content.
Advanced AI infrastructure delivers watermark-free results in seconds. No waiting in queues, no processing delays - just instant, professional-grade watermark removal.
Intelligent inpainting technology reconstructs watermark areas seamlessly while maintaining original image quality, colors, textures, and details perfectly.
State-of-the-art AI models trained specifically for watermark removal deliver exceptional results. Professional quality output suitable for commercial use and high-end projects.
Works with all image formats (JPG, PNG, GIF, BMP) and removes any type of watermark. From photos to documents, product images to artwork - we clean them all perfectly.
Remove watermarks from unlimited images without waiting periods or daily restrictions
Remove any type of watermark including complex, transparent, and multi-layered marks
Skip queues and get instant watermark removal with dedicated processing power
Access to latest AI models for highest quality watermark removal results
Higher resolution input images (up to 10MB) generally produce better watermark removal results and finer details.
Images with simple backgrounds work best. Complex patterns behind watermarks may require multiple processing attempts.
Use "Use as Input" feature to refine results. Multiple iterations can remove stubborn or complex watermarks completely.
JPG and PNG formats work best. Avoid heavily compressed images as they may affect watermark detection accuracy.
Powered by Omni Creator
The ultimate AI watermark removal platform • Professional results, unlimited processing