Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ logger = logging.getLogger(__name__)
|
|
| 14 |
try:
|
| 15 |
model = MOMENTPipeline.from_pretrained(
|
| 16 |
"AutonLab/MOMENT-1-large",
|
| 17 |
-
model_kwargs={"task_name": "reconstruction"},
|
| 18 |
)
|
| 19 |
model.init()
|
| 20 |
logger.info("Model loaded successfully")
|
|
@@ -58,7 +58,7 @@ def detect_anomalies(data_input, sensitivity=3.0):
|
|
| 58 |
values_3d = values.reshape(1, -1, 1)
|
| 59 |
|
| 60 |
# Get reconstruction
|
| 61 |
-
reconstructed = model.reconstruct(values_3d)
|
| 62 |
|
| 63 |
# Calculate reconstruction error (MAE)
|
| 64 |
errors = np.abs(values - reconstructed[0,:,0])
|
|
@@ -74,34 +74,40 @@ def detect_anomalies(data_input, sensitivity=3.0):
|
|
| 74 |
ax.scatter(
|
| 75 |
df.loc[df['is_anomaly'], 'timestamp'],
|
| 76 |
df.loc[df['is_anomaly'], 'value'],
|
| 77 |
-
color='red', s=100, label='Anomaly'
|
| 78 |
)
|
| 79 |
-
ax.set_title(
|
|
|
|
|
|
|
| 80 |
ax.legend()
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# Prepare outputs
|
| 83 |
stats = {
|
| 84 |
"data_points": len(df),
|
| 85 |
-
"
|
| 86 |
"detection_threshold": float(threshold),
|
| 87 |
-
"
|
|
|
|
| 88 |
}
|
| 89 |
|
| 90 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
logger.error(f"Detection error: {str(e)}")
|
| 94 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
|
| 98 |
-
gr.Markdown("## π Equipment Anomaly Detection using MOMENT")
|
| 99 |
-
|
| 100 |
-
with gr.Row():
|
| 101 |
-
with gr.Column():
|
| 102 |
-
data_input = gr.Textbox(
|
| 103 |
-
label="Paste time-series data (CSV format)",
|
| 104 |
-
value="""timestamp,value
|
| 105 |
2025-04-01 00:00:00,100
|
| 106 |
2025-04-01 01:00:00,102
|
| 107 |
2025-04-01 02:00:00,98
|
|
@@ -114,30 +120,41 @@ with gr.Blocks(title="MOMENT Anomaly Detector") as demo:
|
|
| 114 |
2025-04-01 09:00:00,98
|
| 115 |
2025-04-01 10:00:00,99
|
| 116 |
2025-04-01 11:00:00,102
|
| 117 |
-
2025-04-01 12:00:00,101"""
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
)
|
| 120 |
sensitivity = gr.Slider(
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
value=3.0,
|
| 124 |
-
step=0.1,
|
| 125 |
-
label="Detection Sensitivity (Z-Score)"
|
| 126 |
)
|
| 127 |
-
|
| 128 |
|
| 129 |
with gr.Column():
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
label="Processed
|
| 134 |
-
|
|
|
|
| 135 |
)
|
| 136 |
|
| 137 |
-
|
| 138 |
detect_anomalies,
|
| 139 |
inputs=[data_input, sensitivity],
|
| 140 |
-
outputs=[
|
| 141 |
)
|
| 142 |
|
| 143 |
if __name__ == "__main__":
|
|
|
|
| 14 |
try:
|
| 15 |
model = MOMENTPipeline.from_pretrained(
|
| 16 |
"AutonLab/MOMENT-1-large",
|
| 17 |
+
model_kwargs={"task_name": "reconstruction"},
|
| 18 |
)
|
| 19 |
model.init()
|
| 20 |
logger.info("Model loaded successfully")
|
|
|
|
| 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])
|
|
|
|
| 74 |
ax.scatter(
|
| 75 |
df.loc[df['is_anomaly'], 'timestamp'],
|
| 76 |
df.loc[df['is_anomaly'], 'value'],
|
| 77 |
+
color='red', s=100, label=f'Anomaly (>{threshold:.2f})'
|
| 78 |
)
|
| 79 |
+
ax.set_title('Sensor Data Anomaly Detection')
|
| 80 |
+
ax.set_xlabel('Timestamp')
|
| 81 |
+
ax.set_ylabel('Value')
|
| 82 |
ax.legend()
|
| 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 |
|
| 109 |
+
# Default data with clear anomaly
|
| 110 |
+
DEFAULT_DATA = """timestamp,value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
2025-04-01 00:00:00,100
|
| 112 |
2025-04-01 01:00:00,102
|
| 113 |
2025-04-01 02:00:00,98
|
|
|
|
| 120 |
2025-04-01 09:00:00,98
|
| 121 |
2025-04-01 10:00:00,99
|
| 122 |
2025-04-01 11:00:00,102
|
| 123 |
+
2025-04-01 12:00:00,101"""
|
| 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():
|
| 132 |
+
data_input = gr.Textbox(
|
| 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__":
|