Spaces:
Running
Running
Commit
·
c276a99
1
Parent(s):
3062328
init
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import os
|
|
| 4 |
import shutil
|
| 5 |
import tempfile
|
| 6 |
import time
|
| 7 |
-
from util import process_image_edit, process_local_image_edit
|
| 8 |
from nfsw import NSFWDetector
|
| 9 |
|
| 10 |
# 配置参数
|
|
@@ -149,8 +149,6 @@ def edit_image_interface(input_image, prompt, request: gr.Request, progress=gr.P
|
|
| 149 |
IP_Dict[client_ip] = 0
|
| 150 |
IP_Dict[client_ip] += 1
|
| 151 |
|
| 152 |
-
# 获取IP属地信息
|
| 153 |
-
country_info = get_country_info_safe(client_ip)
|
| 154 |
|
| 155 |
if input_image is None:
|
| 156 |
return None, "Please upload an image first"
|
|
@@ -170,7 +168,7 @@ def edit_image_interface(input_image, prompt, request: gr.Request, progress=gr.P
|
|
| 170 |
nsfw_result = nsfw_detector.predict_pil_label_only(input_image)
|
| 171 |
|
| 172 |
if nsfw_result.lower() == "nsfw":
|
| 173 |
-
print(f"🔍 NSFW检测结果: ❌❌❌ {nsfw_result} - IP: {client_ip}
|
| 174 |
# 检查NSFW频率限制
|
| 175 |
is_rate_limited, wait_time = check_nsfw_rate_limit(client_ip)
|
| 176 |
|
|
@@ -178,7 +176,7 @@ def edit_image_interface(input_image, prompt, request: gr.Request, progress=gr.P
|
|
| 178 |
# 超过频率限制,显示等待提示并阻止继续
|
| 179 |
wait_minutes = int(wait_time / 60) + 1 # 向上取整到分钟
|
| 180 |
window_info = get_current_window_info(client_ip)
|
| 181 |
-
print(f"⚠️ NSFW频率限制 - IP: {client_ip}
|
| 182 |
print(f" 时间窗口: {window_info['window_start']} - {window_info['window_end']}")
|
| 183 |
print(f" 当前计数: {window_info['current_count']}/{NSFW_LIMIT}, 需要等待 {wait_minutes} 分钟")
|
| 184 |
return None, f"❌ Please wait {wait_minutes} minutes before generating again"
|
|
@@ -187,15 +185,12 @@ def edit_image_interface(input_image, prompt, request: gr.Request, progress=gr.P
|
|
| 187 |
record_nsfw_detection(client_ip)
|
| 188 |
window_info = get_current_window_info(client_ip)
|
| 189 |
else:
|
| 190 |
-
print(f"🔍 NSFW检测结果: ✅✅✅ {nsfw_result} - IP: {client_ip}
|
| 191 |
|
| 192 |
except Exception as e:
|
| 193 |
print(f"⚠️ NSFW检测失败: {e}")
|
| 194 |
# 检测失败时允许继续处理
|
| 195 |
|
| 196 |
-
if IP_Dict[client_ip]>10 and country_info.lower() in ["印度", "巴基斯坦"]:
|
| 197 |
-
print(f"❌ Content not allowed - IP: {client_ip}({country_info}), count: {IP_Dict[client_ip]}, prompt: {prompt.strip()}")
|
| 198 |
-
return None, "❌ Content not allowed. Please modify your prompt"
|
| 199 |
|
| 200 |
result_url = None
|
| 201 |
status_message = ""
|
|
@@ -207,17 +202,17 @@ def edit_image_interface(input_image, prompt, request: gr.Request, progress=gr.P
|
|
| 207 |
|
| 208 |
try:
|
| 209 |
# 打印成功访问的信息
|
| 210 |
-
print(f"✅ Processing started - IP: {client_ip}
|
| 211 |
|
| 212 |
# Call image editing processing function
|
| 213 |
result_url, message = process_image_edit(input_image, prompt.strip(), progress_callback)
|
| 214 |
|
| 215 |
if result_url:
|
| 216 |
-
print(f"✅ Processing completed successfully - IP: {client_ip}
|
| 217 |
progress(1.0, desc="Processing completed")
|
| 218 |
return result_url, "✅ " + message
|
| 219 |
else:
|
| 220 |
-
print(f"❌ Processing failed - IP: {client_ip}
|
| 221 |
return None, "❌ " + message
|
| 222 |
|
| 223 |
except Exception as e:
|
|
@@ -252,7 +247,12 @@ def use_local_output_as_input(output_image):
|
|
| 252 |
if output_image is not None:
|
| 253 |
# 对于局部编辑,我们需要创建一个新的ImageEditor格式
|
| 254 |
# 将输出图片作为背景,不包含任何编辑层
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
return None, None
|
| 257 |
|
| 258 |
def load_global_input_from_state(state_image):
|
|
@@ -276,8 +276,6 @@ def local_edit_interface(image_dict, prompt, request: gr.Request, progress=gr.Pr
|
|
| 276 |
IP_Dict[client_ip] = 0
|
| 277 |
IP_Dict[client_ip] += 1
|
| 278 |
|
| 279 |
-
# 获取IP属地信息
|
| 280 |
-
country_info = get_country_info_safe(client_ip)
|
| 281 |
|
| 282 |
if image_dict is None:
|
| 283 |
return None, "Please upload an image and draw the area to edit"
|
|
@@ -306,14 +304,14 @@ def local_edit_interface(image_dict, prompt, request: gr.Request, progress=gr.Pr
|
|
| 306 |
nsfw_result = nsfw_detector.predict_pil_label_only(base_image)
|
| 307 |
|
| 308 |
if nsfw_result.lower() == "nsfw":
|
| 309 |
-
print(f"🔍 NSFW检测结果: ❌❌❌ {nsfw_result} - IP: {client_ip}
|
| 310 |
# 检查NSFW频率限制
|
| 311 |
is_rate_limited, wait_time = check_nsfw_rate_limit(client_ip)
|
| 312 |
|
| 313 |
if is_rate_limited:
|
| 314 |
wait_minutes = int(wait_time / 60) + 1
|
| 315 |
window_info = get_current_window_info(client_ip)
|
| 316 |
-
print(f"⚠️ NSFW频率限制 - IP: {client_ip}
|
| 317 |
print(f" 时间窗口: {window_info['window_start']} - {window_info['window_end']}")
|
| 318 |
print(f" 当前计数: {window_info['current_count']}/{NSFW_LIMIT}, 需要等待 {wait_minutes} 分钟")
|
| 319 |
return None, f"❌ Please wait {wait_minutes} minutes before generating again"
|
|
@@ -321,15 +319,12 @@ def local_edit_interface(image_dict, prompt, request: gr.Request, progress=gr.Pr
|
|
| 321 |
record_nsfw_detection(client_ip)
|
| 322 |
window_info = get_current_window_info(client_ip)
|
| 323 |
else:
|
| 324 |
-
print(f"🔍 NSFW检测结果: ✅✅✅ {nsfw_result} - IP: {client_ip}
|
| 325 |
|
| 326 |
except Exception as e:
|
| 327 |
print(f"⚠️ NSFW检测失败: {e}")
|
| 328 |
|
| 329 |
# IP访问限制检查
|
| 330 |
-
if IP_Dict[client_ip]>10 and country_info.lower() in ["印度", "巴基斯坦"]:
|
| 331 |
-
print(f"❌ Content not allowed - IP: {client_ip}({country_info}), count: {IP_Dict[client_ip]}, prompt: {prompt.strip()}")
|
| 332 |
-
return None, "❌ Content not allowed. Please modify your prompt"
|
| 333 |
|
| 334 |
result_url = None
|
| 335 |
status_message = ""
|
|
@@ -340,17 +335,17 @@ def local_edit_interface(image_dict, prompt, request: gr.Request, progress=gr.Pr
|
|
| 340 |
progress(0.5, desc=message)
|
| 341 |
|
| 342 |
try:
|
| 343 |
-
print(f"✅ Local editing started - IP: {client_ip}
|
| 344 |
|
| 345 |
# 调用局部图像编辑处理函数
|
| 346 |
result_url, message = process_local_image_edit(base_image, layers, prompt.strip(), progress_callback)
|
| 347 |
|
| 348 |
if result_url:
|
| 349 |
-
print(f"✅ Local editing completed successfully - IP: {client_ip}
|
| 350 |
progress(1.0, desc="Processing completed")
|
| 351 |
return result_url, "✅ " + message
|
| 352 |
else:
|
| 353 |
-
print(f"❌ Local editing processing failed - IP: {client_ip}
|
| 354 |
return None, "❌ " + message
|
| 355 |
|
| 356 |
except Exception as e:
|
|
|
|
| 4 |
import shutil
|
| 5 |
import tempfile
|
| 6 |
import time
|
| 7 |
+
from util import process_image_edit, process_local_image_edit
|
| 8 |
from nfsw import NSFWDetector
|
| 9 |
|
| 10 |
# 配置参数
|
|
|
|
| 149 |
IP_Dict[client_ip] = 0
|
| 150 |
IP_Dict[client_ip] += 1
|
| 151 |
|
|
|
|
|
|
|
| 152 |
|
| 153 |
if input_image is None:
|
| 154 |
return None, "Please upload an image first"
|
|
|
|
| 168 |
nsfw_result = nsfw_detector.predict_pil_label_only(input_image)
|
| 169 |
|
| 170 |
if nsfw_result.lower() == "nsfw":
|
| 171 |
+
print(f"🔍 NSFW检测结果: ❌❌❌ {nsfw_result} - IP: {client_ip}")
|
| 172 |
# 检查NSFW频率限制
|
| 173 |
is_rate_limited, wait_time = check_nsfw_rate_limit(client_ip)
|
| 174 |
|
|
|
|
| 176 |
# 超过频率限制,显示等待提示并阻止继续
|
| 177 |
wait_minutes = int(wait_time / 60) + 1 # 向上取整到分钟
|
| 178 |
window_info = get_current_window_info(client_ip)
|
| 179 |
+
print(f"⚠️ NSFW频率限制 - IP: {client_ip}")
|
| 180 |
print(f" 时间窗口: {window_info['window_start']} - {window_info['window_end']}")
|
| 181 |
print(f" 当前计数: {window_info['current_count']}/{NSFW_LIMIT}, 需要等待 {wait_minutes} 分钟")
|
| 182 |
return None, f"❌ Please wait {wait_minutes} minutes before generating again"
|
|
|
|
| 185 |
record_nsfw_detection(client_ip)
|
| 186 |
window_info = get_current_window_info(client_ip)
|
| 187 |
else:
|
| 188 |
+
print(f"🔍 NSFW检测结果: ✅✅✅ {nsfw_result} - IP: {client_ip}")
|
| 189 |
|
| 190 |
except Exception as e:
|
| 191 |
print(f"⚠️ NSFW检测失败: {e}")
|
| 192 |
# 检测失败时允许继续处理
|
| 193 |
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
result_url = None
|
| 196 |
status_message = ""
|
|
|
|
| 202 |
|
| 203 |
try:
|
| 204 |
# 打印成功访问的信息
|
| 205 |
+
print(f"✅ Processing started - IP: {client_ip}, count: {IP_Dict[client_ip]}, prompt: {prompt.strip()}", flush=True)
|
| 206 |
|
| 207 |
# Call image editing processing function
|
| 208 |
result_url, message = process_image_edit(input_image, prompt.strip(), progress_callback)
|
| 209 |
|
| 210 |
if result_url:
|
| 211 |
+
print(f"✅ Processing completed successfully - IP: {client_ip}, result_url: {result_url}", flush=True)
|
| 212 |
progress(1.0, desc="Processing completed")
|
| 213 |
return result_url, "✅ " + message
|
| 214 |
else:
|
| 215 |
+
print(f"❌ Processing failed - IP: {client_ip}, error: {message}", flush=True)
|
| 216 |
return None, "❌ " + message
|
| 217 |
|
| 218 |
except Exception as e:
|
|
|
|
| 247 |
if output_image is not None:
|
| 248 |
# 对于局部编辑,我们需要创建一个新的ImageEditor格式
|
| 249 |
# 将输出图片作为背景,不包含任何编辑层
|
| 250 |
+
editor_data = {
|
| 251 |
+
"background": output_image,
|
| 252 |
+
"layers": [],
|
| 253 |
+
"composite": output_image # 添加composite键以避免KeyError
|
| 254 |
+
}
|
| 255 |
+
return editor_data, output_image
|
| 256 |
return None, None
|
| 257 |
|
| 258 |
def load_global_input_from_state(state_image):
|
|
|
|
| 276 |
IP_Dict[client_ip] = 0
|
| 277 |
IP_Dict[client_ip] += 1
|
| 278 |
|
|
|
|
|
|
|
| 279 |
|
| 280 |
if image_dict is None:
|
| 281 |
return None, "Please upload an image and draw the area to edit"
|
|
|
|
| 304 |
nsfw_result = nsfw_detector.predict_pil_label_only(base_image)
|
| 305 |
|
| 306 |
if nsfw_result.lower() == "nsfw":
|
| 307 |
+
print(f"🔍 NSFW检测结果: ❌❌❌ {nsfw_result} - IP: {client_ip}")
|
| 308 |
# 检查NSFW频率限制
|
| 309 |
is_rate_limited, wait_time = check_nsfw_rate_limit(client_ip)
|
| 310 |
|
| 311 |
if is_rate_limited:
|
| 312 |
wait_minutes = int(wait_time / 60) + 1
|
| 313 |
window_info = get_current_window_info(client_ip)
|
| 314 |
+
print(f"⚠️ NSFW频率限制 - IP: {client_ip}")
|
| 315 |
print(f" 时间窗口: {window_info['window_start']} - {window_info['window_end']}")
|
| 316 |
print(f" 当前计数: {window_info['current_count']}/{NSFW_LIMIT}, 需要等待 {wait_minutes} 分钟")
|
| 317 |
return None, f"❌ Please wait {wait_minutes} minutes before generating again"
|
|
|
|
| 319 |
record_nsfw_detection(client_ip)
|
| 320 |
window_info = get_current_window_info(client_ip)
|
| 321 |
else:
|
| 322 |
+
print(f"🔍 NSFW检测结果: ✅✅✅ {nsfw_result} - IP: {client_ip}")
|
| 323 |
|
| 324 |
except Exception as e:
|
| 325 |
print(f"⚠️ NSFW检测失败: {e}")
|
| 326 |
|
| 327 |
# IP访问限制检查
|
|
|
|
|
|
|
|
|
|
| 328 |
|
| 329 |
result_url = None
|
| 330 |
status_message = ""
|
|
|
|
| 335 |
progress(0.5, desc=message)
|
| 336 |
|
| 337 |
try:
|
| 338 |
+
print(f"✅ Local editing started - IP: {client_ip}, count: {IP_Dict[client_ip]}, prompt: {prompt.strip()}", flush=True)
|
| 339 |
|
| 340 |
# 调用局部图像编辑处理函数
|
| 341 |
result_url, message = process_local_image_edit(base_image, layers, prompt.strip(), progress_callback)
|
| 342 |
|
| 343 |
if result_url:
|
| 344 |
+
print(f"✅ Local editing completed successfully - IP: {client_ip}, result_url: {result_url}", flush=True)
|
| 345 |
progress(1.0, desc="Processing completed")
|
| 346 |
return result_url, "✅ " + message
|
| 347 |
else:
|
| 348 |
+
print(f"❌ Local editing processing failed - IP: {client_ip}, error: {message}", flush=True)
|
| 349 |
return None, "❌ " + message
|
| 350 |
|
| 351 |
except Exception as e:
|
util.py
CHANGED
|
@@ -140,43 +140,6 @@ def upload_user_img_r2(clientIp, timeId, img):
|
|
| 140 |
return res
|
| 141 |
|
| 142 |
|
| 143 |
-
@func_timeout.func_set_timeout(10)
|
| 144 |
-
def get_country_info(ip):
|
| 145 |
-
"""Get country information for IP address"""
|
| 146 |
-
try:
|
| 147 |
-
# Use the new API URL
|
| 148 |
-
url = f"https://qifu-api.baidubce.com/ip/geo/v1/district?ip={ip}"
|
| 149 |
-
ret = requests.get(url)
|
| 150 |
-
ret.raise_for_status() # Raises exception if request fails (e.g. 404, 500)
|
| 151 |
-
|
| 152 |
-
json_data = ret.json()
|
| 153 |
-
|
| 154 |
-
# Based on new JSON structure, country info is under 'data' -> 'country' path
|
| 155 |
-
if json_data.get("code") == "Success":
|
| 156 |
-
country = json_data.get("data", {}).get("country")
|
| 157 |
-
return country if country else "Unknown"
|
| 158 |
-
else:
|
| 159 |
-
# Handle API error codes
|
| 160 |
-
print(f"API request failed: {json_data.get('msg', 'Unknown error')}")
|
| 161 |
-
return "Unknown"
|
| 162 |
-
|
| 163 |
-
except requests.exceptions.RequestException as e:
|
| 164 |
-
print(f"Network request failed: {e}")
|
| 165 |
-
return "Unknown"
|
| 166 |
-
except Exception as e:
|
| 167 |
-
print(f"Failed to get IP location: {e}")
|
| 168 |
-
return "Unknown"
|
| 169 |
-
|
| 170 |
-
def get_country_info_safe(ip):
|
| 171 |
-
"""Safely get IP location info, returns Unknown on error"""
|
| 172 |
-
try:
|
| 173 |
-
return get_country_info(ip)
|
| 174 |
-
except func_timeout.FunctionTimedOut:
|
| 175 |
-
print(f"IP location request timeout: {ip}")
|
| 176 |
-
return "Unknown"
|
| 177 |
-
except Exception as e:
|
| 178 |
-
print(f"Failed to get IP location: {e}")
|
| 179 |
-
return "Unknown"
|
| 180 |
|
| 181 |
def create_mask_from_layers(base_image, layers):
|
| 182 |
"""
|
|
|
|
| 140 |
return res
|
| 141 |
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
def create_mask_from_layers(base_image, layers):
|
| 145 |
"""
|