File size: 990 Bytes
693e699
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6b7d0e
 
 
 
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
from fastapi import FastAPI
from pydantic import BaseModel
from sentence_transformers import SentenceTransformer
from typing import List

# Load the pre-trained sentence transformer model
bge_small_model = SentenceTransformer('BAAI/bge-small-en-v1.5', device="cpu")
all_mp_net_model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2', device="cpu")

# Initialize FastAPI app
app = FastAPI()

# Request body model
class TextInput(BaseModel):
    text: List[str]  # List of sentences or text data
    model_name: str

# Route to calculate embeddings
@app.post("/get-embedding/")
async def get_embedding(input: TextInput):
    # Generate embeddings using the sentence transformer model
    if input.model_name == "BM":
        embeddings = all_mp_net_model.encode(input.text)
    else:
        embeddings = bge_small_model.encode(input.text)
    return {"embeddings": embeddings.tolist()}

@app.get("/status")
async def status():
    return {"status": "Server is up and running"}