zainulabedin949 commited on
Commit
6646cc2
·
verified ·
1 Parent(s): 591a49c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -90
app.py CHANGED
@@ -1,135 +1,105 @@
1
  import gradio as gr
2
- import numpy as np
3
  import pandas as pd
 
4
  from momentfm import MOMENTPipeline
5
  import matplotlib.pyplot as plt
6
  from io import StringIO
7
 
8
- # Initialize the MOMENT model
9
  model = MOMENTPipeline.from_pretrained(
10
  "AutonLab/MOMENT-1-large",
11
  model_kwargs={"task_name": "reconstruction"},
12
  )
13
  model.init()
14
 
15
- def detect_anomalies(data_input, threshold=0.05):
16
- """
17
- Process time-series data and detect anomalies using MOMENT model
18
- """
19
  try:
20
- # Handle different input types
21
  if isinstance(data_input, str):
22
- # Try to read as CSV
23
- try:
24
- df = pd.read_csv(StringIO(data_input))
25
- except:
26
- # Try to read as JSON
27
- try:
28
- df = pd.read_json(StringIO(data_input))
29
- except:
30
- return "Error: Could not parse input data. Please provide valid CSV or JSON."
31
- elif isinstance(data_input, dict):
32
- df = pd.DataFrame(data_input)
33
  else:
34
- return "Error: Unsupported input format"
35
-
36
- # Check for required columns
37
  if 'timestamp' not in df.columns or 'value' not in df.columns:
38
- return "Error: Data must contain 'timestamp' and 'value' columns"
39
-
40
- # Convert timestamp to datetime if needed
41
  df['timestamp'] = pd.to_datetime(df['timestamp'])
42
  df = df.sort_values('timestamp')
43
 
44
- # Prepare data for MOMENT model
45
- time_series = df['value'].values.astype(float)
46
-
47
- # Get reconstruction from the model
48
- reconstruction = model.reconstruct(time_series)
49
 
50
- # Calculate reconstruction error
51
- error = np.abs(time_series - reconstruction)
 
52
 
53
- # Detect anomalies based on threshold
54
- df['anomaly_score'] = error
55
- df['is_anomaly'] = error > threshold * np.max(error)
 
56
 
57
  # Create plot
58
- fig, ax = plt.subplots(figsize=(12, 6))
59
- ax.plot(df['timestamp'], df['value'], label='Original', color='blue')
60
  ax.scatter(
61
- df[df['is_anomaly']]['timestamp'],
62
- df[df['is_anomaly']]['value'],
63
- color='red',
64
- label='Anomaly'
65
  )
66
- ax.set_title('Time Series with Anomalies Detected')
67
- ax.set_xlabel('Timestamp')
68
- ax.set_ylabel('Value')
69
  ax.legend()
70
- ax.grid(True)
71
 
72
  # Prepare results
73
- anomalies = df[df['is_anomaly']]
74
  stats = {
75
  "total_points": len(df),
76
- "anomalies_detected": len(anomalies),
77
- "anomaly_percentage": f"{100 * len(anomalies)/len(df):.2f}%",
78
- "max_anomaly_score": np.max(error),
79
- "threshold_used": threshold
80
  }
81
 
82
- return fig, stats, df.to_dict(orient='records')
83
-
84
  except Exception as e:
85
- return f"Error processing data: {str(e)}"
86
 
87
- # Create Gradio interface
88
- with gr.Blocks(title="Equipment Anomaly Detection") as demo:
89
- gr.Markdown("# 🛠️ Equipment Sensor Anomaly Detection")
90
- gr.Markdown("""
91
- **Detect anomalies in equipment sensor data using the MOMENT-1-large model**
92
- - Upload CSV/JSON data with 'timestamp' and 'value' columns
93
- - Adjust the sensitivity threshold as needed
94
- - Get visual and statistical results
95
- """)
96
 
97
  with gr.Row():
98
  with gr.Column():
99
- input_data = gr.Textbox(
100
- label="Paste your time-series data (CSV/JSON)",
101
- placeholder="timestamp,value\n2023-01-01,1.2\n2023-01-02,1.5...",
102
- lines=5
103
- )
104
- file_upload = gr.File(label="Or upload a file")
105
- threshold = gr.Slider(
106
- minimum=0.01,
107
- maximum=0.2,
108
- value=0.05,
109
- step=0.01,
110
- label="Anomaly Detection Sensitivity (lower = more sensitive)"
 
 
 
 
 
111
  )
112
- submit_btn = gr.Button("Detect Anomalies", variant="primary")
113
-
 
114
  with gr.Column():
115
- plot_output = gr.Plot(label="Anomaly Detection Results")
116
- stats_output = gr.JSON(label="Detection Statistics")
117
- data_output = gr.JSON(label="Processed Data with Anomaly Scores")
118
-
119
- # Handle file upload
120
- def process_file(file):
121
- if file:
122
- with open(file.name, 'r') as f:
123
- return f.read()
124
- return ""
125
-
126
- file_upload.change(process_file, inputs=file_upload, outputs=input_data)
127
 
128
  submit_btn.click(
129
  detect_anomalies,
130
- inputs=[input_data, threshold],
131
  outputs=[plot_output, stats_output, data_output]
132
  )
133
 
134
- if __name__ == "__main__":
135
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
 
2
  import pandas as pd
3
+ import numpy as np
4
  from momentfm import MOMENTPipeline
5
  import matplotlib.pyplot as plt
6
  from io import StringIO
7
 
8
+ # Initialize model
9
  model = MOMENTPipeline.from_pretrained(
10
  "AutonLab/MOMENT-1-large",
11
  model_kwargs={"task_name": "reconstruction"},
12
  )
13
  model.init()
14
 
15
+ def detect_anomalies(data_input, threshold=0.1):
 
 
 
16
  try:
17
+ # Read data
18
  if isinstance(data_input, str):
19
+ df = pd.read_csv(StringIO(data_input))
 
 
 
 
 
 
 
 
 
 
20
  else:
21
+ return "Error: Please provide CSV data"
22
+
23
+ # Validate columns
24
  if 'timestamp' not in df.columns or 'value' not in df.columns:
25
+ return "Error: CSV must contain 'timestamp' and 'value' columns", None, None
26
+
27
+ # Convert timestamp and sort
28
  df['timestamp'] = pd.to_datetime(df['timestamp'])
29
  df = df.sort_values('timestamp')
30
 
31
+ # Get values as numpy array
32
+ values = df['value'].values.astype(float)
 
 
 
33
 
34
+ # Detect anomalies
35
+ reconstruction = model.reconstruct(values)
36
+ errors = np.abs(values - reconstruction)
37
 
38
+ # Apply threshold (using relative error)
39
+ threshold_value = threshold * np.max(errors)
40
+ df['anomaly_score'] = errors
41
+ df['is_anomaly'] = errors > threshold_value
42
 
43
  # Create plot
44
+ fig, ax = plt.subplots(figsize=(10, 4))
45
+ ax.plot(df['timestamp'], df['value'], label='Value', color='blue')
46
  ax.scatter(
47
+ df.loc[df['is_anomaly'], 'timestamp'],
48
+ df.loc[df['is_anomaly'], 'value'],
49
+ color='red', label='Anomaly'
 
50
  )
51
+ ax.set_title('Sensor Data with Anomalies')
 
 
52
  ax.legend()
 
53
 
54
  # Prepare results
 
55
  stats = {
56
  "total_points": len(df),
57
+ "anomalies_detected": sum(df['is_anomaly']),
58
+ "max_anomaly_score": float(np.max(errors)),
59
+ "threshold_used": float(threshold_value)
 
60
  }
61
 
62
+ return fig, stats, df.to_dict('records')
63
+
64
  except Exception as e:
65
+ return f"Error: {str(e)}", None, None
66
 
67
+ # Gradio interface
68
+ with gr.Blocks() as demo:
69
+ gr.Markdown("## 🛠️ Equipment Anomaly Detection")
 
 
 
 
 
 
70
 
71
  with gr.Row():
72
  with gr.Column():
73
+ data_input = gr.Textbox(
74
+ label="Paste CSV data (timestamp,value)",
75
+ value="""timestamp,value
76
+ 2025-04-01 00:00:00,100
77
+ 2025-04-01 01:00:00,102
78
+ 2025-04-01 02:00:00,98
79
+ 2025-04-01 03:00:00,105
80
+ 2025-04-01 04:00:00,103
81
+ 2025-04-01 05:00:00,107
82
+ 2025-04-01 06:00:00,200
83
+ 2025-04-01 07:00:00,108
84
+ 2025-04-01 08:00:00,110
85
+ 2025-04-01 09:00:00,98
86
+ 2025-04-01 10:00:00,99
87
+ 2025-04-01 11:00:00,102
88
+ 2025-04-01 12:00:00,101""",
89
+ lines=10
90
  )
91
+ threshold = gr.Slider(0.01, 0.5, value=0.1, label="Anomaly Threshold")
92
+ submit_btn = gr.Button("Detect Anomalies")
93
+
94
  with gr.Column():
95
+ plot_output = gr.Plot()
96
+ stats_output = gr.JSON(label="Statistics")
97
+ data_output = gr.JSON(label="Detailed Results")
 
 
 
 
 
 
 
 
 
98
 
99
  submit_btn.click(
100
  detect_anomalies,
101
+ inputs=[data_input, threshold],
102
  outputs=[plot_output, stats_output, data_output]
103
  )
104
 
105
+ demo.launch()