Pepe_1 / model_loader.py
nileshhanotia's picture
Create model_loader.py
d0e5d92 verified
raw
history blame
1.37 kB
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)