Spaces:
Sleeping
Sleeping
Upload 9 files
Browse files- .gitattributes +7 -0
- animal_images/cat.png +3 -0
- animal_images/frog.png +3 -0
- animal_images/hippo.png +3 -0
- animal_images/jaguar.png +3 -0
- animal_images/sloth.png +3 -0
- animal_images/toucan.png +3 -0
- animal_images/turtle.png +3 -0
- app.py +101 -0
- requirements.txt +4 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,10 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
animal_images/cat.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
animal_images/frog.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
animal_images/hippo.png filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
animal_images/jaguar.png filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
animal_images/sloth.png filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
animal_images/toucan.png filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
animal_images/turtle.png filter=lfs diff=lfs merge=lfs -text
|
animal_images/cat.png
ADDED
|
Git LFS Details
|
animal_images/frog.png
ADDED
|
Git LFS Details
|
animal_images/hippo.png
ADDED
|
Git LFS Details
|
animal_images/jaguar.png
ADDED
|
Git LFS Details
|
animal_images/sloth.png
ADDED
|
Git LFS Details
|
animal_images/toucan.png
ADDED
|
Git LFS Details
|
animal_images/turtle.png
ADDED
|
Git LFS Details
|
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
"""
|
| 4 |
+
Created on Thu Jan 29 11:12:02 2026
|
| 5 |
+
|
| 6 |
+
@author: atulkar
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import gradio as gr
|
| 11 |
+
from transformers import pipeline
|
| 12 |
+
|
| 13 |
+
# -----------------------------
|
| 14 |
+
# Load Image Classification pipeline (pretrained)
|
| 15 |
+
# -----------------------------
|
| 16 |
+
# Good general-purpose ImageNet-style classifier
|
| 17 |
+
clf = pipeline(
|
| 18 |
+
task="image-classification",
|
| 19 |
+
model="google/vit-base-patch16-224"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# -----------------------------
|
| 23 |
+
# Locate example images (works locally + on HF Spaces)
|
| 24 |
+
# -----------------------------
|
| 25 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 26 |
+
EXAMPLES_DIR = os.path.join(BASE_DIR, "animal_images")
|
| 27 |
+
|
| 28 |
+
EXAMPLE_FILES = [
|
| 29 |
+
"cat.png",
|
| 30 |
+
"frog.png",
|
| 31 |
+
"hippo.png",
|
| 32 |
+
"jaguar.png",
|
| 33 |
+
"sloth.png",
|
| 34 |
+
"toucan.png",
|
| 35 |
+
"turtle.png",
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
examples = []
|
| 39 |
+
missing = []
|
| 40 |
+
for fname in EXAMPLE_FILES:
|
| 41 |
+
fpath = os.path.join(EXAMPLES_DIR, fname)
|
| 42 |
+
if os.path.exists(fpath):
|
| 43 |
+
examples.append([fpath])
|
| 44 |
+
else:
|
| 45 |
+
missing.append(fname)
|
| 46 |
+
|
| 47 |
+
# -----------------------------
|
| 48 |
+
# Prediction function
|
| 49 |
+
# -----------------------------
|
| 50 |
+
def classify_image(img):
|
| 51 |
+
"""
|
| 52 |
+
img comes in as a PIL image (because gr.Image(type="pil"))
|
| 53 |
+
Returns a dict for gr.Label: {label: confidence}
|
| 54 |
+
"""
|
| 55 |
+
if img is None:
|
| 56 |
+
return {}
|
| 57 |
+
|
| 58 |
+
preds = clf(img, top_k=3)
|
| 59 |
+
return {p["label"]: float(p["score"]) for p in preds}
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# -----------------------------
|
| 63 |
+
# Build Gradio App
|
| 64 |
+
# -----------------------------
|
| 65 |
+
with gr.Blocks(title="Animal Image Classifier") as demo:
|
| 66 |
+
gr.Markdown("# Animal Image Classifier")
|
| 67 |
+
gr.Markdown(
|
| 68 |
+
"Upload an animal image (or click an example). "
|
| 69 |
+
"This app uses a Hugging Face `image-classification` pipeline."
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
with gr.Column(scale=1):
|
| 74 |
+
inp = gr.Image(type="pil", label="Input Image")
|
| 75 |
+
with gr.Row():
|
| 76 |
+
btn = gr.Button("Submit", variant="primary")
|
| 77 |
+
clr = gr.Button("Clear")
|
| 78 |
+
with gr.Column(scale=1):
|
| 79 |
+
out = gr.Label(num_top_classes=3, label="Top Predictions")
|
| 80 |
+
|
| 81 |
+
btn.click(fn=classify_image, inputs=inp, outputs=out)
|
| 82 |
+
clr.click(fn=lambda: (None, {}), inputs=None, outputs=[inp, out])
|
| 83 |
+
|
| 84 |
+
if examples:
|
| 85 |
+
gr.Examples(
|
| 86 |
+
examples=examples,
|
| 87 |
+
inputs=inp,
|
| 88 |
+
label="Examples (from ./animal_images/)",
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
if missing:
|
| 92 |
+
gr.Markdown(
|
| 93 |
+
"\n\nMake sure the folder is next to `app.py` locally, "
|
| 94 |
+
"and uploaded into your Hugging Face Space repo when deploying."
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# -----------------------------
|
| 98 |
+
# Launch
|
| 99 |
+
# -----------------------------
|
| 100 |
+
if __name__ == "__main__":
|
| 101 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
pillow
|