File size: 857 Bytes
19aacd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"

@app.post("/embed")
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
    }