chiichann commited on
Commit
7fdb879
Β·
1 Parent(s): c8c8e70

Add 'About' section to explain app purpose and features

Browse files
Files changed (3) hide show
  1. app.py +49 -10
  2. dataset.zip +3 -0
  3. 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
- if os.getenv("HF_HOME"): # Hugging Face
16
- BASE_DIR = "/home/user/app/dataset"
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("/home/user/app/")
30
- print("βœ… Dataset extracted!")
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" # Fixed model filename
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 'cat_dog_model.h5' to your Hugging Face repository.")
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