Wootang01 commited on
Commit
ad55483
·
1 Parent(s): 8bcafdc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+
4
+ from timm import create_model
5
+ from timm.data import resolve_data_config
6
+ from timm.data.transforms_factory 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
+ model = create_model('resnet50', pretrained=True)
11
+ transform = create_transform(
12
+ **resolve_data_config({}, model=model)
13
+ )
14
+
15
+ model.eval()
16
+
17
+ def predict_fn(img)"
18
+ img = img.convert('RGB')
19
+ img = transform(img).unsqueeze(0)
20
+ with torch.no_grad():
21
+ out = model(img)
22
+
23
+ probabilities = torch.nn.functional.softmax(out[0], dim=0)
24
+ values, indices = torch.topk(probabilities, k=3)
25
+ return {LABELS[i]: v.item() for i, v in zip(indices, values)}
26
+
27
+ gr.Interface(predict_fn, gr.inputs.Image(type='pil'), outputs='label').launch()
28
+