Spaces:
Runtime error
Runtime error
| import tensorflow as tf | |
| import gradio as gr | |
| import numpy as np | |
| from PIL import Image | |
| from tensorflow.keras.preprocessing import image | |
| # --------------------------------------------------------- | |
| # Load trained model (inference only) | |
| # --------------------------------------------------------- | |
| MODEL_PATH = "breast_cancer_model.h5" | |
| model = tf.keras.models.load_model( | |
| "breast_cancer_model.keras", | |
| compile=False | |
| ) | |
| # --------------------------------------------------------- | |
| # Prediction function | |
| # --------------------------------------------------------- | |
| def predict_image(img: Image.Image): | |
| """ | |
| Takes a PIL image as input and returns prediction text | |
| """ | |
| # Resize image to model input size | |
| img = img.resize((224, 224)) | |
| # Convert to array and normalize | |
| img_array = image.img_to_array(img) / 255.0 | |
| img_array = np.expand_dims(img_array, axis=0) | |
| # Predict | |
| prediction = model.predict(img_array)[0][0] | |
| # Output | |
| if prediction > 0.5: | |
| return f"Cancer Detected (Confidence: {prediction:.2f})" | |
| else: | |
| return f"Healthy (Confidence: {1 - prediction:.2f})" | |
| # --------------------------------------------------------- | |
| # Gradio Interface | |
| # --------------------------------------------------------- | |
| interface = gr.Interface( | |
| fn=predict_image, | |
| inputs=gr.Image(type="pil", label="Upload Histopathology Image"), | |
| outputs=gr.Textbox(label="Prediction Result"), | |
| title="Breast Cancer Detection System", | |
| description=( | |
| "Upload a breast histopathology image to detect cancer using a " | |
| "VGG16-based deep learning model.\n\n" | |
| "⚠️ This tool is for research and educational purposes only." | |
| ), | |
| allow_flagging="never" | |
| ) | |
| # --------------------------------------------------------- | |
| # Launch App | |
| # --------------------------------------------------------- | |
| if __name__ == "__main__": | |
| interface.launch() | |