Spaces:
Running
Running
Commit
·
46c4c23
1
Parent(s):
8522cfd
init
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ SLOW2_TRY_N = 20 # Slow phase start: 32 tries
|
|
| 16 |
RATE_LIMIT_60 = 25 # Full restriction: blocked after 40 tries
|
| 17 |
|
| 18 |
# Time window configuration (minutes)
|
| 19 |
-
PHASE_1_WINDOW =
|
| 20 |
PHASE_2_WINDOW = 10 # 25-32 tries: 10 minutes
|
| 21 |
PHASE_3_WINDOW = 20 # 32-40 tries: 20 minutes
|
| 22 |
MAX_IMAGES_PER_WINDOW = 2 # Max images per time window
|
|
@@ -27,25 +27,27 @@ IP_Generation_Count = {} # Record total generation count for each IP
|
|
| 27 |
IP_Rate_Limit_Track = {} # Record generation count and timestamp in current time window for each IP
|
| 28 |
IP_Country_Cache = {} # Cache IP country information to avoid repeated queries
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Restricted countries list (these countries have lower usage limits)
|
| 31 |
RESTRICTED_COUNTRIES = ["印度", "巴基斯坦"]
|
| 32 |
RESTRICTED_COUNTRY_LIMIT = 5 # Max usage for restricted countries
|
| 33 |
|
| 34 |
def query_ip_country(client_ip):
|
| 35 |
"""
|
| 36 |
-
Query IP address
|
| 37 |
|
| 38 |
Returns:
|
| 39 |
-
|
| 40 |
"""
|
| 41 |
# Check cache first - no API call for subsequent visits
|
| 42 |
if client_ip in IP_Country_Cache:
|
| 43 |
-
|
| 44 |
-
print(f"💾 IP缓存命中 - IP: {client_ip}, 国家: {cached_country} (无需重复查询)")
|
| 45 |
-
return cached_country
|
| 46 |
|
| 47 |
-
# First time visit - query API
|
| 48 |
-
print(f"🔍 首次IP访问,开始查询 - IP: {client_ip}")
|
| 49 |
try:
|
| 50 |
import requests
|
| 51 |
api_url = f"https://api.vore.top/api/IPdata?ip={client_ip}"
|
|
@@ -54,21 +56,25 @@ def query_ip_country(client_ip):
|
|
| 54 |
if response.status_code == 200:
|
| 55 |
data = response.json()
|
| 56 |
if data.get("code") == 200 and "ipdata" in data:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
# Query failed, cache as
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
return
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
-
# Exception occurred, cache as
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
return
|
| 72 |
|
| 73 |
def is_restricted_country_ip(client_ip):
|
| 74 |
"""
|
|
@@ -77,7 +83,8 @@ def is_restricted_country_ip(client_ip):
|
|
| 77 |
Returns:
|
| 78 |
bool: True if from restricted country
|
| 79 |
"""
|
| 80 |
-
|
|
|
|
| 81 |
return country in RESTRICTED_COUNTRIES
|
| 82 |
|
| 83 |
def get_ip_max_limit(client_ip):
|
|
@@ -196,6 +203,39 @@ def check_rate_limit_for_phase(client_ip, phase):
|
|
| 196 |
|
| 197 |
return False, 0, track_data['count']
|
| 198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
def record_generation_attempt(client_ip, phase):
|
| 200 |
"""
|
| 201 |
Record generation attempt
|
|
@@ -203,6 +243,9 @@ def record_generation_attempt(client_ip, phase):
|
|
| 203 |
# Increment total count
|
| 204 |
increment_ip_generation_count(client_ip)
|
| 205 |
|
|
|
|
|
|
|
|
|
|
| 206 |
# Record time window count
|
| 207 |
if phase in ['rate_limit_1', 'rate_limit_2', 'rate_limit_3']:
|
| 208 |
window_key = f"{client_ip}_{phase}"
|
|
@@ -290,8 +333,9 @@ def edit_image_interface(input_image, prompt, request: gr.Request, progress=gr.P
|
|
| 290 |
# Get user current phase
|
| 291 |
current_phase = get_ip_phase(client_ip)
|
| 292 |
current_count = get_ip_generation_count(client_ip)
|
|
|
|
| 293 |
|
| 294 |
-
print(f"📊 User phase info - IP: {client_ip},
|
| 295 |
|
| 296 |
# Check if user reached the like button tip threshold
|
| 297 |
show_like_tip = (current_count >= TIP_TRY_N)
|
|
@@ -639,8 +683,9 @@ def local_edit_interface(image_dict, prompt, reference_image, request: gr.Reques
|
|
| 639 |
# Get user current phase
|
| 640 |
current_phase = get_ip_phase(client_ip)
|
| 641 |
current_count = get_ip_generation_count(client_ip)
|
|
|
|
| 642 |
|
| 643 |
-
print(f"📊 Local edit user phase info - IP: {client_ip},
|
| 644 |
|
| 645 |
# Check if user reached the like button tip threshold
|
| 646 |
show_like_tip = (current_count >= TIP_TRY_N)
|
|
|
|
| 16 |
RATE_LIMIT_60 = 25 # Full restriction: blocked after 40 tries
|
| 17 |
|
| 18 |
# Time window configuration (minutes)
|
| 19 |
+
PHASE_1_WINDOW = 3 # 15-25 tries: 5 minutes
|
| 20 |
PHASE_2_WINDOW = 10 # 25-32 tries: 10 minutes
|
| 21 |
PHASE_3_WINDOW = 20 # 32-40 tries: 20 minutes
|
| 22 |
MAX_IMAGES_PER_WINDOW = 2 # Max images per time window
|
|
|
|
| 27 |
IP_Rate_Limit_Track = {} # Record generation count and timestamp in current time window for each IP
|
| 28 |
IP_Country_Cache = {} # Cache IP country information to avoid repeated queries
|
| 29 |
|
| 30 |
+
# Country usage statistics
|
| 31 |
+
Country_Usage_Stats = {} # Track usage count by country
|
| 32 |
+
Total_Request_Count = 0 # Total request counter for periodic printing
|
| 33 |
+
PRINT_STATS_INTERVAL = 10 # Print stats every N requests
|
| 34 |
+
|
| 35 |
# Restricted countries list (these countries have lower usage limits)
|
| 36 |
RESTRICTED_COUNTRIES = ["印度", "巴基斯坦"]
|
| 37 |
RESTRICTED_COUNTRY_LIMIT = 5 # Max usage for restricted countries
|
| 38 |
|
| 39 |
def query_ip_country(client_ip):
|
| 40 |
"""
|
| 41 |
+
Query IP address geo information (only query once per IP)
|
| 42 |
|
| 43 |
Returns:
|
| 44 |
+
dict: {"country": str, "region": str, "city": str}
|
| 45 |
"""
|
| 46 |
# Check cache first - no API call for subsequent visits
|
| 47 |
if client_ip in IP_Country_Cache:
|
| 48 |
+
return IP_Country_Cache[client_ip]
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
# First time visit - query API (silent query)
|
|
|
|
| 51 |
try:
|
| 52 |
import requests
|
| 53 |
api_url = f"https://api.vore.top/api/IPdata?ip={client_ip}"
|
|
|
|
| 56 |
if response.status_code == 200:
|
| 57 |
data = response.json()
|
| 58 |
if data.get("code") == 200 and "ipdata" in data:
|
| 59 |
+
ipdata = data["ipdata"]
|
| 60 |
+
geo_info = {
|
| 61 |
+
"country": ipdata.get("info1", "Unknown"),
|
| 62 |
+
"region": ipdata.get("info2", "Unknown"),
|
| 63 |
+
"city": ipdata.get("info3", "Unknown")
|
| 64 |
+
}
|
| 65 |
+
IP_Country_Cache[client_ip] = geo_info
|
| 66 |
+
return geo_info
|
| 67 |
|
| 68 |
+
# Query failed, cache as default
|
| 69 |
+
default_geo = {"country": "Unknown", "region": "Unknown", "city": "Unknown"}
|
| 70 |
+
IP_Country_Cache[client_ip] = default_geo
|
| 71 |
+
return default_geo
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
+
# Exception occurred, cache as default
|
| 75 |
+
default_geo = {"country": "Unknown", "region": "Unknown", "city": "Unknown"}
|
| 76 |
+
IP_Country_Cache[client_ip] = default_geo
|
| 77 |
+
return default_geo
|
| 78 |
|
| 79 |
def is_restricted_country_ip(client_ip):
|
| 80 |
"""
|
|
|
|
| 83 |
Returns:
|
| 84 |
bool: True if from restricted country
|
| 85 |
"""
|
| 86 |
+
geo_info = query_ip_country(client_ip)
|
| 87 |
+
country = geo_info["country"]
|
| 88 |
return country in RESTRICTED_COUNTRIES
|
| 89 |
|
| 90 |
def get_ip_max_limit(client_ip):
|
|
|
|
| 203 |
|
| 204 |
return False, 0, track_data['count']
|
| 205 |
|
| 206 |
+
def update_country_stats(client_ip):
|
| 207 |
+
"""
|
| 208 |
+
Update country usage statistics and print periodically
|
| 209 |
+
"""
|
| 210 |
+
global Total_Request_Count, Country_Usage_Stats
|
| 211 |
+
|
| 212 |
+
# Get country info
|
| 213 |
+
geo_info = IP_Country_Cache.get(client_ip, {"country": "Unknown", "region": "Unknown", "city": "Unknown"})
|
| 214 |
+
country = geo_info["country"]
|
| 215 |
+
|
| 216 |
+
# Update country stats
|
| 217 |
+
if country not in Country_Usage_Stats:
|
| 218 |
+
Country_Usage_Stats[country] = 0
|
| 219 |
+
Country_Usage_Stats[country] += 1
|
| 220 |
+
|
| 221 |
+
# Increment total request counter
|
| 222 |
+
Total_Request_Count += 1
|
| 223 |
+
|
| 224 |
+
# Print stats every N requests
|
| 225 |
+
if Total_Request_Count % PRINT_STATS_INTERVAL == 0:
|
| 226 |
+
print("\n" + "="*60)
|
| 227 |
+
print(f"📊 国家使用统计 (总请求数: {Total_Request_Count})")
|
| 228 |
+
print("="*60)
|
| 229 |
+
|
| 230 |
+
# Sort by usage count (descending)
|
| 231 |
+
sorted_stats = sorted(Country_Usage_Stats.items(), key=lambda x: x[1], reverse=True)
|
| 232 |
+
|
| 233 |
+
for country_name, count in sorted_stats:
|
| 234 |
+
percentage = (count / Total_Request_Count) * 100
|
| 235 |
+
print(f" {country_name}: {count} 次 ({percentage:.1f}%)")
|
| 236 |
+
|
| 237 |
+
print("="*60 + "\n")
|
| 238 |
+
|
| 239 |
def record_generation_attempt(client_ip, phase):
|
| 240 |
"""
|
| 241 |
Record generation attempt
|
|
|
|
| 243 |
# Increment total count
|
| 244 |
increment_ip_generation_count(client_ip)
|
| 245 |
|
| 246 |
+
# Update country statistics
|
| 247 |
+
update_country_stats(client_ip)
|
| 248 |
+
|
| 249 |
# Record time window count
|
| 250 |
if phase in ['rate_limit_1', 'rate_limit_2', 'rate_limit_3']:
|
| 251 |
window_key = f"{client_ip}_{phase}"
|
|
|
|
| 333 |
# Get user current phase
|
| 334 |
current_phase = get_ip_phase(client_ip)
|
| 335 |
current_count = get_ip_generation_count(client_ip)
|
| 336 |
+
geo_info = IP_Country_Cache.get(client_ip, {"country": "Unknown", "region": "Unknown", "city": "Unknown"})
|
| 337 |
|
| 338 |
+
print(f"📊 User phase info - IP: {client_ip}, Location: {geo_info['country']}/{geo_info['region']}/{geo_info['city']}, Phase: {current_phase}, Count: {current_count}")
|
| 339 |
|
| 340 |
# Check if user reached the like button tip threshold
|
| 341 |
show_like_tip = (current_count >= TIP_TRY_N)
|
|
|
|
| 683 |
# Get user current phase
|
| 684 |
current_phase = get_ip_phase(client_ip)
|
| 685 |
current_count = get_ip_generation_count(client_ip)
|
| 686 |
+
geo_info = IP_Country_Cache.get(client_ip, {"country": "Unknown", "region": "Unknown", "city": "Unknown"})
|
| 687 |
|
| 688 |
+
print(f"📊 Local edit user phase info - IP: {client_ip}, Location: {geo_info['country']}/{geo_info['region']}/{geo_info['city']}, Phase: {current_phase}, Count: {current_count}")
|
| 689 |
|
| 690 |
# Check if user reached the like button tip threshold
|
| 691 |
show_like_tip = (current_count >= TIP_TRY_N)
|