Spaces:
Sleeping
Sleeping
| import datasets | |
| import gradio as gr | |
| from transformers import AutoFeatureExtractor, AutoModelForImageClassification | |
| import torch | |
| dataset = datasets.load_dataset('beans') | |
| extractor = AutoFeatureExtractor.from_pretrained("rahult/bean_classification") | |
| model = AutoModelForImageClassification.from_pretrained("rahult/bean_classification") | |
| model.eval() | |
| labels = dataset['train'].features['labels'].names | |
| def classify(im): | |
| features = extractor(im, return_tensors='pt') | |
| with torch.no_grad(): | |
| logits = model(**features).logits | |
| probability = torch.nn.functional.softmax(logits, dim=-1) | |
| probs = probability[0].detach().numpy() | |
| confidences = {label: float(probs[i]) for i, label in enumerate(labels)} | |
| return confidences | |
| interface = gr.Interface(fn=classify, inputs="image", outputs="label", allow_flagging='manual') | |
| interface.launch() |