File size: 2,169 Bytes
e5bdb67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#+--------------------------------------------------------------------------------------------+
# 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: 12 Feb 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


# 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
interface = 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."
)

# Launch the Gradio app
interface.launch()