Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import io
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
import streamlit as st
|
| 4 |
from fastai.vision.all import load_learner, PILImage
|
|
@@ -89,6 +90,26 @@ st.markdown("""
|
|
| 89 |
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
|
| 90 |
}
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
/* Image container */
|
| 93 |
.uploaded-image {
|
| 94 |
border-radius: 15px;
|
|
@@ -97,6 +118,24 @@ st.markdown("""
|
|
| 97 |
margin: 2rem 0;
|
| 98 |
}
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
/* Probability bars */
|
| 101 |
.prob-bar {
|
| 102 |
background: #f0f2f6;
|
|
@@ -139,7 +178,7 @@ def load_model():
|
|
| 139 |
learner = load_learner(MODEL_PATH)
|
| 140 |
return learner
|
| 141 |
except Exception as e:
|
| 142 |
-
st.error(f⚠️ Error loading model:\n\n{e}")
|
| 143 |
return None
|
| 144 |
|
| 145 |
def predict(learner, img_bytes: bytes):
|
|
@@ -148,6 +187,22 @@ def predict(learner, img_bytes: bytes):
|
|
| 148 |
pred, pred_idx, probs = learner.predict(img)
|
| 149 |
return pred, probs
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
def main():
|
| 152 |
# Header
|
| 153 |
st.title("🎮 Pokémon Gen 9 Classifier")
|
|
@@ -163,26 +218,46 @@ def main():
|
|
| 163 |
|
| 164 |
# Example images section
|
| 165 |
st.markdown("---")
|
| 166 |
-
st.markdown("### 🖼️ Try with Example Images")
|
| 167 |
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
examples_path = Path("examples")
|
| 170 |
|
| 171 |
-
if
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
# Store the selected example in session state
|
| 183 |
st.session_state.example_image = img_path
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
else:
|
| 187 |
st.info("💡 **Tip:** Create an 'examples' folder with sample Pokémon images to display them here!")
|
| 188 |
|
|
|
|
| 1 |
import io
|
| 2 |
+
import random
|
| 3 |
from pathlib import Path
|
| 4 |
import streamlit as st
|
| 5 |
from fastai.vision.all import load_learner, PILImage
|
|
|
|
| 90 |
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
|
| 91 |
}
|
| 92 |
|
| 93 |
+
/* Navigation buttons styling */
|
| 94 |
+
div[data-testid="column"] .stButton > button {
|
| 95 |
+
padding: 0.5rem 1rem;
|
| 96 |
+
font-size: 0.9rem;
|
| 97 |
+
min-width: 50px;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
/* Page number buttons */
|
| 101 |
+
.stButton > button[kind="primary"] {
|
| 102 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 103 |
+
font-weight: 700;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
.stButton > button[kind="secondary"] {
|
| 107 |
+
background: white;
|
| 108 |
+
color: #667eea;
|
| 109 |
+
border: 2px solid #667eea;
|
| 110 |
+
font-weight: 500;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
/* Image container */
|
| 114 |
.uploaded-image {
|
| 115 |
border-radius: 15px;
|
|
|
|
| 118 |
margin: 2rem 0;
|
| 119 |
}
|
| 120 |
|
| 121 |
+
/* Example image containers */
|
| 122 |
+
.example-image-container {
|
| 123 |
+
aspect-ratio: 1 / 1;
|
| 124 |
+
overflow: hidden;
|
| 125 |
+
border-radius: 10px;
|
| 126 |
+
margin-bottom: 0.5rem;
|
| 127 |
+
background: #f0f2f6;
|
| 128 |
+
display: flex;
|
| 129 |
+
align-items: center;
|
| 130 |
+
justify-content: center;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
.example-image-container img {
|
| 134 |
+
width: 100%;
|
| 135 |
+
height: 100%;
|
| 136 |
+
object-fit: cover;
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
/* Probability bars */
|
| 140 |
.prob-bar {
|
| 141 |
background: #f0f2f6;
|
|
|
|
| 178 |
learner = load_learner(MODEL_PATH)
|
| 179 |
return learner
|
| 180 |
except Exception as e:
|
| 181 |
+
st.error(f"⚠️ Error loading model:\n\n{e}")
|
| 182 |
return None
|
| 183 |
|
| 184 |
def predict(learner, img_bytes: bytes):
|
|
|
|
| 187 |
pred, pred_idx, probs = learner.predict(img)
|
| 188 |
return pred, probs
|
| 189 |
|
| 190 |
+
def get_random_examples(examples_path, num_examples=5):
|
| 191 |
+
"""Get random example images from the examples folder."""
|
| 192 |
+
if not examples_path.exists():
|
| 193 |
+
return []
|
| 194 |
+
|
| 195 |
+
all_images = (list(examples_path.glob("*.jpg")) +
|
| 196 |
+
list(examples_path.glob("*.png")) +
|
| 197 |
+
list(examples_path.glob("*.jpeg")))
|
| 198 |
+
|
| 199 |
+
if not all_images:
|
| 200 |
+
return []
|
| 201 |
+
|
| 202 |
+
# Get random sample (or all if less than num_examples)
|
| 203 |
+
num_to_show = min(num_examples, len(all_images))
|
| 204 |
+
return random.sample(all_images, num_to_show)
|
| 205 |
+
|
| 206 |
def main():
|
| 207 |
# Header
|
| 208 |
st.title("🎮 Pokémon Gen 9 Classifier")
|
|
|
|
| 218 |
|
| 219 |
# Example images section
|
| 220 |
st.markdown("---")
|
|
|
|
| 221 |
|
| 222 |
+
col_header = st.columns([4, 1])
|
| 223 |
+
with col_header[0]:
|
| 224 |
+
st.markdown("### 🖼️ Try with Example Images")
|
| 225 |
+
with col_header[1]:
|
| 226 |
+
if st.button("🔄 Shuffle", help="Load 5 new random examples"):
|
| 227 |
+
st.rerun()
|
| 228 |
+
|
| 229 |
+
# Define example images path
|
| 230 |
examples_path = Path("examples")
|
| 231 |
|
| 232 |
+
# Initialize random examples in session state if not exists
|
| 233 |
+
if 'random_examples' not in st.session_state:
|
| 234 |
+
st.session_state.random_examples = get_random_examples(examples_path, 5)
|
| 235 |
+
|
| 236 |
+
if st.session_state.random_examples:
|
| 237 |
+
# Display examples in a grid (always 5 columns for consistency)
|
| 238 |
+
cols = st.columns(5)
|
| 239 |
|
| 240 |
+
for idx in range(5):
|
| 241 |
+
with cols[idx]:
|
| 242 |
+
if idx < len(st.session_state.random_examples):
|
| 243 |
+
img_path = st.session_state.random_examples[idx]
|
| 244 |
+
# Use HTML container for consistent sizing
|
| 245 |
+
st.markdown(
|
| 246 |
+
'<div class="example-image-container">',
|
| 247 |
+
unsafe_allow_html=True
|
| 248 |
+
)
|
| 249 |
+
st.image(str(img_path), use_container_width=True)
|
| 250 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 251 |
+
st.caption(img_path.stem)
|
| 252 |
+
if st.button("Use", key=f"example_{idx}"):
|
| 253 |
# Store the selected example in session state
|
| 254 |
st.session_state.example_image = img_path
|
| 255 |
+
st.rerun()
|
| 256 |
+
else:
|
| 257 |
+
# Empty placeholder to maintain grid consistency
|
| 258 |
+
st.markdown('<div style="height: 200px;"></div>', unsafe_allow_html=True)
|
| 259 |
+
elif examples_path.exists():
|
| 260 |
+
st.info("No example images found in the 'examples' folder.")
|
| 261 |
else:
|
| 262 |
st.info("💡 **Tip:** Create an 'examples' folder with sample Pokémon images to display them here!")
|
| 263 |
|