Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from PIL import Image
|
3 |
+
import torchvision.transforms as transforms
|
4 |
+
import numpy as np
|
5 |
+
import json
|
6 |
+
import requests
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
|
9 |
+
import gradio as gr
|
10 |
+
|
11 |
+
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
12 |
+
|
13 |
+
efficientnet = torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_efficientnet_b0', pretrained=True)
|
14 |
+
utils = torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_convnets_processing_utils')
|
15 |
+
|
16 |
+
efficientnet.eval().to(device)
|
17 |
+
|
18 |
+
def inference(img):
|
19 |
+
batch = torch.cat(
|
20 |
+
[utils.prepare_input_from_uri(img)]
|
21 |
+
).to(device)
|
22 |
+
with torch.no_grad():
|
23 |
+
output = torch.nn.functional.softmax(efficientnet(batch), dim=1)
|
24 |
+
|
25 |
+
|
26 |
+
results = utils.pick_n_best(predictions=output, n=5)
|
27 |
+
|
28 |
+
return results
|
29 |
+
|
30 |
+
gr.Interface(inference,gr.inputs.Image(type="file"),"text").launch()
|