Spaces:
Running
Running
Add 'About' section to explain app purpose and features
Browse files- app.py +49 -10
- dataset.zip +3 -0
- requirements.txt +0 -0
app.py
CHANGED
@@ -7,17 +7,32 @@ import altair as alt
|
|
7 |
from PIL import Image
|
8 |
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
9 |
import zipfile
|
|
|
|
|
10 |
|
11 |
# π¨ App Title
|
12 |
st.title("πΆπ± Cat vs Dog Classifier")
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
# β
Detect Environment & Set Dataset Path
|
15 |
-
|
16 |
-
|
17 |
-
ZIP_PATH = "/home/user/app/dataset.zip" # If dataset is uploaded as a ZIP
|
18 |
-
else: # Local Machine
|
19 |
-
BASE_DIR = r"C:\Users\Cherilyn\Downloads\CCS229\cat_vs._dog_classifier\dataset"
|
20 |
-
ZIP_PATH = None # No ZIP needed locally
|
21 |
|
22 |
TRAIN_DIR = os.path.join(BASE_DIR, "train")
|
23 |
TEST_DIR = os.path.join(BASE_DIR, "test")
|
@@ -26,8 +41,8 @@ TEST_DIR = os.path.join(BASE_DIR, "test")
|
|
26 |
if ZIP_PATH and os.path.exists(ZIP_PATH):
|
27 |
if not os.path.exists(BASE_DIR): # Avoid re-extracting
|
28 |
with zipfile.ZipFile(ZIP_PATH, "r") as zip_ref:
|
29 |
-
zip_ref.extractall(
|
30 |
-
|
31 |
|
32 |
# π Check if dataset exists
|
33 |
if not os.path.exists(TRAIN_DIR):
|
@@ -45,13 +60,13 @@ if not os.path.exists(cat_dir) or not os.path.exists(dog_dir):
|
|
45 |
# π Constants
|
46 |
IMG_SIZE = (150, 150)
|
47 |
BATCH_SIZE = 32
|
48 |
-
MODEL_PATH = "cats_dogs_model.h5" #
|
49 |
|
50 |
# π― Load Model
|
51 |
if os.path.exists(MODEL_PATH):
|
52 |
model = tf.keras.models.load_model(MODEL_PATH)
|
53 |
else:
|
54 |
-
st.error("β No trained model found. Please upload '
|
55 |
st.stop()
|
56 |
|
57 |
# π· Image Preprocessing
|
@@ -131,3 +146,27 @@ with tab3:
|
|
131 |
|
132 |
st.subheader("Prediction:")
|
133 |
st.write(f"This is a **{label}** with **{confidence*100:.2f}%** confidence.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
from PIL import Image
|
8 |
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
9 |
import zipfile
|
10 |
+
import io
|
11 |
+
import shutil
|
12 |
|
13 |
# π¨ App Title
|
14 |
st.title("πΆπ± Cat vs Dog Classifier")
|
15 |
|
16 |
+
# π About the App
|
17 |
+
st.write(
|
18 |
+
"""
|
19 |
+
## About This App
|
20 |
+
This is a machine learning application that classifies images into two categories:
|
21 |
+
**Cats π±** and **Dogs πΆ**. The model is trained using a deep learning architecture
|
22 |
+
called Convolutional Neural Networks (CNNs) and is able to distinguish between images
|
23 |
+
of cats and dogs with high accuracy.
|
24 |
+
### Features:
|
25 |
+
- **Dataset Overview**: View the number of images in the dataset, categorized by "Cats" and "Dogs".
|
26 |
+
- **Model Evaluation**: Check the model's performance on the validation set, including accuracy and loss.
|
27 |
+
- **Image Classification**: Upload an image, and the model will predict whether it's a cat or a dog, along with the confidence level.
|
28 |
+
- **Download Test Folder**: Download a ZIP file containing the test images.
|
29 |
+
The app is powered by **Streamlit** for an interactive user interface and **TensorFlow** for image classification.
|
30 |
+
"""
|
31 |
+
)
|
32 |
+
|
33 |
# β
Detect Environment & Set Dataset Path
|
34 |
+
BASE_DIR = "dataset" # In Hugging Face Spaces, the dataset folder should be at the root of the Space
|
35 |
+
ZIP_PATH = "dataset.zip" # If dataset is uploaded as a ZIP (make sure it's in the same directory as app.py)
|
|
|
|
|
|
|
|
|
36 |
|
37 |
TRAIN_DIR = os.path.join(BASE_DIR, "train")
|
38 |
TEST_DIR = os.path.join(BASE_DIR, "test")
|
|
|
41 |
if ZIP_PATH and os.path.exists(ZIP_PATH):
|
42 |
if not os.path.exists(BASE_DIR): # Avoid re-extracting
|
43 |
with zipfile.ZipFile(ZIP_PATH, "r") as zip_ref:
|
44 |
+
zip_ref.extractall(BASE_DIR) # Extract into dataset folder
|
45 |
+
st.success("β
Dataset extracted!")
|
46 |
|
47 |
# π Check if dataset exists
|
48 |
if not os.path.exists(TRAIN_DIR):
|
|
|
60 |
# π Constants
|
61 |
IMG_SIZE = (150, 150)
|
62 |
BATCH_SIZE = 32
|
63 |
+
MODEL_PATH = "cats_dogs_model.h5" # Ensure the model is uploaded to Hugging Face Space
|
64 |
|
65 |
# π― Load Model
|
66 |
if os.path.exists(MODEL_PATH):
|
67 |
model = tf.keras.models.load_model(MODEL_PATH)
|
68 |
else:
|
69 |
+
st.error("β No trained model found. Please upload 'cats_dogs_model.h5' to your Hugging Face repository.")
|
70 |
st.stop()
|
71 |
|
72 |
# π· Image Preprocessing
|
|
|
146 |
|
147 |
st.subheader("Prediction:")
|
148 |
st.write(f"This is a **{label}** with **{confidence*100:.2f}%** confidence.")
|
149 |
+
|
150 |
+
# **New Feature: Download the 'test' folder as a ZIP**
|
151 |
+
def zip_folder(folder_path):
|
152 |
+
# Create an in-memory zip file
|
153 |
+
zip_buffer = io.BytesIO()
|
154 |
+
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
|
155 |
+
for root, dirs, files in os.walk(folder_path):
|
156 |
+
for file in files:
|
157 |
+
zip_file.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), folder_path))
|
158 |
+
zip_buffer.seek(0) # Go to the beginning of the file
|
159 |
+
return zip_buffer
|
160 |
+
|
161 |
+
# Button to download 'test' folder
|
162 |
+
if os.path.exists(TEST_DIR):
|
163 |
+
st.write("### Download Test Folder")
|
164 |
+
zip_buffer = zip_folder(TEST_DIR)
|
165 |
+
st.download_button(
|
166 |
+
label="Download Test Folder as ZIP",
|
167 |
+
data=zip_buffer,
|
168 |
+
file_name="test_folder.zip",
|
169 |
+
mime="application/zip"
|
170 |
+
)
|
171 |
+
else:
|
172 |
+
st.warning(f"β Test folder not found at `{TEST_DIR}`")
|
dataset.zip
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e740e196ea75b73fed88e3b7c7c45b2d15714060814c02e90c7151be68e8d1d7
|
3 |
+
size 67592295
|
requirements.txt
CHANGED
Binary files a/requirements.txt and b/requirements.txt differ
|
|