import os from huggingface_hub import hf_hub_download from fastai.learner import load_learner import gradio as gr from gradio.components import Image from gradio import Label SECRET_TOKEN = os.environ["SECRET_TOKEN"] # learner = from_pretrained_fastai("artificeresearch/spiritvision") learner = load_learner(hf_hub_download("artificeresearch/spiritvision", "model.pkl", token=SECRET_TOKEN)) labels = learner.dls.vocab def predict_fn(img): """ :param img: img is a PIL image object :return: prediction and probabilities """ pred, pred_idx, probs = learner.predict(img) return {labels[i]: float(probs[i]) for i in range(len(labels))} gr.Interface(predict_fn, Image(shape=(512, 512)), outputs=Label(num_top_classes=3), ).launch()