Spaces:
Sleeping
Sleeping
Commit
·
a378000
1
Parent(s):
869f4d7
Refactor app.py and utils.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,13 @@
|
|
| 1 |
import glob
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import get_token
|
| 4 |
-
from utils import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from flagging import myHuggingFaceDatasetSaver
|
| 6 |
|
| 7 |
|
|
@@ -33,12 +39,19 @@ model.iou = 0.4
|
|
| 33 |
model.max_det = 100
|
| 34 |
model.agnostic = True # NMS class-agnostic
|
| 35 |
|
| 36 |
-
#
|
| 37 |
dataset_name = "SEA-AI/crowdsourced-sea-images"
|
| 38 |
hf_writer = myHuggingFaceDatasetSaver(get_token(), dataset_name)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
title = gr.HTML(TITLE)
|
| 43 |
|
| 44 |
with gr.Row():
|
|
@@ -68,17 +81,22 @@ with gr.Blocks(css=css) as demo:
|
|
| 68 |
cache_examples=True,
|
| 69 |
)
|
| 70 |
|
| 71 |
-
# add components to clear
|
| 72 |
clear.add([img_input, img_url, img_output])
|
| 73 |
|
| 74 |
# event listeners
|
| 75 |
img_url.change(load_image_from_url, [img_url], img_input)
|
| 76 |
-
submit.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
# event listeners with decorators
|
| 79 |
-
@img_output.change(inputs=[img_output], outputs=[flag, notice])
|
| 80 |
-
def show_hide(
|
| 81 |
-
visible =
|
| 82 |
return {
|
| 83 |
flag: gr.Button("Flag", visible=visible, interactive=True),
|
| 84 |
notice: gr.Markdown(value=NOTICE, visible=visible),
|
|
@@ -87,17 +105,21 @@ with gr.Blocks(css=css) as demo:
|
|
| 87 |
# This needs to be called prior to the first call to callback.flag()
|
| 88 |
hf_writer.setup([img_input], "flagged")
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
flag.click(lambda: gr.Info("Thank you for contributing!")).then(
|
| 92 |
-
lambda: {flag: gr.Button("Flag", interactive=False)}, [], [flag]
|
| 93 |
).then(
|
| 94 |
lambda *args: hf_writer.flag(args),
|
| 95 |
[img_input, flag],
|
| 96 |
[],
|
| 97 |
preprocess=False,
|
|
|
|
| 98 |
).then(
|
| 99 |
-
lambda: load_badges(
|
| 100 |
)
|
| 101 |
|
|
|
|
|
|
|
|
|
|
| 102 |
if __name__ == "__main__":
|
| 103 |
-
demo.queue().launch()
|
|
|
|
| 1 |
import glob
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import get_token
|
| 4 |
+
from utils import (
|
| 5 |
+
load_model,
|
| 6 |
+
load_image_from_url,
|
| 7 |
+
inference,
|
| 8 |
+
load_badges,
|
| 9 |
+
count_flagged_images_from_csv,
|
| 10 |
+
)
|
| 11 |
from flagging import myHuggingFaceDatasetSaver
|
| 12 |
|
| 13 |
|
|
|
|
| 39 |
model.max_det = 100
|
| 40 |
model.agnostic = True # NMS class-agnostic
|
| 41 |
|
| 42 |
+
# Flagging
|
| 43 |
dataset_name = "SEA-AI/crowdsourced-sea-images"
|
| 44 |
hf_writer = myHuggingFaceDatasetSaver(get_token(), dataset_name)
|
| 45 |
|
| 46 |
+
|
| 47 |
+
def get_flagged_count():
|
| 48 |
+
"""Count flagged images in dataset."""
|
| 49 |
+
return count_flagged_images_from_csv(dataset_name)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
theme = gr.themes.Default(primary_hue=gr.themes.colors.indigo)
|
| 53 |
+
with gr.Blocks(theme=theme, css=css) as demo:
|
| 54 |
+
badges = gr.HTML(load_badges(get_flagged_count()))
|
| 55 |
title = gr.HTML(TITLE)
|
| 56 |
|
| 57 |
with gr.Row():
|
|
|
|
| 81 |
cache_examples=True,
|
| 82 |
)
|
| 83 |
|
| 84 |
+
# add components to clear when clear button is clicked
|
| 85 |
clear.add([img_input, img_url, img_output])
|
| 86 |
|
| 87 |
# event listeners
|
| 88 |
img_url.change(load_image_from_url, [img_url], img_input)
|
| 89 |
+
submit.click(
|
| 90 |
+
lambda image: inference(model, image),
|
| 91 |
+
[img_input],
|
| 92 |
+
img_output,
|
| 93 |
+
api_name="inference",
|
| 94 |
+
)
|
| 95 |
|
| 96 |
# event listeners with decorators
|
| 97 |
+
@img_output.change(inputs=[img_output], outputs=[flag, notice], show_api=False)
|
| 98 |
+
def show_hide(_img_ouput):
|
| 99 |
+
visible = _img_ouput is not None
|
| 100 |
return {
|
| 101 |
flag: gr.Button("Flag", visible=visible, interactive=True),
|
| 102 |
notice: gr.Markdown(value=NOTICE, visible=visible),
|
|
|
|
| 105 |
# This needs to be called prior to the first call to callback.flag()
|
| 106 |
hf_writer.setup([img_input], "flagged")
|
| 107 |
|
| 108 |
+
# Sequential logic when flag button is clicked
|
| 109 |
+
flag.click(lambda: gr.Info("Thank you for contributing!"), show_api=False).then(
|
| 110 |
+
lambda: {flag: gr.Button("Flag", interactive=False)}, [], [flag], show_api=False
|
| 111 |
).then(
|
| 112 |
lambda *args: hf_writer.flag(args),
|
| 113 |
[img_input, flag],
|
| 114 |
[],
|
| 115 |
preprocess=False,
|
| 116 |
+
show_api=False,
|
| 117 |
).then(
|
| 118 |
+
lambda: load_badges(get_flagged_count()), [], badges, show_api=False
|
| 119 |
)
|
| 120 |
|
| 121 |
+
# called during initial load in browser
|
| 122 |
+
demo.load(lambda: load_badges(get_flagged_count()), [], badges, show_api=False)
|
| 123 |
+
|
| 124 |
if __name__ == "__main__":
|
| 125 |
+
demo.queue().launch() # show_api=False)
|
utils.py
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
|
|
| 1 |
import requests
|
| 2 |
from io import BytesIO
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import yolov5
|
| 6 |
from yolov5.utils.plots import Annotator, colors
|
| 7 |
import gradio as gr
|
| 8 |
from huggingface_hub import get_token
|
| 9 |
-
import time
|
| 10 |
|
| 11 |
|
| 12 |
def load_model(model_path, img_size=640):
|
|
|
|
| 13 |
model = yolov5.load(model_path, hf_token=get_token())
|
| 14 |
model.img_size = img_size # add img_size attribute
|
| 15 |
return model
|
| 16 |
|
| 17 |
|
| 18 |
def load_image_from_url(url):
|
|
|
|
| 19 |
if not url: # empty or None
|
| 20 |
return gr.Image(interactive=True)
|
| 21 |
try:
|
|
@@ -27,6 +30,7 @@ def load_image_from_url(url):
|
|
| 27 |
|
| 28 |
|
| 29 |
def inference(model, image):
|
|
|
|
| 30 |
results = model(image, size=model.img_size)
|
| 31 |
annotator = Annotator(np.asarray(image))
|
| 32 |
for *box, _, cls in reversed(results.pred[0]):
|
|
@@ -36,7 +40,9 @@ def inference(model, image):
|
|
| 36 |
return annotator.im
|
| 37 |
|
| 38 |
|
| 39 |
-
def
|
|
|
|
|
|
|
| 40 |
headers = {"Authorization": f"Bearer {get_token()}"}
|
| 41 |
API_URL = f"https://datasets-server.huggingface.co/size?dataset={dataset_name}"
|
| 42 |
|
|
@@ -58,8 +64,15 @@ def count_flagged_images(dataset_name, trials=10):
|
|
| 58 |
return 0
|
| 59 |
|
| 60 |
|
| 61 |
-
def
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
return f"""
|
| 64 |
<p style="display: flex">
|
| 65 |
<img alt="" src="https://img.shields.io/badge/SEA.AI-beta-blue">
|
|
|
|
| 1 |
+
import time
|
| 2 |
import requests
|
| 3 |
from io import BytesIO
|
| 4 |
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
from PIL import Image
|
| 7 |
import yolov5
|
| 8 |
from yolov5.utils.plots import Annotator, colors
|
| 9 |
import gradio as gr
|
| 10 |
from huggingface_hub import get_token
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
def load_model(model_path, img_size=640):
|
| 14 |
+
"""Load model from HuggingFace Hub."""
|
| 15 |
model = yolov5.load(model_path, hf_token=get_token())
|
| 16 |
model.img_size = img_size # add img_size attribute
|
| 17 |
return model
|
| 18 |
|
| 19 |
|
| 20 |
def load_image_from_url(url):
|
| 21 |
+
"""Load image from URL."""
|
| 22 |
if not url: # empty or None
|
| 23 |
return gr.Image(interactive=True)
|
| 24 |
try:
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
def inference(model, image):
|
| 33 |
+
"""Run inference on image and return annotated image."""
|
| 34 |
results = model(image, size=model.img_size)
|
| 35 |
annotator = Annotator(np.asarray(image))
|
| 36 |
for *box, _, cls in reversed(results.pred[0]):
|
|
|
|
| 40 |
return annotator.im
|
| 41 |
|
| 42 |
|
| 43 |
+
def count_flagged_images_via_api(dataset_name, trials=10):
|
| 44 |
+
"""Count flagged images via API. Might be slow."""
|
| 45 |
+
|
| 46 |
headers = {"Authorization": f"Bearer {get_token()}"}
|
| 47 |
API_URL = f"https://datasets-server.huggingface.co/size?dataset={dataset_name}"
|
| 48 |
|
|
|
|
| 64 |
return 0
|
| 65 |
|
| 66 |
|
| 67 |
+
def count_flagged_images_from_csv(dataset_name):
|
| 68 |
+
"""Count flagged images from CSV. Fast but relies on local files."""
|
| 69 |
+
dataset_name = dataset_name.split("/")[-1]
|
| 70 |
+
df = pd.read_csv(f"./flagged/{dataset_name}/data.csv")
|
| 71 |
+
return len(df)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def load_badges(n):
|
| 75 |
+
"""Load badges."""
|
| 76 |
return f"""
|
| 77 |
<p style="display: flex">
|
| 78 |
<img alt="" src="https://img.shields.io/badge/SEA.AI-beta-blue">
|