Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,44 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
-
from
|
| 3 |
-
from
|
| 4 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Initialize FastAPI
|
| 8 |
+
app = FastAPI(title="Davidic Sermon Embeddings API")
|
| 9 |
+
|
| 10 |
+
# Add CORS Middleware to allow requests from Vercel
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"], # Allows all origins
|
| 14 |
+
allow_credentials=True,
|
| 15 |
+
allow_methods=["*"],
|
| 16 |
+
allow_headers=["*"],
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Load model at startup
|
| 20 |
+
# We use all-MiniLM-L6-v2 which is small and fast on CPU
|
| 21 |
+
print("Loading model...")
|
| 22 |
+
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 23 |
+
print("Model loaded.")
|
| 24 |
+
|
| 25 |
+
class EmbedRequest(BaseModel):
|
| 26 |
+
text: str
|
| 27 |
+
|
| 28 |
+
@app.get("/")
|
| 29 |
+
def health_check():
|
| 30 |
+
return {"status": "running", "model": "all-MiniLM-L6-v2"}
|
| 31 |
+
|
| 32 |
+
@app.post("/embed")
|
| 33 |
+
def embed(request: EmbedRequest):
|
| 34 |
+
try:
|
| 35 |
+
# Generate embedding
|
| 36 |
+
# tolist() converts numpy array to standard python list
|
| 37 |
+
embedding = model.encode(request.text).tolist()
|
| 38 |
+
return embedding
|
| 39 |
+
except Exception as e:
|
| 40 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
import uvicorn
|
| 44 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|