Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,62 +56,5 @@ if uploaded_file:
|
|
| 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 |
-
import requests
|
| 61 |
-
import numpy as np
|
| 62 |
-
from PIL import Image
|
| 63 |
-
from tensorflow import keras
|
| 64 |
-
|
| 65 |
-
# Cache the model download and loading to avoid reloading on every interaction
|
| 66 |
-
@st.cache_resource
|
| 67 |
-
def load_model():
|
| 68 |
-
# URL of the model file on Hugging Face
|
| 69 |
-
model_url = "https://huggingface.co/spaces/langatnaomi10/CNN/resolve/main/trained_model.h5"
|
| 70 |
-
|
| 71 |
-
# Download the file
|
| 72 |
-
model_path = "trained_model.h5"
|
| 73 |
-
response = requests.get(model_url, stream=True)
|
| 74 |
-
with open(model_path, "wb") as file:
|
| 75 |
-
for chunk in response.iter_content(chunk_size=1024):
|
| 76 |
-
if chunk:
|
| 77 |
-
file.write(chunk)
|
| 78 |
-
|
| 79 |
-
# Load the saved model
|
| 80 |
-
model = keras.models.load_model(model_path)
|
| 81 |
-
return model
|
| 82 |
-
|
| 83 |
-
# Load the model
|
| 84 |
-
try:
|
| 85 |
-
model = load_model()
|
| 86 |
-
except Exception as e:
|
| 87 |
-
st.error(f"Failed to load the model: {e}")
|
| 88 |
-
st.stop()
|
| 89 |
|
| 90 |
-
# Define a prediction function
|
| 91 |
-
def predict_axle_configuration(image):
|
| 92 |
-
# Resize and preprocess the image
|
| 93 |
-
image = image.resize((128, 128)) # Resize to match model input size
|
| 94 |
-
image_array = np.array(image) / 255.0 # Normalize pixel values to [0, 1]
|
| 95 |
-
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
| 96 |
-
|
| 97 |
-
# Make prediction
|
| 98 |
-
prediction = model.predict(image_array)
|
| 99 |
-
return prediction
|
| 100 |
-
|
| 101 |
-
# Streamlit UI
|
| 102 |
-
st.title("Vehicle Axle Configuration Prediction")
|
| 103 |
-
uploaded_file = st.file_uploader("Upload a vehicle image", type=['jpg', 'jpeg', 'png'])
|
| 104 |
-
|
| 105 |
-
if uploaded_file:
|
| 106 |
-
try:
|
| 107 |
-
img = Image.open(uploaded_file)
|
| 108 |
-
st.image(img, caption='Uploaded Image', use_column_width=True)
|
| 109 |
-
st.write("Classifying...")
|
| 110 |
-
|
| 111 |
-
# Get prediction
|
| 112 |
-
result = predict_axle_configuration(img)
|
| 113 |
-
|
| 114 |
-
# Display prediction (assuming result is a probability or class index)
|
| 115 |
-
st.write(f"Predicted Axle Configuration: {result}")
|
| 116 |
-
except Exception as e:
|
| 117 |
-
st.error(f"An error occurred during prediction: {e}")
|
|
|
|
| 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|