File size: 948 Bytes
a07db6d
 
 
 
 
 
60aff9e
a07db6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import torch
from datasets import load_dataset
from transformers import AutoFeatureExtractor, AutoModelForImageClassification

# This should be the same as the first line of Python code in this Colab notebook
dataset = load_dataset('beans')
feature_extractor = AutoFeatureExtractor.from_pretrained("saved_model_files")
model = AutoModelForImageClassification.from_pretrained("saved_model_files")

labels = dataset['train'].features['labels'].names

def classify(im):
  features = feature_extractor(im, return_tensors='pt')
  inp = model(**features)
  logits = torch.nn.functional.softmax(inp.logits, dim=-1)
  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

import gradio as gr

interface = gr.Interface(fn=classify, inputs=gr.Image(shape=(224, 224)), outputs="text")

interface.launch(debug=True)