#+--------------------------------------------------------------------------------------------+
# Breast Cancer Prediction
# Using Neural Networks and Tensorflow
# Prediction using Gradio on Hugging Face
# Written by: Prakash R. Kota
# Written on: 12 Feb 2025
# Last update: 17 Mar 2025
# Data Set from
# Original:
# https://archive.ics.uci.edu/dataset/17/breast+cancer+wisconsin+diagnostic
# With Header:
# https://www.kaggle.com/code/nancyalaswad90/analysis-breast-cancer-prediction-dataset
#
# Input Data Format for Gradio must be in the above header format with 30 features
# The header has 32 features listed, but ignore the first 2 header columns
#+--------------------------------------------------------------------------------------------+
import tensorflow as tf
import numpy as np
import gradio as gr
import joblib
from sklearn.preprocessing import StandardScaler
# Load the trained model
model = tf.keras.models.load_model("PRK_BC_NN_Model.keras")
# Load the saved Scaler
scaler = joblib.load("PRK_BC_NN_Scaler.pkl")
# Function to process input and make predictions
def predict(input_text):
# Convert input string into a NumPy array of shape (1, 30)
input_data = np.array([list(map(float, input_text.split(",")))])
# Ensure the input shape is correct
if input_data.shape != (1, 30):
return "Error: Please enter exactly 30 numerical values separated by commas."
# Transform the input data using the loded scaler
input_data_scaled = scaler.transform(input_data)
# Make a prediction
prediction = model.predict(input_data_scaled)
# Convert prediction to a binary outcome (assuming classification)
result = "Malignant" if prediction[0][0] > 0.5 else "Benign"
return f"Prediction: {result} (Confidence: {prediction[0][0]:.2f})"
import gradio as gr
# Create the Gradio interface
iface = gr.Interface(
fn=predict,
inputs=gr.Textbox(label="Enter 30 feature values, comma-separated"),
outputs="text",
title="Breast Cancer Prediction",
description="Enter 30 numerical feature values separated by commas to predict whether the biopsy is Malignant or Benign."
)
# Add the Markdown footer with a clickable hyperlink
footer = gr.Markdown("""
To test the Model, please copy the data from the original article, excluding the first two data points.
For convenience, the data is reproduced here:
17.99,10.38,122.8,1001,0.1184,0.2776,0.3001,0.1471,0.2419,0.07871,1.095,0.9053,8.589,153.4,0.006399,0.04904,0.05373,0.01587,0.03003,0.006193,25.38,17.33,184.6,2019,0.1622,0.6656,0.7119,0.2654,0.4601,0.1189
The original article -
The Power of Machine Learning in Medical Diagnosis – Breast Cancer Mini Case using Neural Networks
""")
# Launch the interface with the footer
with gr.Blocks() as demo:
iface.render()
footer.render()
# Ensure the app launches when executed
if __name__ == "__main__":
demo.launch(share=True)
# The first version had the interface function for the Gradio markdown
# Launch the Gradio app
#interface.launch()