File size: 1,037 Bytes
8adccd0
 
 
 
 
 
 
 
 
 
 
e9572e7
 
 
 
 
 
 
 
8adccd0
04505d6
8adccd0
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import datasets
import gradio as gr
from transformers import AutoFeatureExtractor, AutoModelForImageClassification

dataset = datasets.load_dataset('beans', 'full_size')

extractor = AutoFeatureExtractor.from_pretrained('saved_model_files')
model = AutoModelForImageClassification.from_pretrained('saved_model_files')

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

def classify(im):
  features = extractor(im, return_tensors='pt')
  logits = model(features['pixel_values'])[-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

interface = gr.Interface(fn=classify, inputs=gr.Image(shape=(200, 200)),  outputs=gr.outputs.Label(num_top_classes=3),
             examples=['leaf1.png', 'leaf2.png', 'leaf3.jpg', 'leaf4.jpg'], title='Leaf Classification App', description='Check if the leaves of your plant are healthy!', flagging_dir='flagged_examples/')

interface.launch(debug=True)