Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI
|
| 3 |
-
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
from threading import Thread
|
| 6 |
from typing import Optional
|
| 7 |
-
from fastapi.responses import JSONResponse
|
| 8 |
|
| 9 |
# Initialize FastAPI
|
| 10 |
app = FastAPI(title="Sentence Transformer API")
|
|
@@ -13,40 +12,46 @@ app = FastAPI(title="Sentence Transformer API")
|
|
| 13 |
model: Optional[SentenceTransformer] = None
|
| 14 |
|
| 15 |
def load_model():
|
| 16 |
-
"""
|
| 17 |
global model
|
| 18 |
try:
|
| 19 |
model = SentenceTransformer('bge-small-zh')
|
| 20 |
-
print("
|
| 21 |
except Exception as e:
|
| 22 |
-
print(f"
|
| 23 |
|
| 24 |
-
# Start
|
| 25 |
Thread(target=load_model, daemon=True).start()
|
| 26 |
|
| 27 |
-
class TextInput(BaseModel):
|
| 28 |
-
text: str
|
| 29 |
-
|
| 30 |
@app.get("/")
|
| 31 |
def health_check():
|
| 32 |
-
"""
|
| 33 |
return {
|
| 34 |
"status": "ready",
|
| 35 |
"model_loaded": model is not None
|
| 36 |
}
|
| 37 |
|
| 38 |
@app.post("/embed")
|
| 39 |
-
async def embed_text(
|
| 40 |
-
"""Embed text and return vector"""
|
| 41 |
if model is None:
|
| 42 |
-
return
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI
|
| 3 |
+
import uvicorn
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
from threading import Thread
|
| 6 |
from typing import Optional
|
|
|
|
| 7 |
|
| 8 |
# Initialize FastAPI
|
| 9 |
app = FastAPI(title="Sentence Transformer API")
|
|
|
|
| 12 |
model: Optional[SentenceTransformer] = None
|
| 13 |
|
| 14 |
def load_model():
|
| 15 |
+
"""Background thread for model loading"""
|
| 16 |
global model
|
| 17 |
try:
|
| 18 |
model = SentenceTransformer('bge-small-zh')
|
| 19 |
+
print("Model loaded successfully")
|
| 20 |
except Exception as e:
|
| 21 |
+
print(f"Model loading failed: {str(e)}")
|
| 22 |
|
| 23 |
+
# Start model loading immediately
|
| 24 |
Thread(target=load_model, daemon=True).start()
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
@app.get("/")
|
| 27 |
def health_check():
|
| 28 |
+
"""Required health check endpoint"""
|
| 29 |
return {
|
| 30 |
"status": "ready",
|
| 31 |
"model_loaded": model is not None
|
| 32 |
}
|
| 33 |
|
| 34 |
@app.post("/embed")
|
| 35 |
+
async def embed_text(text: str):
|
|
|
|
| 36 |
if model is None:
|
| 37 |
+
return {"error": "Model still loading"}, 503
|
| 38 |
|
| 39 |
+
# Convert text to list if single string
|
| 40 |
+
input_text = [text] if isinstance(text, str) else text
|
| 41 |
+
|
| 42 |
+
embeddings = model.encode(input_text)
|
| 43 |
+
|
| 44 |
+
return {
|
| 45 |
+
"text": text,
|
| 46 |
+
"embedding": embeddings.tolist(),
|
| 47 |
+
"dimension": len(embeddings[0]) if isinstance(embeddings, list) else len(embeddings)
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
port = int(os.environ.get("PORT", 7860))
|
| 52 |
+
uvicorn.run(
|
| 53 |
+
"app:app",
|
| 54 |
+
host="0.0.0.0",
|
| 55 |
+
port=port,
|
| 56 |
+
reload=False # Disable auto-reload in production
|
| 57 |
+
)
|