saramneena commited on
Commit
014679a
ยท
verified ยท
1 Parent(s): 90765bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
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")