Spaces:
Sleeping
Sleeping
done
Browse files
app.py
CHANGED
|
@@ -1,133 +1,52 @@
|
|
| 1 |
-
# from tensorflow.keras.models import load_model
|
| 2 |
-
# from tensorflow.keras.initializers import Orthogonal
|
| 3 |
-
# from tensorflow.keras.utils import custom_object_scope
|
| 4 |
-
# from tensorflow.keras.layers import LSTM
|
| 5 |
-
# import gradio as gr
|
| 6 |
-
# import pandas as pd
|
| 7 |
-
# import numpy as np
|
| 8 |
-
# # Initialize LSTM layer correctly without time_major
|
| 9 |
-
# lstm_layer = LSTM(64, return_sequences=True)
|
| 10 |
-
|
| 11 |
-
# # Register custom initializers or objects when loading the model
|
| 12 |
-
# with custom_object_scope({'Orthogonal': Orthogonal}):
|
| 13 |
-
# model = load_model('models/lstm-combinedmodel.h5')
|
| 14 |
-
|
| 15 |
-
# def predict_from_csv(file_path):
|
| 16 |
-
# # Load the data from CSV
|
| 17 |
-
# data = pd.read_csv(file_path)
|
| 18 |
-
|
| 19 |
-
# # Reorder and preprocess data if necessary
|
| 20 |
-
# required_columns = ['CAN ID', 'RTR', 'DLC', 'Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6', 'Data7', 'Data8']
|
| 21 |
-
# data = data[required_columns]
|
| 22 |
-
|
| 23 |
-
# # Convert data to numpy array or the format your model expects
|
| 24 |
-
# input_data = data.values
|
| 25 |
-
|
| 26 |
-
# # Predict using the model
|
| 27 |
-
# predictions = model.predict(input_data)
|
| 28 |
-
|
| 29 |
-
# # Determine the predicted class and confidence
|
| 30 |
-
# predicted_class = np.argmax(predictions, axis=1)[0]
|
| 31 |
-
# confidence = np.max(predictions, axis=1)[0]
|
| 32 |
-
|
| 33 |
-
# # Map numeric class to label
|
| 34 |
-
# class_labels = {0: "Normal", 1: "Anomaly"}
|
| 35 |
-
# label = class_labels[predicted_class]
|
| 36 |
-
# output = f"Predicted Class: {label}, Confidence: {confidence:.4f}"
|
| 37 |
-
|
| 38 |
-
# return output
|
| 39 |
-
|
| 40 |
-
# def interface_func(uploaded_file):
|
| 41 |
-
# # Use the prediction function on the uploaded file path
|
| 42 |
-
# predictions = predict_from_csv(uploaded_file.name)
|
| 43 |
-
# return predictions
|
| 44 |
-
|
| 45 |
-
# iface = gr.Interface(fn=interface_func,
|
| 46 |
-
# inputs=gr.File(label="Upload CSV"),
|
| 47 |
-
# outputs="text",
|
| 48 |
-
# description="Upload a CSV file with the specified columns to predict.")
|
| 49 |
-
|
| 50 |
-
# iface.launch()
|
| 51 |
-
|
| 52 |
-
import pandas as pd
|
| 53 |
-
import gradio as gr
|
| 54 |
-
import numpy as np
|
| 55 |
-
from io import StringIO
|
| 56 |
from tensorflow.keras.models import load_model
|
| 57 |
from tensorflow.keras.initializers import Orthogonal
|
| 58 |
from tensorflow.keras.utils import custom_object_scope
|
| 59 |
from tensorflow.keras.layers import LSTM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
#
|
| 62 |
with custom_object_scope({'Orthogonal': Orthogonal}):
|
| 63 |
model = load_model('models/lstm-combinedmodel.h5')
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
content = None
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
elif hasattr(uploaded_file, 'file'):
|
| 74 |
-
# If the file is stored in a `file` attribute
|
| 75 |
-
content = uploaded_file.file.read()
|
| 76 |
-
elif hasattr(uploaded_file, 'getvalue'):
|
| 77 |
-
# In case it's a different object, like a StringIO
|
| 78 |
-
content = uploaded_file.getvalue()
|
| 79 |
-
else:
|
| 80 |
-
# If none of these work, raise an error
|
| 81 |
-
raise ValueError("The file format provided is not supported.")
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
|
| 85 |
-
content = content.decode('utf-8')
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
# Dictionary to hold data extracted from text file
|
| 91 |
-
dfdict = {}
|
| 92 |
-
|
| 93 |
-
# Process each line from the file-like object
|
| 94 |
-
for line in file_like:
|
| 95 |
-
line = line.strip().split() # Split line and strip extra whitespace
|
| 96 |
-
if 'Timestamp:' in line:
|
| 97 |
-
line.remove('Timestamp:')
|
| 98 |
-
if 'ID:' in line:
|
| 99 |
-
line.remove('ID:')
|
| 100 |
-
if 'DLC:' in line:
|
| 101 |
-
line.remove('DLC:')
|
| 102 |
-
|
| 103 |
-
# Extract timestamp as key and remaining data as value
|
| 104 |
-
if len(line) > 2:
|
| 105 |
-
key = float(line[0])
|
| 106 |
-
value = line[1:]
|
| 107 |
-
dfdict[key] = value
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
return df
|
| 113 |
-
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
|
| 120 |
-
|
| 121 |
-
return df.to_html() # You can also return other outputs if needed
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
outputs="html",
|
| 129 |
-
description="Upload a text file with CAN data to visualize the DataFrame."
|
| 130 |
-
)
|
| 131 |
|
| 132 |
-
# Launch the interface
|
| 133 |
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from tensorflow.keras.models import load_model
|
| 2 |
from tensorflow.keras.initializers import Orthogonal
|
| 3 |
from tensorflow.keras.utils import custom_object_scope
|
| 4 |
from tensorflow.keras.layers import LSTM
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import numpy as np
|
| 8 |
+
# Initialize LSTM layer correctly without time_major
|
| 9 |
+
lstm_layer = LSTM(64, return_sequences=True)
|
| 10 |
|
| 11 |
+
# Register custom initializers or objects when loading the model
|
| 12 |
with custom_object_scope({'Orthogonal': Orthogonal}):
|
| 13 |
model = load_model('models/lstm-combinedmodel.h5')
|
| 14 |
|
| 15 |
+
def predict_from_csv(file_path):
|
| 16 |
+
# Load the data from CSV
|
| 17 |
+
data = pd.read_csv(file_path)
|
|
|
|
| 18 |
|
| 19 |
+
# Reorder and preprocess data if necessary
|
| 20 |
+
required_columns = ['CAN ID', 'RTR', 'DLC', 'Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6', 'Data7', 'Data8']
|
| 21 |
+
data = data[required_columns]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Convert data to numpy array or the format your model expects
|
| 24 |
+
input_data = data.values
|
|
|
|
| 25 |
|
| 26 |
+
# Predict using the model
|
| 27 |
+
predictions = model.predict(input_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Determine the predicted class and confidence
|
| 30 |
+
predicted_class = np.argmax(predictions, axis=1)[0]
|
| 31 |
+
confidence = np.max(predictions, axis=1)[0]
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Map numeric class to label
|
| 34 |
+
class_labels = {0: "Normal", 1: "Anomaly"}
|
| 35 |
+
label = class_labels[predicted_class]
|
| 36 |
+
output = f"Predicted Class: {label}, Confidence: {confidence:.4f}"
|
| 37 |
|
| 38 |
+
return output
|
|
|
|
| 39 |
|
| 40 |
+
def interface_func(uploaded_file):
|
| 41 |
+
# Use the prediction function on the uploaded file path
|
| 42 |
+
predictions = predict_from_csv(uploaded_file.name)
|
| 43 |
+
return predictions
|
| 44 |
|
| 45 |
+
iface = gr.Interface(fn=interface_func,
|
| 46 |
+
inputs=gr.File(label="Upload CSV"),
|
| 47 |
+
outputs="text",
|
| 48 |
+
description="Upload a CSV file with the specified columns to predict.")
|
|
|
|
|
|
|
|
|
|
| 49 |
|
|
|
|
| 50 |
iface.launch()
|
| 51 |
+
|
| 52 |
+
|