Spaces:
Sleeping
Sleeping
File size: 1,086 Bytes
b06f653 7d49ba4 b06f653 | 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 50 | import os
import torch
from fastapi import FastAPI
from pydantic import BaseModel
from sentence_transformers import SentenceTransformer
MODEL_ID = os.environ.get("MODEL_ID", "google/embeddinggemma-2")
app = FastAPI()
model = SentenceTransformer(
MODEL_ID,
device="cpu",
config_kwargs={"vision_config": None, "audio_config": None},
)
model.max_seq_length = 2048
class EmbedRequest(BaseModel):
texts: list[str]
task_prefix: str = "SearchQuery"
dimensions: int = 768
@app.get("/")
def root():
return {"ok": True, "endpoints": ["/health", "/embed", "/docs"]}
@app.get("/health")
def health():
return {"ok": True}
@app.post("/embed")
def embed(richiesta: EmbedRequest):
vettori = model.encode(
richiesta.texts,
prompt_name=richiesta.task_prefix,
normalize_embeddings=True,
batch_size=32,
show_progress_bar=False,
truncate_dim=richiesta.dimensions,
)
return {
"embeddings": vettori.tolist(),
"dimensions": richiesta.dimensions,
"count": len(richiesta.texts),
}
|