Files changed (1) hide show
  1. app.py +16 -19
app.py CHANGED
@@ -1,25 +1,23 @@
 
1
  import streamlit as st
2
  import requests
3
  import numpy as np
4
  from PIL import Image
5
- from tensorflow import keras
6
 
7
- # Cache the model download and loading to avoid reloading on every interaction
 
 
 
 
 
 
 
8
  @st.cache_resource
9
  def load_model():
10
- # URL of the model file on Hugging Face
11
- model_url = "https://huggingface.co/spaces/langatnaomi10/CNN/resolve/main/trained_model.pkl"
12
-
13
- # Download the file
14
- model_path = "trained_model.pkl"
15
- response = requests.get(model_url, stream=True)
16
- with open(model_path, "wb") as file:
17
- for chunk in response.iter_content(chunk_size=1024):
18
- if chunk:
19
- file.write(chunk)
20
-
21
- # Load the saved model
22
- model = keras.models.load_model(model_path)
23
  return model
24
 
25
  # Load the model
@@ -29,7 +27,7 @@ except Exception as e:
29
  st.error(f"Failed to load the model: {e}")
30
  st.stop()
31
 
32
- # Define a prediction function
33
  def predict_axle_configuration(image):
34
  # Resize and preprocess the image
35
  image = image.resize((128, 128)) # Resize to match model input size
@@ -49,12 +47,11 @@ if uploaded_file:
49
  img = Image.open(uploaded_file)
50
  st.image(img, caption='Uploaded Image', use_column_width=True)
51
  st.write("Classifying...")
52
-
53
  # Get prediction
54
  result = predict_axle_configuration(img)
55
-
56
  # Display prediction (assuming result is a probability or class index)
57
  st.write(f"Predicted Axle Configuration: {result}")
58
  except Exception as e:
59
  st.error(f"An error occurred during prediction: {e}")
60
-
 
1
+ import os
2
  import streamlit as st
3
  import requests
4
  import numpy as np
5
  from PIL import Image
6
+ import pickle # Using pickle since the model is saved as a .pkl file
7
 
8
+ # Define the absolute path for the model
9
+ MODEL_PATH = "/app/trained_model.pkl"
10
+
11
+ # Ensure the model has the correct read permissions
12
+ if os.path.exists(MODEL_PATH):
13
+ os.chmod(MODEL_PATH, 0o644)
14
+
15
+ # Cache model loading to avoid repeated downloads
16
  @st.cache_resource
17
  def load_model():
18
+ # Load the trained model from the saved .pkl file
19
+ with open(MODEL_PATH, "rb") as file:
20
+ model = pickle.load(file)
 
 
 
 
 
 
 
 
 
 
21
  return model
22
 
23
  # Load the model
 
27
  st.error(f"Failed to load the model: {e}")
28
  st.stop()
29
 
30
+ # Define the prediction function
31
  def predict_axle_configuration(image):
32
  # Resize and preprocess the image
33
  image = image.resize((128, 128)) # Resize to match model input size
 
47
  img = Image.open(uploaded_file)
48
  st.image(img, caption='Uploaded Image', use_column_width=True)
49
  st.write("Classifying...")
50
+
51
  # Get prediction
52
  result = predict_axle_configuration(img)
53
+
54
  # Display prediction (assuming result is a probability or class index)
55
  st.write(f"Predicted Axle Configuration: {result}")
56
  except Exception as e:
57
  st.error(f"An error occurred during prediction: {e}")