Jayabalambika commited on
Commit
6c5daeb
Β·
1 Parent(s): a1fddda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -55
app.py CHANGED
@@ -27,6 +27,10 @@ y = np.concatenate(
27
  )
28
 
29
  def load_hf_model_hub():
 
 
 
 
30
  repo_id="sklearn-docs/anomaly-detection"
31
  download_repo = "downloaded-model"
32
  hub_utils.download(repo_id=repo_id, dst=download_repo)
@@ -50,77 +54,34 @@ def visualize_input_data():
50
 
51
 
52
 
53
- from sklearn.inspection import DecisionBoundaryDisplay
54
-
55
- def plot_decision_boundary():
56
- # progress(0, desc="Starting...")
57
- # plt.clear()
58
- plt.clf()
59
- time.sleep(1)
60
-
61
- disp = DecisionBoundaryDisplay.from_estimator(
62
- loaded_model,
63
- X,
64
- response_method="predict",
65
- alpha=0.5,
66
- )
67
- fig1 = plt.figure(1, facecolor="w", figsize=(5, 5))
68
- scatter = plt.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k")
69
- # disp.ax_.
70
- disp.ax_.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k")
71
- handles, labels = scatter.legend_elements()
72
- disp.ax_.set_title("Binary decision boundary \nof IsolationForest")
73
- plt.axis("square")
74
-
75
- plt.legend(handles=handles, labels=["outliers", "inliers"], title="true class")
76
- # plt.savefig('decision_boundary.png',dpi=300, bbox_inches = "tight")
77
-
78
- return fig1
79
-
80
- def plot_path_length():
81
- plt.clf()
82
-
83
- time.sleep(1)
84
- disp = DecisionBoundaryDisplay.from_estimator(
85
- loaded_model,
86
- X,
87
- response_method="decision_function",
88
- alpha=0.5,
89
- )
90
- fig2 = plt.figure(1, facecolor="w", figsize=(5, 5))
91
- scatter = disp.ax_.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k")
92
- handles, labels = scatter.legend_elements()
93
- disp.ax_.set_title("Path length decision boundary \nof IsolationForest")
94
- plt.axis("square")
95
- plt.legend(handles=handles, labels=["outliers", "inliers"], title="true class")
96
- plt.colorbar(disp.ax_.collections[1])
97
- # plt.savefig('plot_path.png',dpi=300, bbox_inches = "tight")
98
- return fig2
99
-
100
 
101
 
102
  title = " An example using IsolationForest for anomaly detection."
 
 
 
103
 
104
  with gr.Blocks(title=title) as demo:
105
- gr.Markdown(f"# {title}")
106
 
 
 
 
 
107
 
108
  gr.Markdown(" **https://scikit-learn.org/stable/auto_examples/ensemble/plot_isolation_forest.html#sphx-glr-auto-examples-ensemble-plot-isolation-forest-py**")
109
 
110
  loaded_model = load_hf_model_hub()
111
 
112
  with gr.Tab("Visualize Input dataset"):
113
- btn = gr.Button(value="Visualize input dataset")
114
- btn.click(visualize_input_data, outputs= gr.Plot(label='Visualizing input dataset') )
115
 
116
  with gr.Tab("Plot Decision Boundary"):
117
- btn_decision = gr.Button(value="Plot decision boundary")
118
- btn_decision.click(plot_decision_boundary, outputs= gr.Plot(label='Plot decision boundary') )
119
 
120
  with gr.Tab("Plot Path"):
121
- btn_path = gr.Button(value="Path length decision boundary")
122
- btn_path.click(plot_path_length, outputs= gr.Plot(label='Path length decision boundary') )
123
-
124
 
125
  gr.Markdown( f"## Success")
126
  demo.launch()
 
27
  )
28
 
29
  def load_hf_model_hub():
30
+ '''
31
+ Load the directory containing pretrained model
32
+ and files from the model repository
33
+ '''
34
  repo_id="sklearn-docs/anomaly-detection"
35
  download_repo = "downloaded-model"
36
  hub_utils.download(repo_id=repo_id, dst=download_repo)
 
54
 
55
 
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
 
59
  title = " An example using IsolationForest for anomaly detection."
60
+ description1 = "The isolation forest is an Ensemble of Isolation trees and it isolates the datapoints using recursive random partitioning."
61
+ description2 = "In case of outliers the number of splits required is greater than those required for inliers."
62
+ description3 = "We will use the toy dataset as given in the scikit-learn page for Isolation Forest."
63
 
64
  with gr.Blocks(title=title) as demo:
 
65
 
66
+ gr.Markdown(f"# {title}")
67
+ gr.Markdown(f"# {description1}")
68
+ gr.Markdown(f"# {description2}")
69
+ gr.Markdown(f"# {description3}")
70
 
71
  gr.Markdown(" **https://scikit-learn.org/stable/auto_examples/ensemble/plot_isolation_forest.html#sphx-glr-auto-examples-ensemble-plot-isolation-forest-py**")
72
 
73
  loaded_model = load_hf_model_hub()
74
 
75
  with gr.Tab("Visualize Input dataset"):
76
+ btn = gr.Button(value="Visualize input dataset")
77
+ btn.click(visualize_input_data, outputs= gr.Plot(label='Visualizing input dataset') )
78
 
79
  with gr.Tab("Plot Decision Boundary"):
80
+ image_decision = gr.Image('./downloaded-model/decision_boundary.png')
 
81
 
82
  with gr.Tab("Plot Path"):
83
+ image_path = gr.Image('./downloaded-model/plot_path.png')
84
+
 
85
 
86
  gr.Markdown( f"## Success")
87
  demo.launch()