Luis Marques commited on
Commit
16fe969
1 Parent(s): 1027377

update app and include examples

Browse files
Files changed (8) hide show
  1. app.py +23 -23
  2. carminhao1.jpeg +0 -0
  3. carminhao2.jpeg +0 -0
  4. carminhao3.jpeg +0 -0
  5. carro1.jpeg +0 -0
  6. carro2.jpeg +0 -0
  7. carro3.jpeg +0 -0
  8. requirements.txt +0 -2
app.py CHANGED
@@ -1,34 +1,34 @@
 
 
1
  import gradio as gr
2
- import torch
3
- from PIL import Image
4
- import torchvision.transforms as transforms
5
 
6
- model = torch.load("model.pkl", map_location=torch.device("cpu"))
7
- model.eval()
8
 
 
9
 
10
- def preprocess_image(image):
11
- transform = transforms.Compose(
12
- [
13
- transforms.Resize((224, 224)),
14
- transforms.ToTensor(),
15
- transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
16
- ]
17
- )
18
- image_tensor = transform(image)
19
- return image_tensor.unsqueeze(0)
20
 
 
21
 
22
- def classify_image(image):
23
- input_tensor = preprocess_image(image)
24
 
25
- with torch.no_grad():
26
- outputs = model(input_tensor)
27
- predictions = torch.argmax(outputs, dim=1).item()
28
 
29
- return f"Predicted class: {predictions}"
30
 
 
 
31
 
32
- iface = gr.Interface(fn=classify_image, inputs="image", outputs="text")
 
 
 
 
 
 
 
33
 
34
- iface.launch()
 
 
 
 
1
+ import warnings
2
+ from fastai.vision.all import *
3
  import gradio as gr
 
 
 
4
 
5
+ warnings.simplefilter("ignore", category=UserWarning)
 
6
 
7
+ learn = load_learner("model.pkl")
8
 
9
+ warnings.resetwarnings()
 
 
 
 
 
 
 
 
 
10
 
11
+ categories = ("heavy truck", "car", "bike")
12
 
 
 
13
 
14
+ def classify_image(img):
15
+ _, _, probs = learn.predict(img)
16
+ return dict(zip(categories, map(float, probs)))
17
 
 
18
 
19
+ image = gr.Image()
20
+ label = gr.Label()
21
 
22
+ examples = [
23
+ "caminhao1.jpg",
24
+ "caminhao2.jpg",
25
+ "caminhao3.jpg",
26
+ "carro1.jpg",
27
+ "carro2.jpg",
28
+ "carro3.jpg",
29
+ ]
30
 
31
+ intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
32
+
33
+
34
+ intf.launch(inline=False)
carminhao1.jpeg ADDED
carminhao2.jpeg ADDED
carminhao3.jpeg ADDED
carro1.jpeg ADDED
carro2.jpeg ADDED
carro3.jpeg ADDED
requirements.txt CHANGED
@@ -1,4 +1,2 @@
1
  gradio==2.1.2
2
- torch
3
- torchvision
4
  fastai
 
1
  gradio==2.1.2
 
 
2
  fastai