Spaces:
Sleeping
Sleeping
Commit ·
bd5f5c9
0
Parent(s):
main
Browse files- .gitignore +61 -0
- app.py +152 -0
- cats_v_dogs_classification.ipynb +0 -0
- requirements.txt +6 -0
.gitignore
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Byte-compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
|
| 6 |
+
# C extensions
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Distribution / packaging
|
| 10 |
+
.Python
|
| 11 |
+
build/
|
| 12 |
+
develop-eggs/
|
| 13 |
+
dist/
|
| 14 |
+
downloads/
|
| 15 |
+
eggs/
|
| 16 |
+
.eggs/
|
| 17 |
+
lib/
|
| 18 |
+
lib64/
|
| 19 |
+
parts/
|
| 20 |
+
sdist/
|
| 21 |
+
var/
|
| 22 |
+
wheels/
|
| 23 |
+
*.egg-info/
|
| 24 |
+
.installed.cfg
|
| 25 |
+
*.egg
|
| 26 |
+
|
| 27 |
+
# PyInstaller
|
| 28 |
+
# Usually these files are written by a python script from a template
|
| 29 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 30 |
+
*.manifest
|
| 31 |
+
*.spec
|
| 32 |
+
|
| 33 |
+
# Installer logs
|
| 34 |
+
pip-log.txt
|
| 35 |
+
pip-delete-this-directory.txt
|
| 36 |
+
|
| 37 |
+
# Unit test / coverage reports
|
| 38 |
+
htmlcov/
|
| 39 |
+
.tox/
|
| 40 |
+
.nox/
|
| 41 |
+
.coverage
|
| 42 |
+
.coverage.*
|
| 43 |
+
.cache
|
| 44 |
+
.pytest_cache/
|
| 45 |
+
.hypothesis/
|
| 46 |
+
|
| 47 |
+
# Environments
|
| 48 |
+
.env
|
| 49 |
+
.venv
|
| 50 |
+
venv/
|
| 51 |
+
ENV/
|
| 52 |
+
env/
|
| 53 |
+
venv.bak/
|
| 54 |
+
.vscode/
|
| 55 |
+
|
| 56 |
+
# Streamlit secrets
|
| 57 |
+
.streamlit/
|
| 58 |
+
|
| 59 |
+
# Local model files
|
| 60 |
+
*.keras
|
| 61 |
+
*.h5
|
app.py
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import numpy as np
|
| 7 |
+
import requests
|
| 8 |
+
import os
|
| 9 |
+
from tqdm import tqdm
|
| 10 |
+
|
| 11 |
+
# Set page config
|
| 12 |
+
st.set_page_config(
|
| 13 |
+
page_title="Cat vs. Dog Classifier",
|
| 14 |
+
page_icon="🐾",
|
| 15 |
+
layout="wide",
|
| 16 |
+
initial_sidebar_state="expanded",
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Custom CSS for styling
|
| 20 |
+
st.markdown(
|
| 21 |
+
"""
|
| 22 |
+
<style>
|
| 23 |
+
.main {
|
| 24 |
+
background-color: #f5f5f5;
|
| 25 |
+
}
|
| 26 |
+
.st-bk {
|
| 27 |
+
background-color: #ffffff;
|
| 28 |
+
border-radius: 10px;
|
| 29 |
+
padding: 20px;
|
| 30 |
+
}
|
| 31 |
+
.st-emotion-cache-1v0mbdj > img{
|
| 32 |
+
border-radius: 10px;
|
| 33 |
+
}
|
| 34 |
+
</style>
|
| 35 |
+
""",
|
| 36 |
+
unsafe_allow_html=True,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# --- Model Downloading and Loading ---
|
| 40 |
+
def download_file_from_google_drive(id, destination):
|
| 41 |
+
URL = f'https://drive.google.com/uc?export=download&id={id}'
|
| 42 |
+
session = requests.Session()
|
| 43 |
+
response = session.get(URL, stream=True)
|
| 44 |
+
|
| 45 |
+
token = None
|
| 46 |
+
for key, value in response.cookies.items():
|
| 47 |
+
if key.startswith('download_warning'):
|
| 48 |
+
token = value
|
| 49 |
+
break
|
| 50 |
+
|
| 51 |
+
if token:
|
| 52 |
+
params = {'id': id, 'confirm': token}
|
| 53 |
+
response = session.get(URL, params=params, stream=True)
|
| 54 |
+
|
| 55 |
+
total_size = int(response.headers.get('content-length', 0))
|
| 56 |
+
block_size = 1024 # 1 Kibibyte
|
| 57 |
+
|
| 58 |
+
progress_bar = tqdm(total=total_size, unit='iB', unit_scale=True)
|
| 59 |
+
with open(destination, 'wb') as f:
|
| 60 |
+
for data in response.iter_content(block_size):
|
| 61 |
+
progress_bar.update(len(data))
|
| 62 |
+
f.write(data)
|
| 63 |
+
progress_bar.close()
|
| 64 |
+
|
| 65 |
+
if total_size != 0 and progress_bar.n != total_size:
|
| 66 |
+
st.error("An error occurred during file download.")
|
| 67 |
+
return False
|
| 68 |
+
return True
|
| 69 |
+
|
| 70 |
+
@st.cache_resource
|
| 71 |
+
def load_keras_model():
|
| 72 |
+
"""
|
| 73 |
+
Downloads the model from Google Drive if not present, then loads it.
|
| 74 |
+
The `st.cache_resource` decorator ensures the model is loaded only once.
|
| 75 |
+
"""
|
| 76 |
+
MODEL_PATH = "my_model.keras"
|
| 77 |
+
FILE_ID = "1M-HNEJqbz6PzjhX6WHHKLPbjZpPRWLjP" # Replace with your file ID
|
| 78 |
+
|
| 79 |
+
if not os.path.exists(MODEL_PATH):
|
| 80 |
+
st.info("Model not found locally. Downloading from Google Drive... (this may take a moment)")
|
| 81 |
+
download_file_from_google_drive(FILE_ID, MODEL_PATH)
|
| 82 |
+
st.success("Model downloaded successfully!")
|
| 83 |
+
|
| 84 |
+
try:
|
| 85 |
+
model = load_model(MODEL_PATH)
|
| 86 |
+
return model
|
| 87 |
+
except Exception as e:
|
| 88 |
+
st.error(f"Error loading model: {e}")
|
| 89 |
+
st.info("Please ensure the Google Drive File ID is correct and the file is accessible.")
|
| 90 |
+
return None
|
| 91 |
+
|
| 92 |
+
model = load_keras_model()
|
| 93 |
+
|
| 94 |
+
# --- Image Preprocessing ---
|
| 95 |
+
def preprocess_image(image):
|
| 96 |
+
"""
|
| 97 |
+
Preprocesses the uploaded image to fit the model's input requirements.
|
| 98 |
+
- Resizes to (256, 256)
|
| 99 |
+
- Converts to a NumPy array
|
| 100 |
+
- Normalizes pixel values
|
| 101 |
+
- Expands dimensions for the model
|
| 102 |
+
"""
|
| 103 |
+
img = image.resize((256, 256))
|
| 104 |
+
img_array = np.array(img)
|
| 105 |
+
img_array = img_array / 255.0
|
| 106 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
| 107 |
+
return img_array
|
| 108 |
+
|
| 109 |
+
# --- UI Layout ---
|
| 110 |
+
st.title("🐾 Cat vs. Dog Image Classifier")
|
| 111 |
+
st.markdown(
|
| 112 |
+
"Upload an image of a cat or a dog, and the model will predict which one it is!"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
col1, col2 = st.columns(2)
|
| 116 |
+
|
| 117 |
+
with col1:
|
| 118 |
+
st.header("Upload Image")
|
| 119 |
+
uploaded_file = st.file_uploader(
|
| 120 |
+
"Choose an image...", type=["jpg", "jpeg", "png"]
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
if uploaded_file is not None and model is not None:
|
| 124 |
+
# Display the uploaded image
|
| 125 |
+
image = Image.open(uploaded_file)
|
| 126 |
+
|
| 127 |
+
with col1:
|
| 128 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 129 |
+
|
| 130 |
+
# Preprocess the image and make a prediction
|
| 131 |
+
processed_image = preprocess_image(image)
|
| 132 |
+
prediction = model.predict(processed_image)
|
| 133 |
+
confidence = prediction[0][0]
|
| 134 |
+
|
| 135 |
+
with col2:
|
| 136 |
+
st.header("Prediction")
|
| 137 |
+
if confidence > 0.5:
|
| 138 |
+
st.markdown(
|
| 139 |
+
f"## This is a Dog! 🐶"
|
| 140 |
+
)
|
| 141 |
+
st.progress(confidence)
|
| 142 |
+
st.write(f"**Confidence:** {confidence:.2f}")
|
| 143 |
+
else:
|
| 144 |
+
st.markdown(
|
| 145 |
+
f"## This is a Cat! 🐱"
|
| 146 |
+
)
|
| 147 |
+
st.progress(1-confidence)
|
| 148 |
+
st.write(f"**Confidence:** {1-confidence:.2f}")
|
| 149 |
+
else:
|
| 150 |
+
with col2:
|
| 151 |
+
st.info("Please upload an image to see the prediction.")
|
| 152 |
+
|
cats_v_dogs_classification.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
tensorflow
|
| 3 |
+
numpy
|
| 4 |
+
Pillow
|
| 5 |
+
requests
|
| 6 |
+
tqdm
|