Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import numpy as np
|
4 |
+
import tensorflow as tf
|
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]
|
30 |
+
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
31 |
+
|
32 |
+
# Make prediction
|
33 |
+
prediction = model.predict(image_array)
|
34 |
+
return "Decorated" if prediction[0] > 0.5 else "Undecorated"
|
35 |
+
|
36 |
+
# Set up Streamlit interface with Christmas theme
|
37 |
+
st.set_page_config(page_title="Tree Decoration Predictor", page_icon="π")
|
38 |
+
|
39 |
+
# Custom CSS for Christmas theme
|
40 |
+
st.markdown("""
|
41 |
+
<style>
|
42 |
+
body {
|
43 |
+
background-color: #fae1dc; /* Soft pink background */
|
44 |
+
color: #1b5e20; /* Deep green text */
|
45 |
+
font-family: 'Comic Sans MS', cursive, sans-serif;
|
46 |
+
}
|
47 |
+
.css-18e3th9 {
|
48 |
+
background-color: #d32f2f; /* Christmas red button */
|
49 |
+
color: white;
|
50 |
+
}
|
51 |
+
.css-1lcbm2e {
|
52 |
+
background-color: #388e3c; /* Christmas green button */
|
53 |
+
color: white;
|
54 |
+
}
|
55 |
+
.stButton>button {
|
56 |
+
background-color: #f44336; /* Red button color */
|
57 |
+
color: white;
|
58 |
+
border-radius: 12px;
|
59 |
+
padding: 10px;
|
60 |
+
font-size: 16px;
|
61 |
+
}
|
62 |
+
.stButton>button:hover {
|
63 |
+
background-color: #c62828; /* Darker red on hover */
|
64 |
+
}
|
65 |
+
.stMarkdown {
|
66 |
+
font-size: 18px;
|
67 |
+
}
|
68 |
+
.stTab {
|
69 |
+
font-size: 20px;
|
70 |
+
font-weight: bold;
|
71 |
+
color: #388e3c; /* Christmas green */
|
72 |
+
}
|
73 |
+
.stImage {
|
74 |
+
border: 2px solid #388e3c; /* Green border around images */
|
75 |
+
}
|
76 |
+
</style>
|
77 |
+
""", unsafe_allow_html=True)
|
78 |
+
|
79 |
+
# Title of the page
|
80 |
+
st.title("π Tree Decoration Predictor π")
|
81 |
+
|
82 |
+
# Create tabs for better organization
|
83 |
+
tab1, tab2 = st.tabs(["Upload Image", "Tree Image URLs"])
|
84 |
+
|
85 |
+
# Upload Image Tab
|
86 |
+
with tab1:
|
87 |
+
uploaded_image = st.file_uploader("Upload an image of a tree", type=["jpg", "jpeg", "png"])
|
88 |
+
|
89 |
+
if uploaded_image:
|
90 |
+
image = Image.open(uploaded_image)
|
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:
|
99 |
+
st.subheader("π Tree Image Samples π")
|
100 |
+
st.markdown("""
|
101 |
+
View some of my decorated and undecorated tree samples for the Model here:
|
102 |
+
[View Trees](https://www.dropbox.com/scl/fo/cuzo12z39cxv6joz7gz2o/ACf5xSjT7nHqMRdgh21GYlc?raw=1)
|
103 |
+
|
104 |
+
Download the tree samples pictures to test them on the model yourself here:
|
105 |
+
[Download Trees](https://www.dropbox.com/scl/fo/cuzo12z39cxv6joz7gz2o/ACf5xSjT7nHqMRdgh21GYlc?raw=1&dl=1)
|
106 |
+
""")
|
107 |
+
|
108 |
+
# Add download link for images if needed
|
109 |
+
st.markdown("[Download the image list](https://raw.githubusercontent.com/willco-afk/tree-samples/main/tree_images.txt)")
|