Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Define the API schema for the request body
|
| 8 |
+
class EmbeddingRequest(BaseModel):
|
| 9 |
+
text: list[str]
|
| 10 |
+
|
| 11 |
+
# Initialize FastAPI app
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
# Check for GPU and load model accordingly
|
| 15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
+
model_name = os.getenv("MODEL_NAME")
|
| 17 |
+
model = SentenceTransformer(model_name, device=device)
|
| 18 |
+
|
| 19 |
+
# Define the embedding endpoint
|
| 20 |
+
@app.post("/embed")
|
| 21 |
+
async def get_embeddings(request: EmbeddingRequest):
|
| 22 |
+
try:
|
| 23 |
+
# Get embeddings for the input text
|
| 24 |
+
embeddings = model.encode(request.text, convert_to_numpy=True).tolist()
|
| 25 |
+
return {"embeddings": embeddings}
|
| 26 |
+
except Exception as e:
|
| 27 |
+
raise HTTPException(status_code=500, detail=str(e))
|