willco-afk commited on
Commit
28d87da
·
verified ·
1 Parent(s): e26a53c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -9
app.py CHANGED
@@ -3,20 +3,32 @@ import os
3
  import numpy as np
4
  import tensorflow as tf
5
  from PIL import Image
6
- import requests
7
 
8
- # Path to your model file (corrected path)
9
- MODEL_PATH = '/Users/user/Desktop/a.links/your_trained_model.keras.zip'
10
 
11
- # Check if the model file exists and load it
 
12
  if os.path.exists(MODEL_PATH):
13
- model = tf.keras.models.load_model(MODEL_PATH)
14
- print("Model loaded successfully!")
 
 
 
 
 
 
 
 
15
  else:
16
  print(f"Model file not found at {MODEL_PATH}")
17
 
18
- # Define the function to predict decoration
19
  def predict_decoration(image: Image.Image):
 
 
 
20
  # Preprocess the image to match the model input format
21
  image = image.resize((224, 224)) # Resize to match model's expected input size
22
  image_array = np.array(image) / 255.0 # Normalize the image to [0, 1]
@@ -84,8 +96,11 @@ with tab1:
84
  st.image(image, caption="Uploaded Tree Image", use_container_width=True)
85
 
86
  if st.button("Predict Decoration"):
87
- prediction = predict_decoration(image)
88
- st.write(f"Prediction: {prediction}")
 
 
 
89
 
90
  # Tree Image URLs Tab
91
  with tab2:
 
3
  import numpy as np
4
  import tensorflow as tf
5
  from PIL import Image
6
+ import zipfile
7
 
8
+ # Path to your model zip file in the Hugging Face Space
9
+ MODEL_PATH = '/app/your_trained_model.keras.zip' # Adjust this path for Hugging Face
10
 
11
+ # Check if the model file exists and load the model
12
+ model = None # Initialize model variable outside the try block
13
  if os.path.exists(MODEL_PATH):
14
+ try:
15
+ # Unzip the model file in the Hugging Face Space directory if needed
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
+ # If model is not loaded, return an error in the prediction function
28
  def predict_decoration(image: Image.Image):
29
+ if model is None:
30
+ raise ValueError("Model is not loaded, cannot make predictions.")
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
  st.image(image, caption="Uploaded Tree Image", use_container_width=True)
97
 
98
  if st.button("Predict Decoration"):
99
+ try:
100
+ prediction = predict_decoration(image)
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: