-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
49 lines (40 loc) · 1.38 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from sentence_transformers import SentenceTransformer
from typing import List
model_loaded = False
model = None
class SentenceInput(BaseModel):
sentences: List[str]
class EncodingOutput(BaseModel):
encodings: List[List[float]]
async def load_model():
global model, model_loaded
loop = asyncio.get_event_loop()
model = await loop.run_in_executor(None, SentenceTransformer, "BAAI/bge-m3")
model_loaded = True
@asynccontextmanager
async def lifespan(app: FastAPI):
asyncio.create_task(load_model())
yield
app = FastAPI(lifespan=lifespan)
@app.post("/encode", response_model=EncodingOutput)
async def encode_sentences(input: SentenceInput):
if not model_loaded:
raise HTTPException(status_code=503, detail="Model not loaded yet")
try:
encodings = model.encode(input.sentences)
encodings_list = encodings.tolist()
return EncodingOutput(encodings=encodings_list)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/readiness")
async def readiness_probe():
if model_loaded:
return {"status": "ready"}
raise HTTPException(status_code=503, detail="Model not loaded yet")
@app.get("/liveness")
async def liveness_probe():
return {"status": "alive"}