Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,41 @@
|
|
| 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 |
-
|
| 7 |
|
| 8 |
-
# ๐น Download
|
| 9 |
-
MODEL_PATH = hf_hub_download(
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# ๐น Load the model
|
| 13 |
model = load_model(MODEL_PATH)
|
| 14 |
|
| 15 |
# ๐น Initialize FastAPI app
|
| 16 |
-
app = FastAPI(title="
|
| 17 |
|
| 18 |
@app.get("/")
|
| 19 |
-
def root():
|
| 20 |
-
return {"message": "API is
|
| 21 |
|
| 22 |
@app.post("/predict")
|
| 23 |
async def predict(file: UploadFile = File(...)):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
from fastapi import FastAPI, UploadFile, File
|
| 3 |
+
from fastapi.responses import JSONResponse
|
| 4 |
from keras.models import load_model
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
| 8 |
+
import uvicorn
|
| 9 |
|
| 10 |
+
# ๐น Download model from Hugging Face Hub
|
| 11 |
+
MODEL_PATH = hf_hub_download(
|
| 12 |
+
repo_id="aadityaramrame/blood-cell-cancer-detector",
|
| 13 |
+
filename="cancer_classifier.h5"
|
| 14 |
+
)
|
| 15 |
|
| 16 |
+
# ๐น Load the trained model
|
| 17 |
model = load_model(MODEL_PATH)
|
| 18 |
|
| 19 |
# ๐น Initialize FastAPI app
|
| 20 |
+
app = FastAPI(title="Cancer Detection API")
|
| 21 |
|
| 22 |
@app.get("/")
|
| 23 |
+
async def root():
|
| 24 |
+
return {"message": "๐ Cancer Detection API is live!"}
|
| 25 |
|
| 26 |
@app.post("/predict")
|
| 27 |
async def predict(file: UploadFile = File(...)):
|
| 28 |
+
try:
|
| 29 |
+
image = Image.open(file.file).convert("RGB").resize((64, 64)) # use training size
|
| 30 |
+
img_array = np.expand_dims(np.array(image) / 255.0, axis=0)
|
| 31 |
+
prediction = model.predict(img_array)
|
| 32 |
+
result = int(np.argmax(prediction))
|
| 33 |
+
return JSONResponse(content={"prediction": result})
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# ๐น This line is CRUCIAL for Hugging Face Spaces
|
| 39 |
+
# It ensures the FastAPI app is visible when the Space starts
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|