sourize commited on
Commit
3bfdb3b
·
verified ·
1 Parent(s): 1641d9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -21
app.py CHANGED
@@ -1,35 +1,30 @@
1
  import streamlit as st
2
  import numpy as np
3
- import joblib
4
  import pandas as pd
 
5
  from tensorflow.keras.models import load_model
6
 
7
- # Load the trained autoencoder model
8
- try:
9
- model = load_model("autoencoder_model.keras")
10
- except Exception as e:
11
- st.error(f"Error loading model: {e}")
12
- st.stop()
 
 
 
13
 
14
- # Load the scaler
15
- try:
16
- with open("scaler.pkl", "rb") as file:
17
- scaler = joblib.load(file)
18
- # Check that the scaler has a transform method
19
- if not hasattr(scaler, "transform"):
20
- raise ValueError("Loaded scaler object does not have a 'transform' method. Please save a valid scaler.")
21
- except Exception as e:
22
- st.error(f"Error loading scaler: {e}")
23
- st.stop()
24
 
25
  # Set an anomaly threshold (adjust based on your model calibration)
26
- THRESHOLD = 0.7
27
 
28
  st.title("Maritime Anomaly Detection")
29
  st.markdown("### Input the following AIS features:")
30
 
31
  # Taking inputs from the user
32
- timestamp_str = st.text_input("# Timestamp (Format: DD/MM/YYYY HH:MM:SS)", value="27/02/2024 03:42:19")
33
  mmsi = st.number_input("MMSI", value=0.0)
34
  latitude = st.number_input("Latitude", value=0.0)
35
  longitude = st.number_input("Longitude", value=0.0)
@@ -39,7 +34,7 @@ heading = st.number_input("Heading", value=0.0)
39
 
40
  if st.button("Run Detection"):
41
  try:
42
- # Convert timestamp to seconds since epoch
43
  timestamp_sec = pd.to_datetime(timestamp_str, format="%d/%m/%Y %H:%M:%S").value / 1e9
44
  except Exception as e:
45
  st.error(f"Invalid Timestamp Format: {e}")
@@ -49,7 +44,7 @@ if st.button("Run Detection"):
49
  input_data = np.array([[timestamp_sec, mmsi, latitude, longitude, sog, cog, heading]])
50
 
51
  try:
52
- # Scale the input data using the scaler's transform method
53
  input_scaled = scaler.transform(input_data)
54
 
55
  # Reconstruct the input using the autoencoder
 
1
  import streamlit as st
2
  import numpy as np
 
3
  import pandas as pd
4
+ import joblib
5
  from tensorflow.keras.models import load_model
6
 
7
+ # Cache the model so it's loaded only once per session
8
+ @st.cache_resource
9
+ def load_my_model():
10
+ return load_model("autoencoder_model.keras")
11
+
12
+ # Cache the scaler as well
13
+ @st.cache_resource
14
+ def load_my_scaler():
15
+ return joblib.load("scaler1.pkl")
16
 
17
+ model = load_my_model()
18
+ scaler = load_my_scaler()
 
 
 
 
 
 
 
 
19
 
20
  # Set an anomaly threshold (adjust based on your model calibration)
21
+ THRESHOLD = 0.1
22
 
23
  st.title("Maritime Anomaly Detection")
24
  st.markdown("### Input the following AIS features:")
25
 
26
  # Taking inputs from the user
27
+ timestamp_str = st.text_input("# Timestamp (Format: DD/MM/YYYY HH:MM:SS)", "27/02/2024 03:42:19")
28
  mmsi = st.number_input("MMSI", value=0.0)
29
  latitude = st.number_input("Latitude", value=0.0)
30
  longitude = st.number_input("Longitude", value=0.0)
 
34
 
35
  if st.button("Run Detection"):
36
  try:
37
+ # Convert the timestamp to seconds since epoch
38
  timestamp_sec = pd.to_datetime(timestamp_str, format="%d/%m/%Y %H:%M:%S").value / 1e9
39
  except Exception as e:
40
  st.error(f"Invalid Timestamp Format: {e}")
 
44
  input_data = np.array([[timestamp_sec, mmsi, latitude, longitude, sog, cog, heading]])
45
 
46
  try:
47
+ # Scale the input data
48
  input_scaled = scaler.transform(input_data)
49
 
50
  # Reconstruct the input using the autoencoder