File size: 2,574 Bytes
492eddd 596c0d1 492eddd 596c0d1 492eddd 596c0d1 413dba1 596c0d1 492eddd 596c0d1 492eddd 596c0d1 492eddd 596c0d1 492eddd 596c0d1 492eddd 596c0d1 492eddd 596c0d1 |
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 |
import gradio as gr
import joblib
import numpy as np
import pandas as pd
import tensorflow as tf
# Assuming you have a function that creates the same model architecture
def create_model(input_shape):
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=input_shape),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(1000, activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.8),
tf.keras.layers.Dense(1000, activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.8),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=3e-6),
loss='binary_crossentropy',
metrics=['accuracy'])
return model
# Load model weights
input_shape = [18] # Adjust based on your feature count
model = create_model(input_shape)
model.load_weights('model_2.h5')
# Load the scaler
scaler = joblib.load('scaler.joblib')
features = [
'PRNCPTX_HEPATECTOMY RESCJ TOTAL RIGHT LOBECTOMY', 'OPTIME', 'HYPERMED_Yes',
'PRNCPTX_HEPATECTOMY RESCJ TRISEGMENTECTOMY', 'PRPLATE', 'PRINR',
'HEP_APPROACH_Laparoscopic', 'PRALKPH', 'HEP_PRINGLE_Yes',
'HEP_SEC_HISTOLOGIC_Colorectal metastasis', 'HEP_CON_PARTRES', 'SEX_male',
'ASACLAS', 'HEP_SEC_TUMORSIZE', 'HEP_SEC_NUMTUMORS', 'PRSGOT', 'HEP_VIRAL_None',
'HEP_NEOTHERAPY_140101_Preoperative systemic chemotherapy'
]
categorical_features = [
'HYPERMED_Yes', 'HEP_APPROACH_Laparoscopic', 'HEP_PRINGLE_Yes',
'SEX_male', 'HEP_VIRAL_None'
]
def predict_outcome(*args):
inputs = list(args)
# Convert 'Yes'/'No' inputs to 1/0 for the model
for i, feature in enumerate(features):
if feature in categorical_features:
inputs[i] = 1 if inputs[i] == 'Yes' else 0
input_data = np.array(inputs).reshape(1, -1)
# Scale inputs
inputs_scaled = scaler.transform(input_data)
# Predict
prediction = model.predict(inputs_scaled)
outcome_probability = prediction.flatten()[0]
return f'Probability of Post Hepatectomy Liver Failure: {outcome_probability:.2%}'
# Define Gradio inputs
inputs = []
for feature in features:
if feature in categorical_features:
inputs.append(gr.Radio(label=feature, choices=['Yes', 'No']))
else:
inputs.append(gr.Number(label=feature))
# Set up and launch the Gradio interface
iface = gr.Interface(fn=predict_outcome, inputs=inputs, outputs='text')
iface.launch()
|