Spaces:
Running
Running
Commit
·
1a8b29b
1
Parent(s):
8721ec2
init
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import threading
|
|
| 3 |
import os
|
| 4 |
import shutil
|
| 5 |
import tempfile
|
| 6 |
-
from util import process_image_edit, get_country_info_safe
|
| 7 |
from nfsw import NSFWDetector
|
| 8 |
|
| 9 |
IP_Dict = {}
|
|
@@ -32,11 +32,32 @@ def edit_image_interface(input_image, prompt, request: gr.Request, progress=gr.P
|
|
| 32 |
|
| 33 |
# 获取IP属地信息
|
| 34 |
country_info = get_country_info_safe(client_ip)
|
|
|
|
| 35 |
|
| 36 |
# 检查IP是否因NSFW违规过多而被屏蔽 3
|
| 37 |
if client_ip in NSFW_Dict and NSFW_Dict[client_ip] >= 5:
|
| 38 |
print(f"❌ IP blocked due to excessive NSFW violations - IP: {client_ip}({country_info}), violations: {NSFW_Dict[client_ip]}")
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
if input_image is None:
|
| 42 |
return None, "Please upload an image first"
|
|
@@ -61,8 +82,33 @@ def edit_image_interface(input_image, prompt, request: gr.Request, progress=gr.P
|
|
| 61 |
if client_ip not in NSFW_Dict:
|
| 62 |
NSFW_Dict[client_ip] = 0
|
| 63 |
NSFW_Dict[client_ip] += 1
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
print(f"⚠️ NSFW检测失败: {e}")
|
|
|
|
| 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 = {}
|
|
|
|
| 32 |
|
| 33 |
# 获取IP属地信息
|
| 34 |
country_info = get_country_info_safe(client_ip)
|
| 35 |
+
location_info = get_location_info_safe(client_ip)
|
| 36 |
|
| 37 |
# 检查IP是否因NSFW违规过多而被屏蔽 3
|
| 38 |
if client_ip in NSFW_Dict and NSFW_Dict[client_ip] >= 5:
|
| 39 |
print(f"❌ IP blocked due to excessive NSFW violations - IP: {client_ip}({country_info}), violations: {NSFW_Dict[client_ip]}")
|
| 40 |
+
|
| 41 |
+
# 检查是否需要特殊处理(中文prompt或中国IP)
|
| 42 |
+
has_chinese = contains_chinese(prompt.strip()) if prompt else False
|
| 43 |
+
is_china_ip = location_info.get("country", "").lower() == "中国"
|
| 44 |
+
|
| 45 |
+
if has_chinese or is_china_ip:
|
| 46 |
+
# 构建特殊警告信息
|
| 47 |
+
prov = location_info.get("prov", "未知省份")
|
| 48 |
+
city = location_info.get("city", "未知城市")
|
| 49 |
+
isp = location_info.get("isp", "Unknown")
|
| 50 |
+
|
| 51 |
+
# 处理城市名称
|
| 52 |
+
city_name = city.replace("市", "") if city != "未知城市" else "当地"
|
| 53 |
+
|
| 54 |
+
# 处理ISP名称,去掉"中国"前缀
|
| 55 |
+
isp_name = isp.replace("中国", "") if isp != "Unknown" else "未知"
|
| 56 |
+
|
| 57 |
+
warning_msg = f"您的ip地址为{client_ip},地点为{prov}-{city},您的网络服务商为{isp_name},检测到您目前正在浏览,制作淫秽内容,已经上报给 {city_name}市公安局,已加急处理,请耐心等待警察上门调查"
|
| 58 |
+
return None, f"❌ {warning_msg}"
|
| 59 |
+
else:
|
| 60 |
+
return None, f"❌ Your ip {client_ip},your region has been blocked for too much nsfw content"
|
| 61 |
|
| 62 |
if input_image is None:
|
| 63 |
return None, "Please upload an image first"
|
|
|
|
| 82 |
if client_ip not in NSFW_Dict:
|
| 83 |
NSFW_Dict[client_ip] = 0
|
| 84 |
NSFW_Dict[client_ip] += 1
|
| 85 |
+
|
| 86 |
+
# 检查是否需要特殊处理(中文prompt或中国IP)
|
| 87 |
+
has_chinese = contains_chinese(prompt.strip())
|
| 88 |
+
is_china_ip = location_info.get("country", "").lower() == "中国"
|
| 89 |
+
|
| 90 |
+
if has_chinese or is_china_ip:
|
| 91 |
+
# 构建特殊警告信息
|
| 92 |
+
prov = location_info.get("prov", "未知省份")
|
| 93 |
+
city = location_info.get("city", "未知城市")
|
| 94 |
+
isp = location_info.get("isp", "Unknown")
|
| 95 |
+
|
| 96 |
+
# 处理城市名称
|
| 97 |
+
city_name = city.replace("市", "") if city != "未知城市" else "当地"
|
| 98 |
+
|
| 99 |
+
# 处理ISP名称,去掉"中国"前缀
|
| 100 |
+
isp_name = isp.replace("中国", "") if isp != "Unknown" else "未知"
|
| 101 |
+
|
| 102 |
+
warning_msg = f"您的ip地址为{client_ip},地点为{prov}-{city},您的网络服务商为{isp_name},检测到您目前正在浏览,制作淫秽内容,已经上报给 {city_name}市公安局,已加急处理,请耐心等待警察上门调查"
|
| 103 |
+
|
| 104 |
+
print(f"⚠️ 特殊NSFW警告 - IP: {client_ip}, 位置: {prov}-{city}, ISP: {isp_name}, 中文Prompt: {has_chinese}, 中国IP: {is_china_ip}")
|
| 105 |
+
print(warning_msg)
|
| 106 |
+
|
| 107 |
+
return None, f"❌ {warning_msg}"
|
| 108 |
+
else:
|
| 109 |
+
# 常规NSFW提示
|
| 110 |
+
print(f"❌ NSFW image detected - IP: {client_ip}({country_info}), violations: {NSFW_Dict[client_ip]}")
|
| 111 |
+
return None, f"❌ Your ip {client_ip},your region has been blocked for too much nsfw content"
|
| 112 |
|
| 113 |
except Exception as e:
|
| 114 |
print(f"⚠️ NSFW检测失败: {e}")
|
util.py
CHANGED
|
@@ -167,6 +167,45 @@ def get_country_info(ip):
|
|
| 167 |
print(f"获取IP属地失败: {e}")
|
| 168 |
return "Unknown"
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
def get_country_info_safe(ip):
|
| 171 |
"""安全获取IP属地信息,出错时返回Unknown"""
|
| 172 |
try:
|
|
@@ -178,6 +217,22 @@ def get_country_info_safe(ip):
|
|
| 178 |
print(f"获取IP属地失败: {e}")
|
| 179 |
return "Unknown"
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
|
| 183 |
|
|
|
|
| 167 |
print(f"获取IP属地失败: {e}")
|
| 168 |
return "Unknown"
|
| 169 |
|
| 170 |
+
@func_timeout.func_set_timeout(10)
|
| 171 |
+
def get_location_info(ip):
|
| 172 |
+
"""获取IP对应的详细位置信息"""
|
| 173 |
+
try:
|
| 174 |
+
# 使用您指定的新接口 URL
|
| 175 |
+
url = f"https://qifu-api.baidubce.com/ip/geo/v1/district?ip={ip}"
|
| 176 |
+
ret = requests.get(url)
|
| 177 |
+
ret.raise_for_status()
|
| 178 |
+
|
| 179 |
+
json_data = ret.json()
|
| 180 |
+
|
| 181 |
+
if json_data.get("code") == "Success":
|
| 182 |
+
data = json_data.get("data", {})
|
| 183 |
+
return {
|
| 184 |
+
"country": data.get("country", "Unknown"),
|
| 185 |
+
"prov": data.get("prov", "Unknown"),
|
| 186 |
+
"city": data.get("city", "Unknown"),
|
| 187 |
+
"isp": data.get("isp", "Unknown"),
|
| 188 |
+
"owner": data.get("owner", "Unknown"),
|
| 189 |
+
"full_data": data
|
| 190 |
+
}
|
| 191 |
+
else:
|
| 192 |
+
print(f"API请求失败: {json_data.get('msg', '未知错误')}")
|
| 193 |
+
return {
|
| 194 |
+
"country": "Unknown",
|
| 195 |
+
"prov": "Unknown",
|
| 196 |
+
"city": "Unknown",
|
| 197 |
+
"isp": "Unknown",
|
| 198 |
+
"owner": "Unknown",
|
| 199 |
+
"full_data": {}
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
except requests.exceptions.RequestException as e:
|
| 203 |
+
print(f"网络请求失败: {e}")
|
| 204 |
+
return {"country": "Unknown", "prov": "Unknown", "city": "Unknown", "isp": "Unknown", "owner": "Unknown", "full_data": {}}
|
| 205 |
+
except Exception as e:
|
| 206 |
+
print(f"获取IP属地失败: {e}")
|
| 207 |
+
return {"country": "Unknown", "prov": "Unknown", "city": "Unknown", "isp": "Unknown", "owner": "Unknown", "full_data": {}}
|
| 208 |
+
|
| 209 |
def get_country_info_safe(ip):
|
| 210 |
"""安全获取IP属地信息,出错时返回Unknown"""
|
| 211 |
try:
|
|
|
|
| 217 |
print(f"获取IP属地失败: {e}")
|
| 218 |
return "Unknown"
|
| 219 |
|
| 220 |
+
def get_location_info_safe(ip):
|
| 221 |
+
"""安全获取IP详细位置信息,出错时返回默认值"""
|
| 222 |
+
try:
|
| 223 |
+
return get_location_info(ip)
|
| 224 |
+
except func_timeout.FunctionTimedOut:
|
| 225 |
+
print(f"获取IP位置超时: {ip}")
|
| 226 |
+
return {"country": "Unknown", "prov": "Unknown", "city": "Unknown", "isp": "Unknown", "owner": "Unknown", "full_data": {}}
|
| 227 |
+
except Exception as e:
|
| 228 |
+
print(f"获取IP位置失败: {e}")
|
| 229 |
+
return {"country": "Unknown", "prov": "Unknown", "city": "Unknown", "isp": "Unknown", "owner": "Unknown", "full_data": {}}
|
| 230 |
+
|
| 231 |
+
def contains_chinese(text):
|
| 232 |
+
"""检测文本是否包含中文字符"""
|
| 233 |
+
import re
|
| 234 |
+
return bool(re.search(r'[\u4e00-\u9fff]', text))
|
| 235 |
+
|
| 236 |
|
| 237 |
|
| 238 |
|