CoolFace
Apppublic

minnos/caltech-101-classifier

sourceHugging Facemitupdated 5mo agoView on Hugging Face
0likes
app.py44 linesDownload Raw Back to root
1import streamlit as st2from fastai.vision.all import load_learner, PILImage3from pathlib import Path4import requests5import torch6import os7from io import BytesIO8 9os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'10 11MODEL_URL = "https://huggingface.co/minnos/caltech-101-classifier/resolve/main/caltech_classifier_cpu.pkl"12MODEL_PATH = Path("caltech_classifier_cpu.pkl")13 14st.set_page_config(page_title="Caltech-101 Classifier", layout="centered")15st.title("Caltech-101 Image Classifier")16st.write("Upload any image and the model will classify it into one of 101 object categories.")17 18@st.cache_resource19def load_model():20    if not MODEL_PATH.exists():21        with st.spinner("Downloading model (84MB)..."):22            r = requests.get(MODEL_URL, stream=True)23            with open(MODEL_PATH, 'wb') as f:24                for chunk in r.iter_content(chunk_size=8192):25                    f.write(chunk)26    return load_learner(MODEL_PATH, cpu=True)27 28learn = load_model()29 30uploaded_file = st.file_uploader("Choose an image", type=["jpg","jpeg","png"])31 32if uploaded_file is not None:33    img = PILImage.create(uploaded_file)34    st.image(img, caption="Uploaded image", use_container_width=True)35    36    with st.spinner("Classifying..."):37        pred, idx, probs = learn.predict(img)38    39    st.subheader(f"Prediction: **{str(pred).replace('_',' ').title()}**")40    st.metric("Confidence", f"{float(probs[idx])*100:.1f}%")41    42    top5 = sorted(zip(learn.dls.vocab, probs), key=lambda x: x[1], reverse=True)[:5]43    st.bar_chart({k.replace('_',' ').title(): float(v) for k,v in top5})44