Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def generate_analysis_report(data_input, sensitivity=3.0):
|
| 2 |
-
"""Generate a textual analysis report
|
| 3 |
try:
|
| 4 |
# Process and validate data
|
| 5 |
df = pd.read_csv(StringIO(data_input))
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
df = df.sort_values('timestamp')
|
| 9 |
|
| 10 |
# Prepare data for model
|
|
@@ -20,45 +43,63 @@ def generate_analysis_report(data_input, sensitivity=3.0):
|
|
| 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 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
"""
|
| 54 |
-
return report
|
| 55 |
|
| 56 |
except Exception as e:
|
| 57 |
-
return f"
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,7 +112,18 @@ timestamp,value
|
|
| 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 |
-
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
from momentfm import MOMENTPipeline
|
| 4 |
+
from io import StringIO
|
| 5 |
+
|
| 6 |
+
# Initialize model globally
|
| 7 |
+
model = MOMENTPipeline.from_pretrained(
|
| 8 |
+
"AutonLab/MOMENT-1-large",
|
| 9 |
+
model_kwargs={"task_name": "reconstruction"},
|
| 10 |
+
)
|
| 11 |
+
model.init()
|
| 12 |
+
|
| 13 |
def generate_analysis_report(data_input, sensitivity=3.0):
|
| 14 |
+
"""Generate a comprehensive textual analysis report"""
|
| 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 |
+
# Check for invalid data
|
| 28 |
+
if df.isnull().any().any():
|
| 29 |
+
return "Error: Invalid data format (check timestamp/value formats)"
|
| 30 |
+
|
| 31 |
df = df.sort_values('timestamp')
|
| 32 |
|
| 33 |
# Prepare data for model
|
|
|
|
| 43 |
threshold = median + sensitivity * (1.4826 * mad)
|
| 44 |
|
| 45 |
# Identify anomalies
|
| 46 |
+
anomalies = df[errors > threshold].copy()
|
| 47 |
+
anomalies['anomaly_score'] = errors[errors > threshold]
|
| 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 at: {pd.Timestamp.now()}
|
| 57 |
+
Detection sensitivity: {sensitivity} (z-score)
|
| 58 |
|
| 59 |
+
DATA OVERVIEW
|
| 60 |
+
-------------
|
| 61 |
+
Time period: {df['timestamp'].min()} to {df['timestamp'].max()}
|
| 62 |
+
Total observations: {len(df)}
|
| 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 DETECTION RESULTS
|
| 68 |
+
-------------------------
|
| 69 |
+
Detection threshold: {threshold:.2f}
|
| 70 |
+
Anomalies detected: {len(anomalies)} ({len(anomalies)/len(df):.1%} of data)
|
| 71 |
+
Strongest anomaly: {errors.max():.2f} at {df.loc[errors.argmax(), 'timestamp']}
|
| 72 |
|
| 73 |
+
TOP ANOMALIES
|
| 74 |
+
-------------
|
| 75 |
+
{anomalies[['timestamp', 'value', 'anomaly_score']].head(15).to_string(index=False, float_format='%.2f')}
|
| 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 {min(3, len(anomalies))} anomalous readings
|
| 85 |
+
2. Check equipment around {anomalies['timestamp'].iloc[0]} for potential issues
|
| 86 |
+
3. Consider recalibration if anomalies cluster in specific time periods
|
| 87 |
+
4. Review maintenance logs around detected anomalies
|
| 88 |
"""
|
| 89 |
+
return report.strip()
|
| 90 |
|
| 91 |
except Exception as e:
|
| 92 |
+
return f"ANALYSIS ERROR: {str(e)}"
|
| 93 |
|
| 94 |
+
# Gradio Interface for the report-only version
|
| 95 |
+
import gradio as gr
|
| 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(label="Paste CSV Data", lines=10, value="""timestamp,value
|
| 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 |
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 |
+
sensitivity = gr.Slider(1.0, 5.0, value=3.0, label="Detection Sensitivity")
|
| 117 |
+
submit_btn = gr.Button("Generate Report", variant="primary")
|
| 118 |
+
|
| 119 |
+
with gr.Column():
|
| 120 |
+
report_output = gr.Textbox(label="Analysis Report", lines=20, interactive=False)
|
| 121 |
+
|
| 122 |
+
submit_btn.click(
|
| 123 |
+
generate_analysis_report,
|
| 124 |
+
inputs=[data_input, sensitivity],
|
| 125 |
+
outputs=report_output
|
| 126 |
+
)
|
| 127 |
|
| 128 |
+
if __name__ == "__main__":
|
| 129 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|