nileshhanotia commited on
Commit
d0e5d92
1 Parent(s): b23b96d

Create model_loader.py

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