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 +105 -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,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import zipfile
|
| 3 |
+
from functools import lru_cache
|
| 4 |
+
from glob import glob
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
ZIP_PATH = "animal_images.zip"
|
| 12 |
+
EXAMPLES_DIR = "animal_images"
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def ensure_examples_extracted():
|
| 16 |
+
"""
|
| 17 |
+
If animal_images.zip exists and animal_images/ does not, extract it.
|
| 18 |
+
Works both locally and in HF Spaces.
|
| 19 |
+
"""
|
| 20 |
+
if os.path.exists(EXAMPLES_DIR):
|
| 21 |
+
return
|
| 22 |
+
|
| 23 |
+
if os.path.exists(ZIP_PATH):
|
| 24 |
+
os.makedirs(EXAMPLES_DIR, exist_ok=True)
|
| 25 |
+
with zipfile.ZipFile(ZIP_PATH, "r") as z:
|
| 26 |
+
z.extractall(EXAMPLES_DIR)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def get_example_image_paths(max_examples: int = 7):
|
| 30 |
+
"""
|
| 31 |
+
Returns up to `max_examples` image file paths for Gradio examples.
|
| 32 |
+
"""
|
| 33 |
+
ensure_examples_extracted()
|
| 34 |
+
|
| 35 |
+
patterns = ["*.png", "*.jpg", "*.jpeg", "*.webp", "*.bmp"]
|
| 36 |
+
paths = []
|
| 37 |
+
for pat in patterns:
|
| 38 |
+
paths.extend(glob(os.path.join(EXAMPLES_DIR, "**", pat), recursive=True))
|
| 39 |
+
|
| 40 |
+
# Keep it stable and limited to 7
|
| 41 |
+
paths = sorted(paths)[:max_examples]
|
| 42 |
+
return paths
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@lru_cache(maxsize=1)
|
| 46 |
+
def get_classifier():
|
| 47 |
+
"""
|
| 48 |
+
Load the HF image-classification pipeline once and reuse it.
|
| 49 |
+
"""
|
| 50 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 51 |
+
|
| 52 |
+
# Solid default ImageNet classifier (good for common animals)
|
| 53 |
+
model_id = "google/vit-base-patch16-224"
|
| 54 |
+
|
| 55 |
+
return pipeline(
|
| 56 |
+
task="image-classification",
|
| 57 |
+
model=model_id,
|
| 58 |
+
device=device
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def classify_image(img):
|
| 63 |
+
"""
|
| 64 |
+
img is a PIL Image from gr.Image(type="pil").
|
| 65 |
+
Return a dict that gr.Label can render nicely (label -> confidence).
|
| 66 |
+
"""
|
| 67 |
+
clf = get_classifier()
|
| 68 |
+
preds = clf(img, top_k=5)
|
| 69 |
+
|
| 70 |
+
# Convert to {label: score} for gr.Label
|
| 71 |
+
out = {p["label"]: float(p["score"]) for p in preds}
|
| 72 |
+
return out
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def build_demo():
|
| 76 |
+
example_paths = get_example_image_paths(7)
|
| 77 |
+
examples = [[p] for p in example_paths] # safer format for Gradio examples
|
| 78 |
+
|
| 79 |
+
with gr.Blocks() as demo:
|
| 80 |
+
gr.Markdown(
|
| 81 |
+
"# Animal Image Classifier\n"
|
| 82 |
+
"Upload an image (or click an example) to classify it with a pretrained Hugging Face vision model."
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
with gr.Row():
|
| 86 |
+
inp = gr.Image(type="pil", label="Upload an animal photo")
|
| 87 |
+
out = gr.Label(num_top_classes=5, label="Predictions (Top 5)")
|
| 88 |
+
|
| 89 |
+
btn = gr.Button("Classify")
|
| 90 |
+
|
| 91 |
+
btn.click(fn=classify_image, inputs=inp, outputs=out)
|
| 92 |
+
|
| 93 |
+
gr.Markdown("## Examples")
|
| 94 |
+
gr.Examples(
|
| 95 |
+
examples=examples,
|
| 96 |
+
inputs=inp,
|
| 97 |
+
label="Click an example image below"
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
return demo
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
demo = build_demo()
|
| 105 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
pillow
|