Spaces:
Sleeping
Sleeping
File size: 1,810 Bytes
4f82165 d70af30 8a2c474 8c73bdf 01beeb3 d70af30 01beeb3 8a2c474 d70af30 4f82165 01beeb3 755f5cd 01beeb3 755f5cd 01beeb3 755f5cd 01beeb3 4f82165 01beeb3 755f5cd 01beeb3 1aa1298 01beeb3 755f5cd 01beeb3 1aa1298 01beeb3 4f82165 755f5cd 01beeb3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
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()
|