selfit-camera commited on
Commit
8522cfd
·
1 Parent(s): 5922f35
Files changed (2) hide show
  1. app.py +94 -5
  2. util.py +1 -1
app.py CHANGED
@@ -12,8 +12,8 @@ from nfsw import NSFWDetector
12
  TIP_TRY_N = 4 # Show like button tip after 12 tries
13
  FREE_TRY_N = 10 # Free phase: first 15 tries without restrictions
14
  SLOW_TRY_N = 15 # Slow phase start: 25 tries
15
- SLOW2_TRY_N = 25 # Slow phase start: 32 tries
16
- RATE_LIMIT_60 = 30 # Full restriction: blocked after 40 tries
17
 
18
  # Time window configuration (minutes)
19
  PHASE_1_WINDOW = 2 # 15-25 tries: 5 minutes
@@ -25,6 +25,72 @@ IP_Dict = {}
25
  # IP generation statistics and time window tracking
26
  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
 
29
  def get_ip_generation_count(client_ip):
30
  """
@@ -51,14 +117,29 @@ def get_ip_phase(client_ip):
51
  str: 'free', 'rate_limit_1', 'rate_limit_2', 'rate_limit_3', 'blocked'
52
  """
53
  count = get_ip_generation_count(client_ip)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
 
55
  if count < FREE_TRY_N:
56
  return 'free'
57
  elif count < SLOW_TRY_N:
58
  return 'rate_limit_1' # NSFW blur + 5 minutes 2 images
59
  elif count < SLOW2_TRY_N:
60
  return 'rate_limit_2' # NSFW blur + 10 minutes 2 images
61
- elif count < RATE_LIMIT_60:
62
  return 'rate_limit_3' # NSFW blur + 20 minutes 2 images
63
  else:
64
  return 'blocked' # Generation blocked
@@ -239,7 +320,11 @@ def edit_image_interface(input_image, prompt, request: gr.Request, progress=gr.P
239
  '>&#128640; Unlimited Generation</a>
240
  </div>
241
  """
242
- return None, f"❌ You have reached Hugging Face's free generation limit. Please visit https://omnicreator.net/#generator for unlimited generation", gr.update(value=blocked_button_html, visible=True)
 
 
 
 
243
 
244
  # Check rate limit (applies to rate_limit phases)
245
  if current_phase in ['rate_limit_1', 'rate_limit_2', 'rate_limit_3']:
@@ -584,7 +669,11 @@ def local_edit_interface(image_dict, prompt, reference_image, request: gr.Reques
584
  '>&#128640; Unlimited Generation</a>
585
  </div>
586
  """
587
- return None, f"❌ You have reached Hugging Face's free generation limit. Please visit https://omnicreator.net/#generator for unlimited generation", gr.update(value=blocked_button_html, visible=True)
 
 
 
 
588
 
589
  # Check rate limit (applies to rate_limit phases)
590
  if current_phase in ['rate_limit_1', 'rate_limit_2', 'rate_limit_3']:
 
12
  TIP_TRY_N = 4 # Show like button tip after 12 tries
13
  FREE_TRY_N = 10 # Free phase: first 15 tries without restrictions
14
  SLOW_TRY_N = 15 # Slow phase start: 25 tries
15
+ 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 = 2 # 15-25 tries: 5 minutes
 
25
  # IP generation statistics and time window tracking
26
  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 country information (only query once per IP)
37
+
38
+ Returns:
39
+ str: Country name, or None if query fails
40
+ """
41
+ # Check cache first - no API call for subsequent visits
42
+ if client_ip in IP_Country_Cache:
43
+ cached_country = IP_Country_Cache[client_ip]
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}"
52
+ response = requests.get(api_url, timeout=5)
53
+
54
+ if response.status_code == 200:
55
+ data = response.json()
56
+ if data.get("code") == 200 and "ipdata" in data:
57
+ country = data["ipdata"].get("info1", "Unknown")
58
+ IP_Country_Cache[client_ip] = country
59
+ print(f"🌍 API查询成功 - IP: {client_ip}, 国家: {country}")
60
+ return country
61
+
62
+ # Query failed, cache as "Unknown" to avoid repeated queries
63
+ IP_Country_Cache[client_ip] = "Unknown"
64
+ print(f"⚠️ API查询失败 - IP: {client_ip}, 状态码: {response.status_code}")
65
+ return "Unknown"
66
+
67
+ except Exception as e:
68
+ # Exception occurred, cache as "Unknown"
69
+ IP_Country_Cache[client_ip] = "Unknown"
70
+ print(f"⚠️ API查询异常 - IP: {client_ip}, 错误: {str(e)}")
71
+ return "Unknown"
72
+
73
+ def is_restricted_country_ip(client_ip):
74
+ """
75
+ Check if IP is from a restricted country
76
+
77
+ Returns:
78
+ bool: True if from restricted country
79
+ """
80
+ country = query_ip_country(client_ip)
81
+ return country in RESTRICTED_COUNTRIES
82
+
83
+ def get_ip_max_limit(client_ip):
84
+ """
85
+ Get max usage limit for IP based on country
86
+
87
+ Returns:
88
+ int: Max usage limit
89
+ """
90
+ if is_restricted_country_ip(client_ip):
91
+ return RESTRICTED_COUNTRY_LIMIT
92
+ else:
93
+ return RATE_LIMIT_60
94
 
95
  def get_ip_generation_count(client_ip):
96
  """
 
117
  str: 'free', 'rate_limit_1', 'rate_limit_2', 'rate_limit_3', 'blocked'
118
  """
119
  count = get_ip_generation_count(client_ip)
120
+ max_limit = get_ip_max_limit(client_ip)
121
+
122
+ # For restricted countries, check if they've reached their limit
123
+ if is_restricted_country_ip(client_ip):
124
+ if count >= max_limit:
125
+ return 'blocked'
126
+ elif count >= max_limit - 2: # Last 2 attempts
127
+ return 'rate_limit_3'
128
+ elif count >= max_limit - 3: # 3rd attempt from end
129
+ return 'rate_limit_2'
130
+ elif count >= max_limit - 4: # 4th attempt from end
131
+ return 'rate_limit_1'
132
+ else:
133
+ return 'free'
134
 
135
+ # For normal countries, use standard limits
136
  if count < FREE_TRY_N:
137
  return 'free'
138
  elif count < SLOW_TRY_N:
139
  return 'rate_limit_1' # NSFW blur + 5 minutes 2 images
140
  elif count < SLOW2_TRY_N:
141
  return 'rate_limit_2' # NSFW blur + 10 minutes 2 images
142
+ elif count < max_limit:
143
  return 'rate_limit_3' # NSFW blur + 20 minutes 2 images
144
  else:
145
  return 'blocked' # Generation blocked
 
320
  '>&#128640; Unlimited Generation</a>
321
  </div>
322
  """
323
+
324
+ # Use same message for all users to avoid discrimination perception
325
+ blocked_message = f"❌ You have reached Hugging Face's free generation limit. Please visit https://omnicreator.net/#generator for unlimited generation"
326
+
327
+ return None, blocked_message, gr.update(value=blocked_button_html, visible=True)
328
 
329
  # Check rate limit (applies to rate_limit phases)
330
  if current_phase in ['rate_limit_1', 'rate_limit_2', 'rate_limit_3']:
 
669
  '>&#128640; Unlimited Generation</a>
670
  </div>
671
  """
672
+
673
+ # Use same message for all users to avoid discrimination perception
674
+ blocked_message = f"❌ You have reached Hugging Face's free generation limit. Please visit https://omnicreator.net/#generator for unlimited generation"
675
+
676
+ return None, blocked_message, gr.update(value=blocked_button_html, visible=True)
677
 
678
  # Check rate limit (applies to rate_limit phases)
679
  if current_phase in ['rate_limit_1', 'rate_limit_2', 'rate_limit_3']:
util.py CHANGED
@@ -359,7 +359,7 @@ def check_task_status(task_id):
359
  tasks_ahead = queue_info.get('tasks_ahead', 0)
360
  current_priority = queue_info.get('current_priority', 0)
361
  description = queue_info.get('description', '')
362
- print(f"📊 Queue Status - Tasks ahead: {tasks_ahead}, Priority: {current_priority}, Status: {status}")
363
 
364
  return status, image_url, task_data
365
  else:
 
359
  tasks_ahead = queue_info.get('tasks_ahead', 0)
360
  current_priority = queue_info.get('current_priority', 0)
361
  description = queue_info.get('description', '')
362
+ # print(f"📊 Queue Status - Tasks ahead: {tasks_ahead}, Priority: {current_priority}, Status: {status}")
363
 
364
  return status, image_url, task_data
365
  else: