Aadityaramrame's picture
Update app.py
8aa6871 verified
raw
history blame
1.54 kB
from fastapi import FastAPI, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from tensorflow.keras.models import load_model
import numpy as np
# ---------- Load the Trained Model ----------
MODEL_PATH = "cancer_classifier.h5"
model = load_model(MODEL_PATH)
# ---------- Initialize FastAPI ----------
app = FastAPI(title="GenAI Health Insight API", version="2.0")
# ---------- Enable CORS (so your friend’s frontend can call it) ----------
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Or set ["https://yourfrontend.vercel.app"] for extra safety
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ---------- Root Endpoint ----------
@app.get("/")
def home():
return {"message": "Welcome to GenAI Health Insight API 🚀"}
# ---------- Prediction Endpoint ----------
@app.post("/predict")
async def predict(request: Request):
"""
Input example (JSON):
{
"features": [value1, value2, ..., valueN]
}
"""
data = await request.json()
input_data = data.get("features")
if input_data is None:
raise HTTPException(status_code=400, detail="Missing 'features' in request body")
# Prepare and predict
input_array = np.array(input_data).reshape(1, -1)
prediction = model.predict(input_array)
result = float(prediction[0][0])
return {"prediction": result, "status": "success"}
# ---------- Health Check Endpoint ----------
@app.get("/health")
def health():
return {"status": "healthy"}