Wootang01 commited on
Commit
ada2446
·
1 Parent(s): c0c36f2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+
4
+ from timmm import create_model
5
+ from timm.data import resolve_data_config
6
+ from timm.data.transform import create_transform
7
+
8
+ IMAGENET_1k_URL = "https://storage.googleapis.com/bit_models/ilsvrc2012_wordnet_lemmas.txt"
9
+ LABELS = requests.get(IMAGENET_1k_URL).text.strip().split('\n')
10
+
11
+ model = create_model('resnet50', pretrained=True)
12
+
13
+ transform = create_transform(
14
+ **resolve_data_config({}, model=model)
15
+ )
16
+ model.eval()
17
+
18
+ def predict_fn(img):
19
+ img = img.convert('RGB')
20
+ img = transform(img).unsqueeze(0)
21
+
22
+ with torch.no_grad():
23
+ out = model(img)
24
+
25
+ probabilites = torch.nn.functional.softmax(out[0], dim=0)
26
+
27
+ values, indices = torch.topk(probabilites, k=5)
28
+
29
+ return {LABELS[i]: v.item() for i, v in zip(indices, values)}
30
+
31
+ gr.Interface(predict_fn, gr.inputs.Image(type='pil'), outputs='label').launch()