Spaces:
Runtime error
Runtime error
explore interface
Browse files- app.py +1 -1
- requirements.txt +1 -0
- src/assets/.gitignore +2 -0
- src/constants.py +22 -0
- src/explore_interface.py +60 -2
- src/global_variables.py +44 -0
app.py
CHANGED
|
@@ -22,5 +22,5 @@ demo = gr.TabbedInterface(
|
|
| 22 |
title="Explore & Label Concepts",
|
| 23 |
analytics_enabled=True,
|
| 24 |
)
|
| 25 |
-
|
| 26 |
demo.launch()
|
|
|
|
| 22 |
title="Explore & Label Concepts",
|
| 23 |
analytics_enabled=True,
|
| 24 |
)
|
| 25 |
+
|
| 26 |
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
jsonlines
|
src/assets/.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*
|
| 2 |
+
!.gitignore
|
src/constants.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Constants use in the space.
|
| 2 |
+
"""
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import pathlib
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
DATASET_NAME = "Xmaster6y/fruit-vegetable-concepts"
|
| 9 |
+
CONCEPTS = [
|
| 10 |
+
# Shapes
|
| 11 |
+
"sphere",
|
| 12 |
+
"cube",
|
| 13 |
+
"cylinder",
|
| 14 |
+
# Colors
|
| 15 |
+
"red",
|
| 16 |
+
"green",
|
| 17 |
+
"orange",
|
| 18 |
+
"yellow",
|
| 19 |
+
]
|
| 20 |
+
ASSETS_FOLDER = pathlib.Path(__file__).parent / "assets"
|
| 21 |
+
|
| 22 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
src/explore_interface.py
CHANGED
|
@@ -1,12 +1,70 @@
|
|
| 1 |
"""Interface to explore images and labels.
|
| 2 |
"""
|
| 3 |
|
|
|
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
with gr.Blocks() as interface:
|
| 8 |
with gr.Row():
|
| 9 |
with gr.Column():
|
| 10 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
label="Image",
|
| 12 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""Interface to explore images and labels.
|
| 2 |
"""
|
| 3 |
|
| 4 |
+
import random
|
| 5 |
+
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
+
from src import global_variables
|
| 9 |
+
from src.constants import CONCEPTS, ASSETS_FOLDER, DATASET_NAME
|
| 10 |
+
|
| 11 |
+
def get_next_image(
|
| 12 |
+
split: str,
|
| 13 |
+
concepts: list,
|
| 14 |
+
current_metadata: dict,
|
| 15 |
+
selected_concepts: list,
|
| 16 |
+
):
|
| 17 |
+
if concepts != selected_concepts:
|
| 18 |
+
for split, values in global_variables.all_metadata.items():
|
| 19 |
+
current_metadata[split] = [x for x in values if all([c in x["concepts"] for c in concepts])]
|
| 20 |
+
selected_concepts = concepts
|
| 21 |
+
if split == "all":
|
| 22 |
+
split = random.choice(["train", "validation", "test"])
|
| 23 |
+
try:
|
| 24 |
+
sample = random.choice(current_metadata[split])
|
| 25 |
+
image_path = f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/{sample['file_name']}"
|
| 26 |
+
return image_path, sample["class"], sample["concepts"], current_metadata, selected_concepts
|
| 27 |
+
except IndexError:
|
| 28 |
+
gr.Warning("No image found for the selected filter.")
|
| 29 |
+
return None, None, None, current_metadata, selected_concepts
|
| 30 |
|
| 31 |
+
with gr.Blocks() as interface:
|
| 32 |
with gr.Row():
|
| 33 |
with gr.Column():
|
| 34 |
+
with gr.Group():
|
| 35 |
+
gr.Markdown(
|
| 36 |
+
"## # Image Selection",
|
| 37 |
+
)
|
| 38 |
+
split = gr.Radio(
|
| 39 |
+
label="Split",
|
| 40 |
+
choices=["train", "validation", "test", "all"],
|
| 41 |
+
value="train",
|
| 42 |
+
)
|
| 43 |
+
concepts = gr.CheckboxGroup(
|
| 44 |
+
label="Concepts",
|
| 45 |
+
choices=CONCEPTS,
|
| 46 |
+
)
|
| 47 |
+
button = gr.Button(
|
| 48 |
+
value="Next",
|
| 49 |
+
)
|
| 50 |
+
with gr.Group():
|
| 51 |
+
gr.Markdown(
|
| 52 |
+
"## # Image Info",
|
| 53 |
+
)
|
| 54 |
+
im_class = gr.Textbox(
|
| 55 |
+
label="Class",
|
| 56 |
+
)
|
| 57 |
+
im_concepts = gr.JSON(
|
| 58 |
+
label="Concepts",
|
| 59 |
+
)
|
| 60 |
+
with gr.Column():
|
| 61 |
+
image = gr.Image(
|
| 62 |
label="Image",
|
| 63 |
)
|
| 64 |
+
current_metadata = gr.State(global_variables.all_metadata)
|
| 65 |
+
selected_concepts = gr.State([])
|
| 66 |
+
button.click(
|
| 67 |
+
get_next_image,
|
| 68 |
+
outputs=[image, im_class, im_concepts, current_metadata, selected_concepts],
|
| 69 |
+
inputs=[split, concepts, current_metadata, selected_concepts]
|
| 70 |
+
)
|
src/global_variables.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Global variables used in the space.
|
| 2 |
+
"""
|
| 3 |
+
|
| 4 |
+
from huggingface_hub import HfApi
|
| 5 |
+
import jsonlines
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
from src.constants import DATASET_NAME, HF_TOKEN, ASSETS_FOLDER
|
| 10 |
+
|
| 11 |
+
hf_api: HfApi
|
| 12 |
+
all_metadata: dict
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def setup():
|
| 16 |
+
global hf_api
|
| 17 |
+
global all_metadata
|
| 18 |
+
hf_api = HfApi(token=HF_TOKEN)
|
| 19 |
+
hf_api.snapshot_download(
|
| 20 |
+
local_dir=f"{ASSETS_FOLDER}/{DATASET_NAME}",
|
| 21 |
+
repo_id=DATASET_NAME,
|
| 22 |
+
repo_type="dataset",
|
| 23 |
+
)
|
| 24 |
+
all_metadata = {}
|
| 25 |
+
for split in ["train", "validation", "test"]:
|
| 26 |
+
all_metadata[split] = []
|
| 27 |
+
with jsonlines.open(f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/metadata.jsonl") as reader:
|
| 28 |
+
for row in reader:
|
| 29 |
+
all_metadata[split].append(row)
|
| 30 |
+
|
| 31 |
+
def save_metadata():
|
| 32 |
+
global all_metadata
|
| 33 |
+
for split, values in all_metadata.items():
|
| 34 |
+
with jsonlines.open(f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/metadata.jsonl", mode='w') as writer:
|
| 35 |
+
writer.write_all(values)
|
| 36 |
+
hf_api.upload_file(
|
| 37 |
+
path_or_fileobj=f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/metadata.jsonl",
|
| 38 |
+
path_in_repo=f"data/{split}/metadata.jsonl",
|
| 39 |
+
repo_id=DATASET_NAME,
|
| 40 |
+
repo_type="dataset",
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
if gr.NO_RELOAD:
|
| 44 |
+
setup()
|