willco-afk commited on
Commit
f0ecbdc
·
verified ·
1 Parent(s): f5776f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -33
app.py CHANGED
@@ -1,36 +1,72 @@
1
- import os import streamlit as st import tensorflow as tf from PIL import Image import numpy as np from huggingface_hub import login, hf_hub_download
2
- Authenticate with Hugging Face token (if available)
 
 
 
 
3
 
4
- hf_token = os.environ.get("HF_TOKEN") if hf_token: login(token=hf_token)
5
- Download and load the model from the Hugging Face Hub
 
 
6
 
7
- repo_id = os.environ.get("MODEL_ID", "willco-afk/tree-test-x") # Get repo ID from secret or default filename = "your_trained_model.keras" # Updated filename cache_dir = "./models" # Local directory to cache the model os.makedirs(cache_dir, exist_ok=True)
 
 
 
 
 
 
8
  model_path = hf_hub_download(repo_id=repo_id, filename=filename, cache_dir=cache_dir)
9
- Load the model
10
-
11
- model = tf.keras.models.load_model(model_path)
12
- Streamlit UI
13
-
14
- st.title("Christmas Tree Classifier") st.write("Upload an image of a Christmas tree to classify it:")
15
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
16
- if uploaded_file is not None:
17
- # Display the uploaded image
18
- image = Image.open(uploaded_file)
19
- # Updated Line:
20
- st.image(image, caption="Uploaded Image.", use_container_width=True)
21
- st.write("")
22
- st.write("Classifying...")
23
-
24
- # Preprocess the image
25
- image = image.resize((224, 224)) # Resize to match your model's input size
26
- image_array = np.array(image) / 255.0 # Normalize pixel values
27
- image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
28
-
29
- # Make prediction
30
- prediction = model.predict(image_array)
31
-
32
- # Get predicted class
33
- predicted_class = "Decorated" if prediction[0][0] >= 0.5 else "Undecorated"
34
-
35
- # Display the prediction
36
- st.write(f"Prediction: {predicted_class}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ import tensorflow as tf
4
+ from PIL import Image
5
+ import numpy as np
6
+ from huggingface_hub import login, hf_hub_download
7
 
8
+ # Authenticate with Hugging Face token (if available)
9
+ hf_token = os.environ.get("HF_TOKEN")
10
+ if hf_token:
11
+ login(token=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_resnet50.keras.zip" # Updated filename
16
+ cache_dir = "./models" # Local directory to cache the model
17
+ os.makedirs(cache_dir, exist_ok=True)
18
+
19
+ # Download the model file from Hugging Face
20
  model_path = hf_hub_download(repo_id=repo_id, filename=filename, cache_dir=cache_dir)
21
+
22
+ # Extract and load the model
23
+ model_unzipped_path = os.path.join(cache_dir, "your_trained_model_resnet50.keras") # Path where we will extract the model
24
+ if not os.path.exists(model_unzipped_path):
25
+ with zipfile.ZipFile(model_path, 'r') as zip_ref:
26
+ zip_ref.extractall(cache_dir)
27
+ print(f"Model unzipped to {model_unzipped_path}")
28
+
29
+ # Load the model
30
+ model = tf.keras.models.load_model(model_unzipped_path)
31
+
32
+ # Function for image prediction
33
+ def predict_decoration(image: Image.Image):
34
+ # Preprocess the image to match the model input size
35
+ image = image.resize((224, 224)) # Resize to match model's expected input size
36
+ image_array = np.array(image) / 255.0 # Normalize image to [0, 1]
37
+ image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
38
+
39
+ # Make prediction
40
+ prediction = model.predict(image_array)
41
+ return "Decorated" if prediction[0][0] >= 0.5 else "Undecorated"
42
+
43
+ # Streamlit UI
44
+ st.title("🎄 Christmas Tree Classifier 🎄")
45
+ st.write("Upload an image of a Christmas tree to classify it:")
46
+
47
+ # Create tabs
48
+ tab1, tab2 = st.tabs(["Christmas Tree Classifier", "Sample Images"])
49
+
50
+ # Tab 1: Christmas Tree Classifier
51
+ with tab1:
52
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
53
+
54
+ if uploaded_file is not None:
55
+ # Display the uploaded image
56
+ image = Image.open(uploaded_file)
57
+ st.image(image, caption="Uploaded Image.", use_container_width=True)
58
+
59
+ st.write("Classifying...")
60
+
61
+ # Get prediction
62
+ predicted_class = predict_decoration(image)
63
+ st.write(f"Prediction: {predicted_class}")
64
+
65
+ # Tab 2: Sample Images (with text and links only)
66
+ with tab2:
67
+ st.header("Sample Images for the Model")
68
+ st.write("View some of my decorated and undecorated tree samples for the Model here:")
69
+ st.write("[Dropbox Link for Viewing Samples](https://www.dropbox.com/scl/fo/cuzo12z39cxv6joz7gz2o/h?rlkey=w10usqhkngf2uxwvllgnqb8tf&st=ld22fl4c&dl=0)")
70
+
71
+ st.write("Download the tree sample images to test them on the model yourself here:")
72
+ st.write("[Dropbox Link for Downloading Samples](https://www.dropbox.com/scl/fo/cuzo12z39cxv6joz7gz2o/h?rlkey=w10usqhkngf2uxwvllgnqb8tf&st=ld22fl4c&dl=1)")