Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -5,30 +5,25 @@ import tensorflow as tf
|
|
| 5 |
from PIL import Image
|
| 6 |
import zipfile
|
| 7 |
|
| 8 |
-
# Path to your model
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
with zipfile.ZipFile(MODEL_PATH, 'r') as zip_ref:
|
| 17 |
-
zip_ref.extractall("/app") # Adjust the extraction directory in Hugging Face Space
|
| 18 |
-
|
| 19 |
-
# Now load the model
|
| 20 |
-
model = tf.keras.models.load_model("/app/your_trained_model.keras") # Adjust to the unzipped path
|
| 21 |
-
print("Model loaded successfully!")
|
| 22 |
-
except Exception as e:
|
| 23 |
-
print(f"Error loading model: {e}")
|
| 24 |
-
else:
|
| 25 |
-
print(f"Model file not found at {MODEL_PATH}")
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
|
|
|
|
|
|
| 32 |
# Preprocess the image to match the model input format
|
| 33 |
image = image.resize((224, 224)) # Resize to match model's expected input size
|
| 34 |
image_array = np.array(image) / 255.0 # Normalize the image to [0, 1]
|
|
@@ -96,11 +91,8 @@ with tab1:
|
|
| 96 |
st.image(image, caption="Uploaded Tree Image", use_container_width=True)
|
| 97 |
|
| 98 |
if st.button("Predict Decoration"):
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
st.write(f"Prediction: {prediction}")
|
| 102 |
-
except ValueError as e:
|
| 103 |
-
st.error(str(e))
|
| 104 |
|
| 105 |
# Tree Image URLs Tab
|
| 106 |
with tab2:
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import zipfile
|
| 7 |
|
| 8 |
+
# Path to your zipped model file
|
| 9 |
+
ZIP_MODEL_PATH = '/app/your_trained_model.keras.zip' # Correct this path if needed
|
| 10 |
+
UNZIPPED_MODEL_PATH = '/app/your_trained_model.keras' # Path where the model will be extracted
|
| 11 |
|
| 12 |
+
# Unzip the model if it hasn't been unzipped already
|
| 13 |
+
if not os.path.exists(UNZIPPED_MODEL_PATH):
|
| 14 |
+
with zipfile.ZipFile(ZIP_MODEL_PATH, 'r') as zip_ref:
|
| 15 |
+
zip_ref.extractall('/app')
|
| 16 |
+
print(f"Model unzipped to {UNZIPPED_MODEL_PATH}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Load the model
|
| 19 |
+
try:
|
| 20 |
+
model = tf.keras.models.load_model(UNZIPPED_MODEL_PATH)
|
| 21 |
+
print("Model loaded successfully!")
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"Error loading model: {e}")
|
| 24 |
|
| 25 |
+
# Define the function to predict decoration
|
| 26 |
+
def predict_decoration(image: Image.Image):
|
| 27 |
# Preprocess the image to match the model input format
|
| 28 |
image = image.resize((224, 224)) # Resize to match model's expected input size
|
| 29 |
image_array = np.array(image) / 255.0 # Normalize the image to [0, 1]
|
|
|
|
| 91 |
st.image(image, caption="Uploaded Tree Image", use_container_width=True)
|
| 92 |
|
| 93 |
if st.button("Predict Decoration"):
|
| 94 |
+
prediction = predict_decoration(image)
|
| 95 |
+
st.write(f"Prediction: {prediction}")
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# Tree Image URLs Tab
|
| 98 |
with tab2:
|