Spaces:
Build error
Build error
lime
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ from pycaret.classification import *
|
|
| 13 |
import warnings
|
| 14 |
warnings.filterwarnings("ignore", category=FutureWarning, module="torch.storage")
|
| 15 |
|
| 16 |
-
from util import
|
| 17 |
import view
|
| 18 |
|
| 19 |
|
|
@@ -33,17 +33,6 @@ def parse_args(args):
|
|
| 33 |
return parser.parse_args(args)
|
| 34 |
|
| 35 |
|
| 36 |
-
def load_data_and_prepare(data_dir, excel_file, mode, scale, smote):
|
| 37 |
-
# Load train, validation, and test data
|
| 38 |
-
train_df,val_df = load_data(data_dir, excel_file, mode, scale, smote)
|
| 39 |
-
|
| 40 |
-
train_df.drop(columns=['patient_id','target'],inplace = True)
|
| 41 |
-
val_df.drop(columns=['patient_id','target'],inplace = True)
|
| 42 |
-
|
| 43 |
-
train = pd.concat([train_df,val_df],axis=0)
|
| 44 |
-
|
| 45 |
-
return train
|
| 46 |
-
|
| 47 |
|
| 48 |
# Inference function
|
| 49 |
def classify(tabular_data):
|
|
@@ -82,6 +71,64 @@ def classify(tabular_data):
|
|
| 82 |
except Exception as e:
|
| 83 |
return f"An error occurred during classification: {str(e)}"
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
if __name__ == '__main__':
|
| 86 |
args = parse_args(sys.argv[1:])
|
| 87 |
train = load_data_and_prepare(args.data_dir, args.excel_file, args.mode, args.scale, args.smote)
|
|
@@ -119,7 +166,7 @@ if __name__ == '__main__':
|
|
| 119 |
lime_output = gr.HTML(label="LIME Explanation")
|
| 120 |
gr.Examples(examples=examples, inputs=[tabular_input, info])
|
| 121 |
btn.click(fn=classify, inputs=tabular_input, outputs=result_output)
|
| 122 |
-
|
| 123 |
|
| 124 |
# Clear functionality: resets inputs and outputs
|
| 125 |
def clear_fields():
|
|
|
|
| 13 |
import warnings
|
| 14 |
warnings.filterwarnings("ignore", category=FutureWarning, module="torch.storage")
|
| 15 |
|
| 16 |
+
from util import load_data_and_prepare
|
| 17 |
import view
|
| 18 |
|
| 19 |
|
|
|
|
| 33 |
return parser.parse_args(args)
|
| 34 |
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Inference function
|
| 38 |
def classify(tabular_data):
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
return f"An error occurred during classification: {str(e)}"
|
| 73 |
|
| 74 |
+
# Inference function
|
| 75 |
+
def predict_proba_fn(instance):
|
| 76 |
+
"""
|
| 77 |
+
PyCaret의 predict_model을 활용한 확률 예측 함수.
|
| 78 |
+
"""
|
| 79 |
+
# 2D 형태로 변환
|
| 80 |
+
if instance.ndim == 1:
|
| 81 |
+
instance = instance.reshape(1, -1)
|
| 82 |
+
|
| 83 |
+
# DataFrame으로 변환
|
| 84 |
+
instance_df = pd.DataFrame(instance, columns=train.columns)
|
| 85 |
+
|
| 86 |
+
# predict_model을 통해 예측 수행
|
| 87 |
+
predictions = predict_model(model, data=instance_df)
|
| 88 |
+
|
| 89 |
+
# prediction_label이 1이면 prediction_score, 0이면 1-prediction_score
|
| 90 |
+
predictions['class_1_prob'] = np.where(predictions['prediction_label'] == 1,
|
| 91 |
+
predictions['prediction_score'],
|
| 92 |
+
0)
|
| 93 |
+
|
| 94 |
+
predictions['class_0_prob'] = np.where(predictions['prediction_label'] == 0,
|
| 95 |
+
predictions['prediction_score'],
|
| 96 |
+
0)
|
| 97 |
+
|
| 98 |
+
# class_0_prob와 class_1_prob 반환
|
| 99 |
+
return predictions[['class_0_prob', 'class_1_prob']].values
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def explain_with_lime(tabular_data):
|
| 103 |
+
instance = np.array(tabular_data[0],dtype='float')
|
| 104 |
+
|
| 105 |
+
# Create an explainer instance for classification
|
| 106 |
+
explainer = LimeTabularExplainer(
|
| 107 |
+
training_data=train.values, # Use your training data
|
| 108 |
+
feature_names=tabular_header,
|
| 109 |
+
class_names=['intermediate', 'High'], # Replace with actual class names
|
| 110 |
+
mode='classification'
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
# LIME expects a 2D numpy array or DataFrame for input, and we need to provide the correct number of features
|
| 114 |
+
explanation = explainer.explain_instance(
|
| 115 |
+
instance, # Single instance (first row of the tabular data)
|
| 116 |
+
predict_proba_fn, # The prediction function
|
| 117 |
+
num_features=len(tabular_header) # Number of features to display in the explanation
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# Plot LIME explanation
|
| 121 |
+
fig = explanation.as_pyplot_figure()
|
| 122 |
+
fig.set_size_inches(25, 8)
|
| 123 |
+
buf = io.BytesIO()
|
| 124 |
+
fig.savefig(buf, format='png')
|
| 125 |
+
buf.seek(0)
|
| 126 |
+
encoded_image = base64.b64encode(buf.read()).decode('utf-8')
|
| 127 |
+
buf.close()
|
| 128 |
+
plt.close(fig)
|
| 129 |
+
|
| 130 |
+
return f"<img src='data:image/png;base64,{encoded_image}'/>"
|
| 131 |
+
|
| 132 |
if __name__ == '__main__':
|
| 133 |
args = parse_args(sys.argv[1:])
|
| 134 |
train = load_data_and_prepare(args.data_dir, args.excel_file, args.mode, args.scale, args.smote)
|
|
|
|
| 166 |
lime_output = gr.HTML(label="LIME Explanation")
|
| 167 |
gr.Examples(examples=examples, inputs=[tabular_input, info])
|
| 168 |
btn.click(fn=classify, inputs=tabular_input, outputs=result_output)
|
| 169 |
+
btn.click(fn=explain_with_lime, inputs=tabular_input, outputs=lime_output) # Add LIME button
|
| 170 |
|
| 171 |
# Clear functionality: resets inputs and outputs
|
| 172 |
def clear_fields():
|