fg-mindee commited on
Commit
e6ff5fc
1 Parent(s): 832aa0d

fix: Fixed Gradio app

Browse files
Files changed (1) hide show
  1. app.py +30 -43
app.py CHANGED
@@ -9,46 +9,33 @@ from torchvision.transforms.functional import InterpolationMode
9
  from holocron import models
10
 
11
 
12
- def main(args):
13
-
14
- model = models.rexnet1_3x(pretrained=True).eval()
15
-
16
- preprocessor = Compose([
17
- Resize(model.default_cfg['input_shape'][1:], interpolation=InterpolationMode.BILINEAR),
18
- PILToTensor(),
19
- ConvertImageDtype(torch.float32),
20
- Normalize(model.default_cfg['mean'], model.default_cfg['std'])
21
- ])
22
-
23
- def predict(input):
24
- input = Image.fromarray(input.astype('uint8'), 'RGB')
25
- input = preprocessor(input)
26
- with torch.inference_mode():
27
- prediction = torch.nn.functional.softmax(model(input.unsqueeze(0))[0], dim=0)
28
- return {class_name: float(conf) for class_name, conf in zip(model.default_cfg['classes'], prediction)}
29
-
30
- image = gr.inputs.Image()
31
- outputs = gr.outputs.Label(num_top_classes=3)
32
-
33
- interface = gr.Interface(
34
- fn=predict,
35
- inputs=[image],
36
- outputs=outputs,
37
- title="Holocron: image classification demo",
38
- article=("<p style='text-align: center'><a href='https://github.com/frgfm/Holocron'>" "Github Repo</a> | "
39
- "<a href='https://frgfm.github.io/Holocron/'>Documentation</a></p>"),
40
- live=True,
41
- theme="huggingface",
42
- layout="horizontal",
43
- )
44
-
45
- interface.launch(server_port=args.port, show_error=True)
46
-
47
-
48
- if __name__ == '__main__':
49
- parser = argparse.ArgumentParser(description='Holocron image classification demo',
50
- formatter_class=argparse.ArgumentDefaultsHelpFormatter)
51
- parser.add_argument("--port", type=int, default=8001, help="Port on which the webserver will be run")
52
- args = parser.parse_args()
53
-
54
- main(args)
 
9
  from holocron import models
10
 
11
 
12
+ model = models.rexnet1_3x(pretrained=True).eval()
13
+
14
+ preprocessor = Compose([
15
+ Resize(model.default_cfg['input_shape'][1:], interpolation=InterpolationMode.BILINEAR),
16
+ PILToTensor(),
17
+ ConvertImageDtype(torch.float32),
18
+ Normalize(model.default_cfg['mean'], model.default_cfg['std'])
19
+ ])
20
+
21
+ def predict(img):
22
+ img = Image.fromarray(img.astype('uint8'), 'RGB')
23
+ img = preprocessor(img)
24
+ with torch.inference_mode():
25
+ prediction = torch.nn.functional.softmax(model(img.unsqueeze(0))[0], dim=0)
26
+ return {class_name: float(conf) for class_name, conf in zip(model.default_cfg['classes'], prediction)}
27
+
28
+ image = gr.inputs.Image()
29
+ outputs = gr.outputs.Label(num_top_classes=3)
30
+
31
+ gr.Interface(
32
+ fn=predict,
33
+ inputs=[image],
34
+ outputs=outputs,
35
+ title="Holocron: image classification demo",
36
+ article=("<p style='text-align: center'><a href='https://github.com/frgfm/Holocron'>" "Github Repo</a> | "
37
+ "<a href='https://frgfm.github.io/Holocron/'>Documentation</a></p>"),
38
+ live=True,
39
+ theme="huggingface",
40
+ layout="horizontal",
41
+ ).launch(enable_queue=True,cache_examples=True)