willco-afk commited on
Commit
e406126
·
verified ·
1 Parent(s): 47823ae

Update app.py

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