Spaces:
Running
Running
# app.py | |
import gradio as gr | |
import pandas as pd | |
import numpy as np | |
from transformers import pipeline | |
from sklearn.ensemble import RandomForestClassifier | |
import joblib | |
# Load fine-tuned Hugging Face model for anomaly detection | |
anomaly_detection = pipeline("text-classification", model="./fine_tuned_anomaly_model") | |
# Load the Random Forest model for failure prediction | |
failure_prediction_model = joblib.load('failure_prediction_model.pkl') | |
# Function to preprocess logs for anomaly detection | |
def preprocess_logs(logs): | |
logs['timestamp'] = pd.to_datetime(logs['timestamp']) | |
logs['log_message'] = logs['log_message'].str.lower() | |
return logs | |
# Function to detect anomalies | |
def detect_anomaly(logs): | |
preprocessed_logs = preprocess_logs(logs) | |
results = [] | |
for log in preprocessed_logs['log_message']: | |
anomaly_result = anomaly_detection(log) | |
results.append(anomaly_result[0]['label']) | |
return results | |
# Function to predict failures based on historical data and metrics | |
def predict_failure(device_metrics): | |
metrics_array = np.array([device_metrics['cpu_usage'], device_metrics['memory_usage'], device_metrics['error_rate']]).reshape(1, -1) | |
failure_prediction = failure_prediction_model.predict(metrics_array) | |
return failure_prediction | |
# Gradio interface to upload log files and check anomaly detection and failure prediction | |
def process_logs_and_predict(log_file, metrics): | |
logs = pd.read_json(log_file) | |
anomalies = detect_anomaly(logs) | |
failure_pred = predict_failure(metrics) | |
return f"Anomalies Detected: {anomalies}, Failure Prediction: {failure_pred}" | |
# Set up Gradio interface for the dashboard | |
iface = gr.Interface(fn=process_logs_and_predict, | |
inputs=["file", "json"], | |
outputs="text", | |
title="Cisco Device Monitoring", | |
description="Upload log files to detect anomalies and predict potential device failures.") | |
iface.launch() | |