TheInCube / app.py
DSatishchandra's picture
Update app.py
7fa8ebf verified
raw
history blame
2.54 kB
# app.py
import gradio as gr
import pandas as pd
import numpy as np
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
from sklearn.ensemble import RandomForestClassifier
import joblib
# Using the Hugging Face model "huggingface-course/distilbert-base-uncased-finetuned-imdb"
# Load the tokenizer and model separately
tokenizer = AutoTokenizer.from_pretrained("huggingface-course/distilbert-base-uncased-finetuned-imdb")
model = AutoModelForSequenceClassification.from_pretrained("huggingface-course/distilbert-base-uncased-finetuned-imdb")
# Define pipeline for anomaly detection using the loaded model and tokenizer
anomaly_detection = pipeline("text-classification", model=model, tokenizer=tokenizer)
# Load the Random Forest model for failure prediction (this should be trained and saved as failure_prediction_model.pkl)
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']) # Assuming the Hugging Face model outputs label as result
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()