Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,9 @@ import pandas as pd
|
|
| 2 |
import numpy as np
|
| 3 |
from momentfm import MOMENTPipeline
|
| 4 |
from io import StringIO
|
|
|
|
| 5 |
|
| 6 |
-
# Initialize model
|
| 7 |
model = MOMENTPipeline.from_pretrained(
|
| 8 |
"AutonLab/MOMENT-1-large",
|
| 9 |
model_kwargs={"task_name": "reconstruction"},
|
|
@@ -11,95 +12,85 @@ model = MOMENTPipeline.from_pretrained(
|
|
| 11 |
model.init()
|
| 12 |
|
| 13 |
def generate_analysis_report(data_input, sensitivity=3.0):
|
| 14 |
-
"""Generate
|
| 15 |
try:
|
| 16 |
-
# Process and validate data
|
| 17 |
df = pd.read_csv(StringIO(data_input))
|
| 18 |
|
| 19 |
-
# Validate columns
|
| 20 |
if 'timestamp' not in df.columns or 'value' not in df.columns:
|
| 21 |
return "Error: CSV must contain 'timestamp' and 'value' columns"
|
| 22 |
|
| 23 |
-
# Convert data types
|
| 24 |
df['timestamp'] = pd.to_datetime(df['timestamp'], errors='coerce')
|
| 25 |
df['value'] = pd.to_numeric(df['value'], errors='coerce')
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
return "Error: Invalid data format (check timestamp/value formats)"
|
| 30 |
|
| 31 |
-
df = df.sort_values('timestamp')
|
| 32 |
|
| 33 |
-
# Prepare data for model
|
| 34 |
-
values = df['value'].values.astype(np.float32)
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
reconstructed = model.reconstruct(
|
| 38 |
-
errors = np.abs(df['value'].values - reconstructed[0,:,0])
|
| 39 |
|
| 40 |
-
# Calculate
|
|
|
|
| 41 |
median = np.median(errors)
|
| 42 |
mad = np.median(np.abs(errors - median))
|
| 43 |
threshold = median + sensitivity * (1.4826 * mad)
|
| 44 |
|
| 45 |
# Identify anomalies
|
| 46 |
-
anomalies = df
|
| 47 |
-
anomalies['anomaly_score'] = errors
|
| 48 |
-
anomalies = anomalies.sort_values('anomaly_score', ascending=False)
|
| 49 |
-
|
| 50 |
-
normal_points = df[errors <= threshold]
|
| 51 |
|
| 52 |
# Generate report
|
| 53 |
report = f"""
|
| 54 |
EQUIPMENT ANALYSIS REPORT
|
| 55 |
========================
|
| 56 |
-
Generated
|
| 57 |
-
|
| 58 |
|
| 59 |
-
DATA
|
| 60 |
-
|
| 61 |
Time period: {df['timestamp'].min()} to {df['timestamp'].max()}
|
| 62 |
-
|
| 63 |
Value range: {df['value'].min():.2f} to {df['value'].max():.2f}
|
| 64 |
Median value: {df['value'].median():.2f}
|
| 65 |
-
Mean value: {df['value'].mean():.2f}
|
| 66 |
|
| 67 |
-
ANOMALY
|
| 68 |
-
|
| 69 |
Detection threshold: {threshold:.2f}
|
| 70 |
-
Anomalies
|
| 71 |
-
|
| 72 |
|
| 73 |
TOP ANOMALIES
|
| 74 |
-------------
|
| 75 |
-
{anomalies[['timestamp', 'value', 'anomaly_score']].head(
|
| 76 |
-
|
| 77 |
-
NORMAL OPERATION SUMMARY
|
| 78 |
-
------------------------
|
| 79 |
-
Typical value range: {normal_points['value'].min():.2f} to {normal_points['value'].max():.2f}
|
| 80 |
-
Stable period duration: {pd.Timedelta(normal_points['timestamp'].max() - normal_points['timestamp'].min())}
|
| 81 |
|
| 82 |
RECOMMENDATIONS
|
| 83 |
---------------
|
| 84 |
-
1. Investigate top
|
| 85 |
-
2. Check
|
| 86 |
-
3. Consider recalibration if anomalies
|
| 87 |
-
4. Review
|
| 88 |
"""
|
| 89 |
return report.strip()
|
| 90 |
|
| 91 |
except Exception as e:
|
| 92 |
-
return f"ANALYSIS
|
| 93 |
|
| 94 |
-
# Gradio Interface
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
with gr.Blocks() as demo:
|
| 98 |
-
gr.Markdown("## 📄 Equipment Analysis Report Generator")
|
| 99 |
|
| 100 |
with gr.Row():
|
| 101 |
with gr.Column():
|
| 102 |
-
data_input = gr.Textbox(
|
|
|
|
|
|
|
| 103 |
2025-04-01 00:00:00,100
|
| 104 |
2025-04-01 01:00:00,102
|
| 105 |
2025-04-01 02:00:00,98
|
|
@@ -112,12 +103,18 @@ with gr.Blocks() as demo:
|
|
| 112 |
2025-04-01 09:00:00,98
|
| 113 |
2025-04-01 10:00:00,99
|
| 114 |
2025-04-01 11:00:00,102
|
| 115 |
-
2025-04-01 12:00:00,101"""
|
| 116 |
-
|
|
|
|
|
|
|
| 117 |
submit_btn = gr.Button("Generate Report", variant="primary")
|
| 118 |
-
|
| 119 |
with gr.Column():
|
| 120 |
-
report_output = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
submit_btn.click(
|
| 123 |
generate_analysis_report,
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from momentfm import MOMENTPipeline
|
| 4 |
from io import StringIO
|
| 5 |
+
import gradio as gr
|
| 6 |
|
| 7 |
+
# Initialize model with proper configuration
|
| 8 |
model = MOMENTPipeline.from_pretrained(
|
| 9 |
"AutonLab/MOMENT-1-large",
|
| 10 |
model_kwargs={"task_name": "reconstruction"},
|
|
|
|
| 12 |
model.init()
|
| 13 |
|
| 14 |
def generate_analysis_report(data_input, sensitivity=3.0):
|
| 15 |
+
"""Generate comprehensive textual analysis report"""
|
| 16 |
try:
|
| 17 |
+
# Process and validate input data
|
| 18 |
df = pd.read_csv(StringIO(data_input))
|
| 19 |
|
|
|
|
| 20 |
if 'timestamp' not in df.columns or 'value' not in df.columns:
|
| 21 |
return "Error: CSV must contain 'timestamp' and 'value' columns"
|
| 22 |
|
|
|
|
| 23 |
df['timestamp'] = pd.to_datetime(df['timestamp'], errors='coerce')
|
| 24 |
df['value'] = pd.to_numeric(df['value'], errors='coerce')
|
| 25 |
|
| 26 |
+
if df.isnull().values.any():
|
| 27 |
+
return "Error: Invalid data in timestamp or value columns"
|
|
|
|
| 28 |
|
| 29 |
+
df = df.sort_values('timestamp').dropna()
|
| 30 |
|
| 31 |
+
# Prepare data for model (3D array format)
|
| 32 |
+
values = df['value'].values.astype(np.float32)
|
| 33 |
+
values_3d = values.reshape(1, -1, 1) # Reshape to [batch, sequence, features]
|
| 34 |
|
| 35 |
+
# Correct reconstruction call with proper parameter
|
| 36 |
+
reconstructed = model.reconstruct(X=values_3d) # Using named parameter
|
|
|
|
| 37 |
|
| 38 |
+
# Calculate errors and detect anomalies
|
| 39 |
+
errors = np.abs(values - reconstructed[0,:,0])
|
| 40 |
median = np.median(errors)
|
| 41 |
mad = np.median(np.abs(errors - median))
|
| 42 |
threshold = median + sensitivity * (1.4826 * mad)
|
| 43 |
|
| 44 |
# Identify anomalies
|
| 45 |
+
anomalies = df.copy()
|
| 46 |
+
anomalies['anomaly_score'] = errors
|
| 47 |
+
anomalies = anomalies[errors > threshold].sort_values('anomaly_score', ascending=False)
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Generate report
|
| 50 |
report = f"""
|
| 51 |
EQUIPMENT ANALYSIS REPORT
|
| 52 |
========================
|
| 53 |
+
Generated: {pd.Timestamp.now().strftime('%Y-%m-%d %H:%M:%S')}
|
| 54 |
+
Sensitivity: {sensitivity} (z-score)
|
| 55 |
|
| 56 |
+
DATA SUMMARY
|
| 57 |
+
------------
|
| 58 |
Time period: {df['timestamp'].min()} to {df['timestamp'].max()}
|
| 59 |
+
Data points: {len(df)}
|
| 60 |
Value range: {df['value'].min():.2f} to {df['value'].max():.2f}
|
| 61 |
Median value: {df['value'].median():.2f}
|
|
|
|
| 62 |
|
| 63 |
+
ANOMALY FINDINGS
|
| 64 |
+
----------------
|
| 65 |
Detection threshold: {threshold:.2f}
|
| 66 |
+
Anomalies found: {len(anomalies)} ({len(anomalies)/len(df):.1%})
|
| 67 |
+
Most severe: {errors.max():.2f} at {df.loc[errors.argmax(), 'timestamp']}
|
| 68 |
|
| 69 |
TOP ANOMALIES
|
| 70 |
-------------
|
| 71 |
+
{anomalies[['timestamp', 'value', 'anomaly_score']].head(10).to_string(index=False, float_format='%.2f')}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
RECOMMENDATIONS
|
| 74 |
---------------
|
| 75 |
+
1. Investigate top 3 anomalies for potential equipment issues
|
| 76 |
+
2. Check maintenance records around {anomalies['timestamp'].iloc[0].strftime('%Y-%m-%d %H:%M')}
|
| 77 |
+
3. Consider recalibration if anomalies persist
|
| 78 |
+
4. Review sensor health if anomalies cluster in time
|
| 79 |
"""
|
| 80 |
return report.strip()
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
+
return f"ANALYSIS FAILED: {str(e)}"
|
| 84 |
|
| 85 |
+
# Gradio Interface
|
| 86 |
+
with gr.Blocks(title="Equipment Analysis Reporter") as demo:
|
| 87 |
+
gr.Markdown("## 🏭 Equipment Health Analysis Report")
|
|
|
|
|
|
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
with gr.Column():
|
| 91 |
+
data_input = gr.Textbox(
|
| 92 |
+
label="Paste CSV Data (timestamp,value)",
|
| 93 |
+
value="""timestamp,value
|
| 94 |
2025-04-01 00:00:00,100
|
| 95 |
2025-04-01 01:00:00,102
|
| 96 |
2025-04-01 02:00:00,98
|
|
|
|
| 103 |
2025-04-01 09:00:00,98
|
| 104 |
2025-04-01 10:00:00,99
|
| 105 |
2025-04-01 11:00:00,102
|
| 106 |
+
2025-04-01 12:00:00,101""",
|
| 107 |
+
lines=10
|
| 108 |
+
)
|
| 109 |
+
sensitivity = gr.Slider(1.0, 5.0, value=3.0, step=0.1, label="Detection Sensitivity")
|
| 110 |
submit_btn = gr.Button("Generate Report", variant="primary")
|
| 111 |
+
|
| 112 |
with gr.Column():
|
| 113 |
+
report_output = gr.Textbox(
|
| 114 |
+
label="Analysis Report",
|
| 115 |
+
lines=20,
|
| 116 |
+
interactive=False
|
| 117 |
+
)
|
| 118 |
|
| 119 |
submit_btn.click(
|
| 120 |
generate_analysis_report,
|