Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import uvicorn, os
|
| 5 |
+
|
| 6 |
+
MODEL_ID = os.getenv("EMB_MODEL", "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
| 7 |
+
model = SentenceTransformer(MODEL_ID)
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
class Req(BaseModel):
|
| 11 |
+
texts: list[str]
|
| 12 |
+
|
| 13 |
+
@app.get("/health")
|
| 14 |
+
def health():
|
| 15 |
+
return {"ok": True}
|
| 16 |
+
|
| 17 |
+
@app.post("/generate")
|
| 18 |
+
def emb(r: Req):
|
| 19 |
+
if not r.texts:
|
| 20 |
+
return {"vectors": []}
|
| 21 |
+
vecs = model.encode(r.texts, convert_to_numpy=True).tolist()
|
| 22 |
+
return {"vectors": vecs}
|
| 23 |
+
|
| 24 |
+
if __name__ == "__main__":
|
| 25 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|