DHEIVER commited on
Commit
60610b0
·
1 Parent(s): 754b6c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -21
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
  import pandas as pd
3
  import numpy as np
4
  import json
@@ -21,7 +22,6 @@ def normalize_data(data):
21
  df_test_value = (data - scaler["mean"]) / scaler["std"]
22
  return df_test_value
23
 
24
-
25
  def plot_test_data(df_test_value):
26
  fig, ax = plt.subplots(figsize=(12, 6))
27
  df_test_value.plot(legend=False, ax=ax)
@@ -30,13 +30,10 @@ def plot_test_data(df_test_value):
30
  ax.set_title("Input Test Data")
31
  return fig
32
 
33
-
34
  def get_anomalies(df_test_value):
35
  # Create sequences from test values.
36
  x_test = create_sequences(df_test_value.values)
37
-
38
- # Load the pre-trained model.
39
- model = load_pretrained_model()
40
 
41
  # Get test MAE loss.
42
  x_test_pred = model.predict(x_test)
@@ -47,12 +44,11 @@ def get_anomalies(df_test_value):
47
  anomalies = test_mae_loss > scaler["threshold"]
48
  return anomalies
49
 
50
-
51
  def plot_anomalies(df_test_value, data, anomalies):
52
  # data i is an anomaly if samples [(i - timesteps + 1) to (i)] are anomalies
53
  anomalous_data_indices = []
54
  for data_idx in range(TIME_STEPS - 1, len(df_test_value) - TIME_STEPS + 1):
55
- if np.all(anomalies[data_idx - TIME_STEPS + 1 : data_idx]):
56
  anomalous_data_indices.append(data_idx)
57
  df_subset = data.iloc[anomalous_data_indices]
58
  fig, ax = plt.subplots(figsize=(12, 6))
@@ -62,24 +58,18 @@ def plot_anomalies(df_test_value, data, anomalies):
62
  ax.set_ylabel("Value")
63
  ax.set_title("Anomalous Data Points")
64
  return fig
65
-
66
-
67
  def master(file):
68
- # Read file
69
  data = pd.read_csv(file, parse_dates=True, index_col="timestamp")
70
  df_test_value = normalize_data(data)
71
-
72
- # Plot input test data
73
  plot1 = plot_test_data(df_test_value)
74
-
75
- # Predict anomalies
76
  anomalies = get_anomalies(df_test_value)
77
-
78
- # Plot anomalous data points
79
  plot2 = plot_anomalies(df_test_value, data, anomalies)
80
-
81
- return plot1, plot2
82
-
83
 
84
  outputs = gr.outputs.Image()
85
 
@@ -92,5 +82,4 @@ iface = gr.Interface(
92
  description="Anomaly detection of timeseries data."
93
  )
94
 
95
- iface.launch()
96
-
 
1
  import gradio as gr
2
+ from huggingface_hub import from_pretrained_keras
3
  import pandas as pd
4
  import numpy as np
5
  import json
 
22
  df_test_value = (data - scaler["mean"]) / scaler["std"]
23
  return df_test_value
24
 
 
25
  def plot_test_data(df_test_value):
26
  fig, ax = plt.subplots(figsize=(12, 6))
27
  df_test_value.plot(legend=False, ax=ax)
 
30
  ax.set_title("Input Test Data")
31
  return fig
32
 
 
33
  def get_anomalies(df_test_value):
34
  # Create sequences from test values.
35
  x_test = create_sequences(df_test_value.values)
36
+ model = from_pretrained_keras("keras-io/timeseries-anomaly-detection")
 
 
37
 
38
  # Get test MAE loss.
39
  x_test_pred = model.predict(x_test)
 
44
  anomalies = test_mae_loss > scaler["threshold"]
45
  return anomalies
46
 
 
47
  def plot_anomalies(df_test_value, data, anomalies):
48
  # data i is an anomaly if samples [(i - timesteps + 1) to (i)] are anomalies
49
  anomalous_data_indices = []
50
  for data_idx in range(TIME_STEPS - 1, len(df_test_value) - TIME_STEPS + 1):
51
+ if np.all(anomalies[data_idx - TIME_STEPS + 1 : data_idx]):
52
  anomalous_data_indices.append(data_idx)
53
  df_subset = data.iloc[anomalous_data_indices]
54
  fig, ax = plt.subplots(figsize=(12, 6))
 
58
  ax.set_ylabel("Value")
59
  ax.set_title("Anomalous Data Points")
60
  return fig
61
+
 
62
  def master(file):
63
+ # read file
64
  data = pd.read_csv(file, parse_dates=True, index_col="timestamp")
65
  df_test_value = normalize_data(data)
66
+ # plot input test data
 
67
  plot1 = plot_test_data(df_test_value)
68
+ # predict
 
69
  anomalies = get_anomalies(df_test_value)
70
+ #plot anomalous data points
 
71
  plot2 = plot_anomalies(df_test_value, data, anomalies)
72
+ return plot2
 
 
73
 
74
  outputs = gr.outputs.Image()
75
 
 
82
  description="Anomaly detection of timeseries data."
83
  )
84
 
85
+ iface.launch()