kevinconka commited on
Commit
0f0204b
·
1 Parent(s): 97dfc4f

Better demo title + error management

Browse files
Files changed (3) hide show
  1. app.py +10 -8
  2. model_yolov5.py +1 -0
  3. utils.py +6 -0
app.py CHANGED
@@ -9,6 +9,7 @@ import glob
9
  import gradio as gr
10
  from huggingface_hub import get_token
11
  from utils import (
 
12
  load_image_from_url,
13
  load_badges,
14
  FlaggedCounter,
@@ -18,10 +19,11 @@ from model_yolov5 import load_model, inference
18
 
19
 
20
  TITLE = """
21
- <h1> RGB Detection Demo </h1>
22
  <p align="center">
23
- Give it a try! Upload an image or enter a URL to an image and click
24
- <code>Submit</code>.
 
25
  </p>
26
  """
27
 
@@ -57,7 +59,9 @@ with gr.Blocks(theme=theme, css=css) as demo:
57
 
58
  with gr.Row():
59
  with gr.Column():
60
- img_input = gr.Image(label="input", interactive=True)
 
 
61
  img_url = gr.Textbox(
62
  lines=1,
63
  placeholder="or enter URL to image here",
@@ -68,9 +72,7 @@ with gr.Blocks(theme=theme, css=css) as demo:
68
  clear = gr.ClearButton()
69
  submit = gr.Button("Submit", variant="primary")
70
  with gr.Column():
71
- img_output = gr.Image(
72
- label="output", interactive=False, show_share_button=True
73
- )
74
  flag = gr.Button("Flag", visible=False)
75
  notice = gr.Markdown(value=NOTICE, visible=False)
76
 
@@ -87,7 +89,7 @@ with gr.Blocks(theme=theme, css=css) as demo:
87
 
88
  # event listeners
89
  img_url.change(load_image_from_url, [img_url], img_input)
90
- submit.click(
91
  lambda image: inference(model, image),
92
  [img_input],
93
  img_output,
 
9
  import gradio as gr
10
  from huggingface_hub import get_token
11
  from utils import (
12
+ check_image,
13
  load_image_from_url,
14
  load_badges,
15
  FlaggedCounter,
 
19
 
20
 
21
  TITLE = """
22
+ <h1> 🌊 SEA.AI's Machine Vision Showcase ✨ </h1>
23
  <p align="center">
24
+ Ahoy! Explore our object detection technology!
25
+ Upload a maritime scene image or use a URL, and click
26
+ <code>Submit</code> to see the results.
27
  </p>
28
  """
29
 
 
59
 
60
  with gr.Row():
61
  with gr.Column():
62
+ img_input = gr.Image(
63
+ label="input", interactive=True, sources=["upload", "clipboard"]
64
+ )
65
  img_url = gr.Textbox(
66
  lines=1,
67
  placeholder="or enter URL to image here",
 
72
  clear = gr.ClearButton()
73
  submit = gr.Button("Submit", variant="primary")
74
  with gr.Column():
75
+ img_output = gr.Image(label="output", interactive=False)
 
 
76
  flag = gr.Button("Flag", visible=False)
77
  notice = gr.Markdown(value=NOTICE, visible=False)
78
 
 
89
 
90
  # event listeners
91
  img_url.change(load_image_from_url, [img_url], img_input)
92
+ submit.click(check_image, [img_input]).success(
93
  lambda image: inference(model, image),
94
  [img_input],
95
  img_output,
model_yolov5.py CHANGED
@@ -12,6 +12,7 @@ def load_model(model_path, img_size=640):
12
  model.img_size = img_size # add img_size attribute
13
  return model
14
 
 
15
  @spaces.GPU
16
  def inference(model, image):
17
  """Run inference on image and return annotated image."""
 
12
  model.img_size = img_size # add img_size attribute
13
  return model
14
 
15
+
16
  @spaces.GPU
17
  def inference(model, image):
18
  """Run inference on image and return annotated image."""
utils.py CHANGED
@@ -9,6 +9,12 @@ import gradio as gr
9
  from huggingface_hub import get_token
10
 
11
 
 
 
 
 
 
 
12
  def load_image_from_url(url):
13
  """Load image from URL."""
14
  if not url: # empty or None
 
9
  from huggingface_hub import get_token
10
 
11
 
12
+ def check_image(image):
13
+ """Check image."""
14
+ if image is None:
15
+ raise gr.Error("Oops! It looks like you forgot to upload an image.")
16
+
17
+
18
  def load_image_from_url(url):
19
  """Load image from URL."""
20
  if not url: # empty or None