File size: 3,256 Bytes
0f6e76d
 
 
 
 
 
56711ad
0f6e76d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
460b704
0f6e76d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56711ad
0f6e76d
 
 
 
 
 
 
56711ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f6e76d
56711ad
 
 
0f6e76d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#+--------------------------------------------------------------------------------------------+
# 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. <br>
    For convenience, the data is reproduced here: <br>
    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 <br>
    The original article - 
    <a href="https://prakashkota.com/2025/02/11/the-power-of-machine-learning-in-medical-diagnosis-breast-cancer-mini-case-using-neural-networks/" target="_blank">
    The Power of Machine Learning in Medical Diagnosis – Breast Cancer Mini Case using Neural Networks</a>
""")

# 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()