KG0101 commited on
Commit
596c0d1
1 Parent(s): 9c99596

Upload app.py

Browse files

Reverting to pre-definition

Files changed (1) hide show
  1. app.py +21 -35
app.py CHANGED
@@ -1,8 +1,10 @@
1
  import gradio as gr
2
  import joblib
3
  import numpy as np
 
4
  import tensorflow as tf
5
 
 
6
  def create_model(input_shape):
7
  model = tf.keras.models.Sequential([
8
  tf.keras.layers.Flatten(input_shape=input_shape),
@@ -20,8 +22,12 @@ def create_model(input_shape):
20
  metrics=['accuracy'])
21
  return model
22
 
23
- model = create_model([18])
 
 
24
  model.load_weights('model_2.h5')
 
 
25
  scaler = joblib.load('scaler.joblib')
26
 
27
  features = [
@@ -33,27 +39,6 @@ features = [
33
  'HEP_NEOTHERAPY_140101_Preoperative systemic chemotherapy'
34
  ]
35
 
36
- feature_descriptions = {
37
- 'PRNCPTX_HEPATECTOMY RESCJ TOTAL RIGHT LOBECTOMY': "Principal Text: Hepatectomy Resection Total Right Lobectomy",
38
- 'OPTIME': "Operation Time",
39
- 'HYPERMED_Yes': "Hypertension Medication: Yes/No",
40
- 'PRNCPTX_HEPATECTOMY RESCJ TRISEGMENTECTOMY': "Principal Text: Hepatectomy Resection Tri-segmentectomy",
41
- 'PRPLATE': "Preoperative Platelet Count",
42
- 'PRINR': "Preoperative INR Level",
43
- 'HEP_APPROACH_Laparoscopic': "Hepatectomy Approach: Laparoscopic",
44
- 'PRALKPH': "Preoperative Alkaline Phosphatase Level",
45
- 'HEP_PRINGLE_Yes': "Pringle Maneuver Used: Yes/No",
46
- 'HEP_SEC_HISTOLOGIC_Colorectal metastasis': "Secondary Histologic Diagnosis: Colorectal Metastasis",
47
- 'HEP_CON_PARTRES': "Concurrent Partial Resection",
48
- 'SEX_male': "Patient Gender: Male",
49
- 'ASACLAS': "ASA Classification",
50
- 'HEP_SEC_TUMORSIZE': "Secondary Tumor Size",
51
- 'HEP_SEC_NUMTUMORS': "Number of Secondary Tumors",
52
- 'PRSGOT': "Preoperative SGOT Levels",
53
- 'HEP_VIRAL_None': "No Viral Hepatitis",
54
- 'HEP_NEOTHERAPY_140101_Preoperative systemic chemotherapy': "Preoperative Systemic Chemotherapy"
55
- }
56
-
57
  categorical_features = [
58
  'HYPERMED_Yes', 'HEP_APPROACH_Laparoscopic', 'HEP_PRINGLE_Yes',
59
  'SEX_male', 'HEP_VIRAL_None'
@@ -61,25 +46,26 @@ categorical_features = [
61
 
62
  def predict_outcome(*args):
63
  inputs = list(args)
 
64
  for i, feature in enumerate(features):
65
  if feature in categorical_features:
66
  inputs[i] = 1 if inputs[i] == 'Yes' else 0
67
  input_data = np.array(inputs).reshape(1, -1)
 
68
  inputs_scaled = scaler.transform(input_data)
 
69
  prediction = model.predict(inputs_scaled)
70
  outcome_probability = prediction.flatten()[0]
71
- prediction_text = f'Probability of Post Hepatectomy Liver Failure: {outcome_probability:.2%}'
72
- markdown_text = "More details on the variables used in NSQIP can be found in the [NSQIP PUF User Guide 2021] (https://www.facs.org/media/wd2hlqzv/nsqip_puf_userguide_2021.pdf)."
73
- return prediction_text, markdown_text
74
-
75
- inputs = [gr.Radio(label=feature_descriptions.get(feature), choices=['Yes', 'No']) if feature in categorical_features else gr.Number(label=feature_descriptions.get(feature)) for feature in features]
76
 
77
- output_text = gr.Textbox(label="Probability of Liver Failure")
78
- link_info = gr.Markdown()
 
 
 
 
 
79
 
80
- iface = gr.Interface(
81
- fn=predict_outcome,
82
- inputs=[gr.Radio(label=feature_descriptions.get(feature), choices=['Yes', 'No']) if feature in categorical_features else gr.Number(label=feature_descriptions.get(feature)) for feature in features],
83
- outputs=[output_text, link_info],
84
- title="NSQIP Post Hepatectomy Liver Failure Calculator"
85
- )
 
1
  import gradio as gr
2
  import joblib
3
  import numpy as np
4
+ import pandas as pd
5
  import tensorflow as tf
6
 
7
+ # Assuming you have a function that creates the same model architecture
8
  def create_model(input_shape):
9
  model = tf.keras.models.Sequential([
10
  tf.keras.layers.Flatten(input_shape=input_shape),
 
22
  metrics=['accuracy'])
23
  return model
24
 
25
+ # Load model weights
26
+ input_shape = [18] # Adjust based on your feature count
27
+ model = create_model(input_shape)
28
  model.load_weights('model_2.h5')
29
+
30
+ # Load the scaler
31
  scaler = joblib.load('scaler.joblib')
32
 
33
  features = [
 
39
  'HEP_NEOTHERAPY_140101_Preoperative systemic chemotherapy'
40
  ]
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  categorical_features = [
43
  'HYPERMED_Yes', 'HEP_APPROACH_Laparoscopic', 'HEP_PRINGLE_Yes',
44
  'SEX_male', 'HEP_VIRAL_None'
 
46
 
47
  def predict_outcome(*args):
48
  inputs = list(args)
49
+ # Convert 'Yes'/'No' inputs to 1/0 for the model
50
  for i, feature in enumerate(features):
51
  if feature in categorical_features:
52
  inputs[i] = 1 if inputs[i] == 'Yes' else 0
53
  input_data = np.array(inputs).reshape(1, -1)
54
+ # Scale inputs
55
  inputs_scaled = scaler.transform(input_data)
56
+ # Predict
57
  prediction = model.predict(inputs_scaled)
58
  outcome_probability = prediction.flatten()[0]
59
+ return f'Probability of Post Hepatectomy Liver Failure: {outcome_probability:.2%}'
 
 
 
 
60
 
61
+ # Define Gradio inputs
62
+ inputs = []
63
+ for feature in features:
64
+ if feature in categorical_features:
65
+ inputs.append(gr.Radio(label=feature, choices=['Yes', 'No']))
66
+ else:
67
+ inputs.append(gr.Number(label=feature))
68
 
69
+ # Set up and launch the Gradio interface
70
+ iface = gr.Interface(fn=predict_outcome, inputs=inputs, outputs='text')
71
+ iface.launch()