Spaces:
Sleeping
Sleeping
init
Browse files
app.py
CHANGED
@@ -62,20 +62,30 @@ from tensorflow.keras.layers import LSTM
|
|
62 |
with custom_object_scope({'Orthogonal': Orthogonal}):
|
63 |
model = load_model('models/lstm-combinedmodel.h5')
|
64 |
|
65 |
-
# Function to parse
|
66 |
def parse_text_file(file):
|
67 |
-
#
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
content = content.decode('utf-8')
|
71 |
-
file_like = StringIO(content) # Use StringIO to create a file-like object from the string
|
72 |
|
73 |
-
|
|
|
|
|
74 |
dfdict = {}
|
75 |
|
76 |
-
#
|
77 |
for line in file_like:
|
78 |
-
line = line.strip().split() # Split line into parts,
|
79 |
if 'Timestamp:' in line:
|
80 |
line.remove('Timestamp:') # Remove specific label
|
81 |
if 'ID:' in line:
|
@@ -83,8 +93,8 @@ def parse_text_file(file):
|
|
83 |
if 'DLC:' in line:
|
84 |
line.remove('DLC:')
|
85 |
|
86 |
-
#
|
87 |
-
if line:
|
88 |
key = float(line[0])
|
89 |
value = line[1:]
|
90 |
dfdict[key] = value # Add to dictionary
|
@@ -94,48 +104,19 @@ def parse_text_file(file):
|
|
94 |
df.index.name = 'Timestamp' # Set index name
|
95 |
return df
|
96 |
|
97 |
-
# Function to use the model to make predictions from the DataFrame
|
98 |
-
def make_predictions(dataframe):
|
99 |
-
# Ensure DataFrame has the necessary columns
|
100 |
-
required_columns = ['CAN ID', 'RTR', 'DLC', 'Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6', 'Data7', 'Data8']
|
101 |
-
if not all(col in dataframe.columns for col in required_columns):
|
102 |
-
raise ValueError("Missing required columns in the DataFrame.")
|
103 |
-
|
104 |
-
# Convert to the format expected by the model
|
105 |
-
input_data = dataframe[required_columns].values
|
106 |
-
|
107 |
-
# Predict using the model
|
108 |
-
predictions = model.predict(input_data)
|
109 |
-
|
110 |
-
# Determine the predicted class and confidence
|
111 |
-
predicted_class = np.argmax(predictions, axis=1)
|
112 |
-
confidence = np.max(predictions, axis=1)
|
113 |
-
|
114 |
-
# Map numeric class to label
|
115 |
-
class_labels = {0: "Normal", 1: "Anomaly"}
|
116 |
-
|
117 |
-
results = []
|
118 |
-
for cls, conf in zip(predicted_class, confidence):
|
119 |
-
results.append(f"Predicted Class: {class_labels[cls]}, Confidence: {conf:.4f}")
|
120 |
-
|
121 |
-
return results
|
122 |
-
|
123 |
-
# Gradio interface function
|
124 |
def interface_func(uploaded_file):
|
125 |
# Parse the text file into a DataFrame
|
126 |
df = parse_text_file(uploaded_file)
|
127 |
|
128 |
-
#
|
129 |
-
|
130 |
-
|
131 |
-
return "\n".join(predictions) # Return predictions as text
|
132 |
|
133 |
# Set up the Gradio interface
|
134 |
iface = gr.Interface(
|
135 |
fn=interface_func,
|
136 |
inputs=gr.File(label="Upload a text file"),
|
137 |
-
outputs="
|
138 |
-
description="Upload a text file with CAN data to
|
139 |
)
|
140 |
|
141 |
# Launch the interface
|
|
|
62 |
with custom_object_scope({'Orthogonal': Orthogonal}):
|
63 |
model = load_model('models/lstm-combinedmodel.h5')
|
64 |
|
65 |
+
# Function to parse the uploaded file from Gradio
|
66 |
def parse_text_file(file):
|
67 |
+
# Gradio uploads files as a 'werkzeug.datastructures.FileStorage' object
|
68 |
+
# which acts like a file object. Reading it directly if .read() doesn't work.
|
69 |
+
# We need to check the type and handle it accordingly.
|
70 |
+
if hasattr(file, 'read'):
|
71 |
+
content = file.read()
|
72 |
+
elif hasattr(file, 'file'):
|
73 |
+
# In case Gradio passes the FileStorage object where the file is accessible via '.file'
|
74 |
+
content = file.file.read()
|
75 |
+
else:
|
76 |
+
raise ValueError("The file format provided is not supported.")
|
77 |
+
|
78 |
+
if isinstance(content, bytes): # Check if the content is bytes, decode it
|
79 |
content = content.decode('utf-8')
|
|
|
80 |
|
81 |
+
file_like = StringIO(content) # Convert string to a file-like object
|
82 |
+
|
83 |
+
# Dictionary to hold data extracted from text file
|
84 |
dfdict = {}
|
85 |
|
86 |
+
# Process each line from file-like object
|
87 |
for line in file_like:
|
88 |
+
line = line.strip().split() # Split line into parts, removing newlines and extra spaces
|
89 |
if 'Timestamp:' in line:
|
90 |
line.remove('Timestamp:') # Remove specific label
|
91 |
if 'ID:' in line:
|
|
|
93 |
if 'DLC:' in line:
|
94 |
line.remove('DLC:')
|
95 |
|
96 |
+
# Ensure line has enough elements to extract a key and value
|
97 |
+
if len(line) > 2:
|
98 |
key = float(line[0])
|
99 |
value = line[1:]
|
100 |
dfdict[key] = value # Add to dictionary
|
|
|
104 |
df.index.name = 'Timestamp' # Set index name
|
105 |
return df
|
106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
def interface_func(uploaded_file):
|
108 |
# Parse the text file into a DataFrame
|
109 |
df = parse_text_file(uploaded_file)
|
110 |
|
111 |
+
# Optionally process the DataFrame further or directly show it
|
112 |
+
return df.to_html()
|
|
|
|
|
113 |
|
114 |
# Set up the Gradio interface
|
115 |
iface = gr.Interface(
|
116 |
fn=interface_func,
|
117 |
inputs=gr.File(label="Upload a text file"),
|
118 |
+
outputs="html",
|
119 |
+
description="Upload a text file with CAN data to convert it to a DataFrame."
|
120 |
)
|
121 |
|
122 |
# Launch the interface
|