Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,106 +1,64 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import numpy as np
|
4 |
-
import matplotlib.pyplot as plt
|
5 |
-
from io import StringIO
|
6 |
-
from momentfm import MOMENTPipeline
|
7 |
-
import logging
|
8 |
-
|
9 |
-
# Configure logging
|
10 |
-
logging.basicConfig(level=logging.INFO)
|
11 |
-
logger = logging.getLogger(__name__)
|
12 |
-
|
13 |
-
# Initialize model with reconstruction task
|
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")
|
21 |
-
except Exception as e:
|
22 |
-
logger.error(f"Model loading failed: {str(e)}")
|
23 |
-
raise
|
24 |
-
|
25 |
-
def validate_data(data_input):
|
26 |
-
"""Validate and process input data"""
|
27 |
try:
|
|
|
28 |
df = pd.read_csv(StringIO(data_input))
|
29 |
-
|
30 |
-
# Validate columns
|
31 |
-
if not all(col in df.columns for col in ['timestamp', 'value']):
|
32 |
-
raise ValueError("CSV must contain timestamp and value columns")
|
33 |
-
|
34 |
-
# Convert and validate data
|
35 |
df['timestamp'] = pd.to_datetime(df['timestamp'])
|
36 |
df['value'] = pd.to_numeric(df['value'])
|
37 |
-
df = df.sort_values('timestamp')
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
except Exception as e:
|
42 |
-
logger.error(f"Data validation error: {str(e)}")
|
43 |
-
raise ValueError(f"Invalid data format: {str(e)}")
|
44 |
-
|
45 |
-
def detect_anomalies(data_input, sensitivity=3.0):
|
46 |
-
"""Perform reconstruction-based anomaly detection"""
|
47 |
-
try:
|
48 |
-
df = validate_data(data_input)
|
49 |
-
values = df['value'].values.astype(np.float32)
|
50 |
-
|
51 |
-
# Reshape to 3D format expected by MOMENT
|
52 |
-
values_3d = values.reshape(1, -1, 1)
|
53 |
|
54 |
# Get reconstruction
|
55 |
-
reconstructed = model.reconstruct(
|
56 |
-
errors = np.abs(values - reconstructed[0,:,0])
|
57 |
|
58 |
-
#
|
59 |
median = np.median(errors)
|
60 |
mad = np.median(np.abs(errors - median))
|
61 |
threshold = median + sensitivity * (1.4826 * mad)
|
62 |
|
63 |
-
#
|
64 |
-
df[
|
65 |
-
|
66 |
-
|
67 |
-
# Create plot
|
68 |
-
fig, ax = plt.subplots(figsize=(12, 5))
|
69 |
-
ax.plot(df['timestamp'], df['value'], 'b-', label='Value')
|
70 |
-
ax.scatter(
|
71 |
-
df.loc[df['is_anomaly'], 'timestamp'],
|
72 |
-
df.loc[df['is_anomaly'], 'value'],
|
73 |
-
color='red', s=100, label=f'Anomaly (score > {threshold:.2f})'
|
74 |
-
)
|
75 |
-
ax.set_title('Sensor Data with Anomalies Detected')
|
76 |
-
ax.set_xlabel('Timestamp')
|
77 |
-
ax.set_ylabel('Value')
|
78 |
-
ax.legend()
|
79 |
-
ax.grid(True)
|
80 |
-
plt.tight_layout()
|
81 |
|
82 |
-
#
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
except Exception as e:
|
98 |
-
|
99 |
-
logger.error(f"Detection error: {error_msg}")
|
100 |
-
return None, {"error": error_msg}, None
|
101 |
|
102 |
-
#
|
103 |
-
|
|
|
104 |
2025-04-01 00:00:00,100
|
105 |
2025-04-01 01:00:00,102
|
106 |
2025-04-01 02:00:00,98
|
@@ -113,39 +71,7 @@ DEFAULT_DATA = """timestamp,value
|
|
113 |
2025-04-01 09:00:00,98
|
114 |
2025-04-01 10:00:00,99
|
115 |
2025-04-01 11:00:00,102
|
116 |
-
2025-04-01 12:00:00,101
|
117 |
-
|
118 |
-
# Create Gradio interface
|
119 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
120 |
-
gr.Markdown("""
|
121 |
-
# 🏭 Equipment Anomaly Detection
|
122 |
-
### Using MOMENT-1-large foundation model
|
123 |
-
""")
|
124 |
-
|
125 |
-
with gr.Row():
|
126 |
-
with gr.Column():
|
127 |
-
input_data = gr.Textbox(
|
128 |
-
label="Enter CSV Data",
|
129 |
-
value=DEFAULT_DATA,
|
130 |
-
lines=10,
|
131 |
-
placeholder="timestamp,value\n2025-01-01 00:00:00,100\n..."
|
132 |
-
)
|
133 |
-
sensitivity = gr.Slider(
|
134 |
-
1.0, 5.0, value=3.0, step=0.1,
|
135 |
-
label="Detection Sensitivity"
|
136 |
-
)
|
137 |
-
analyze_btn = gr.Button("Detect Anomalies!", variant="primary")
|
138 |
-
|
139 |
-
with gr.Column():
|
140 |
-
plot_output = gr.Plot(label="Detection Results")
|
141 |
-
stats_output = gr.JSON(label="Statistics Summary")
|
142 |
-
records_output = gr.JSON(label="Sample Records (First 20)")
|
143 |
-
|
144 |
-
analyze_btn.click(
|
145 |
-
detect_anomalies,
|
146 |
-
inputs=[input_data, sensitivity],
|
147 |
-
outputs=[plot_output, stats_output, records_output]
|
148 |
-
)
|
149 |
|
150 |
-
|
151 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
1 |
+
def generate_analysis_report(data_input, sensitivity=3.0):
|
2 |
+
"""Generate a textual analysis report without visual outputs"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
try:
|
4 |
+
# Process and validate data
|
5 |
df = pd.read_csv(StringIO(data_input))
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
df['timestamp'] = pd.to_datetime(df['timestamp'])
|
7 |
df['value'] = pd.to_numeric(df['value'])
|
8 |
+
df = df.sort_values('timestamp')
|
9 |
|
10 |
+
# Prepare data for model
|
11 |
+
values = df['value'].values.astype(np.float32).reshape(1, -1, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Get reconstruction
|
14 |
+
reconstructed = model.reconstruct(values)
|
15 |
+
errors = np.abs(df['value'].values - reconstructed[0,:,0])
|
16 |
|
17 |
+
# Calculate threshold (modified z-score)
|
18 |
median = np.median(errors)
|
19 |
mad = np.median(np.abs(errors - median))
|
20 |
threshold = median + sensitivity * (1.4826 * mad)
|
21 |
|
22 |
+
# Identify anomalies
|
23 |
+
anomalies = df[errors > threshold]
|
24 |
+
normal_points = df[errors <= threshold]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
# Generate report
|
27 |
+
report = f"""
|
28 |
+
EQUIPMENT ANALYSIS REPORT
|
29 |
+
========================
|
30 |
+
|
31 |
+
Basic Statistics:
|
32 |
+
- Total data points: {len(df)}
|
33 |
+
- Time period covered: {df['timestamp'].min()} to {df['timestamp'].max()}
|
34 |
+
- Value range: {df['value'].min():.2f} to {df['value'].max():.2f}
|
35 |
+
- Median value: {df['value'].median():.2f}
|
36 |
+
|
37 |
+
Anomaly Detection Results:
|
38 |
+
- Detection threshold: {threshold:.2f}
|
39 |
+
- Anomalies detected: {len(anomalies)} ({len(anomalies)/len(df):.1%} of data)
|
40 |
+
- Strongest anomaly: {errors.max():.2f} at {df.loc[errors.argmax(), 'timestamp']}
|
41 |
+
|
42 |
+
Anomaly Details:
|
43 |
+
{anomalies.to_string(index=False)}
|
44 |
+
|
45 |
+
Normal Operation Summary:
|
46 |
+
- Typical value range: {normal_points['value'].min():.2f} to {normal_points['value'].max():.2f}
|
47 |
+
- Stable period duration: {pd.Timedelta(normal_points['timestamp'].max() - normal_points['timestamp'].min())}
|
48 |
+
|
49 |
+
Recommendations:
|
50 |
+
1. Investigate {len(anomalies)} anomalous readings
|
51 |
+
2. Focus on period around {anomalies['timestamp'].iloc[0]} for root cause analysis
|
52 |
+
3. Consider recalibration if anomalies persist
|
53 |
+
"""
|
54 |
+
return report
|
55 |
|
56 |
except Exception as e:
|
57 |
+
return f"Analysis failed: {str(e)}"
|
|
|
|
|
58 |
|
59 |
+
# Example usage:
|
60 |
+
report = generate_analysis_report("""
|
61 |
+
timestamp,value
|
62 |
2025-04-01 00:00:00,100
|
63 |
2025-04-01 01:00:00,102
|
64 |
2025-04-01 02:00:00,98
|
|
|
71 |
2025-04-01 09:00:00,98
|
72 |
2025-04-01 10:00:00,99
|
73 |
2025-04-01 11:00:00,102
|
74 |
+
2025-04-01 12:00:00,101
|
75 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
print(report)
|
|