Spaces:
Runtime error
Runtime error
Nithila Ariyapperuma
commited on
Commit
·
f9d719f
1
Parent(s):
87cec53
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
from timm import create_model
|
6 |
+
from timm.data import resolve_data_config
|
7 |
+
from timm.data.tranforms import create_transform
|
8 |
+
|
9 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
10 |
+
|
11 |
+
|
12 |
+
extractor = AutoFeatureExtractor.from_pretrained("abhishek/autotrain_fashion_mnist_vit_base")
|
13 |
+
model = AutoModelForImageClassification.from_pretrained("abhishek/autotrain_fashion_mnist_vit_base")
|
14 |
+
transform = create_transform(
|
15 |
+
**resolve_data_config({}, model=model)
|
16 |
+
)
|
17 |
+
|
18 |
+
model.eval()
|
19 |
+
|
20 |
+
def predict_fn(img):
|
21 |
+
img = img.convert('RGB')
|
22 |
+
img = transform(img).unsqueeze(0)
|
23 |
+
|
24 |
+
with torch.no_grad():
|
25 |
+
out = model(img)
|
26 |
+
|
27 |
+
probabilites = torch.nn.functional.softmax(out[0], dim=0)
|
28 |
+
|
29 |
+
values, indices = torch.topk(probabilites, k=5)
|
30 |
+
|
31 |
+
return {LABELS[i]: v.item() for i, v in zip(indices, values)}
|
32 |
+
|
33 |
+
gr.Interface(predict_fn, gr.inputs.Image(type='pil'), outputs='label').launch()
|