GabrielML commited on
Commit
bda029c
·
1 Parent(s): 159fcff

Add multiple improvements

Browse files
Files changed (2) hide show
  1. src/app.py +16 -22
  2. src/utils.py +2 -0
src/app.py CHANGED
@@ -1,8 +1,8 @@
1
  import gradio as gr
2
  from utils import load_specific_model, inference
3
- import markdown
4
 
5
- current_model = None # Initialize the current model as None
 
6
 
7
  # Define a set of example images
8
  example_images = [
@@ -13,23 +13,17 @@ example_images = [
13
  ("Beispielbild Wertstoff", "src/examples/Wertstoff.jpg")
14
  ]
15
 
16
- def load_model(model_name):
17
- global current_model
18
- if model_name is None:
19
  raise gr.Error("No model selected!")
20
- if current_model is not None:
21
- current_model = None
22
-
23
- current_model = load_specific_model(model_name)
24
- current_model.eval()
25
-
26
- def predict(inp):
27
- global current_model
28
- print(f"\nPredicting with {current_model}\n")
29
- if current_model is None:
30
- raise gr.Error("No model loaded!")
31
-
32
- confidences = inference(current_model, inp)
33
  print(f"\nConfidences: {confidences}\n")
34
  return confidences
35
 
@@ -57,15 +51,15 @@ with gr.Blocks() as demo:
57
  )
58
 
59
  model_dropdown = gr.Dropdown(
60
- ["EfficientNet-B3", "EfficientNet-B4", "vgg19", "resnet50", "dinov2_vits14"],
 
61
  label="Model",
62
  info="Select a model to use.",
63
  scale=1,
64
  )
65
- model_dropdown.change(load_model, model_dropdown, show_progress=True, queue=True)
66
 
67
- predict_button = gr.Button(label="Predict", info="Click to make a prediction.", scale=1)
68
- predict_button.click(fn=predict, inputs=user_image, outputs=output, queue=True)
69
 
70
  gr.Markdown("## Example Images")
71
  gr.Markdown("You can just drag and drop these images into the image uploader above!")
 
1
  import gradio as gr
2
  from utils import load_specific_model, inference
 
3
 
4
+ # current_model = None # Initialize the current model as None
5
+ MODEL_NAMES = ["EfficientNet-B3", "EfficientNet-B4", "vgg19", "resnet50", "dinov2_vits14"]
6
 
7
  # Define a set of example images
8
  example_images = [
 
13
  ("Beispielbild Wertstoff", "src/examples/Wertstoff.jpg")
14
  ]
15
 
16
+ def predict(inp_image, inp_dropdown):
17
+ if inp_dropdown is None:
 
18
  raise gr.Error("No model selected!")
19
+ if inp_image is None:
20
+ raise gr.Error("No image uploaded!")
21
+ if inp_dropdown not in MODEL_NAMES:
22
+ raise gr.Error("Invalid model selected!")
23
+
24
+ print(f"\nInput: {inp_dropdown}\n")
25
+ current_model = load_specific_model(inp_dropdown)
26
+ confidences = inference(current_model, inp_image)
 
 
 
 
 
27
  print(f"\nConfidences: {confidences}\n")
28
  return confidences
29
 
 
51
  )
52
 
53
  model_dropdown = gr.Dropdown(
54
+ MODEL_NAMES,
55
+ value="EfficientNet-B3",
56
  label="Model",
57
  info="Select a model to use.",
58
  scale=1,
59
  )
 
60
 
61
+ predict_button = gr.Button(value="Predict", label="Predict", info="Click to make a prediction.", scale=1)
62
+ predict_button.click(fn=predict, inputs=[user_image, model_dropdown], outputs=output, queue=True)
63
 
64
  gr.Markdown("## Example Images")
65
  gr.Markdown("You can just drag and drop these images into the image uploader above!")
src/utils.py CHANGED
@@ -3,6 +3,7 @@ from torchvision import models
3
  from efficientnet_pytorch import EfficientNet
4
  import torch
5
  from CustomModels import DinoVisionClassifier
 
6
 
7
  classes = {0: 'Glas', 1: 'Organic', 2: 'Papier', 3: 'Restmüll', 4: 'Wertstoff'}
8
 
@@ -23,6 +24,7 @@ transform_dinov2 = transforms.Compose(
23
  ]
24
  )
25
 
 
26
  def load_specific_model(model_name):
27
  current_model = None
28
  if model_name == "EfficientNet-B3":
 
3
  from efficientnet_pytorch import EfficientNet
4
  import torch
5
  from CustomModels import DinoVisionClassifier
6
+ from functools import lru_cache
7
 
8
  classes = {0: 'Glas', 1: 'Organic', 2: 'Papier', 3: 'Restmüll', 4: 'Wertstoff'}
9
 
 
24
  ]
25
  )
26
 
27
+ @lru_cache
28
  def load_specific_model(model_name):
29
  current_model = None
30
  if model_name == "EfficientNet-B3":