gonzalocordova commited on
Commit
57a5006
·
1 Parent(s): 8283ce0

feat: First version of app UI

Browse files
Files changed (2) hide show
  1. app.py +40 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import os
4
+ from torchvision import transforms
5
+ from huggingface_hub import hf_hub_download
6
+
7
+
8
+ REPO_ID = "gonzalocordova/DistractionDetectorCNN"
9
+ FILENAME = "model_with_extended_dataset_resnet50_2023-03-28_10-42-07.pth"
10
+ hf_hub_download(repo_id=REPO_ID, filename=FILENAME, output_dir=os.path.join(os.getcwd(), "model"))
11
+ model = torch.load(os.path.join(os.getcwd(), "model"))
12
+ model.eval()
13
+
14
+ transform = transforms.Compose([
15
+ transforms.Resize(224),
16
+ transforms.ToTensor(),
17
+ ])
18
+
19
+
20
+ def predict_fn(image_path, raw_output=False):
21
+ """
22
+ This function will predict the class of an image
23
+ :param image_path: The path of the image
24
+ :param raw_output: If True, it will return the raw output of the model
25
+ :return: Tuple (real class, predicted class, probability)
26
+ """
27
+
28
+ image = image.convert('RGB')
29
+ image = transform(image)
30
+ image = image.unsqueeze(0)
31
+
32
+ with torch.no_grad():
33
+ output = model(image)
34
+
35
+ probabilities = torch.exp(output)
36
+ top_p, top_class = probabilities.topk(1, dim=1)
37
+
38
+ return top_class.numpy()[0][0]
39
+
40
+ gr.Interface(predict_fn, gr.inputs.Image(type="pil", label="Input Image"), outputs="label").launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ torch==2.0.0
3
+ torchvision==0.15.1