save changes
Browse files- README.md +6 -5
- app.py +101 -0
- requirements.txt +7 -0
README.md
CHANGED
@@ -1,13 +1,14 @@
|
|
1 |
---
|
2 |
-
title: Testapp Cats Dogs Cnn Mode
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: streamlit
|
7 |
-
sdk_version: 1.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
|
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Testapp Cats Dogs Cnn Mode Tf
|
3 |
+
emoji: 🧠
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: yellow
|
6 |
sdk: streamlit
|
7 |
+
sdk_version: 1.41.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
11 |
+
short_description: App to test the CNN Model pf the cats and dogs recognition
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import os
|
6 |
+
from huggingface_hub import login
|
7 |
+
from tensorflow import keras
|
8 |
+
import requests
|
9 |
+
|
10 |
+
# Get Hugging Face token from environment variables
|
11 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
12 |
+
|
13 |
+
if not HF_TOKEN:
|
14 |
+
st.error("HF_TOKEN environment variable not set. Please set it before running the app.")
|
15 |
+
st.stop() # Stop execution if token is missing
|
16 |
+
|
17 |
+
# Authenticate with Hugging Face
|
18 |
+
try:
|
19 |
+
login(token=HF_TOKEN)
|
20 |
+
st.success("Successfully logged in to Hugging Face Hub!")
|
21 |
+
except Exception as e:
|
22 |
+
st.error(f"Hugging Face login failed: {e}")
|
23 |
+
st.stop()
|
24 |
+
|
25 |
+
model_url = "https://huggingface.co/louiecerv/cats_dogs_recognition_tf_cnn/resolve/main/cats_dogs_recognition_tf_cnn.keras"
|
26 |
+
model_filename = "cats_dogs_recognition_tf_cnn.keras" # Or any name you prefer
|
27 |
+
model_filepath = model_filename # Save in the current directory
|
28 |
+
|
29 |
+
try:
|
30 |
+
# 1. Download the model file
|
31 |
+
response = requests.get(model_url, stream=True)
|
32 |
+
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
|
33 |
+
with open(model_filepath, "wb") as f:
|
34 |
+
for chunk in response.iter_content(chunk_size=8192):
|
35 |
+
f.write(chunk)
|
36 |
+
|
37 |
+
# 2. Load the model from the local file
|
38 |
+
model = tf.keras.models.load_model(model_filepath)
|
39 |
+
st.success(f"Model loaded successfully from: {model_filepath}")
|
40 |
+
|
41 |
+
# Optional: Clean up the downloaded file (if desired)
|
42 |
+
# os.remove(model_filepath) # Uncomment if you want to delete the file after loading
|
43 |
+
|
44 |
+
except requests.exceptions.RequestException as e:
|
45 |
+
st.error(f"Error downloading model: {e}")
|
46 |
+
st.stop()
|
47 |
+
except Exception as e:
|
48 |
+
st.error(f"Error loading model: {e}")
|
49 |
+
st.stop()
|
50 |
+
|
51 |
+
# Image preprocessing function with cropping
|
52 |
+
def preprocess_image(image):
|
53 |
+
# 1. Crop to square aspect ratio
|
54 |
+
width, height = image.size
|
55 |
+
new_size = min(width, height)
|
56 |
+
left = (width - new_size) // 2
|
57 |
+
top = (height - new_size) // 2
|
58 |
+
right = (width + new_size) // 2
|
59 |
+
bottom = (height + new_size) // 2
|
60 |
+
image = image.crop((left, top, right, bottom))
|
61 |
+
|
62 |
+
# 2. Resize
|
63 |
+
image = image.resize((128, 128)) # Use PIL's resize for consistency
|
64 |
+
|
65 |
+
# 3. Convert to NumPy array if it's a PIL image
|
66 |
+
image_np = np.array(image)
|
67 |
+
|
68 |
+
# 4. Ensure 3 channels (RGB)
|
69 |
+
if image_np.ndim == 2 or image_np.shape[2] == 1:
|
70 |
+
image_np = np.stack([image_np] * 3, axis=-1)
|
71 |
+
|
72 |
+
# 5. Normalize
|
73 |
+
image_np = image_np.astype(np.float32) / 255.0
|
74 |
+
image_np = np.expand_dims(image_np, axis=0)
|
75 |
+
return image_np
|
76 |
+
|
77 |
+
# Streamlit app
|
78 |
+
st.title("Cat vs. Dog Image Classifier")
|
79 |
+
|
80 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
81 |
+
|
82 |
+
if uploaded_file is not None:
|
83 |
+
image = Image.open(uploaded_file)
|
84 |
+
st.image(image, caption="Uploaded Image.", use_container_width=True)
|
85 |
+
|
86 |
+
processed_image = preprocess_image(image) # Pass the PIL Image directly
|
87 |
+
|
88 |
+
# Display preprocessed image (optional, but helpful for debugging)
|
89 |
+
processed_image_display = tf.squeeze(processed_image, axis=0).numpy() * 255.0
|
90 |
+
processed_image_display = processed_image_display.astype(np.uint8)
|
91 |
+
processed_image_display = Image.fromarray(processed_image_display)
|
92 |
+
st.image(processed_image_display, caption="Preprocessed Image", use_container_width=True)
|
93 |
+
|
94 |
+
|
95 |
+
predictions = model.predict(processed_image)
|
96 |
+
class_index = np.argmax(predictions)
|
97 |
+
class_names = ["Cat", "Dog"]
|
98 |
+
predicted_class = class_names[class_index]
|
99 |
+
confidence = predictions[0][class_index] * 100
|
100 |
+
|
101 |
+
st.write(f"## Prediction: {predicted_class} (Confidence: {confidence:.2f}%)")
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
tensorflow
|
3 |
+
Pillow
|
4 |
+
numpy
|
5 |
+
transformers
|
6 |
+
torch
|
7 |
+
keras
|