Spaces:
Sleeping
Sleeping
wissemkarous
commited on
Commit
•
755f5cd
1
Parent(s):
93b931b
done
Browse files
app.py
CHANGED
@@ -62,62 +62,72 @@ 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 the uploaded file
|
66 |
-
def parse_text_file(
|
67 |
-
#
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
75 |
else:
|
|
|
76 |
raise ValueError("The file format provided is not supported.")
|
77 |
-
|
78 |
-
|
|
|
79 |
content = content.decode('utf-8')
|
80 |
-
|
81 |
-
|
|
|
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
|
89 |
if 'Timestamp:' in line:
|
90 |
-
line.remove('Timestamp:')
|
91 |
if 'ID:' in line:
|
92 |
line.remove('ID:')
|
93 |
if 'DLC:' in line:
|
94 |
line.remove('DLC:')
|
95 |
|
96 |
-
#
|
97 |
if len(line) > 2:
|
98 |
key = float(line[0])
|
99 |
value = line[1:]
|
100 |
-
dfdict[key] = value
|
101 |
|
102 |
# Convert dictionary to DataFrame
|
103 |
df = pd.DataFrame.from_dict(dfdict, orient='index', columns=['CAN ID', 'RTR', 'DLC', 'Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6', 'Data7', 'Data8'])
|
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 |
-
#
|
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
|
120 |
)
|
121 |
|
122 |
# Launch the interface
|
123 |
-
iface.launch()
|
|
|
62 |
with custom_object_scope({'Orthogonal': Orthogonal}):
|
63 |
model = load_model('models/lstm-combinedmodel.h5')
|
64 |
|
65 |
+
# Function to parse the uploaded file into a DataFrame
|
66 |
+
def parse_text_file(uploaded_file):
|
67 |
+
# Initialize content variable
|
68 |
+
content = None
|
69 |
+
|
70 |
+
# Determine how to get content from the uploaded file
|
71 |
+
if hasattr(uploaded_file, 'read'):
|
72 |
+
content = uploaded_file.read() # Most common case
|
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 |
+
# If content is in bytes, decode it to a string
|
84 |
+
if isinstance(content, bytes):
|
85 |
content = content.decode('utf-8')
|
86 |
+
|
87 |
+
# Create a file-like object from the content
|
88 |
+
file_like = StringIO(content)
|
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 |
# Convert dictionary to DataFrame
|
110 |
df = pd.DataFrame.from_dict(dfdict, orient='index', columns=['CAN ID', 'RTR', 'DLC', 'Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6', 'Data7', 'Data8'])
|
111 |
+
df.index.name = 'Timestamp' # Set the index name
|
112 |
return df
|
113 |
|
114 |
+
|
115 |
+
# Gradio interface function
|
116 |
def interface_func(uploaded_file):
|
117 |
# Parse the text file into a DataFrame
|
118 |
df = parse_text_file(uploaded_file)
|
119 |
|
120 |
+
# Return the DataFrame as an HTML table for visualization
|
121 |
+
return df.to_html() # You can also return other outputs if needed
|
122 |
+
|
123 |
|
124 |
# Set up the Gradio interface
|
125 |
iface = gr.Interface(
|
126 |
fn=interface_func,
|
127 |
inputs=gr.File(label="Upload a text file"),
|
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()
|