Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,28 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi
|
| 3 |
-
from
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
-
import tensorflow as tf
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
|
| 8 |
-
# 🔹 Download model
|
| 9 |
-
MODEL_PATH = hf_hub_download(
|
| 10 |
-
|
| 11 |
-
filename="cancer_classifier.h5"
|
| 12 |
-
)
|
| 13 |
|
| 14 |
-
# 🔹 Load model
|
| 15 |
model = load_model(MODEL_PATH)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
app = FastAPI(title="
|
| 19 |
|
| 20 |
-
# ---------- Enable CORS (so your friend’s frontend can call it) ----------
|
| 21 |
-
app.add_middleware(
|
| 22 |
-
CORSMiddleware,
|
| 23 |
-
allow_origins=["*"], # Or set ["https://yourfrontend.vercel.app"] for extra safety
|
| 24 |
-
allow_credentials=True,
|
| 25 |
-
allow_methods=["*"],
|
| 26 |
-
allow_headers=["*"],
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
# ---------- Root Endpoint ----------
|
| 30 |
@app.get("/")
|
| 31 |
-
def
|
| 32 |
-
return {"message": "
|
| 33 |
|
| 34 |
-
# ---------- Prediction Endpoint ----------
|
| 35 |
@app.post("/predict")
|
| 36 |
-
async def predict(
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
}
|
| 42 |
-
"""
|
| 43 |
-
data = await request.json()
|
| 44 |
-
input_data = data.get("features")
|
| 45 |
-
|
| 46 |
-
if input_data is None:
|
| 47 |
-
raise HTTPException(status_code=400, detail="Missing 'features' in request body")
|
| 48 |
-
|
| 49 |
-
# Prepare and predict
|
| 50 |
-
input_array = np.array(input_data).reshape(1, -1)
|
| 51 |
-
prediction = model.predict(input_array)
|
| 52 |
-
result = float(prediction[0][0])
|
| 53 |
-
|
| 54 |
-
return {"prediction": result, "status": "success"}
|
| 55 |
-
|
| 56 |
-
# ---------- Health Check Endpoint ----------
|
| 57 |
-
@app.get("/health")
|
| 58 |
-
def health():
|
| 59 |
-
return {"status": "healthy"}
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
from fastapi import FastAPI, UploadFile, File
|
| 3 |
+
from keras.models import load_model
|
| 4 |
+
from PIL import Image
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
|
| 8 |
+
# 🔹 Download your model from Hugging Face Hub
|
| 9 |
+
MODEL_PATH = hf_hub_download(repo_id="aadityaramrame/blood-cell-cancer-detector",
|
| 10 |
+
filename="cancer_classifier.h5")
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# 🔹 Load the model
|
| 13 |
model = load_model(MODEL_PATH)
|
| 14 |
|
| 15 |
+
# 🔹 Initialize FastAPI app
|
| 16 |
+
app = FastAPI(title="Blood Cell Cancer Detection API")
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
@app.get("/")
|
| 19 |
+
def root():
|
| 20 |
+
return {"message": "API is running successfully 🚀"}
|
| 21 |
|
|
|
|
| 22 |
@app.post("/predict")
|
| 23 |
+
async def predict(file: UploadFile = File(...)):
|
| 24 |
+
image = Image.open(file.file).resize((64, 64)) # adjust size as per training
|
| 25 |
+
img_array = np.expand_dims(np.array(image) / 255.0, axis=0)
|
| 26 |
+
prediction = model.predict(img_array)
|
| 27 |
+
result = int(np.argmax(prediction))
|
| 28 |
+
return {"prediction": result}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|