import gradio as gr from keras.models import load_model from huggingface_hub import hf_hub_download from PIL import Image import numpy as np # ------------------------------- # MODEL LOADING # ------------------------------- MODEL_PATH = hf_hub_download( repo_id="aadityaramrame/blood-cell-cancer-detector", filename="cancer_classifier.h5" ) model = load_model(MODEL_PATH) # Class mapping CLASSES = [ "platelet", "monocyte", "lymphocyte", "erythroblast", "eosinophil", "basophil" ] # ------------------------------- # PREDICTION FUNCTION # ------------------------------- def classify_cancer(image): try: image = image.convert("RGB").resize((224, 224)) img_array = np.expand_dims(np.array(image) / 255.0, axis=0) prediction = model.predict(img_array) predicted_class = int(np.argmax(prediction)) confidence = float(np.max(prediction)) label = CLASSES[predicted_class] return f"🧫 **Predicted Cell Type:** {label}\n📊 **Confidence:** {confidence:.3f}" except Exception as e: return f"⚠️ Error: {str(e)}" # ------------------------------- # GRADIO INTERFACE # ------------------------------- demo = gr.Interface( fn=classify_cancer, inputs=gr.Image(type="pil", label="📸 Upload Blood Cell Image"), outputs=gr.Markdown(label="Result"), title="🧬 Blood Cell Cancer Detection", description=( "Upload a blood cell image to classify its type using a trained CNN model.\n" "Model trained on microscopic blood cell images for cancer detection." ), theme="soft" ) # ------------------------------- # LAUNCH # ------------------------------- if __name__ == "__main__": demo.launch()