GuhanAein commited on
Commit
c3c8b77
·
verified ·
1 Parent(s): 1940bfd

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +21 -1
main.py CHANGED
@@ -7,13 +7,33 @@ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
7
  from llama_index.vector_stores.faiss import FaissVectorStore
8
  import faiss
9
  import os
10
- from huggingface_hub import login
 
 
 
 
11
 
12
  app = FastAPI()
13
 
14
  # Set HF_HOME to a writable directory
15
  os.environ["HF_HOME"] = "/app/.cache"
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Log in to Hugging Face
18
  hf_token = os.getenv("HF_TOKEN")
19
  if not hf_token:
 
7
  from llama_index.vector_stores.faiss import FaissVectorStore
8
  import faiss
9
  import os
10
+ from huggingface_hub import login, snapshot_download
11
+ from huggingface_hub.utils import configure_http_backend
12
+ from requests.adapters import HTTPAdapter
13
+ from urllib3.util.retry import Retry
14
+ import requests
15
 
16
  app = FastAPI()
17
 
18
  # Set HF_HOME to a writable directory
19
  os.environ["HF_HOME"] = "/app/.cache"
20
 
21
+ # Configure requests with retries and a longer timeout
22
+ def custom_http_backend():
23
+ session = requests.Session()
24
+ retries = Retry(
25
+ total=3, # Retry 3 times
26
+ backoff_factor=1, # Wait 1, 2, 4 seconds between retries
27
+ status_forcelist=[429, 500, 502, 503, 504], # Retry on these HTTP status codes
28
+ )
29
+ adapter = HTTPAdapter(max_retries=retries)
30
+ session.mount("https://", adapter)
31
+ session.timeout = 60 # Increase timeout to 60 seconds
32
+ return session
33
+
34
+ # Set the custom HTTP backend for huggingface_hub
35
+ configure_http_backend(backend_factory=custom_http_backend)
36
+
37
  # Log in to Hugging Face
38
  hf_token = os.getenv("HF_TOKEN")
39
  if not hf_token: