Spaces:
Runtime error
Runtime error
aldan.creo
commited on
Commit
·
1e83e3d
1
Parent(s):
a5ef230
ImageNet
Browse files- app.py +116 -13
- imagenet_categories_data.json +0 -0
- requirements.txt +3 -0
app.py
CHANGED
@@ -1,27 +1,94 @@
|
|
|
|
1 |
import logging
|
2 |
import os
|
|
|
3 |
|
4 |
import gradio as gr
|
|
|
5 |
from dotenv import load_dotenv
|
6 |
|
|
|
7 |
logger = logging.getLogger(__name__)
|
8 |
-
logger.setLevel(logging.
|
9 |
|
10 |
load_dotenv()
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
def get_user_prompt():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
return {
|
15 |
-
"images":
|
16 |
-
|
17 |
-
|
18 |
-
"images/1.jpeg",
|
19 |
-
],
|
20 |
-
"labels": [
|
21 |
-
"A roll of string",
|
22 |
-
"A camera",
|
23 |
-
"A loudspeaker",
|
24 |
-
],
|
25 |
}
|
26 |
|
27 |
|
@@ -56,16 +123,45 @@ with gr.Blocks(theme=theme) as demo:
|
|
56 |
gr.Markdown(
|
57 |
"""We want to teach the Maker Faire Bot some creativity. Help us get ideas on what you'd build!"""
|
58 |
)
|
|
|
59 |
with gr.Row(variant="panel") as row:
|
60 |
for i in range(len(user_prompt.value["images"])):
|
61 |
with gr.Column(variant="default") as col:
|
62 |
-
gr.Image(
|
63 |
user_prompt.value["images"][i],
|
64 |
-
label=user_prompt.value["
|
65 |
interactive=False,
|
66 |
show_download_button=False,
|
67 |
show_share_button=False,
|
68 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
user_answer_object = gr.Textbox(
|
71 |
autofocus=True,
|
@@ -106,6 +202,13 @@ with gr.Blocks(theme=theme) as demo:
|
|
106 |
preprocess=False,
|
107 |
)
|
108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
gr.Markdown(
|
110 |
"""
|
111 |
This is an experimental project. Your data is anonymous and will be used to train an AI model. By using this tool, you agree to our [policy](https://makerfaire.com/privacy).
|
|
|
1 |
+
import json
|
2 |
import logging
|
3 |
import os
|
4 |
+
from functools import partial
|
5 |
|
6 |
import gradio as gr
|
7 |
+
from datasets import Dataset, load_dataset
|
8 |
from dotenv import load_dotenv
|
9 |
|
10 |
+
logging.basicConfig(level=logging.INFO)
|
11 |
logger = logging.getLogger(__name__)
|
12 |
+
logger.setLevel(logging.INFO)
|
13 |
|
14 |
load_dotenv()
|
15 |
|
16 |
+
# dataset = load_dataset("detection-datasets/coco")
|
17 |
+
it_dataset = load_dataset(
|
18 |
+
"imagenet-1k", split="train", streaming=True, trust_remote_code=True
|
19 |
+
).shuffle(42)
|
20 |
+
|
21 |
+
|
22 |
+
def gen_from_iterable_dataset(iterable_ds):
|
23 |
+
"""
|
24 |
+
Convert an iterable dataset to a generator
|
25 |
+
"""
|
26 |
+
yield from iterable_ds
|
27 |
+
|
28 |
+
|
29 |
+
# imagenet_categories_data.json is a JSON file containing a hierarchy of ImageNet categories.
|
30 |
+
# We want to take all categories under "artifact, artefact".
|
31 |
+
# Each node has this structure:
|
32 |
+
# {
|
33 |
+
# "id": 1,
|
34 |
+
# "name": "entity",
|
35 |
+
# "children": ...
|
36 |
+
# }
|
37 |
+
with open("imagenet_categories_data.json") as f:
|
38 |
+
data = json.load(f)
|
39 |
+
|
40 |
+
# Recursively find all categories under "artifact, artefact".
|
41 |
+
# We want to get all the "index" values of the leaf nodes. Nodes that are not leaf nodes have a "children" key.
|
42 |
+
def find_categories(node):
|
43 |
+
if "children" in node:
|
44 |
+
for child in node["children"]:
|
45 |
+
yield from find_categories(child)
|
46 |
+
elif "index" in node:
|
47 |
+
yield node["index"]
|
48 |
+
|
49 |
+
broad_categories = data["children"]
|
50 |
+
artifact_category = next(
|
51 |
+
filter(lambda x: x["name"] == "artifact, artefact", broad_categories)
|
52 |
+
)
|
53 |
+
artifact_categories = list(find_categories(artifact_category))
|
54 |
+
logger.info(f"Artifact categories: {artifact_categories}")
|
55 |
+
|
56 |
+
|
57 |
+
def filter_imgs_by_label(x):
|
58 |
+
"""
|
59 |
+
Filter out the images that have label -1
|
60 |
+
"""
|
61 |
+
print(f'label: {x["label"]}')
|
62 |
+
return x["label"] in artifact_categories
|
63 |
+
|
64 |
+
|
65 |
+
dataset = it_dataset.take(1000).filter(filter_imgs_by_label)
|
66 |
+
dataset = Dataset.from_generator(
|
67 |
+
partial(gen_from_iterable_dataset, it_dataset), features=it_dataset.features
|
68 |
+
)
|
69 |
+
dataset_iterable = iter(dataset)
|
70 |
+
|
71 |
|
72 |
def get_user_prompt():
|
73 |
+
# Pick the first 3 images and labels
|
74 |
+
images = []
|
75 |
+
machine_labels = []
|
76 |
+
human_labels = []
|
77 |
+
for i in range(3):
|
78 |
+
data = next(dataset_iterable)
|
79 |
+
logger.info(f"Data: {data}")
|
80 |
+
images.append(data["image"])
|
81 |
+
# Get the label as a human readable string
|
82 |
+
machine_labels.append(data["label"])
|
83 |
+
logger.info(dataset)
|
84 |
+
human_label = dataset.features["label"].int2str(data["label"]) + str(
|
85 |
+
data["label"]
|
86 |
+
)
|
87 |
+
human_labels.append(human_label)
|
88 |
return {
|
89 |
+
"images": images,
|
90 |
+
"machine_labels": machine_labels,
|
91 |
+
"human_labels": human_labels,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
}
|
93 |
|
94 |
|
|
|
123 |
gr.Markdown(
|
124 |
"""We want to teach the Maker Faire Bot some creativity. Help us get ideas on what you'd build!"""
|
125 |
)
|
126 |
+
image_components = []
|
127 |
with gr.Row(variant="panel") as row:
|
128 |
for i in range(len(user_prompt.value["images"])):
|
129 |
with gr.Column(variant="default") as col:
|
130 |
+
img = gr.Image(
|
131 |
user_prompt.value["images"][i],
|
132 |
+
label=user_prompt.value["human_labels"][i],
|
133 |
interactive=False,
|
134 |
show_download_button=False,
|
135 |
show_share_button=False,
|
136 |
)
|
137 |
+
image_components.append(img)
|
138 |
+
btn = gr.Button("Change", variant="secondary")
|
139 |
+
|
140 |
+
def change_image(user_prompt):
|
141 |
+
data = next(dataset_iterable)
|
142 |
+
logger.info(user_prompt)
|
143 |
+
user_prompt = user_prompt.copy()
|
144 |
+
user_prompt["images"][i] = data["image"]
|
145 |
+
user_prompt["machine_labels"][i] = data["label"]
|
146 |
+
user_prompt["human_labels"][i] = dataset.features["label"].int2str(
|
147 |
+
data["label"]
|
148 |
+
)
|
149 |
+
logger.info(user_prompt)
|
150 |
+
return (
|
151 |
+
user_prompt,
|
152 |
+
user_prompt["images"][i],
|
153 |
+
gr.update(
|
154 |
+
label=user_prompt["human_labels"][i],
|
155 |
+
),
|
156 |
+
)
|
157 |
+
|
158 |
+
btn.click(
|
159 |
+
change_image,
|
160 |
+
inputs=[user_prompt],
|
161 |
+
outputs=[user_prompt, img, img],
|
162 |
+
preprocess=True,
|
163 |
+
postprocess=True,
|
164 |
+
)
|
165 |
|
166 |
user_answer_object = gr.Textbox(
|
167 |
autofocus=True,
|
|
|
202 |
preprocess=False,
|
203 |
)
|
204 |
|
205 |
+
new_prompt_btn = gr.Button("New Prompt", variant="secondary")
|
206 |
+
new_prompt_btn.click(
|
207 |
+
get_user_prompt,
|
208 |
+
outputs=[user_prompt],
|
209 |
+
preprocess=False,
|
210 |
+
)
|
211 |
+
|
212 |
gr.Markdown(
|
213 |
"""
|
214 |
This is an experimental project. Your data is anonymous and will be used to train an AI model. By using this tool, you agree to our [policy](https://makerfaire.com/privacy).
|
imagenet_categories_data.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
datasets==2.19.0
|
2 |
+
gradio==4.28.0
|
3 |
+
python-dotenv==1.0.1
|