Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, HTTPException, Header, Form | |
from sentence_transformers import SentenceTransformer | |
app = FastAPI() | |
# Load the Persian embedding model | |
model = SentenceTransformer("heydariAI/persian-embeddings") | |
# Replace this with your real API key or load from env/config | |
VALID_API_KEY = "91e6c0ae3017ba709284c64c47b6452a" | |
async def get_embedding( | |
api_key: str = Header(..., alias="X-API-Key"), | |
query: str = Form(...) | |
): | |
# Authenticate | |
if api_key != VALID_API_KEY: | |
raise HTTPException(status_code=401, detail="Invalid API key") | |
# Generate embedding | |
try: | |
embedding = model.encode(query).tolist() | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=f"Embedding failed: {str(e)}") | |
return { | |
"query": query, | |
"embedding": embedding | |
} | |