File size: 1,228 Bytes
4bb4208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import List, Literal
from pydantic import BaseModel, Field
from fastapi import FastAPI, APIRouter, Request
from fastapi.middleware.cors import CORSMiddleware
from sentence_transformers import SentenceTransformer
import uvicorn

# Initialize FastAPI app
app = FastAPI()

# CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Load model
model = SentenceTransformer('Alibaba-NLP/gte-multilingual-base', trust_remote_code=True)

# Define data model
class PostEmbeddings(BaseModel):
    type: Literal['default', 'disease', 'gte'] = Field(default='default')
    sentences: List[str]

# Router for embeddings
router = APIRouter(prefix="/retrieval", tags=["retrieval"])

@router.post('/embeddings')
def post_embeddings(request: Request, data: PostEmbeddings):
    embeddings = model.encode(data.sentences)
    return {"data":{"embeddings": embeddings.tolist()}}

# Include router
app.include_router(router)

# Define main function to run the app
def main():
    uvicorn.run("api:app", host="0.0.0.0", port=8000, reload=True)

# Run the app if this script is the main module
if __name__ == "__main__":
    main()