File size: 10,218 Bytes
d2173d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
"""
Gradio UI for Vehicle Diagnostics Agent
"""
import gradio as gr
import sys
from pathlib import Path
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px

# Add parent directory to path
sys.path.append(str(Path(__file__).parent.parent))

from orchestrator import VehicleDiagnosticOrchestrator
from agents.data_ingestion_agent import DataIngestionAgent

# Initialize components
orchestrator = VehicleDiagnosticOrchestrator()
ingestion_agent = DataIngestionAgent()

# Load available vehicles
test_df = ingestion_agent.load_test_data()
available_vehicles = sorted(test_df['vehicle_id'].unique().tolist())


def run_diagnostic(vehicle_id, n_readings):
    """Run diagnostic for a vehicle"""
    try:
        vehicle_id = int(vehicle_id)
        n_readings = int(n_readings) if n_readings else None
        
        # Run diagnostic
        result = orchestrator.diagnose_vehicle(vehicle_id, n_readings)
        
        if not result['success']:
            return f"❌ Error: {result.get('error')}", "", "", None
        
        # Extract results
        anomaly_result = result.get('anomaly_result', {})
        report = result.get('report', {})
        
        # Status summary
        if anomaly_result.get('anomaly_detected'):
            status = f"""
## 🚨 ALERT: Anomalies Detected

**Vehicle ID:** {vehicle_id}  
**Anomaly Score:** {anomaly_result.get('overall_score', 0):.3f}  
**Anomalous Readings:** {anomaly_result.get('num_anomalies', 0)} / {len(anomaly_result.get('anomaly_predictions', []))} ({anomaly_result.get('anomaly_rate', 0):.1%})  
**Status:** ⚠️ Requires Attention
"""
        else:
            status = f"""
## βœ… Vehicle Healthy

**Vehicle ID:** {vehicle_id}  
**Status:** 🟒 All Systems Normal  
**Anomaly Score:** {anomaly_result.get('overall_score', 0):.3f}
"""
        
        # Natural language summary
        nl_summary = report.get('natural_language_summary', 'No summary available')
        
        # Full report
        full_report = report.get('full_report', 'No report available')
        
        # Create visualization
        fig = create_anomaly_visualization(anomaly_result)
        
        return status, nl_summary, full_report, fig
        
    except Exception as e:
        return f"❌ Error: {str(e)}", "", "", None


def create_anomaly_visualization(anomaly_result):
    """Create visualization of anomaly detection results"""
    try:
        timestamps = anomaly_result.get('timestamps', [])
        predictions = anomaly_result.get('anomaly_predictions', [])
        scores = anomaly_result.get('anomaly_scores', [])
        
        if len(timestamps) == 0:
            return None
        
        # Create figure with secondary y-axis
        fig = go.Figure()
        
        # Add anomaly predictions
        fig.add_trace(go.Scatter(
            x=timestamps,
            y=predictions,
            mode='lines',
            name='Anomaly Detected',
            line=dict(color='red', width=2),
            fill='tozeroy',
            fillcolor='rgba(255, 0, 0, 0.2)'
        ))
        
        # Add anomaly scores
        fig.add_trace(go.Scatter(
            x=timestamps,
            y=scores,
            mode='lines',
            name='Anomaly Score',
            line=dict(color='orange', width=1, dash='dot'),
            yaxis='y2'
        ))
        
        # Update layout
        fig.update_layout(
            title='Anomaly Detection Over Time',
            xaxis_title='Timestamp',
            yaxis_title='Anomaly Detected (0/1)',
            yaxis2=dict(
                title='Anomaly Score',
                overlaying='y',
                side='right'
            ),
            hovermode='x unified',
            template='plotly_white',
            height=400
        )
        
        return fig
        
    except Exception as e:
        print(f"Visualization error: {e}")
        return None


def get_vehicle_info(vehicle_id):
    """Get basic info about a vehicle"""
    try:
        vehicle_id = int(vehicle_id)
        vehicle_data = test_df[test_df['vehicle_id'] == vehicle_id]
        
        if len(vehicle_data) == 0:
            return "Vehicle not found"
        
        num_readings = len(vehicle_data)
        has_anomalies = vehicle_data['anomaly'].sum() > 0
        num_anomalies = vehicle_data['anomaly'].sum()
        
        info = f"""
### Vehicle Information

**Vehicle ID:** {vehicle_id}  
**Total Readings:** {num_readings}  
**Known Anomalies:** {num_anomalies} ({num_anomalies/num_readings:.1%})  
**Status:** {'⚠️ Has anomalies' if has_anomalies else 'βœ… Healthy'}
"""
        return info
        
    except Exception as e:
        return f"Error: {str(e)}"


def list_vehicles_with_anomalies():
    """List vehicles that have anomalies"""
    vehicles_with_anomalies = []
    
    for vid in available_vehicles[:50]:  # Limit to first 50
        vehicle_data = test_df[test_df['vehicle_id'] == vid]
        if vehicle_data['anomaly'].sum() > 0:
            vehicles_with_anomalies.append({
                'Vehicle ID': vid,
                'Total Readings': len(vehicle_data),
                'Anomalies': int(vehicle_data['anomaly'].sum()),
                'Anomaly Rate': f"{vehicle_data['anomaly'].sum()/len(vehicle_data):.1%}"
            })
    
    if vehicles_with_anomalies:
        df = pd.DataFrame(vehicles_with_anomalies)
        return df
    else:
        return pd.DataFrame({'Message': ['No vehicles with anomalies found']})


# Create Gradio interface
with gr.Blocks(title="Vehicle Diagnostics Agent") as demo:
    gr.Markdown("""
    # πŸš— Vehicle Diagnostics Agent
    ### Multi-Agent AI System for Predictive Vehicle Diagnostics
    
    This system uses advanced AI agents to analyze vehicle sensor data, detect anomalies, 
    identify root causes, and provide actionable maintenance recommendations.
    """)
    
    with gr.Tab("πŸ” Single Vehicle Diagnostic"):
        gr.Markdown("### Analyze a single vehicle")
        
        with gr.Row():
            with gr.Column(scale=1):
                vehicle_id_input = gr.Dropdown(
                    choices=available_vehicles,
                    label="Select Vehicle ID",
                    value=available_vehicles[0] if available_vehicles else None
                )
                n_readings_input = gr.Number(
                    label="Number of Recent Readings (optional)",
                    value=200,
                    precision=0
                )
                
                diagnose_btn = gr.Button("πŸ”¬ Run Diagnostic", variant="primary", size="lg")
                
                gr.Markdown("---")
                vehicle_info_output = gr.Markdown(label="Vehicle Info")
                
                # Auto-update vehicle info when selection changes
                vehicle_id_input.change(
                    fn=get_vehicle_info,
                    inputs=[vehicle_id_input],
                    outputs=[vehicle_info_output]
                )
        
            with gr.Column(scale=2):
                status_output = gr.Markdown(label="Diagnostic Status")
                summary_output = gr.Textbox(
                    label="πŸ“‹ Summary",
                    lines=5,
                    max_lines=10
                )
                
        with gr.Row():
            anomaly_plot = gr.Plot(label="Anomaly Detection Visualization")
        
        with gr.Row():
            full_report_output = gr.Textbox(
                label="πŸ“„ Full Diagnostic Report",
                lines=20,
                max_lines=30
            )
        
        diagnose_btn.click(
            fn=run_diagnostic,
            inputs=[vehicle_id_input, n_readings_input],
            outputs=[status_output, summary_output, full_report_output, anomaly_plot]
        )
    
    with gr.Tab("πŸ“Š Vehicle Overview"):
        gr.Markdown("### Vehicles with Known Anomalies")
        
        refresh_btn = gr.Button("πŸ”„ Refresh List", variant="secondary")
        vehicles_table = gr.Dataframe(
            value=list_vehicles_with_anomalies(),
            label="Vehicles Requiring Attention"
        )
        
        refresh_btn.click(
            fn=list_vehicles_with_anomalies,
            inputs=[],
            outputs=[vehicles_table]
        )
    
    with gr.Tab("ℹ️ About"):
        gr.Markdown("""
        ## About Vehicle Diagnostics Agent
        
        ### System Architecture
        
        This system employs a multi-agent architecture with the following components:
        
        1. **Data Ingestion Agent** - Loads and prepares vehicle sensor data
        2. **Anomaly Detection Agent** - Uses LSTM neural networks to detect unusual patterns
        3. **Root Cause Analysis Agent** - Identifies the underlying causes of anomalies
        4. **Maintenance Recommendation Agent** - Provides actionable maintenance steps
        5. **Report Generation Agent** - Creates comprehensive diagnostic reports
        
        ### Technology Stack
        
        - **ML Framework:** PyTorch (LSTM-based anomaly detection)
        - **Orchestration:** LangGraph for multi-agent coordination
        - **Backend:** FastAPI for REST API
        - **Frontend:** Gradio for interactive UI
        - **Data Processing:** Pandas, NumPy, Scikit-learn
        
        ### Features
        
        - βœ… Real-time anomaly detection
        - βœ… Root cause analysis with fault code mapping
        - βœ… Maintenance cost estimation
        - βœ… Natural language summaries
        - βœ… Interactive visualizations
        - βœ… Batch processing support
        
        ### Dataset
        
        The system analyzes synthetic vehicle sensor data including:
        - Engine temperature, RPM, speed
        - Battery voltage and health
        - Oil and fuel pressure
        - Tire pressure (all four wheels)
        - Vibration levels
        - And more...
        
        ---
        
        **Version:** 1.0.0  
        **Author:** Vehicle Diagnostics Team  
        **License:** MIT
        """)

# Launch the app
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860, share=False)