Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import pandas as pd
|
5 |
+
import numpy as np
|
6 |
+
from transformers import pipeline
|
7 |
+
from sklearn.ensemble import RandomForestClassifier
|
8 |
+
import joblib
|
9 |
+
|
10 |
+
# Load pre-trained models for Anomaly Detection and Failure Prediction
|
11 |
+
# Assume we have two models: LSTM for anomaly detection and RandomForest for failure prediction
|
12 |
+
|
13 |
+
# Hugging Face anomaly detection pipeline (custom fine-tuned model)
|
14 |
+
anomaly_detection = pipeline("text-classification", model="your-huggingface-model-path")
|
15 |
+
|
16 |
+
# Load the Random Forest model for failure prediction (pre-trained model)
|
17 |
+
failure_prediction_model = joblib.load('failure_prediction_model.pkl')
|
18 |
+
|
19 |
+
# Function to preprocess logs for anomaly detection
|
20 |
+
def preprocess_logs(logs):
|
21 |
+
# Assume logs come in as JSON and require some basic parsing and cleaning
|
22 |
+
logs['timestamp'] = pd.to_datetime(logs['timestamp'])
|
23 |
+
logs['log_message'] = logs['log_message'].str.lower()
|
24 |
+
return logs
|
25 |
+
|
26 |
+
# Function to detect anomalies
|
27 |
+
def detect_anomaly(logs):
|
28 |
+
preprocessed_logs = preprocess_logs(logs)
|
29 |
+
results = []
|
30 |
+
for log in preprocessed_logs['log_message']:
|
31 |
+
anomaly_result = anomaly_detection(log)
|
32 |
+
results.append(anomaly_result[0]['label']) # Assuming the Hugging Face model outputs label as result
|
33 |
+
return results
|
34 |
+
|
35 |
+
# Function to predict failures based on historical data and metrics
|
36 |
+
def predict_failure(device_metrics):
|
37 |
+
# Metrics include CPU usage, memory usage, error rates
|
38 |
+
metrics_array = np.array([device_metrics['cpu_usage'], device_metrics['memory_usage'], device_metrics['error_rate']]).reshape(1, -3)
|
39 |
+
failure_prediction = failure_prediction_model.predict(metrics_array)
|
40 |
+
return failure_prediction
|
41 |
+
|
42 |
+
# Gradio interface to upload log files and check anomaly detection and failure prediction
|
43 |
+
def process_logs_and_predict(log_file, metrics):
|
44 |
+
logs = pd.read_json(log_file)
|
45 |
+
anomalies = detect_anomaly(logs)
|
46 |
+
failure_pred = predict_failure(metrics)
|
47 |
+
|
48 |
+
return f"Anomalies Detected: {anomalies}, Failure Prediction: {failure_pred}"
|
49 |
+
|
50 |
+
# Set up Gradio interface for the dashboard
|
51 |
+
iface = gr.Interface(fn=process_logs_and_predict,
|
52 |
+
inputs=["file", "json"],
|
53 |
+
outputs="text",
|
54 |
+
title="Cisco Device Monitoring",
|
55 |
+
description="Upload log files to detect anomalies and predict potential device failures.")
|
56 |
+
|
57 |
+
iface.launch()
|