Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,8 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi.responses import JSONResponse
|
| 3 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 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 gradio as gr
|
| 9 |
-
|
| 10 |
-
# -------------------------------
|
| 11 |
-
# FASTAPI SETUP
|
| 12 |
-
# -------------------------------
|
| 13 |
-
app = FastAPI(title="Cancer Detection API")
|
| 14 |
-
|
| 15 |
-
app.add_middleware(
|
| 16 |
-
CORSMiddleware,
|
| 17 |
-
allow_origins=["*"],
|
| 18 |
-
allow_credentials=True,
|
| 19 |
-
allow_methods=["*"],
|
| 20 |
-
allow_headers=["*"],
|
| 21 |
-
)
|
| 22 |
|
| 23 |
# -------------------------------
|
| 24 |
# MODEL LOADING
|
|
@@ -27,6 +11,7 @@ MODEL_PATH = hf_hub_download(
|
|
| 27 |
repo_id="aadityaramrame/blood-cell-cancer-detector",
|
| 28 |
filename="cancer_classifier.h5"
|
| 29 |
)
|
|
|
|
| 30 |
model = load_model(MODEL_PATH)
|
| 31 |
|
| 32 |
# Class mapping
|
|
@@ -40,36 +25,7 @@ CLASSES = [
|
|
| 40 |
]
|
| 41 |
|
| 42 |
# -------------------------------
|
| 43 |
-
#
|
| 44 |
-
# -------------------------------
|
| 45 |
-
@app.get("/")
|
| 46 |
-
async def root():
|
| 47 |
-
return {"message": "🚀 Cancer Detection API is live!"}
|
| 48 |
-
|
| 49 |
-
@app.post("/predict")
|
| 50 |
-
async def predict(file: UploadFile = File(...)):
|
| 51 |
-
try:
|
| 52 |
-
image = Image.open(file.file).convert("RGB").resize((224, 224))
|
| 53 |
-
img_array = np.expand_dims(np.array(image) / 255.0, axis=0)
|
| 54 |
-
|
| 55 |
-
prediction = model.predict(img_array)
|
| 56 |
-
predicted_class = int(np.argmax(prediction))
|
| 57 |
-
confidence = float(np.max(prediction))
|
| 58 |
-
|
| 59 |
-
label = CLASSES[predicted_class]
|
| 60 |
-
|
| 61 |
-
return JSONResponse(
|
| 62 |
-
content={
|
| 63 |
-
"predicted_class": label,
|
| 64 |
-
"confidence": round(confidence, 3),
|
| 65 |
-
"raw_index": predicted_class
|
| 66 |
-
}
|
| 67 |
-
)
|
| 68 |
-
except Exception as e:
|
| 69 |
-
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 70 |
-
|
| 71 |
-
# -------------------------------
|
| 72 |
-
# GRADIO FRONTEND
|
| 73 |
# -------------------------------
|
| 74 |
def classify_cancer(image):
|
| 75 |
try:
|
|
@@ -79,19 +35,31 @@ def classify_cancer(image):
|
|
| 79 |
predicted_class = int(np.argmax(prediction))
|
| 80 |
confidence = float(np.max(prediction))
|
| 81 |
label = CLASSES[predicted_class]
|
| 82 |
-
return {label
|
| 83 |
except Exception as e:
|
| 84 |
-
return
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
fn=classify_cancer,
|
| 88 |
-
inputs=gr.Image(type="pil", label="Upload Blood Cell Image"),
|
| 89 |
-
outputs=gr.
|
| 90 |
-
title="
|
| 91 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
theme="soft"
|
| 93 |
)
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from keras.models import load_model
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# -------------------------------
|
| 8 |
# MODEL LOADING
|
|
|
|
| 11 |
repo_id="aadityaramrame/blood-cell-cancer-detector",
|
| 12 |
filename="cancer_classifier.h5"
|
| 13 |
)
|
| 14 |
+
|
| 15 |
model = load_model(MODEL_PATH)
|
| 16 |
|
| 17 |
# Class mapping
|
|
|
|
| 25 |
]
|
| 26 |
|
| 27 |
# -------------------------------
|
| 28 |
+
# PREDICTION FUNCTION
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# -------------------------------
|
| 30 |
def classify_cancer(image):
|
| 31 |
try:
|
|
|
|
| 35 |
predicted_class = int(np.argmax(prediction))
|
| 36 |
confidence = float(np.max(prediction))
|
| 37 |
label = CLASSES[predicted_class]
|
| 38 |
+
return f"🧫 **Predicted Cell Type:** {label}\n📊 **Confidence:** {confidence:.3f}"
|
| 39 |
except Exception as e:
|
| 40 |
+
return f"⚠️ Error: {str(e)}"
|
| 41 |
|
| 42 |
+
# -------------------------------
|
| 43 |
+
# GRADIO INTERFACE
|
| 44 |
+
# -------------------------------
|
| 45 |
+
demo = gr.Interface(
|
| 46 |
fn=classify_cancer,
|
| 47 |
+
inputs=gr.Image(type="pil", label="📸 Upload Blood Cell Image"),
|
| 48 |
+
outputs=gr.Markdown(label="Result"),
|
| 49 |
+
title="🧬 Blood Cell Cancer Detection",
|
| 50 |
+
description=(
|
| 51 |
+
"Upload a blood cell image to classify its type using a trained CNN model.\n"
|
| 52 |
+
"Model trained on microscopic blood cell images for cancer detection."
|
| 53 |
+
),
|
| 54 |
+
examples=[
|
| 55 |
+
["example1.jpg"],
|
| 56 |
+
["example2.jpg"]
|
| 57 |
+
],
|
| 58 |
theme="soft"
|
| 59 |
)
|
| 60 |
|
| 61 |
+
# -------------------------------
|
| 62 |
+
# LAUNCH
|
| 63 |
+
# -------------------------------
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
demo.launch()
|