Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,26 @@
|
|
1 |
### app.py
|
2 |
-
import gradio as gr
|
3 |
import torch
|
4 |
-
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
model
|
9 |
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
preprocess = loaded_model.create_transformer()
|
15 |
-
|
16 |
-
image_tensor = preprocess(image)
|
17 |
-
image_tensor = image_tensor.unsqueeze(0)
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
23 |
|
24 |
-
return f"Predicted class: {predicted_class.item()}"
|
25 |
|
26 |
|
27 |
-
gr.Interface(fn=
|
|
|
|
|
|
|
|
1 |
### app.py
|
|
|
2 |
import torch
|
3 |
+
import requests
|
4 |
+
from PIL import Image
|
5 |
+
from torchvision import transforms
|
6 |
+
import gradio as gr
|
7 |
+
model = torch.hub.load('https://huggingface.co/spaces/gpbhupinder/test/blob/main/model_-%2023%20june%202024%2019_22.pt', pretrained=True).eval()
|
8 |
|
9 |
|
10 |
+
# Download human-readable labels for ImageNet.
|
11 |
+
response = requests.get("https://git.io/JJkYN")
|
12 |
+
labels = response.text.split("\n")
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
def predict(inp):
|
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 |
|
22 |
|
23 |
+
gr.Interface(fn=predict,
|
24 |
+
inputs=gr.Image(type="pil"),
|
25 |
+
outputs=gr.Label(num_top_classes=3),
|
26 |
+
examples=["lion.jpg", "cheetah.jpg"]).launch()
|