Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -78,6 +78,7 @@ from diffusers import FluxPipeline
|
|
| 78 |
# import urllib.parse
|
| 79 |
import subprocess
|
| 80 |
|
|
|
|
| 81 |
IS_ZERO_GPU = bool(os.getenv("SPACES_ZERO_GPU"))
|
| 82 |
HIDE_API = bool(os.getenv("HIDE_API"))
|
| 83 |
if IS_ZERO_GPU:
|
|
@@ -180,6 +181,10 @@ class GuiSD:
|
|
| 180 |
self.active_downloads = set()
|
| 181 |
self.download_lock = threading.Lock()
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
def update_storage_models(self, storage_floor_gb=30, required_inventory_for_purge=3):
|
| 184 |
while get_used_storage_gb() > storage_floor_gb:
|
| 185 |
if len(self.inventory) < required_inventory_for_purge:
|
|
@@ -205,6 +210,33 @@ class GuiSD:
|
|
| 205 |
|
| 206 |
def load_new_model(self, model_name, vae_model, task, controlnet_model, progress=gr.Progress(track_tqdm=True)):
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
lock_key = model_name
|
| 209 |
|
| 210 |
while True:
|
|
|
|
| 78 |
# import urllib.parse
|
| 79 |
import subprocess
|
| 80 |
|
| 81 |
+
os.system("pip list")
|
| 82 |
IS_ZERO_GPU = bool(os.getenv("SPACES_ZERO_GPU"))
|
| 83 |
HIDE_API = bool(os.getenv("HIDE_API"))
|
| 84 |
if IS_ZERO_GPU:
|
|
|
|
| 181 |
self.active_downloads = set()
|
| 182 |
self.download_lock = threading.Lock()
|
| 183 |
|
| 184 |
+
# Anti-abuse: Track new model requests
|
| 185 |
+
self.used_models = []
|
| 186 |
+
self.new_model_history = []
|
| 187 |
+
|
| 188 |
def update_storage_models(self, storage_floor_gb=30, required_inventory_for_purge=3):
|
| 189 |
while get_used_storage_gb() > storage_floor_gb:
|
| 190 |
if len(self.inventory) < required_inventory_for_purge:
|
|
|
|
| 210 |
|
| 211 |
def load_new_model(self, model_name, vae_model, task, controlnet_model, progress=gr.Progress(track_tqdm=True)):
|
| 212 |
|
| 213 |
+
# --- Anti-Abuse Check Start ---
|
| 214 |
+
if model_name in self.used_models:
|
| 215 |
+
# Move to the end to mark as the most recently used (prevents it from being forgotten)
|
| 216 |
+
self.used_models.remove(model_name)
|
| 217 |
+
self.used_models.append(model_name)
|
| 218 |
+
else:
|
| 219 |
+
current_time = datetime.now()
|
| 220 |
+
# Retain history of new model requests from the last 10 minutes (600 seconds)
|
| 221 |
+
self.new_model_history = [
|
| 222 |
+
t for t in self.new_model_history
|
| 223 |
+
if (current_time - t).total_seconds() < 600
|
| 224 |
+
]
|
| 225 |
+
|
| 226 |
+
# Allow a maximum of 5 NEW model requests per 10 minutes
|
| 227 |
+
if len(self.new_model_history) >= 5:
|
| 228 |
+
yield "Rate limit exceeded: Too many new models requested."
|
| 229 |
+
raise gr.Error("Too many new models requested. Please reuse your previously loaded models or wait a few minutes before trying new ones.")
|
| 230 |
+
|
| 231 |
+
# Log the new model attempt
|
| 232 |
+
self.new_model_history.append(current_time)
|
| 233 |
+
self.used_models.append(model_name)
|
| 234 |
+
|
| 235 |
+
# Prevent the bypass list from growing infinitely (cap at 5 most recent models)
|
| 236 |
+
if len(self.used_models) > 5:
|
| 237 |
+
self.used_models.pop(0)
|
| 238 |
+
# --- Anti-Abuse Check End ---
|
| 239 |
+
|
| 240 |
lock_key = model_name
|
| 241 |
|
| 242 |
while True:
|