willco-afk commited on
Commit
3cc7104
·
verified ·
1 Parent(s): 20c1e0b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -26
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 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,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
- 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:
 
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: