French02947 commited on
Commit
169b2d6
·
verified ·
1 Parent(s): 1a72422

Upload 9 files

Browse files
.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

  • SHA256: 8dbe0fc8a74b0c8d146ec75f337713082fdc40ac2c43e986fa0c8c8f2ff14d80
  • Pointer size: 131 Bytes
  • Size of remote file: 139 kB
animal_images/frog.png ADDED

Git LFS Details

  • SHA256: cf168ff5d42d4659ee810c4be7da623969106f4eecf0438ce8ad0104955ce20e
  • Pointer size: 131 Bytes
  • Size of remote file: 314 kB
animal_images/hippo.png ADDED

Git LFS Details

  • SHA256: a1085c6fbec1b0378e500d603c07670402c9c875827ceda32fb85305f3cc41ef
  • Pointer size: 131 Bytes
  • Size of remote file: 552 kB
animal_images/jaguar.png ADDED

Git LFS Details

  • SHA256: 086fe443bdbc479ff3d380f5a3ec2d348158bebb83a33f43263c3eaad593b33b
  • Pointer size: 131 Bytes
  • Size of remote file: 578 kB
animal_images/sloth.png ADDED

Git LFS Details

  • SHA256: 69f7720ee894a7472eb1c65f07726be7d2f1c293fed0b702e0d8b2b4363d78cd
  • Pointer size: 131 Bytes
  • Size of remote file: 414 kB
animal_images/toucan.png ADDED

Git LFS Details

  • SHA256: fe3ad6b6c8a78bd2e2c563002a251fbd1ce74e6728c201e4654e582d0dbed893
  • Pointer size: 131 Bytes
  • Size of remote file: 112 kB
animal_images/turtle.png ADDED

Git LFS Details

  • SHA256: 672334ae4126ea106db7c1a5850b68bee39d5d13badfea909a2c421639d767c6
  • Pointer size: 131 Bytes
  • Size of remote file: 426 kB
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