Spaces:
Sleeping
Sleeping
| 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=["*"], | |
| ) | |
| 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)) | |
| async def health_check() -> dict[str, str]: | |
| """Health check endpoint.""" | |
| return {"status": "ok"} | |
| 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) | |