babaTEEpe commited on
Commit
8825a51
·
verified ·
1 Parent(s): 3685254

Update app.py

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