Spaces:
Running
Running
Commit
·
009432d
1
Parent(s):
ad57895
init
Browse files
app.py
CHANGED
|
@@ -3,11 +3,59 @@ import threading
|
|
| 3 |
import os
|
| 4 |
import shutil
|
| 5 |
import tempfile
|
|
|
|
|
|
|
| 6 |
from util import process_image_edit, get_country_info_safe, get_location_info_safe, contains_chinese
|
| 7 |
from nfsw import NSFWDetector
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
IP_Dict = {}
|
| 10 |
NSFW_Dict = {} # 记录每个IP的NSFW违规次数
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# 初始化NSFW检测器(从Hugging Face下载)
|
| 13 |
try:
|
|
@@ -78,10 +126,17 @@ def edit_image_interface(input_image, prompt, request: gr.Request, progress=gr.P
|
|
| 78 |
print(f"🔍 NSFW检测结果: {nsfw_result} - IP: {client_ip}({country_info})")
|
| 79 |
|
| 80 |
if nsfw_result.lower() == "nsfw":
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# 检查是否需要特殊处理(中文prompt或中国IP)
|
| 87 |
has_chinese = contains_chinese(prompt.strip())
|
|
|
|
| 3 |
import os
|
| 4 |
import shutil
|
| 5 |
import tempfile
|
| 6 |
+
import time
|
| 7 |
+
from collections import defaultdict, deque
|
| 8 |
from util import process_image_edit, get_country_info_safe, get_location_info_safe, contains_chinese
|
| 9 |
from nfsw import NSFWDetector
|
| 10 |
|
| 11 |
+
# 配置参数
|
| 12 |
+
NSFW_TIME_WINDOW = 5 # 时间窗口:5分钟
|
| 13 |
+
NSFW_LIMIT = 6 # 限制次数:6次
|
| 14 |
+
|
| 15 |
IP_Dict = {}
|
| 16 |
NSFW_Dict = {} # 记录每个IP的NSFW违规次数
|
| 17 |
+
NSFW_Time_Dict = defaultdict(deque) # 记录每个IP的NSFW检测时间戳
|
| 18 |
+
|
| 19 |
+
def check_nsfw_rate_limit(client_ip):
|
| 20 |
+
"""
|
| 21 |
+
检查IP的NSFW检测频率限制
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
client_ip (str): 客户端IP地址
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
tuple: (是否超过限制, 剩余等待时间)
|
| 28 |
+
"""
|
| 29 |
+
current_time = time.time()
|
| 30 |
+
time_window_start = current_time - (NSFW_TIME_WINDOW * 60) # 5分钟前的时间戳
|
| 31 |
+
|
| 32 |
+
# 清理过期的时间戳
|
| 33 |
+
while NSFW_Time_Dict[client_ip] and NSFW_Time_Dict[client_ip][0] < time_window_start:
|
| 34 |
+
NSFW_Time_Dict[client_ip].popleft()
|
| 35 |
+
|
| 36 |
+
# 检查是否超过限制
|
| 37 |
+
if len(NSFW_Time_Dict[client_ip]) >= NSFW_LIMIT:
|
| 38 |
+
# 计算需要等待的时间
|
| 39 |
+
oldest_timestamp = NSFW_Time_Dict[client_ip][0]
|
| 40 |
+
wait_time = oldest_timestamp + (NSFW_TIME_WINDOW * 60) - current_time
|
| 41 |
+
return True, max(0, wait_time)
|
| 42 |
+
|
| 43 |
+
return False, 0
|
| 44 |
+
|
| 45 |
+
def record_nsfw_detection(client_ip):
|
| 46 |
+
"""
|
| 47 |
+
记录IP的NSFW检测时间
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
client_ip (str): 客户端IP地址
|
| 51 |
+
"""
|
| 52 |
+
current_time = time.time()
|
| 53 |
+
NSFW_Time_Dict[client_ip].append(current_time)
|
| 54 |
+
|
| 55 |
+
# 记录到NSFW_Dict中(兼容现有逻辑)
|
| 56 |
+
if client_ip not in NSFW_Dict:
|
| 57 |
+
NSFW_Dict[client_ip] = 0
|
| 58 |
+
NSFW_Dict[client_ip] += 1
|
| 59 |
|
| 60 |
# 初始化NSFW检测器(从Hugging Face下载)
|
| 61 |
try:
|
|
|
|
| 126 |
print(f"🔍 NSFW检测结果: {nsfw_result} - IP: {client_ip}({country_info})")
|
| 127 |
|
| 128 |
if nsfw_result.lower() == "nsfw":
|
| 129 |
+
# 检查NSFW频率限制
|
| 130 |
+
is_rate_limited, wait_time = check_nsfw_rate_limit(client_ip)
|
| 131 |
+
|
| 132 |
+
if is_rate_limited:
|
| 133 |
+
# 超过频率限制,显示等待提示
|
| 134 |
+
wait_minutes = int(wait_time / 60) + 1 # 向上取整到分钟
|
| 135 |
+
print(f"⚠️ NSFW频率限制 - IP: {client_ip}({country_info}), 需要等待 {wait_minutes} 分钟")
|
| 136 |
+
return None, f"❌ 请等待 {wait_minutes} 分钟后继续生成"
|
| 137 |
+
|
| 138 |
+
# 记录NSFW检测
|
| 139 |
+
record_nsfw_detection(client_ip)
|
| 140 |
|
| 141 |
# 检查是否需要特殊处理(中文prompt或中国IP)
|
| 142 |
has_chinese = contains_chinese(prompt.strip())
|