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