Commit
Β·
64eb9a6
1
Parent(s):
96f4763
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,9 @@ import numpy as np
|
|
| 7 |
from sklearn.model_selection import train_test_split
|
| 8 |
import gradio as gr
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
-
import
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
#Data preparation
|
|
@@ -35,6 +37,37 @@ def visualize_input_data():
|
|
| 35 |
# plt.show()
|
| 36 |
return fig
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
title = " An example using IsolationForest for anomaly detection."
|
| 39 |
|
| 40 |
with gr.Blocks(title=title) as demo:
|
|
@@ -47,8 +80,23 @@ with gr.Blocks(title=title) as demo:
|
|
| 47 |
|
| 48 |
btn = gr.Button(value="Visualize input dataset")
|
| 49 |
btn.click(visualize_input_data, outputs= gr.Plot(label='Visualizing input dataset') )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
gr.Markdown( f"##
|
| 54 |
demo.launch()
|
|
|
|
| 7 |
from sklearn.model_selection import train_test_split
|
| 8 |
import gradio as gr
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
+
from skops import hub_utils
|
| 11 |
+
import pickle
|
| 12 |
+
|
| 13 |
|
| 14 |
|
| 15 |
#Data preparation
|
|
|
|
| 37 |
# plt.show()
|
| 38 |
return fig
|
| 39 |
|
| 40 |
+
def download_model_skops():
|
| 41 |
+
repo_id="sklearn-docs/anomaly-detection"
|
| 42 |
+
download_repo = "downloaded-model"
|
| 43 |
+
hub_utils.download(repo_id=repo_id, dst=download_repo)
|
| 44 |
+
# repo_copy = mkdtemp(prefix="skops")
|
| 45 |
+
# hub_utils.download(repo_id=repo_id, dst=repo_copy, token=token)
|
| 46 |
+
# print(os.listdir(download_repo))
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
from sklearn.inspection import DecisionBoundaryDisplay
|
| 50 |
+
|
| 51 |
+
def plot_decision_boundary(classifier):
|
| 52 |
+
disp = DecisionBoundaryDisplay.from_estimator(
|
| 53 |
+
classifier,
|
| 54 |
+
X,
|
| 55 |
+
response_method="predict",
|
| 56 |
+
alpha=0.5,
|
| 57 |
+
)
|
| 58 |
+
fig = plt.figure(1, facecolor="w", figsize=(5, 5))
|
| 59 |
+
scatter = plt.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k")
|
| 60 |
+
disp.ax_.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k")
|
| 61 |
+
handles, labels = scatter.legend_elements()
|
| 62 |
+
disp.ax_.set_title("Binary decision boundary \nof IsolationForest")
|
| 63 |
+
plt.axis("square")
|
| 64 |
+
|
| 65 |
+
plt.legend(handles=handles, labels=["outliers", "inliers"], title="true class")
|
| 66 |
+
# plt.savefig('decision_boundary.png',dpi=300, bbox_inches = "tight")
|
| 67 |
+
return fig
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
title = " An example using IsolationForest for anomaly detection."
|
| 72 |
|
| 73 |
with gr.Blocks(title=title) as demo:
|
|
|
|
| 80 |
|
| 81 |
btn = gr.Button(value="Visualize input dataset")
|
| 82 |
btn.click(visualize_input_data, outputs= gr.Plot(label='Visualizing input dataset') )
|
| 83 |
+
# download
|
| 84 |
+
repo_id="sklearn-docs/anomaly-detection"
|
| 85 |
+
download_repo = "downloaded-model"
|
| 86 |
+
hub_utils.download(repo_id=repo_id, dst=download_repo)
|
| 87 |
+
if os.listdir(download_repo):
|
| 88 |
+
# hub_utils.download(repo_id=repo_id, dst=download_repo)
|
| 89 |
+
|
| 90 |
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# print("Empty directory")
|
| 94 |
+
|
| 95 |
+
# print(os.listdir(download_repo))
|
| 96 |
+
loaded_model = pickle.load(open('isolation_forest.pkl', 'rb'))
|
| 97 |
+
btn = gr.Button(value="Plot decision boundary")
|
| 98 |
+
btn.click(plot_decision_boundary, inputs=[loaded_model], outputs= gr.Plot(label='Visualizing input dataset') )
|
| 99 |
+
|
| 100 |
|
| 101 |
+
gr.Markdown( f"## Success")
|
| 102 |
demo.launch()
|