Spaces:
Runtime error
Runtime error
Add Gradio app with WebP support, beautiful UI, and deployment files
Browse files- Fixed WebP image processing by implementing manual preprocessing
- Added beautiful Gradio interface with bird class descriptions and example images
- Created minimal requirements.txt for Hugging Face Spaces deployment
- Updated README with proper Space configuration and documentation
- Added res.pkl model file via Git LFS
README.md
CHANGED
|
@@ -1,14 +1,45 @@
|
|
| 1 |
---
|
| 2 |
-
title: Penguin Or Puffin
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.45.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
| 11 |
-
short_description:
|
| 12 |
---
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Penguin Or Puffin Classifier
|
| 3 |
+
emoji: 🐧
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.45.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
| 11 |
+
short_description: AI classifier for King Penguins, Gentoo Penguins, and Puffins
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# Arctic & Antarctic Bird Classifier
|
| 15 |
+
|
| 16 |
+
An bird classification model that can distinguish between:
|
| 17 |
+
- **King Penguin** (Epic)
|
| 18 |
+
- **Gentoo Penguin** (Classic)
|
| 19 |
+
- **Puffin** (Fake Short Penguin)
|
| 20 |
+
|
| 21 |
+
## Features
|
| 22 |
+
|
| 23 |
+
- Built with FastAI and ResNet architecture
|
| 24 |
+
- Supports WebP, JPG, and PNG image formats
|
| 25 |
+
- Beautiful Gradio interface with example images
|
| 26 |
+
- Handles images from any angle with robust preprocessing
|
| 27 |
+
|
| 28 |
+
## How It Works
|
| 29 |
+
|
| 30 |
+
The model analyzes visual features like:
|
| 31 |
+
- Body shape and size (Penguins are larger and more streamlined)
|
| 32 |
+
- Beak characteristics (Puffins have colorful triangular beaks)
|
| 33 |
+
- Plumage patterns (Each species has unique markings)
|
| 34 |
+
- Overall proportions (Different body-to-head ratios)
|
| 35 |
+
|
| 36 |
+
Try to trick the model into guessing a puffin is a penguin - that would be very impressive!
|
| 37 |
+
|
| 38 |
+
## Technical Details
|
| 39 |
+
|
| 40 |
+
- **Model**: ResNet-based CNN trained with FastAI
|
| 41 |
+
- **Input**: 224x224 RGB images
|
| 42 |
+
- **Preprocessing**: Manual image processing to handle WebP and other formats
|
| 43 |
+
- **Interface**: Gradio web app with custom styling
|
| 44 |
+
|
| 45 |
+
Upload an image and let the AI identify which magnificent bird you've found!
|
app.py
CHANGED
|
@@ -1,8 +1,221 @@
|
|
| 1 |
-
app.py
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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n18 = load_learner('res.pkl')
|
| 8 |
+
labels = learn18.dls.vocab
|
| 9 |
|
| 10 |
+
# Get the model and its transforms for manual processing
|
| 11 |
+
model = learn18.model
|
| 12 |
+
device = next(model.parameters()).device
|
| 13 |
+
|
| 14 |
+
# Standard ImageNet normalization used by most vision models
|
| 15 |
+
mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1).to(device)
|
| 16 |
+
std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1).to(device)
|
| 17 |
+
|
| 18 |
+
def predict(img):
|
| 19 |
+
try:
|
| 20 |
+
print(f"Input type: {type(img)}")
|
| 21 |
+
|
| 22 |
+
# Convert to PIL Image if needed
|
| 23 |
+
if isinstance(img, str):
|
| 24 |
+
img = Image.open(img)
|
| 25 |
+
elif not hasattr(img, 'convert'):
|
| 26 |
+
img = Image.fromarray(np.array(img))
|
| 27 |
+
|
| 28 |
+
# Ensure RGB mode
|
| 29 |
+
if img.mode != 'RGB':
|
| 30 |
+
img = img.convert('RGB')
|
| 31 |
+
|
| 32 |
+
print(f"Processing PIL image: {img.mode}, size: {img.size}")
|
| 33 |
+
|
| 34 |
+
# Manual preprocessing to bypass problematic transforms
|
| 35 |
+
# Resize to 224x224 (typical model input size)
|
| 36 |
+
img_resized = img.resize((224, 224), Image.BILINEAR)
|
| 37 |
+
|
| 38 |
+
# Convert to tensor
|
| 39 |
+
img_array = np.array(img_resized).astype(np.float32) / 255.0
|
| 40 |
+
img_tensor = torch.from_numpy(img_array).permute(2, 0, 1).unsqueeze(0).to(device)
|
| 41 |
+
|
| 42 |
+
# Normalize
|
| 43 |
+
img_normalized = (img_tensor - mean) / std
|
| 44 |
+
|
| 45 |
+
# Run inference
|
| 46 |
+
model.eval()
|
| 47 |
+
with torch.no_grad():
|
| 48 |
+
outputs = model(img_normalized)
|
| 49 |
+
probs = torch.softmax(outputs, dim=1)
|
| 50 |
+
probs = probs.cpu().numpy()[0]
|
| 51 |
+
|
| 52 |
+
print(f"Prediction successful: {len(probs)} classes")
|
| 53 |
+
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(f"Error in predict function: {e}")
|
| 57 |
+
import traceback
|
| 58 |
+
traceback.print_exc()
|
| 59 |
+
return {"Error": 1.0}
|
| 60 |
+
|
| 61 |
+
# Custom CSS for better styling
|
| 62 |
+
custom_css = """
|
| 63 |
+
.gradio-container {
|
| 64 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 65 |
+
}
|
| 66 |
+
.header {
|
| 67 |
+
text-align: center;
|
| 68 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 69 |
+
color: white;
|
| 70 |
+
padding: 2rem;
|
| 71 |
+
border-radius: 10px;
|
| 72 |
+
margin-bottom: 2rem;
|
| 73 |
+
}
|
| 74 |
+
.bird-info {
|
| 75 |
+
background: #f8f9fa;
|
| 76 |
+
padding: 1.5rem;
|
| 77 |
+
border-radius: 10px;
|
| 78 |
+
margin: 1rem 0;
|
| 79 |
+
border-left: 4px solid #667eea;
|
| 80 |
+
}
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
# Detailed descriptions for each bird class
|
| 84 |
+
bird_descriptions = """
|
| 85 |
+
## About the Bird Classes
|
| 86 |
+
|
| 87 |
+
This model can distinguish between three bird species:
|
| 88 |
+
|
| 89 |
+
### **King Penguin:** epic
|
| 90 |
+
- **Habitat**: Sub-Antarctic islands
|
| 91 |
+
- **Size**: Second largest penguin species (70-100 cm tall)
|
| 92 |
+
- **Features**: Orange-yellow neck patches, long curved beak, streamlined body
|
| 93 |
+
- **Fun fact**: Can dive up to 300 meters deep!
|
| 94 |
+
|
| 95 |
+
### **Gentoo Penguin:** classic
|
| 96 |
+
- **Habitat**: Antarctic Peninsula and sub-Antarctic islands
|
| 97 |
+
- **Size**: Third largest penguin species (51-90 cm tall)
|
| 98 |
+
- **Features**: White stripe across the top of the head, bright orange-red beak
|
| 99 |
+
- **Fun fact**: Fastest swimming penguin, reaching speeds up to 36 km/h!
|
| 100 |
+
|
| 101 |
+
### **Puffin:** fake short penguin
|
| 102 |
+
- **Habitat**: North Atlantic and North Pacific coasts
|
| 103 |
+
- **Size**: Much smaller than penguins (25-30 cm tall)
|
| 104 |
+
- **Features**: Colorful triangular beak (especially during breeding season), black and white plumage
|
| 105 |
+
- **Fun fact**: Can hold up to 60 small fish in their beak at once!
|
| 106 |
+
|
| 107 |
+
*Try to trick the model into guessing a puffin is a penguin, that would be very impressive!*
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
example_images = [
|
| 111 |
+
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT61sjxMkdnjjyzqSLuQLojxcvbjVvLT8zHcA&s", # King Penguin
|
| 112 |
+
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQUH6u4B2TmBYZhXBBvVVJN85sn8dCed3XD3g&s", # Gentoo Penguin
|
| 113 |
+
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRJCVzXkCE5haPfcwlJTj1QpY2bpw2-TNXACA&s" # Puffin
|
| 114 |
+
]
|
| 115 |
+
|
| 116 |
+
with gr.Blocks(css=custom_css, title="🐧 Arctic & Antarctic Bird Classifier 🐦") as demo:
|
| 117 |
+
gr.HTML("""
|
| 118 |
+
<div class="header">
|
| 119 |
+
<h1>Made with Resnet18 model</h1>
|
| 120 |
+
<p style="font-size: 1.2em; margin-top: 1rem;">Simple AI-powered bird identification for Penguins and Puffins</p>
|
| 121 |
+
</div>
|
| 122 |
+
""")
|
| 123 |
+
|
| 124 |
+
with gr.Row():
|
| 125 |
+
with gr.Column(scale=1):
|
| 126 |
+
gr.Markdown("### Upload Bird Image")
|
| 127 |
+
input_image = gr.Image(
|
| 128 |
+
type="pil",
|
| 129 |
+
label="Drop an image here or click to upload",
|
| 130 |
+
height=400
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
predict_btn = gr.Button(
|
| 134 |
+
"Analyze (run inference)",
|
| 135 |
+
variant="primary",
|
| 136 |
+
size="lg"
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
gr.Markdown("#### tips:")
|
| 140 |
+
gr.Markdown("""
|
| 141 |
+
- JPG, PNG, and WebP formats supported
|
| 142 |
+
- Works with photos from any angle
|
| 143 |
+
""")
|
| 144 |
+
|
| 145 |
+
with gr.Column(scale=1):
|
| 146 |
+
gr.Markdown("### Detection Results")
|
| 147 |
+
output_label = gr.Label(
|
| 148 |
+
num_top_classes=3,
|
| 149 |
+
label="Species Confidence Scores"
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
gr.Markdown("### 📊 How it works")
|
| 153 |
+
gr.Markdown("""
|
| 154 |
+
This model uses deep learning to analyze visual features like:
|
| 155 |
+
- **Body shape and size** - Penguins are larger and more streamlined
|
| 156 |
+
- **Beak characteristics** - Puffins have colorful triangular beaks
|
| 157 |
+
- **Plumage patterns** - Each species has unique markings
|
| 158 |
+
- **Overall proportions** - Different body-to-head ratios
|
| 159 |
+
""")
|
| 160 |
+
|
| 161 |
+
with gr.Row():
|
| 162 |
+
gr.Markdown("## About the Bird Classes")
|
| 163 |
+
|
| 164 |
+
gr.Markdown("This model can distinguish between three similar looking bird species:")
|
| 165 |
+
|
| 166 |
+
with gr.Row():
|
| 167 |
+
with gr.Column():
|
| 168 |
+
gr.Markdown("### **King Penguin: Epic**")
|
| 169 |
+
gr.Image(
|
| 170 |
+
value=example_images[0],
|
| 171 |
+
label="King Penguin Example",
|
| 172 |
+
height=200,
|
| 173 |
+
interactive=False
|
| 174 |
+
)
|
| 175 |
+
gr.Markdown("""
|
| 176 |
+
- **Habitat**: Sub-Antarctic islands
|
| 177 |
+
- **Size**: Second largest penguin species (70-100 cm tall)
|
| 178 |
+
- **Features**: Orange-yellow neck patches, long curved beak, streamlined body
|
| 179 |
+
- **Fun fact**: Can dive up to 300 meters deep!
|
| 180 |
+
""")
|
| 181 |
+
|
| 182 |
+
with gr.Column():
|
| 183 |
+
gr.Markdown("### **Gentoo Penguin: Classic**")
|
| 184 |
+
gr.Image(
|
| 185 |
+
value=example_images[1],
|
| 186 |
+
label="Gentoo Penguin Example",
|
| 187 |
+
height=200,
|
| 188 |
+
interactive=False
|
| 189 |
+
)
|
| 190 |
+
gr.Markdown("""
|
| 191 |
+
- **Habitat**: Antarctic Peninsula and sub-Antarctic islands
|
| 192 |
+
- **Size**: Third largest penguin species (51-90 cm tall)
|
| 193 |
+
- **Features**: White stripe across the top of the head, bright orange-red beak
|
| 194 |
+
- **Fun fact**: Fastest swimming penguin, reaching speeds up to 36 km/h!
|
| 195 |
+
""")
|
| 196 |
+
|
| 197 |
+
with gr.Column():
|
| 198 |
+
gr.Markdown("### **Puffin: Fake Short Penguin**")
|
| 199 |
+
gr.Image(
|
| 200 |
+
value=example_images[2],
|
| 201 |
+
label="Puffin Example",
|
| 202 |
+
height=200,
|
| 203 |
+
interactive=False
|
| 204 |
+
)
|
| 205 |
+
gr.Markdown("""
|
| 206 |
+
- **Habitat**: North Atlantic and North Pacific coasts
|
| 207 |
+
- **Size**: Much smaller than penguins (25-30 cm tall)
|
| 208 |
+
- **Features**: Colorful triangular beak (especially during breeding season), black and white plumage
|
| 209 |
+
- **Fun fact**: Can hold up to 60 small fish in their beak at once!
|
| 210 |
+
""")
|
| 211 |
+
|
| 212 |
+
gr.Markdown("*Try to trick the model into guessing a puffin is a penguin, that would be very impressive!*")
|
| 213 |
+
|
| 214 |
+
predict_btn.click(
|
| 215 |
+
fn=predict,
|
| 216 |
+
inputs=input_image,
|
| 217 |
+
outputs=output_label
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
if __name__ == "__main__":
|
| 221 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==5.45.0
|
| 2 |
+
fastai==2.8.4
|
| 3 |
+
torch==2.8.0
|
| 4 |
+
torchvision==0.23.0
|
| 5 |
+
pillow==11.3.0
|
| 6 |
+
numpy==2.3.3
|
res.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:21d83ee9f575bcf0c1252e10a942269cebeb71ecbf28a4b3b80746e586fdb152
|
| 3 |
+
size 46988745
|