Spaces:
Sleeping
Sleeping
Add image classifier CPU Space
Browse files- README.md +7 -6
- app.py +39 -0
- image_classifier/__init__.py +3 -0
- image_classifier/service.py +48 -0
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
---
|
| 2 |
-
title: Image Classifier
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 6.10.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Image Classifier CPU
|
| 3 |
+
colorFrom: indigo
|
| 4 |
+
colorTo: blue
|
|
|
|
| 5 |
sdk: gradio
|
|
|
|
| 6 |
app_file: app.py
|
| 7 |
pinned: false
|
| 8 |
+
license: apache-2.0
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# Image Classifier CPU
|
| 12 |
+
|
| 13 |
+
Free CPU image classifier using `google/vit-base-patch16-224`.
|
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
from image_classifier.service import ImageClassifierService
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
service = ImageClassifierService()
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def classify_image(image):
|
| 10 |
+
return service.classify(image)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
with gr.Blocks(
|
| 14 |
+
title="Image Classifier CPU",
|
| 15 |
+
theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="blue"),
|
| 16 |
+
) as demo:
|
| 17 |
+
gr.Markdown(
|
| 18 |
+
"""
|
| 19 |
+
# Image Classifier CPU
|
| 20 |
+
Upload an image and get top predicted labels on free CPU.
|
| 21 |
+
"""
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
image_input = gr.Image(type="pil", label="Input Image")
|
| 25 |
+
run_button = gr.Button("Classify", variant="primary")
|
| 26 |
+
|
| 27 |
+
top_label_output = gr.Textbox(label="Top Label", lines=1)
|
| 28 |
+
top_results_output = gr.Textbox(label="Top Results", lines=6)
|
| 29 |
+
status_output = gr.Textbox(label="Status", lines=2)
|
| 30 |
+
|
| 31 |
+
run_button.click(
|
| 32 |
+
fn=classify_image,
|
| 33 |
+
inputs=[image_input],
|
| 34 |
+
outputs=[top_label_output, top_results_output, status_output],
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
demo.launch()
|
image_classifier/__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .service import ImageClassifierService
|
| 2 |
+
|
| 3 |
+
__all__ = ["ImageClassifierService"]
|
image_classifier/service.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
MODEL_ID = "google/vit-base-patch16-224"
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class ImageClassifierService:
|
| 10 |
+
def __init__(self):
|
| 11 |
+
self.pipe = None
|
| 12 |
+
cpu_count = os.cpu_count() or 1
|
| 13 |
+
torch.set_num_threads(max(1, min(4, cpu_count)))
|
| 14 |
+
|
| 15 |
+
def classify(self, image):
|
| 16 |
+
if image is None:
|
| 17 |
+
return "", "", "Upload an image first."
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
results = self._run_model(image)
|
| 21 |
+
top = results[0]
|
| 22 |
+
top_label = top["label"]
|
| 23 |
+
formatted = self._format_results(results)
|
| 24 |
+
return top_label, formatted, f"Classified image with {MODEL_ID}."
|
| 25 |
+
except Exception as exc:
|
| 26 |
+
return "", "", f"Image classification failed: {type(exc).__name__}: {exc}"
|
| 27 |
+
|
| 28 |
+
def _load_pipeline(self):
|
| 29 |
+
if self.pipe is not None:
|
| 30 |
+
return
|
| 31 |
+
|
| 32 |
+
from transformers import pipeline
|
| 33 |
+
|
| 34 |
+
self.pipe = pipeline(
|
| 35 |
+
"image-classification",
|
| 36 |
+
model=MODEL_ID,
|
| 37 |
+
device=-1,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
def _run_model(self, image):
|
| 41 |
+
self._load_pipeline()
|
| 42 |
+
return self.pipe(image, top_k=5)
|
| 43 |
+
|
| 44 |
+
def _format_results(self, results):
|
| 45 |
+
lines = []
|
| 46 |
+
for item in results:
|
| 47 |
+
lines.append(f"{item['label']}: {item['score'] * 100:.1f}%")
|
| 48 |
+
return "\n".join(lines)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=5.23.0
|
| 2 |
+
huggingface_hub>=0.34.0,<1.0
|
| 3 |
+
Pillow>=10.4.0
|
| 4 |
+
safetensors>=0.5.3
|
| 5 |
+
torch>=2.3.0
|
| 6 |
+
transformers>=4.49.0
|