Spaces:
Sleeping
Sleeping
Upload with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -6,7 +6,6 @@ colorFrom: indigo
|
|
| 6 |
colorTo: indigo
|
| 7 |
sdk: gradio
|
| 8 |
sdk_version: 3.4.1
|
| 9 |
-
|
| 10 |
-
app_file: app.py
|
| 11 |
pinned: false
|
| 12 |
---
|
|
|
|
| 6 |
colorTo: indigo
|
| 7 |
sdk: gradio
|
| 8 |
sdk_version: 3.4.1
|
| 9 |
+
app_file: run.py
|
|
|
|
| 10 |
pinned: false
|
| 11 |
---
|
run.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
model = torch.hub.load("pytorch/vision:v0.6.0", "resnet18", pretrained=True).eval()
|
| 9 |
+
|
| 10 |
+
# Download human-readable labels for ImageNet.
|
| 11 |
+
response = requests.get("https://git.io/JJkYN")
|
| 12 |
+
labels = response.text.split("\n")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def predict(inp):
|
| 16 |
+
inp = Image.fromarray(inp.astype("uint8"), "RGB")
|
| 17 |
+
inp = transforms.ToTensor()(inp).unsqueeze(0)
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
|
| 20 |
+
return {labels[i]: float(prediction[i]) for i in range(1000)}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
inputs = gr.Image()
|
| 24 |
+
outputs = gr.Label(num_top_classes=3)
|
| 25 |
+
|
| 26 |
+
demo = gr.Interface(fn=predict, inputs=inputs, outputs=outputs)
|
| 27 |
+
|
| 28 |
+
if __name__ == "__main__":
|
| 29 |
+
demo.launch()
|