KG0101's picture
Upload app.py
596c0d1 verified
raw
history blame
2.57 kB
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()