zainulabedin949 commited on
Commit
6f850cd
·
verified ·
1 Parent(s): 85d2de2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -0
app.py CHANGED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)