Add async lifespan context manager to preload model and update requirements for flexibility
Browse files- main.py +7 -0
- 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
|
2 |
-
uvicorn
|
3 |
-
python-jose
|
4 |
-
python-multipart
|
5 |
-
pydantic
|
6 |
-
openai
|
7 |
-
pandas
|
8 |
-
numpy
|
9 |
-
torch
|
10 |
-
sentence-transformers
|
11 |
-
datasets
|
12 |
-
huggingface-hub
|
13 |
-
diskcache
|
14 |
-
python-dotenv
|
|
|
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
|