zainulabedin949 commited on
Commit
57d61ff
Β·
verified Β·
1 Parent(s): 54b9385

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -25
app.py CHANGED
@@ -57,8 +57,8 @@ def detect_anomalies(data_input, sensitivity=3.0):
57
  # Reshape to 3D format (batch, sequence, features)
58
  values_3d = values.reshape(1, -1, 1)
59
 
60
- # Get reconstruction
61
- reconstructed = model.reconstruct(X=values_3d) # Explicit parameter name
62
 
63
  # Calculate reconstruction error (MAE)
64
  errors = np.abs(values - reconstructed[0,:,0])
@@ -83,26 +83,25 @@ def detect_anomalies(data_input, sensitivity=3.0):
83
  ax.grid(True)
84
  plt.tight_layout()
85
 
86
- # Prepare outputs
87
- stats = {
88
- "data_points": len(df),
89
- "anomalies_detected": int(df['is_anomaly'].sum()),
90
- "detection_threshold": float(threshold),
91
- "max_anomaly_score": float(np.max(errors)),
92
- "average_value": float(np.mean(values))
93
- }
94
 
95
  return (
96
  fig,
97
- gr.JSON(value=stats),
98
- gr.DataFrame(value=df[['timestamp', 'value', 'anomaly_score', 'is_anomaly']])
 
 
 
 
 
99
  )
100
 
101
  except Exception as e:
102
  logger.error(f"Detection error: {str(e)}")
103
  return (
104
  None,
105
- gr.JSON(value={"error": str(e)}),
106
  None
107
  )
108
 
@@ -124,8 +123,10 @@ DEFAULT_DATA = """timestamp,value
124
 
125
  # Gradio Interface
126
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
127
- gr.Markdown("""πŸ“ˆ **Equipment Anomaly Detection**
128
- Detect unusual patterns in sensor data using MOMENT-1-large model""")
 
 
129
 
130
  with gr.Row():
131
  with gr.Column():
@@ -133,28 +134,23 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
133
  label="Paste CSV Data",
134
  value=DEFAULT_DATA,
135
  lines=10,
136
- max_lines=15,
137
- placeholder="timestamp,value\n2025-..."
138
  )
139
  sensitivity = gr.Slider(
140
  1.0, 5.0, value=3.0, step=0.1,
141
  label="Detection Sensitivity (z-score)"
142
  )
143
- btn = gr.Button("Analyze", variant="primary")
144
 
145
  with gr.Column():
146
  plot = gr.Plot(label="Results")
147
  stats = gr.JSON(label="Detection Statistics")
148
- results_df = gr.DataFrame(
149
- label="Processed Results",
150
- headers=["timestamp", "value", "anomaly_score", "is_anomaly"],
151
- max_rows=10
152
- )
153
 
154
- btn.click(
155
  detect_anomalies,
156
  inputs=[data_input, sensitivity],
157
- outputs=[plot, stats, results_df]
158
  )
159
 
160
  if __name__ == "__main__":
 
57
  # Reshape to 3D format (batch, sequence, features)
58
  values_3d = values.reshape(1, -1, 1)
59
 
60
+ # Get reconstruction - using explicit parameter name
61
+ reconstructed = model.reconstruct(X=values_3d)
62
 
63
  # Calculate reconstruction error (MAE)
64
  errors = np.abs(values - reconstructed[0,:,0])
 
83
  ax.grid(True)
84
  plt.tight_layout()
85
 
86
+ # Limit DataFrame display size
87
+ display_df = df[['timestamp', 'value', 'anomaly_score', 'is_anomaly']].head(20)
 
 
 
 
 
 
88
 
89
  return (
90
  fig,
91
+ {"statistics": {
92
+ "data_points": len(df),
93
+ "anomalies_detected": int(df['is_anomaly'].sum()),
94
+ "detection_threshold": float(threshold),
95
+ "max_anomaly_score": float(np.max(errors))
96
+ }},
97
+ display_df.to_dict('records')
98
  )
99
 
100
  except Exception as e:
101
  logger.error(f"Detection error: {str(e)}")
102
  return (
103
  None,
104
+ {"error": str(e)},
105
  None
106
  )
107
 
 
123
 
124
  # Gradio Interface
125
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
126
+ gr.Markdown("""
127
+ # πŸ“ˆ Equipment Anomaly Detection
128
+ Detect unusual patterns in sensor data using MOMENT-1-large model
129
+ """)
130
 
131
  with gr.Row():
132
  with gr.Column():
 
134
  label="Paste CSV Data",
135
  value=DEFAULT_DATA,
136
  lines=10,
137
+ placeholder="timestamp,value\n2025-01-01, 100\n2025-01-02, 105..."
 
138
  )
139
  sensitivity = gr.Slider(
140
  1.0, 5.0, value=3.0, step=0.1,
141
  label="Detection Sensitivity (z-score)"
142
  )
143
+ analyze_btn = gr.Button("Analyze", variant="primary")
144
 
145
  with gr.Column():
146
  plot = gr.Plot(label="Results")
147
  stats = gr.JSON(label="Detection Statistics")
148
+ results = gr.JSON(label="Top 20 Records")
 
 
 
 
149
 
150
+ analyze_btn.click(
151
  detect_anomalies,
152
  inputs=[data_input, sensitivity],
153
+ outputs=[plot, stats, results]
154
  )
155
 
156
  if __name__ == "__main__":