prakashkota commited on
Commit
0f6e76d
·
verified ·
1 Parent(s): e5bdb67

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #+--------------------------------------------------------------------------------------------+
2
+ # Breast Cancer Prediction
3
+ # Using Neural Networks and Tensorflow
4
+ # Prediction using Gradio on Hugging Face
5
+ # Written by: Prakash R. Kota
6
+ # Written on: 12 Feb 2025
7
+ # Last update: 12 Feb 2025
8
+
9
+ # Data Set from
10
+ # Original:
11
+ # https://archive.ics.uci.edu/dataset/17/breast+cancer+wisconsin+diagnostic
12
+ # With Header:
13
+ # https://www.kaggle.com/code/nancyalaswad90/analysis-breast-cancer-prediction-dataset
14
+ #
15
+ # Input Data Format for Gradio must be in the above header format with 30 features
16
+ # The header has 32 features listed, but ignore the first 2 header columns
17
+ #+--------------------------------------------------------------------------------------------+
18
+
19
+ import tensorflow as tf
20
+ import numpy as np
21
+ import gradio as gr
22
+ import joblib
23
+
24
+
25
+ # Load the trained model
26
+ model = tf.keras.models.load_model("PRK_BC_NN_Model.keras")
27
+
28
+ # Load the saved Scaler
29
+ scaler = joblib.load("PRK_BC_NN_Scaler.pkl")
30
+
31
+ # Function to process input and make predictions
32
+ def predict(input_text):
33
+ # Convert input string into a NumPy array of shape (1, 30)
34
+ input_data = np.array([list(map(float, input_text.split(",")))])
35
+
36
+ # Ensure the input shape is correct
37
+ if input_data.shape != (1, 30):
38
+ return "Error: Please enter exactly 30 numerical values separated by commas."
39
+
40
+ # Transform the input data using the loded scaler
41
+ input_data_scaled = scaler.transform(input_data)
42
+
43
+ # Make a prediction
44
+ prediction = model.predict(input_data_scaled)
45
+
46
+ # Convert prediction to a binary outcome (assuming classification)
47
+ result = "Malignant" if prediction[0][0] > 0.5 else "Benign"
48
+
49
+ return f"Prediction: {result} (Confidence: {prediction[0][0]:.2f})"
50
+
51
+
52
+ import gradio as gr
53
+
54
+ # Create the Gradio interface
55
+ interface = gr.Interface(
56
+ fn=predict,
57
+ inputs=gr.Textbox(label="Enter 30 feature values, comma-separated"),
58
+ outputs="text",
59
+ title="Breast Cancer Prediction",
60
+ description="Enter 30 numerical feature values separated by commas to predict whether the biopsy is Malignant or Benign."
61
+ )
62
+
63
+ # Launch the Gradio app
64
+ interface.launch()
65
+