jafdxc commited on
Commit
a97e303
·
1 Parent(s): 099d2d1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -0
app.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModelForImageClassification, AutoFeatureExtractor
3
+ import gradio as gr
4
+
5
+ model_id = f'jonathanfernandes/vit-base-patch16-224-finetuned-flower'
6
+
7
+ def classify_image(image):
8
+ model = AutoModelForImageClassification.from_pretrained(model_id)
9
+ feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
10
+ inp = feature_extractor(image, return_tensors='pt')
11
+ outp = model(**inp)
12
+ pred = torch.nn.functional.softmax(outp.logits, dim=-1)
13
+ preds = pred[0].cpu().detach().numpy()
14
+ confidence = {label: float(preds[i]) for i, label in enumerate(labels)}
15
+ return confidence
16
+
17
+ interface = gr.Interface(fn=classify_image,
18
+ inputs='image',
19
+ examples=['flower-1.jpg', 'flower-2.jpeg'],
20
+ outputs='label').launch(share=True)