Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Hugging Face's logo
|
| 2 |
+
Hugging Face
|
| 3 |
+
Search models, datasets, users...
|
| 4 |
+
Models
|
| 5 |
+
Datasets
|
| 6 |
+
Spaces
|
| 7 |
+
Docs
|
| 8 |
+
Solutions
|
| 9 |
+
Pricing
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
Spaces:
|
| 14 |
+
|
| 15 |
+
ahydar
|
| 16 |
+
/
|
| 17 |
+
ImageClassifier
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
like
|
| 21 |
+
0
|
| 22 |
+
App
|
| 23 |
+
Files
|
| 24 |
+
Community
|
| 25 |
+
ImageClassifier
|
| 26 |
+
/
|
| 27 |
+
app.py
|
| 28 |
+
ahydar's picture
|
| 29 |
+
ahydar
|
| 30 |
+
Update app.py
|
| 31 |
+
681c2fe
|
| 32 |
+
almost 2 years ago
|
| 33 |
+
raw
|
| 34 |
+
history
|
| 35 |
+
blame
|
| 36 |
+
contribute
|
| 37 |
+
delete
|
| 38 |
+
No virus
|
| 39 |
+
1.1 kB
|
| 40 |
+
import requests
|
| 41 |
+
|
| 42 |
+
import gradio as gr
|
| 43 |
+
import torch
|
| 44 |
+
from timm import create_model
|
| 45 |
+
from timm.data import resolve_data_config
|
| 46 |
+
from timm.data.transforms_factory import create_transform
|
| 47 |
+
|
| 48 |
+
IMAGENET_1k_URL = "https://storage.googleapis.com/bit_models/ilsvrc2012_wordnet_lemmas.txt"
|
| 49 |
+
LABELS = requests.get(IMAGENET_1k_URL).text.strip().split('\n')
|
| 50 |
+
|
| 51 |
+
model = create_model('resnet50', pretrained=True)
|
| 52 |
+
|
| 53 |
+
transform = create_transform(
|
| 54 |
+
**resolve_data_config({}, model=model)
|
| 55 |
+
)
|
| 56 |
+
model.eval()
|
| 57 |
+
|
| 58 |
+
def predict_fn(img):
|
| 59 |
+
img = img.convert('RGB')
|
| 60 |
+
img = transform(img).unsqueeze(0)
|
| 61 |
+
|
| 62 |
+
with torch.no_grad():
|
| 63 |
+
out = model(img)
|
| 64 |
+
|
| 65 |
+
probabilites = torch.nn.functional.softmax(out[0], dim=0)
|
| 66 |
+
|
| 67 |
+
values, indices = torch.topk(probabilites, k=5)
|
| 68 |
+
|
| 69 |
+
return {LABELS[i]: v.item() for i, v in zip(indices, values)}
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
title = "Image Classifier"
|
| 73 |
+
description = "Gradio Demo for Image Classifier built with pretrained model resnet50"
|
| 74 |
+
examples = ['cat.jpg', 'dog.jpg']
|
| 75 |
+
|
| 76 |
+
gr.Interface(predict_fn, gr.inputs.Image(type='pil'), outputs='label', title=title, description=description, examples=examples).launch()
|
| 77 |
+
|
| 78 |
+
|