gpbhupinder commited on
Commit
c3a4f13
·
verified ·
1 Parent(s): b81291c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -1,27 +1,26 @@
1
  ### app.py
2
- import gradio as gr
3
  import torch
4
- from loaded_model import CNNModel
5
- import loaded_model
6
-
7
- model = CNNModel()
8
- model.load_state_dict(torch.load(f="https://huggingface.co/spaces/gpbhupinder/test/blob/main/model_-%2023%20june%202024%2019_22.pt"))
9
 
10
 
11
- # Define a function to make predictions with your model
12
- def classify_image(image):
13
- # Preprocess the image
14
- preprocess = loaded_model.create_transformer()
15
-
16
- image_tensor = preprocess(image)
17
- image_tensor = image_tensor.unsqueeze(0)
18
 
19
- # Make prediction
20
- with torch.no_grad():
21
- output = model(image_tensor)
22
- _, predicted_class = torch.max(output, 1)
 
 
23
 
24
- return f"Predicted class: {predicted_class.item()}"
25
 
26
 
27
- gr.Interface(fn=classify_image, inputs="sketchpad", outputs="label").launch()
 
 
 
 
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()