Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
labels = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
|
7 |
+
|
8 |
+
def classify_image(image):
|
9 |
+
model = AutoModelForImageClassification.from_pretrained(model_id)
|
10 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
|
11 |
+
inp = feature_extractor(image, return_tensors='pt')
|
12 |
+
outp = model(**inp)
|
13 |
+
pred = torch.nn.functional.softmax(outp.logits, dim=-1)
|
14 |
+
preds = pred[0].cpu().detach().numpy()
|
15 |
+
confidence = {label: float(preds[i]) for i, label in enumerate(labels)}
|
16 |
+
return confidence
|
17 |
+
|
18 |
+
interface = gr.Interface(fn=classify_image,
|
19 |
+
inputs='image',
|
20 |
+
examples=['flower-1.jpg', 'flower-2.jpeg'],
|
21 |
+
outputs='label').launch()
|