Spaces:
Runtime error
Runtime error
Commit
·
6587be0
1
Parent(s):
66bca24
initial files
Browse files- README.md +13 -2
- app.py +30 -0
- example1.jpg +0 -0
- example2.jpg +0 -0
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -1,2 +1,13 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Image Recognition Demo
|
| 3 |
+
emoji: 🚀
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: pink
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 2.9.4
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: afl-3.0
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
|
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
from torchvision import transforms
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
Built following https://www.gradio.app/image_classification_in_pytorch/.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
# Load model
|
| 12 |
+
model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).eval()
|
| 13 |
+
|
| 14 |
+
# Download human-readable labels for ImageNet.
|
| 15 |
+
response = requests.get("https://git.io/JJkYN")
|
| 16 |
+
labels = response.text.split("\n")
|
| 17 |
+
|
| 18 |
+
def predict(inp):
|
| 19 |
+
inp = transforms.ToTensor()(inp).unsqueeze(0)
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
|
| 22 |
+
confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
|
| 23 |
+
return confidences
|
| 24 |
+
|
| 25 |
+
gr.Interface(fn=predict,
|
| 26 |
+
inputs=gr.inputs.Image(type="pil"),
|
| 27 |
+
outputs=gr.outputs.Label(num_top_classes=3),
|
| 28 |
+
examples=["example1.jpg", "example2.jpg"],
|
| 29 |
+
theme="default",
|
| 30 |
+
css=".footer{display:none !important}").launch()
|
example1.jpg
ADDED
|
example2.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|