Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,52 +6,74 @@ import numpy as np
|
|
| 6 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
| 7 |
from sklearn.ensemble import RandomForestClassifier
|
| 8 |
import joblib
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
#
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained("huggingface-course/distilbert-base-uncased-finetuned-imdb")
|
| 13 |
model = AutoModelForSequenceClassification.from_pretrained("huggingface-course/distilbert-base-uncased-finetuned-imdb")
|
| 14 |
-
|
| 15 |
-
# Define pipeline for anomaly detection using the loaded model and tokenizer
|
| 16 |
anomaly_detection = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
| 17 |
|
| 18 |
-
# Load the Random Forest model for failure prediction
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def preprocess_logs(logs):
|
| 23 |
logs['timestamp'] = pd.to_datetime(logs['timestamp'])
|
| 24 |
-
logs['log_message'] = logs['log_message'].str.lower()
|
| 25 |
return logs
|
| 26 |
|
| 27 |
-
# Function to detect anomalies
|
| 28 |
def detect_anomaly(logs):
|
| 29 |
preprocessed_logs = preprocess_logs(logs)
|
| 30 |
results = []
|
| 31 |
for log in preprocessed_logs['log_message']:
|
| 32 |
-
anomaly_result = anomaly_detection(log)
|
| 33 |
-
results.append(anomaly_result[0]['label']) #
|
| 34 |
return results
|
| 35 |
|
| 36 |
-
# Function to predict failures based on
|
| 37 |
def predict_failure(device_metrics):
|
|
|
|
| 38 |
metrics_array = np.array([device_metrics['cpu_usage'], device_metrics['memory_usage'], device_metrics['error_rate']]).reshape(1, -1)
|
| 39 |
-
failure_prediction = failure_prediction_model.predict(metrics_array)
|
| 40 |
return failure_prediction
|
| 41 |
|
| 42 |
-
#
|
| 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
|
| 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()
|
|
|
|
| 6 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
| 7 |
from sklearn.ensemble import RandomForestClassifier
|
| 8 |
import joblib
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
+
# Step 1: Load Hugging Face model for anomaly detection
|
| 12 |
+
# Using the "huggingface-course/distilbert-base-uncased-finetuned-imdb" model
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained("huggingface-course/distilbert-base-uncased-finetuned-imdb")
|
| 14 |
model = AutoModelForSequenceClassification.from_pretrained("huggingface-course/distilbert-base-uncased-finetuned-imdb")
|
|
|
|
|
|
|
| 15 |
anomaly_detection = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
| 16 |
|
| 17 |
+
# Step 2: Train or Load the Random Forest model for failure prediction
|
| 18 |
+
if not os.path.exists('failure_prediction_model.pkl'):
|
| 19 |
+
# Sample data (replace this with real Cisco device metrics data)
|
| 20 |
+
data = pd.DataFrame({
|
| 21 |
+
'cpu_usage': [10, 20, 15, 35, 55],
|
| 22 |
+
'memory_usage': [30, 60, 45, 50, 80],
|
| 23 |
+
'error_rate': [0, 1, 0, 2, 5],
|
| 24 |
+
'failure': [0, 1, 0, 1, 1] # 0 = no failure, 1 = failure
|
| 25 |
+
})
|
| 26 |
+
|
| 27 |
+
# Features and target
|
| 28 |
+
X = data[['cpu_usage', 'memory_usage', 'error_rate']]
|
| 29 |
+
y = data['failure']
|
| 30 |
+
|
| 31 |
+
# Train the Random Forest model
|
| 32 |
+
failure_prediction_model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 33 |
+
failure_prediction_model.fit(X, y)
|
| 34 |
|
| 35 |
+
# Save the model for future use
|
| 36 |
+
joblib.dump(failure_prediction_model, 'failure_prediction_model.pkl')
|
| 37 |
+
else:
|
| 38 |
+
# Load the trained model from file
|
| 39 |
+
failure_prediction_model = joblib.load('failure_prediction_model.pkl')
|
| 40 |
+
|
| 41 |
+
# Step 3: Define function to preprocess logs for anomaly detection
|
| 42 |
def preprocess_logs(logs):
|
| 43 |
logs['timestamp'] = pd.to_datetime(logs['timestamp'])
|
| 44 |
+
logs['log_message'] = logs['log_message'].str.lower() # Convert log messages to lowercase for uniformity
|
| 45 |
return logs
|
| 46 |
|
| 47 |
+
# Step 4: Function to detect anomalies in logs
|
| 48 |
def detect_anomaly(logs):
|
| 49 |
preprocessed_logs = preprocess_logs(logs)
|
| 50 |
results = []
|
| 51 |
for log in preprocessed_logs['log_message']:
|
| 52 |
+
anomaly_result = anomaly_detection(log) # Use Hugging Face pipeline for anomaly detection
|
| 53 |
+
results.append(anomaly_result[0]['label']) # Append label (e.g., "POSITIVE" or "NEGATIVE")
|
| 54 |
return results
|
| 55 |
|
| 56 |
+
# Step 5: Function to predict failures based on device metrics
|
| 57 |
def predict_failure(device_metrics):
|
| 58 |
+
# Convert device metrics into a numpy array for prediction
|
| 59 |
metrics_array = np.array([device_metrics['cpu_usage'], device_metrics['memory_usage'], device_metrics['error_rate']]).reshape(1, -1)
|
| 60 |
+
failure_prediction = failure_prediction_model.predict(metrics_array) # Use the Random Forest model for failure prediction
|
| 61 |
return failure_prediction
|
| 62 |
|
| 63 |
+
# Step 6: Function to process logs and predict both anomalies and failures
|
| 64 |
def process_logs_and_predict(log_file, metrics):
|
| 65 |
+
logs = pd.read_json(log_file) # Load logs from the uploaded JSON file
|
| 66 |
+
anomalies = detect_anomaly(logs) # Detect anomalies in logs
|
| 67 |
+
failure_pred = predict_failure(metrics) # Predict failures using device metrics
|
| 68 |
+
|
| 69 |
return f"Anomalies Detected: {anomalies}, Failure Prediction: {failure_pred}"
|
| 70 |
|
| 71 |
+
# Step 7: Set up Gradio interface for uploading logs and metrics for prediction
|
| 72 |
iface = gr.Interface(fn=process_logs_and_predict,
|
| 73 |
inputs=["file", "json"],
|
| 74 |
outputs="text",
|
| 75 |
title="Cisco Device Monitoring",
|
| 76 |
description="Upload log files to detect anomalies and predict potential device failures.")
|
| 77 |
|
| 78 |
+
# Launch the Gradio interface
|
| 79 |
iface.launch()
|