Spaces:
Sleeping
Sleeping
Factor Studios
commited on
Update http_storage.py
Browse files- http_storage.py +62 -33
http_storage.py
CHANGED
|
@@ -118,6 +118,7 @@ class HTTPGPUStorage:
|
|
| 118 |
return {"status": "error", "message": "HTTP client is closing"}
|
| 119 |
|
| 120 |
url = f"{self.api_base}{endpoint}"
|
|
|
|
| 121 |
|
| 122 |
try:
|
| 123 |
# Ensure we have a valid session
|
|
@@ -125,28 +126,46 @@ class HTTPGPUStorage:
|
|
| 125 |
if not self._create_session():
|
| 126 |
return {"status": "error", "message": "Failed to create session"}
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
except Exception as e:
|
| 151 |
self.error_count += 1
|
| 152 |
self.last_error_time = time.time()
|
|
@@ -318,7 +337,13 @@ class HTTPGPUStorage:
|
|
| 318 |
def is_model_loaded(self, model_name: str) -> bool:
|
| 319 |
"""Check if a model is loaded via HTTP API"""
|
| 320 |
try:
|
| 321 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
|
| 323 |
if response and response.get('status') == 'loaded':
|
| 324 |
return True
|
|
@@ -334,18 +359,22 @@ class HTTPGPUStorage:
|
|
| 334 |
retry_delay = 2
|
| 335 |
last_error = None
|
| 336 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
for attempt in range(max_retries):
|
| 338 |
try:
|
| 339 |
-
# Ensure connection is active
|
| 340 |
-
if self._closing:
|
| 341 |
self._closing = False
|
| 342 |
if not self._create_session():
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
|
| 350 |
# Calculate model hash if path provided
|
| 351 |
model_hash = None
|
|
@@ -360,9 +389,9 @@ class HTTPGPUStorage:
|
|
| 360 |
|
| 361 |
response = self._make_request(
|
| 362 |
'POST',
|
| 363 |
-
f'/models/{
|
| 364 |
json=request_data,
|
| 365 |
-
timeout=
|
| 366 |
)
|
| 367 |
|
| 368 |
if response and response.get('status') == 'success':
|
|
|
|
| 118 |
return {"status": "error", "message": "HTTP client is closing"}
|
| 119 |
|
| 120 |
url = f"{self.api_base}{endpoint}"
|
| 121 |
+
timeout = kwargs.pop('timeout', 30) # Allow timeout override, default 30s
|
| 122 |
|
| 123 |
try:
|
| 124 |
# Ensure we have a valid session
|
|
|
|
| 126 |
if not self._create_session():
|
| 127 |
return {"status": "error", "message": "Failed to create session"}
|
| 128 |
|
| 129 |
+
# Add timeout to kwargs if not present
|
| 130 |
+
if 'timeout' not in kwargs:
|
| 131 |
+
kwargs['timeout'] = timeout
|
| 132 |
+
|
| 133 |
+
try:
|
| 134 |
+
response = self.http_session.request(method, url, **kwargs)
|
| 135 |
+
|
| 136 |
+
# Handle authentication errors by recreating session
|
| 137 |
+
if response.status_code == 401:
|
| 138 |
+
logging.warning("Session expired, recreating...")
|
| 139 |
+
if self._create_session():
|
| 140 |
+
response = self.http_session.request(method, url, **kwargs)
|
| 141 |
+
else:
|
| 142 |
+
return {"status": "error", "message": "Failed to recreate session"}
|
| 143 |
+
|
| 144 |
+
response.raise_for_status()
|
| 145 |
+
|
| 146 |
+
# Reset error count on successful request
|
| 147 |
+
self.error_count = 0
|
| 148 |
+
|
| 149 |
+
return response.json()
|
| 150 |
+
|
| 151 |
+
except requests.exceptions.Timeout:
|
| 152 |
+
self.error_count += 1
|
| 153 |
+
self.last_error_time = time.time()
|
| 154 |
+
logging.error(f"Request timed out after {timeout}s: {url}")
|
| 155 |
+
return {"status": "error", "message": "Request timed out"}
|
| 156 |
+
|
| 157 |
+
except requests.exceptions.ConnectionError:
|
| 158 |
+
self.error_count += 1
|
| 159 |
+
self.last_error_time = time.time()
|
| 160 |
+
logging.error(f"Connection failed: {url}")
|
| 161 |
+
return {"status": "error", "message": "HTTP connection unresponsive"}
|
| 162 |
+
|
| 163 |
+
except requests.exceptions.RequestException as e:
|
| 164 |
+
self.error_count += 1
|
| 165 |
+
self.last_error_time = time.time()
|
| 166 |
+
logging.error(f"HTTP request failed: {e}")
|
| 167 |
+
return {"status": "error", "message": f"HTTP request failed: {str(e)}"}
|
| 168 |
+
|
| 169 |
except Exception as e:
|
| 170 |
self.error_count += 1
|
| 171 |
self.last_error_time = time.time()
|
|
|
|
| 337 |
def is_model_loaded(self, model_name: str) -> bool:
|
| 338 |
"""Check if a model is loaded via HTTP API"""
|
| 339 |
try:
|
| 340 |
+
import urllib.parse
|
| 341 |
+
encoded_name = urllib.parse.quote(model_name, safe='')
|
| 342 |
+
response = self._make_request(
|
| 343 |
+
"GET",
|
| 344 |
+
f"/models/{encoded_name}/status",
|
| 345 |
+
timeout=60
|
| 346 |
+
)
|
| 347 |
|
| 348 |
if response and response.get('status') == 'loaded':
|
| 349 |
return True
|
|
|
|
| 359 |
retry_delay = 2
|
| 360 |
last_error = None
|
| 361 |
|
| 362 |
+
# URL encode model name for safety
|
| 363 |
+
import urllib.parse
|
| 364 |
+
encoded_name = urllib.parse.quote(model_name, safe='')
|
| 365 |
+
|
| 366 |
for attempt in range(max_retries):
|
| 367 |
try:
|
| 368 |
+
# Ensure connection is active and create new session if needed
|
| 369 |
+
if self._closing or not self.session_token:
|
| 370 |
self._closing = False
|
| 371 |
if not self._create_session():
|
| 372 |
+
last_error = "Failed to create session"
|
| 373 |
+
logging.error(f"Load attempt {attempt + 1} failed: {last_error}")
|
| 374 |
+
if attempt < max_retries - 1:
|
| 375 |
+
time.sleep(retry_delay * (1.5 ** attempt))
|
| 376 |
+
continue
|
| 377 |
+
break
|
| 378 |
|
| 379 |
# Calculate model hash if path provided
|
| 380 |
model_hash = None
|
|
|
|
| 389 |
|
| 390 |
response = self._make_request(
|
| 391 |
'POST',
|
| 392 |
+
f'/models/{encoded_name}/load',
|
| 393 |
json=request_data,
|
| 394 |
+
timeout=300 # 5 minutes timeout for model loading
|
| 395 |
)
|
| 396 |
|
| 397 |
if response and response.get('status') == 'success':
|