willco-afk commited on
Commit
709e98a
·
verified ·
1 Parent(s): 9268827

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -5
app.py CHANGED
@@ -12,16 +12,37 @@ if hf_token:
12
 
13
  # Download and load the model from the Hugging Face Hub
14
  repo_id = os.environ.get("MODEL_ID", "willco-afk/tree-test-x") # Get repo ID from secret or default
15
- filename = "your_trained_model.keras" # Name of your .keras file
16
- cache_dir = "./models" # Local directory to cache the model (create if it doesn't exist)
17
- os.makedirs(cache_dir, exist_ok=True) # Create the directory if it doesn't exist
18
  model_path = hf_hub_download(repo_id=repo_id, filename=filename, cache_dir=cache_dir)
19
 
20
- # Load the model (this line should replace the Google Drive loading line)
21
  model = tf.keras.models.load_model(model_path)
22
 
23
  # Streamlit UI
24
  st.title("Christmas Tree Classifier")
25
  st.write("Upload an image of a Christmas tree to classify it:")
26
 
27
- # ... (rest of your Streamlit code remains the same) ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  # Download and load the model from the Hugging Face Hub
14
  repo_id = os.environ.get("MODEL_ID", "willco-afk/tree-test-x") # Get repo ID from secret or default
15
+ filename = "your_trained_model.keras" # Updated filename
16
+ cache_dir = "./models" # Local directory to cache the model
17
+ os.makedirs(cache_dir, exist_ok=True)
18
  model_path = hf_hub_download(repo_id=repo_id, filename=filename, cache_dir=cache_dir)
19
 
20
+ # Load the model
21
  model = tf.keras.models.load_model(model_path)
22
 
23
  # Streamlit UI
24
  st.title("Christmas Tree Classifier")
25
  st.write("Upload an image of a Christmas tree to classify it:")
26
 
27
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
28
+
29
+ if uploaded_file is not None:
30
+ # Display the uploaded image
31
+ image = Image.open(uploaded_file)
32
+ st.image(image, caption="Uploaded Image.", use_column_width=True)
33
+ st.write("")
34
+ st.write("Classifying...")
35
+
36
+ # Preprocess the image
37
+ image = image.resize((224, 224)) # Resize to match your model's input size
38
+ image_array = np.array(image) / 255.0 # Normalize pixel values
39
+ image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
40
+
41
+ # Make prediction
42
+ prediction = model.predict(image_array)
43
+
44
+ # Get predicted class
45
+ predicted_class = "Decorated" if prediction[0][0] >= 0.5 else "Undecorated"
46
+
47
+ # Display the prediction
48
+ st.write(f"Prediction: {predicted_class}")