Spaces:
Build error
Build error
import requests | |
import gradio as gr | |
import torch | |
from timm import create_model | |
from timm.data import resolve_data_config | |
from timm.data.transformers import create_transform | |
IMAGENET_1k_URL = "https://storage.googleapis.com/bit_models/ilsvrc2012_wordnet_lemmas.txt" | |
LABELS = requests.get(IMAGENET_1k_URL).text.strip().split('\n') | |
model = create_model('resnet50', pretrained=True) | |
transform = create_transform( | |
**resolve_data_config({}, model=model) | |
) | |
model.eval() | |
def predict_fn(img): | |
img = img.convert('RGB') | |
img = transform(img).unsqueeze(0) | |
with torch.no_grad(): | |
out = model(img) | |
probabilities = torch.nn.functional.softmax(out[0], dim=0) | |
values, indices = torch.topk(probabilities, k=5) | |
return {LABELS[i]: v.item() for i, v in zip(indices, values)} | |
gr.Interface(predict_fn, gr.inputs.Image(type='pli'), outputs='label').launch() |