|
import gradio as gr |
|
import joblib |
|
import numpy as np |
|
import pandas as pd |
|
import tensorflow as tf |
|
|
|
|
|
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 |
|
|
|
|
|
input_shape = [18] |
|
model = create_model(input_shape) |
|
model.load_weights('model_2.h5') |
|
|
|
|
|
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) |
|
|
|
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) |
|
|
|
inputs_scaled = scaler.transform(input_data) |
|
|
|
prediction = model.predict(inputs_scaled) |
|
outcome_probability = prediction.flatten()[0] |
|
return f'Probability of Post Hepatectomy Liver Failure: {outcome_probability:.2%}' |
|
|
|
|
|
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)) |
|
|
|
|
|
iface = gr.Interface(fn=predict_outcome, inputs=inputs, outputs='text') |
|
iface.launch() |
|
|