Update app.py
Browse files
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
|
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.
|
16 |
-
"""
|
17 |
-
Process time-series data and detect anomalies using MOMENT model
|
18 |
-
"""
|
19 |
try:
|
20 |
-
#
|
21 |
if isinstance(data_input, str):
|
22 |
-
|
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:
|
35 |
-
|
36 |
-
#
|
37 |
if 'timestamp' not in df.columns or 'value' not in df.columns:
|
38 |
-
return "Error:
|
39 |
-
|
40 |
-
# Convert timestamp
|
41 |
df['timestamp'] = pd.to_datetime(df['timestamp'])
|
42 |
df = df.sort_values('timestamp')
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
# Get reconstruction from the model
|
48 |
-
reconstruction = model.reconstruct(time_series)
|
49 |
|
50 |
-
#
|
51 |
-
|
|
|
52 |
|
53 |
-
#
|
54 |
-
|
55 |
-
df['
|
|
|
56 |
|
57 |
# Create plot
|
58 |
-
fig, ax = plt.subplots(figsize=(
|
59 |
-
ax.plot(df['timestamp'], df['value'], label='
|
60 |
ax.scatter(
|
61 |
-
df[df['is_anomaly']
|
62 |
-
df[df['is_anomaly']
|
63 |
-
color='red',
|
64 |
-
label='Anomaly'
|
65 |
)
|
66 |
-
ax.set_title('
|
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":
|
77 |
-
"
|
78 |
-
"
|
79 |
-
"threshold_used": threshold
|
80 |
}
|
81 |
|
82 |
-
return fig, stats, df.to_dict(
|
83 |
-
|
84 |
except Exception as e:
|
85 |
-
return f"Error
|
86 |
|
87 |
-
#
|
88 |
-
with gr.Blocks(
|
89 |
-
gr.Markdown("
|
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 |
-
|
100 |
-
label="Paste
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
111 |
)
|
112 |
-
|
113 |
-
|
|
|
114 |
with gr.Column():
|
115 |
-
plot_output = gr.Plot(
|
116 |
-
stats_output = gr.JSON(label="
|
117 |
-
data_output = gr.JSON(label="
|
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=[
|
131 |
outputs=[plot_output, stats_output, data_output]
|
132 |
)
|
133 |
|
134 |
-
|
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()
|
|