Spaces:
Runtime error
Runtime error
| from tensorflow.keras.models import load_model | |
| import pandas as pd | |
| import numpy as np | |
| from sklearn.preprocessing import MinMaxScaler | |
| # Correct path to your saved LSTM model | |
| model = load_model("models/lstm_energy_model.h5") # Adjust the path to the actual model location | |
| def detect_energy_anomalies(file_path): | |
| """ | |
| Detect anomalies in energy generation data (time-series) using LSTM. | |
| Args: | |
| - file_path (str): Path to the CSV/JSON file containing energy generation data | |
| Returns: | |
| - anomaly (str): Energy anomaly detection result | |
| """ | |
| # Load energy generation data (assumes CSV format with timestamp and generation columns) | |
| data = pd.read_csv(file_path) | |
| energy_data = data['energy_generation'].values.reshape(-1, 1) | |
| # Normalize data for LSTM | |
| scaler = MinMaxScaler(feature_range=(0, 1)) | |
| energy_data = scaler.fit_transform(energy_data) | |
| # Prepare data for LSTM | |
| X = [] | |
| for i in range(60, len(energy_data)): | |
| X.append(energy_data[i-60:i, 0]) | |
| X = np.array(X) | |
| X = X.reshape((X.shape[0], X.shape[1], 1)) | |
| # Predict with LSTM model | |
| prediction = model.predict(X) | |
| last_predicted_value = prediction[-1][0] # Get last prediction | |
| # Define threshold for anomaly (e.g., 10% deviation from the normal value) | |
| threshold = 0.1 | |
| anomaly = "Anomaly Detected" if abs(last_predicted_value - energy_data[-1][0]) > threshold else "No Anomaly" | |
| return anomaly | |