Spaces:
Build error
Build error
File size: 1,366 Bytes
d0e5d92 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import time
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from config.settings import MODEL_RETRY_ATTEMPTS, REQUEST_RETRY_ATTEMPTS, BACKOFF_FACTOR
from utils.logger import setup_logger
logger = setup_logger(__name__)
class ModelLoader:
@staticmethod
def create_retry_session():
session = requests.Session()
retry = Retry(
total=REQUEST_RETRY_ATTEMPTS,
backoff_factor=BACKOFF_FACTOR,
status_forcelist=[500, 502, 503, 504]
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
@staticmethod
def load_model_with_retry(model_name, model_class, **kwargs):
for attempt in range(MODEL_RETRY_ATTEMPTS):
try:
logger.info(f"Loading model {model_name} (attempt {attempt + 1}/{MODEL_RETRY_ATTEMPTS})")
return model_class.from_pretrained(model_name, **kwargs)
except Exception as e:
if attempt == MODEL_RETRY_ATTEMPTS - 1:
logger.error(f"Failed to load model {model_name}: {str(e)}")
raise
logger.warning(f"Attempt {attempt + 1} failed, retrying...")
time.sleep(2 ** attempt)
|