eli02 commited on
Commit
8a81462
·
1 Parent(s): 8c23c78

Add async lifespan context manager to preload model and update requirements for flexibility

Browse files
Files changed (2) hide show
  1. main.py +7 -0
  2. requirements.txt +14 -14
main.py CHANGED
@@ -11,6 +11,7 @@ from typing import List, Optional, Dict
11
  from datasets import Dataset, load_dataset
12
  from sentence_transformers import SentenceTransformer
13
  from huggingface_hub import login
 
14
  import pandas as pd
15
  import numpy as np
16
  import torch as t
@@ -22,6 +23,12 @@ from diskcache import Cache
22
  # Configure logging
23
  logging.basicConfig(level=logging.INFO)
24
 
 
 
 
 
 
 
25
  # Initialize FastAPI app
26
  app = FastAPI()
27
 
 
11
  from datasets import Dataset, load_dataset
12
  from sentence_transformers import SentenceTransformer
13
  from huggingface_hub import login
14
+ from contextlib import asynccontextmanager
15
  import pandas as pd
16
  import numpy as np
17
  import torch as t
 
23
  # Configure logging
24
  logging.basicConfig(level=logging.INFO)
25
 
26
+ @asynccontextmanager
27
+ async def lifespan(app: FastAPI):
28
+ # Preload the model
29
+ get_sentence_transformer()
30
+ yield
31
+
32
  # Initialize FastAPI app
33
  app = FastAPI()
34
 
requirements.txt CHANGED
@@ -1,14 +1,14 @@
1
- fastapi==0.109.2
2
- uvicorn==0.27.1
3
- python-jose==3.3.0
4
- python-multipart==0.0.6 # Required for OAuth2 form handling
5
- pydantic==2.6.1
6
- openai==1.12.0
7
- pandas==2.2.0
8
- numpy==1.26.3
9
- torch==2.1.2 # For sentence-transformers
10
- sentence-transformers==2.3.1
11
- datasets==2.17.0
12
- huggingface-hub==0.20.3
13
- diskcache==5.6.3
14
- python-dotenv==1.0.1 # For environment variable management
 
1
+ fastapi
2
+ uvicorn
3
+ python-jose
4
+ python-multipart # Required for OAuth2 form handling
5
+ pydantic
6
+ openai
7
+ pandas
8
+ numpy
9
+ torch # For sentence-transformers
10
+ sentence-transformers
11
+ datasets
12
+ huggingface-hub
13
+ diskcache
14
+ python-dotenv # For environment variable management