Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
model = torch.hub.load("pytorch/vision/v0.17", "resnet18", pre_trained = True).eval()
|
| 7 |
+
response = request.get("https://git.io/JJkYN")
|
| 8 |
+
labels = response.text.split("\n")
|
| 9 |
+
|
| 10 |
+
def get_predictions(inp):
|
| 11 |
+
inp = transforms.ToTensor()(inp).unsqueeze(0)
|
| 12 |
+
predictions = torch.nn.Functional.softmax(model(inp)[0], dim = 0)
|
| 13 |
+
conf = {labels[i]: predictions[i] for i in range(len(labels))}
|
| 14 |
+
return conf
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
iclass = gr.Interface(fn = get_predictions,
|
| 18 |
+
inputs = gr.Image(type = "pil"),
|
| 19 |
+
outputs = gr.Label(num_top_classes = 2)
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
iclass.launch()
|
| 23 |
+
|