brxerq's picture
Upgrade to Gradio 6.30, faster CPU inference, new UI
a732677 verified
Raw History Blame Contribute Delete
2.69 kB
from pathlib import Path
import gradio as gr
import torch
import torch.nn.functional as F
from fastai.vision.all import load_learner
from PIL import Image
# cpu-basic has 2 vCPUs; os.cpu_count() reports the host's cores instead
torch.set_num_threads(2)
learn = load_learner("model.pkl")
model = learn.model.eval()
categories = list(learn.dls.vocab)
# Same as the learner's validation pipeline: squash to 256, center-crop 224, ImageNet normalize
MEAN = torch.tensor([0.485, 0.456, 0.406]).view(3, 1, 1)
STD = torch.tensor([0.229, 0.224, 0.225]).view(3, 1, 1)
def preprocess(img: Image.Image) -> torch.Tensor:
# reducing_gap makes downscaling large uploads much cheaper
img = img.convert("RGB").resize((256, 256), Image.BILINEAR, reducing_gap=3.0)
img = img.crop((16, 16, 240, 240))
x = torch.frombuffer(bytearray(img.tobytes()), dtype=torch.uint8).view(224, 224, 3)
return ((x.permute(2, 0, 1).float() / 255 - MEAN) / STD).unsqueeze(0)
@torch.inference_mode()
def classify_image(img: Image.Image | None) -> dict[str, float]:
"""Identify the bird species in a photo. Returns the 5 most likely of 526 species with confidences."""
if img is None:
raise gr.Error("Upload a bird photo first.")
probs = F.softmax(model(preprocess(img))[0], dim=0)
top = probs.topk(5)
return {categories[i].title(): float(p) for p, i in zip(top.values, top.indices)}
classify_image(Image.new("RGB", (256, 256))) # warm-up so the first visitor isn't slow
examples = sorted(str(p) for p in Path("examples").glob("*.jpg"))
with gr.Blocks(title="Bird Classification") as demo:
gr.Markdown(
f"# 🐦 Bird Classification\n"
f"Upload a photo of a bird to identify it among **{len(categories)} species**. "
"Works best with one bird, clearly visible and filling most of the frame."
)
with gr.Row(equal_height=True):
with gr.Column():
image = gr.Image(type="pil", label="Bird photo", sources=["upload", "webcam", "clipboard"], height=360)
with gr.Row():
clear = gr.ClearButton(value="Clear")
submit = gr.Button("Identify", variant="primary")
label = gr.Label(num_top_classes=5, label="Top 5 predictions")
clear.add([image, label])
submit.click(classify_image, image, label, api_name="classify_image")
image.upload(classify_image, image, label, api_visibility="private")
# after the events, so the public endpoint keeps the name "classify_image"
gr.Examples(examples, inputs=image, outputs=label, fn=classify_image, run_on_click=True, cache_examples=False, api_visibility="private")
demo.queue(max_size=20).launch(theme=gr.themes.Soft())