Spaces:
Sleeping
Sleeping
File size: 1,480 Bytes
5a5e912 3c358dc |
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 |
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
from app.models import EmbedRequest, EmbedResponse
from app.embeddings import embed_text
from logging import getLogger
logger = getLogger(__name__)
app = FastAPI(
title="Embedding API",
description="A simple API to generate text embeddings using Microsoft's `multilingual-e5-large` model.",
version="1.0.0",
)
# Allow simple cross-origin requests from local development (adjust in production)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/embed", response_model=EmbedResponse)
async def embed(request: EmbedRequest) -> dict[str, list[list[float]]]:
"""Generate embeddings for a list of texts."""
try:
vectors = embed_text(request.texts)
return {"embeddings": vectors}
except Exception as e:
logger.exception("Error generating embeddings")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/health")
async def health_check() -> dict[str, str]:
"""Health check endpoint."""
return {"status": "ok"}
@app.get("/", response_model=None)
async def root() -> FileResponse | dict[str, str]:
"""Serve the frontend `index.html` if present, otherwise return small JSON status."""
index_file = "frontend/index.html"
return FileResponse(index_file)
|