from tensorflow.keras.models import load_model from tensorflow.keras.initializers import Orthogonal from tensorflow.keras.utils import custom_object_scope from tensorflow.keras.layers import LSTM import gradio as gr import pandas as pd import numpy as np # Initialize LSTM layer correctly without time_major lstm_layer = LSTM(64, return_sequences=True) # Register custom initializers or objects when loading the model with custom_object_scope({'Orthogonal': Orthogonal}): model = load_model('models/lstm-combinedmodel.h5') def predict_from_csv(file_path): # Load the data from CSV data = pd.read_csv(file_path) # Reorder and preprocess data if necessary required_columns = ['CAN ID', 'RTR', 'DLC', 'Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6', 'Data7', 'Data8'] data = data[required_columns] # Convert data to numpy array or the format your model expects input_data = data.values # Predict using the model predictions = model.predict(input_data) # Determine the predicted class and confidence predicted_class = np.argmax(predictions, axis=1)[0] confidence = np.max(predictions, axis=1)[0] # Map numeric class to label class_labels = {0: "Normal", 1: "Anomaly"} label = class_labels[predicted_class] output = f"Predicted Class: {label}, Confidence: {confidence:.4f}" return output def interface_func(uploaded_file): # Use the prediction function on the uploaded file path predictions = predict_from_csv(uploaded_file.name) return predictions iface = gr.Interface(fn=interface_func, inputs=gr.File(label="Upload CSV"), outputs="text", description="Upload a CSV file with the specified columns to predict.") iface.launch()