Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,52 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
|
| 6 |
-
# Load the
|
| 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 |
-
|
| 35 |
-
|
| 36 |
-
theme="gradio/soft"
|
| 37 |
-
)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from tensorflow.keras.models import load_model
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
+
# ---------- Load the Trained Model ----------
|
| 7 |
+
MODEL_PATH = "cancer_classifier.h5"
|
| 8 |
+
model = load_model(MODEL_PATH)
|
| 9 |
|
| 10 |
+
# ---------- Initialize FastAPI ----------
|
| 11 |
+
app = FastAPI(title="GenAI Health Insight API", version="2.0")
|
| 12 |
|
| 13 |
+
# ---------- Enable CORS (so your friend’s frontend can call it) ----------
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["*"], # Or set ["https://yourfrontend.vercel.app"] for extra safety
|
| 17 |
+
allow_credentials=True,
|
| 18 |
+
allow_methods=["*"],
|
| 19 |
+
allow_headers=["*"],
|
| 20 |
+
)
|
| 21 |
|
| 22 |
+
# ---------- Root Endpoint ----------
|
| 23 |
+
@app.get("/")
|
| 24 |
+
def home():
|
| 25 |
+
return {"message": "Welcome to GenAI Health Insight API 🚀"}
|
| 26 |
|
| 27 |
+
# ---------- Prediction Endpoint ----------
|
| 28 |
+
@app.post("/predict")
|
| 29 |
+
async def predict(request: Request):
|
| 30 |
+
"""
|
| 31 |
+
Input example (JSON):
|
| 32 |
+
{
|
| 33 |
+
"features": [value1, value2, ..., valueN]
|
| 34 |
}
|
| 35 |
+
"""
|
| 36 |
+
data = await request.json()
|
| 37 |
+
input_data = data.get("features")
|
| 38 |
|
| 39 |
+
if input_data is None:
|
| 40 |
+
raise HTTPException(status_code=400, detail="Missing 'features' in request body")
|
| 41 |
+
|
| 42 |
+
# Prepare and predict
|
| 43 |
+
input_array = np.array(input_data).reshape(1, -1)
|
| 44 |
+
prediction = model.predict(input_array)
|
| 45 |
+
result = float(prediction[0][0])
|
| 46 |
+
|
| 47 |
+
return {"prediction": result, "status": "success"}
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# ---------- Health Check Endpoint ----------
|
| 50 |
+
@app.get("/health")
|
| 51 |
+
def health():
|
| 52 |
+
return {"status": "healthy"}
|