Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).eval()
|
| 4 |
+
|
| 5 |
+
import requests
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from torchvision import transforms
|
| 8 |
+
|
| 9 |
+
# Download human-readable labels for ImageNet.
|
| 10 |
+
response = requests.get("https://git.io/JJkYN")
|
| 11 |
+
labels = response.text.split("\n")
|
| 12 |
+
|
| 13 |
+
def predict(inp):
|
| 14 |
+
inp = Image.fromarray(inp.astype('uint8'), 'RGB')
|
| 15 |
+
inp = transforms.ToTensor()(inp).unsqueeze(0)
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
|
| 18 |
+
confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
|
| 19 |
+
return confidences
|
| 20 |
+
|
| 21 |
+
import gradio as gr
|
| 22 |
+
|
| 23 |
+
gr.Interface(fn=predict,
|
| 24 |
+
inputs="image",
|
| 25 |
+
outputs=gr.outputs.Label(num_top_classes=3),
|
| 26 |
+
examples=["lion.jpg"], ["cheetah.jpg"],
|
| 27 |
+
css=".footer{display:none !important}").launch()
|