Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,101 +1,65 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
Flatten(),
|
| 67 |
-
Dense(128, activation='relu'),
|
| 68 |
-
Dropout(0.3),
|
| 69 |
-
Dense(len(np.unique(y_encoded)), activation='softmax')
|
| 70 |
-
])
|
| 71 |
-
model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
|
| 72 |
-
return model
|
| 73 |
-
|
| 74 |
-
model = build_model()
|
| 75 |
-
|
| 76 |
-
# ๐จโ๐ซ Train the model
|
| 77 |
-
with st.spinner("Training model..."):
|
| 78 |
-
model.fit(X_train, y_train, epochs=5, batch_size=32, validation_split=0.1, verbose=0)
|
| 79 |
-
|
| 80 |
-
st.success("โ
Model trained!")
|
| 81 |
-
|
| 82 |
-
# ๐ค Upload images for prediction
|
| 83 |
-
uploaded_files = st.file_uploader("Upload animal images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
|
| 84 |
-
|
| 85 |
-
if uploaded_files:
|
| 86 |
-
st.markdown("### Predictions")
|
| 87 |
-
cols = st.columns(len(uploaded_files[:3]))
|
| 88 |
-
|
| 89 |
-
for i, file in enumerate(uploaded_files[:3]):
|
| 90 |
-
with cols[i]:
|
| 91 |
-
img = Image.open(file).convert("RGB")
|
| 92 |
-
st.image(img, caption="Uploaded", use_container_width=True)
|
| 93 |
-
|
| 94 |
-
img_resized = img.resize(IMAGE_SIZE)
|
| 95 |
-
arr = img_to_array(img_resized) / 255.0
|
| 96 |
-
arr = np.expand_dims(arr, axis=0)
|
| 97 |
-
|
| 98 |
-
pred = model.predict(arr, verbose=0)[0]
|
| 99 |
-
top_idx = np.argmax(pred)
|
| 100 |
-
label = le.inverse_transform([top_idx])[0]
|
| 101 |
-
st.success(f"๐ Prediction: **{label.capitalize()}**")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from tensorflow.keras.models import load_model
|
| 4 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# ๐ง Configure Streamlit
|
| 8 |
+
st.set_page_config(
|
| 9 |
+
page_title="Animal Classifier",
|
| 10 |
+
layout="centered",
|
| 11 |
+
initial_sidebar_state="auto"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# ๐ฆ Load model and class labels
|
| 15 |
+
@st.cache_resource
|
| 16 |
+
def load_model_once():
|
| 17 |
+
return load_model("animal_model.keras")
|
| 18 |
+
|
| 19 |
+
@st.cache_data
|
| 20 |
+
def load_labels():
|
| 21 |
+
return np.load("class_labels.npy", allow_pickle=True).item()
|
| 22 |
+
|
| 23 |
+
model = load_model_once()
|
| 24 |
+
class_indices = load_labels()
|
| 25 |
+
class_labels = list(class_indices.keys())
|
| 26 |
+
|
| 27 |
+
# ๐ท App Title
|
| 28 |
+
st.title("๐พ Animal Image Classifier")
|
| 29 |
+
st.markdown("Upload up to **3 images** and get predictions")
|
| 30 |
+
|
| 31 |
+
# ๐ค Upload Images
|
| 32 |
+
uploaded_files = st.file_uploader(
|
| 33 |
+
"Upload animal images",
|
| 34 |
+
type=["jpg", "jpeg", "png"],
|
| 35 |
+
accept_multiple_files=True
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if uploaded_files:
|
| 39 |
+
# Limit to 3 images for clarity
|
| 40 |
+
uploaded_files = uploaded_files[:3]
|
| 41 |
+
cols = st.columns(len(uploaded_files))
|
| 42 |
+
|
| 43 |
+
for idx, uploaded_file in enumerate(uploaded_files):
|
| 44 |
+
with cols[idx]:
|
| 45 |
+
st.markdown(f"### Image {idx+1}")
|
| 46 |
+
|
| 47 |
+
# ๐ผ๏ธ Load & visually shrink image
|
| 48 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 49 |
+
preview = image.copy()
|
| 50 |
+
preview.thumbnail((150, 150))
|
| 51 |
+
st.image(preview, caption="Preview", use_container_width=True)
|
| 52 |
+
|
| 53 |
+
# ๐งช Preprocess image for prediction
|
| 54 |
+
resized = image.resize((128, 128))
|
| 55 |
+
img_array = img_to_array(resized) / 255.0
|
| 56 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 57 |
+
|
| 58 |
+
# ๐ฎ Model Prediction
|
| 59 |
+
preds = model.predict(img_array)[0]
|
| 60 |
+
top_indices = preds.argsort()[-3:][::-1]
|
| 61 |
+
top_labels = [class_labels[i] for i in top_indices]
|
| 62 |
+
top_scores = [preds[i] for i in top_indices]
|
| 63 |
+
|
| 64 |
+
# โ
Display Top Prediction
|
| 65 |
+
st.success(f"**๐ฎPrediction:** {top_labels[0].capitalize()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|