Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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="WildVision ๐พ | Animal Identifier",
|
| 10 |
+
layout="centered",
|
| 11 |
+
initial_sidebar_state="auto"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
st.title("๐ค Smart Animal Identifier")
|
| 15 |
+
st.markdown("Upload up to **3 animal images** and discover what species they are!")
|
| 16 |
+
|
| 17 |
+
# โ
Debug Check
|
| 18 |
+
st.write("๐ง App started successfully")
|
| 19 |
+
|
| 20 |
+
# ๐ฆ Load model and class labels with error handling
|
| 21 |
+
@st.cache_resource
|
| 22 |
+
def load_model_once():
|
| 23 |
+
return load_model("animal_model.keras")
|
| 24 |
+
|
| 25 |
+
@st.cache_data
|
| 26 |
+
def load_labels():
|
| 27 |
+
return np.load("class_labels.npy", allow_pickle=True).item()
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
model = load_model_once()
|
| 31 |
+
st.success("โ
Model loaded successfully")
|
| 32 |
+
except Exception as e:
|
| 33 |
+
st.error(f"โ Failed to load model: {e}")
|
| 34 |
+
st.stop()
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
class_indices = load_labels()
|
| 38 |
+
st.success("โ
Labels loaded successfully")
|
| 39 |
+
except Exception as e:
|
| 40 |
+
st.error(f"โ Failed to load label file: {e}")
|
| 41 |
+
st.stop()
|
| 42 |
+
|
| 43 |
+
# ๐ Reverse index to get labels from prediction index
|
| 44 |
+
index_to_label = {v: k for k, v in class_indices.items()}
|
| 45 |
+
|
| 46 |
+
# ๐ค Upload Images
|
| 47 |
+
uploaded_files = st.file_uploader(
|
| 48 |
+
"๐ธ Upload your animal photos",
|
| 49 |
+
type=["jpg", "jpeg", "png"],
|
| 50 |
+
accept_multiple_files=True
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
if uploaded_files and len(uploaded_files) > 0:
|
| 54 |
+
uploaded_files = uploaded_files[:3] # Limit to 3 images
|
| 55 |
+
cols = st.columns(len(uploaded_files))
|
| 56 |
+
|
| 57 |
+
for idx, uploaded_file in enumerate(uploaded_files):
|
| 58 |
+
with cols[idx]:
|
| 59 |
+
st.markdown(f"#### ๐ท Image {idx + 1}")
|
| 60 |
+
|
| 61 |
+
# ๐ผ๏ธ Load and preview
|
| 62 |
+
try:
|
| 63 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 64 |
+
preview = image.copy()
|
| 65 |
+
preview.thumbnail((150, 150))
|
| 66 |
+
st.image(preview, caption="Image Preview", use_container_width=True)
|
| 67 |
+
except Exception as e:
|
| 68 |
+
st.error(f"โ ๏ธ Could not load image: {e}")
|
| 69 |
+
continue
|
| 70 |
+
|
| 71 |
+
# ๐งช Preprocess image
|
| 72 |
+
resized = image.resize((128, 128))
|
| 73 |
+
img_array = img_to_array(resized) / 255.0
|
| 74 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 75 |
+
|
| 76 |
+
# ๐ฎ Make prediction
|
| 77 |
+
try:
|
| 78 |
+
preds = model.predict(img_array)[0]
|
| 79 |
+
except Exception as e:
|
| 80 |
+
st.error(f"โ Prediction failed: {e}")
|
| 81 |
+
continue
|
| 82 |
+
|
| 83 |
+
if len(preds) != len(index_to_label):
|
| 84 |
+
st.error("โ ๏ธ Mismatch between model output and class labels.")
|
| 85 |
+
continue
|
| 86 |
+
|
| 87 |
+
# ๐ Display predictions
|
| 88 |
+
top_indices = preds.argsort()[-3:][::-1]
|
| 89 |
+
top_labels = [index_to_label[i] for i in top_indices]
|
| 90 |
+
top_scores = [preds[i] for i in top_indices]
|
| 91 |
+
|
| 92 |
+
st.markdown("#### ๐ง Top Predictions:")
|
| 93 |
+
for label, score in zip(top_labels, top_scores):
|
| 94 |
+
st.write(f"โก๏ธ **{label.capitalize()}**: {score:.2%}")
|
| 95 |
+
|
| 96 |
+
st.success(f"๐ฏ **Most Likely:** *{top_labels[0].capitalize()}*")
|
| 97 |
+
|
| 98 |
+
else:
|
| 99 |
+
st.info("๐ฅ Please upload 1 to 3 animal images to get started.")
|
| 100 |
+
|
| 101 |
+
# ๐ Footer
|
| 102 |
+
st.markdown("---")
|
| 103 |
+
st.caption("๐พ Developed by Neena | Powered by TensorFlow & Streamlit")
|