aldan.creo commited on
Commit
4affe68
·
1 Parent(s): 4db55cd
Files changed (2) hide show
  1. app.py +145 -75
  2. requirements.txt +1 -0
app.py CHANGED
@@ -18,6 +18,7 @@ load_dotenv()
18
  it_dataset = (
19
  load_dataset("imagenet-1k", split="train", streaming=True, trust_remote_code=True)
20
  .shuffle(42)
 
21
  .take(1000)
22
  )
23
 
@@ -58,14 +59,13 @@ with open("imagenet_categories_data.json") as f:
58
  filter(lambda x: x["name"] == "artifact, artefact", broad_categories)
59
  )
60
  artifact_categories = list(find_categories(artifact_category))
61
- logger.info(f"Artifact categories: {artifact_categories}")
62
 
63
 
64
  def filter_imgs_by_label(x):
65
  """
66
  Filter out the images that have label -1
67
  """
68
- logger.info(f'label: {x["label"]} (present: {x["label"] in artifact_categories})')
69
  return x["label"] in artifact_categories
70
 
71
 
@@ -98,12 +98,35 @@ def get_user_prompt():
98
 
99
 
100
  hf_writer = gr.HuggingFaceDatasetSaver(
101
- hf_token=os.environ["HF_TOKEN"], dataset_name="acmc/maker-faire-bot", private=True
 
 
102
  )
103
  csv_writer = gr.CSVLogger()
104
 
105
  theme = gr.themes.Default(primary_hue="cyan", secondary_hue="fuchsia")
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  with gr.Blocks(theme=theme) as demo:
108
  with gr.Row() as header:
109
  gr.Image(
@@ -116,73 +139,109 @@ with gr.Blocks(theme=theme) as demo:
116
  scale=0.2,
117
  )
118
  gr.Markdown(
119
- """
120
- # Maker Faire Bot
121
- """,
122
  visible=False,
123
  )
124
 
125
  user_prompt = gr.State(get_user_prompt())
126
 
127
- gr.Markdown("""# Think about these objects...""")
128
  gr.Markdown(
129
- """We want to teach the Maker Faire Bot some creativity. Help us get ideas on what you'd build!"""
 
 
130
  )
131
  image_components = []
132
  with gr.Row(variant="panel") as row:
133
- for i in range(len(user_prompt.value["images"])):
134
- with gr.Column(variant="default") as col:
135
- img = gr.Image(
136
- user_prompt.value["images"][i],
137
- label=user_prompt.value["human_labels"][i],
138
- interactive=False,
139
- show_download_button=False,
140
- show_share_button=False,
141
- )
142
- image_components.append(img)
143
- btn = gr.Button("Change", variant="secondary")
144
-
145
- def change_image(user_prompt):
146
- logger.info(f"Current user prompt: {user_prompt}")
147
- data = dataset[random.randint(0, len(dataset) - 1)]
148
- user_prompt = user_prompt.copy()
149
- user_prompt["images"][i] = data["image"]
150
- user_prompt["machine_labels"][i] = data["label"]
151
- user_prompt["human_labels"][i] = dataset.features["label"].int2str(
152
- data["label"]
153
- )
154
- return (
155
- user_prompt,
156
- user_prompt["images"][i],
157
- gr.update(
158
- label=user_prompt["human_labels"][i],
159
- ),
160
- )
161
-
162
- btn.click(
163
- change_image,
164
- inputs=[user_prompt],
165
- outputs=[user_prompt, img, img],
166
- preprocess=True,
167
- postprocess=True,
168
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
  user_answer_object = gr.Textbox(
171
  autofocus=True,
172
- placeholder="(example): An digital electronic guitar",
173
- label="What would you build?",
 
174
  )
175
  user_answer_explanation = gr.TextArea(
176
  autofocus=True,
177
- label="How would you build it?",
178
  # The example uses a roll of string, a camera, and a loudspeaker to build an electronic guitar.
179
- placeholder="""To build an electronic guitar, I would:
180
- 1. Use the roll of string to create the strings of the guitar.
181
- 2. Use the camera to capture a live video of the hand movements. That way, I can use an AI model to predict the chords.
182
- 3. Using a computer vision model, identify where the fingers are placed on the strings.
183
- 4. Calculate the sounds that the loudspeaker should produce based on the finger placements.
184
- 5. Play the sound through the loudspeaker.
185
- """,
186
  )
187
 
188
  csv_writer.setup(
@@ -194,20 +253,20 @@ with gr.Blocks(theme=theme) as demo:
194
  flagging_dir="user_data_hf",
195
  )
196
 
197
- submit_btn = gr.Button("Submit", variant="primary")
198
 
199
  def log_results(prompt, object, explanation):
200
  logger.info(f"logging - Prompt: {prompt}")
201
- csv_writer.flag(
202
- [
203
- {
204
- "machine_labels": prompt["machine_labels"],
205
- "human_labels": prompt["human_labels"],
206
- },
207
- object,
208
- explanation,
209
- ]
210
- )
211
  hf_writer.flag(
212
  [
213
  {
@@ -218,25 +277,36 @@ with gr.Blocks(theme=theme) as demo:
218
  explanation,
219
  ]
220
  )
 
221
 
222
  submit_btn.click(
223
  log_results,
224
  inputs=[user_prompt, user_answer_object, user_answer_explanation],
 
225
  preprocess=True,
226
  )
227
 
228
- new_prompt_btn = gr.Button("New Prompt", variant="secondary")
229
- new_prompt_btn.click(
230
- get_user_prompt,
231
- outputs=[user_prompt],
232
- # preprocess=True,
233
- )
 
 
 
 
 
 
 
 
 
 
234
 
235
  gr.Markdown(
236
- """
237
- 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).
238
- """
239
  )
240
-
241
  if __name__ == "__main__":
242
  demo.launch()
 
18
  it_dataset = (
19
  load_dataset("imagenet-1k", split="train", streaming=True, trust_remote_code=True)
20
  .shuffle(42)
21
+ .skip(0)
22
  .take(1000)
23
  )
24
 
 
59
  filter(lambda x: x["name"] == "artifact, artefact", broad_categories)
60
  )
61
  artifact_categories = list(find_categories(artifact_category))
62
+ # logger.info(f"Artifact categories: {artifact_categories}")
63
 
64
 
65
  def filter_imgs_by_label(x):
66
  """
67
  Filter out the images that have label -1
68
  """
 
69
  return x["label"] in artifact_categories
70
 
71
 
 
98
 
99
 
100
  hf_writer = gr.HuggingFaceDatasetSaver(
101
+ hf_token=os.environ["HF_TOKEN"],
102
+ dataset_name="acmc/maker-faire-bot",
103
+ private=True,
104
  )
105
  csv_writer = gr.CSVLogger()
106
 
107
  theme = gr.themes.Default(primary_hue="cyan", secondary_hue="fuchsia")
108
 
109
+ translation_table = {
110
+ "Maker Faire Bot": "Maker Faire Bot",
111
+ "**Think about these objects...**": "**Tænk på disse objekter...**",
112
+ "We want to build a Maker Faire Bot that can generate creative ideas. Help us by providing ideas on what you'd build with the following three objects!": "Vi vil bygge en Maker Faire Bot, der kan generere kreative ideer. Hjælp os ved at give ideer til, hvad du ville bygge med de følgende tre objekter!",
113
+ "Change": "Skift",
114
+ "What would you build with these 3 things?": "Hvad ville du bygge med disse 3 ting?",
115
+ "For example, if you have a roll of string, a camera, and a loudspeaker, you could build an electronic guitar. If you can write in Danish, that's great!": "For eksempel, hvis du har en rulle snor, et kamera og en højttaler, kunne du bygge en elektronisk guitar. Hvis du kan skrive på dansk, er det fantastisk!",
116
+ "It doesn't need to be a very long explanation, just a few sentences to help the bot understand your idea.": "Det behøver ikke være en meget lang forklaring, bare et par sætninger for at hjælpe robotten med at forstå din idé.",
117
+ "Submit": "Indsend",
118
+ "New Prompt": "Ny opgave",
119
+ "How would you build it?": "Hvordan ville du bygge det?",
120
+ "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.": "Dette er et eksperimentelt projekt. Dine data er anonyme og vil blive brugt til at træne en AI-model. Ved at bruge dette værktøj accepterer du vores politik.",
121
+ "(example): An digital electronic guitar": "(eksempel): En digital elektronisk guitar",
122
+ """I would use the roll of string to create the strings of the guitar, and the camera to analyze the hand movements. Then, I would use an AI model to predict the chords and play the sound through the loudspeaker.""": """Jeg ville bruge snoren til at skabe guitarens strenge, og kameraet til at analysere håndbevægelserne. Derefter ville jeg bruge en AI-model til at forudsige akkorderne og afspille lyden gennem højttaleren.""",
123
+ }
124
+
125
+
126
+ def get_bilingual_string(key):
127
+ return f"{translation_table[key]} // {key}"
128
+
129
+
130
  with gr.Blocks(theme=theme) as demo:
131
  with gr.Row() as header:
132
  gr.Image(
 
139
  scale=0.2,
140
  )
141
  gr.Markdown(
142
+ get_bilingual_string("Maker Faire Bot"),
 
 
143
  visible=False,
144
  )
145
 
146
  user_prompt = gr.State(get_user_prompt())
147
 
148
+ gr.Markdown(get_bilingual_string("**Think about these objects...**"))
149
  gr.Markdown(
150
+ get_bilingual_string(
151
+ "We want to build a Maker Faire Bot that can generate creative ideas. Help us by providing ideas on what you'd build with the following three objects!"
152
+ )
153
  )
154
  image_components = []
155
  with gr.Row(variant="panel") as row:
156
+ def change_image(this_i, user_prompt):
157
+ logger.info(
158
+ f"Current user prompt: {user_prompt}, current image index: {this_i}"
159
+ )
160
+ data = dataset[random.randint(0, len(dataset) - 1)]
161
+ new_user_prompt = user_prompt.copy()
162
+ new_user_prompt["images"][this_i] = data["image"]
163
+ new_user_prompt["machine_labels"][this_i] = data["label"]
164
+ new_user_prompt["human_labels"][this_i] = dataset.features[
165
+ "label"
166
+ ].int2str(data["label"])
167
+ logger.info(f"New user prompt: {new_user_prompt}")
168
+ return (
169
+ new_user_prompt,
170
+ new_user_prompt["images"][this_i],
171
+ gr.update(
172
+ label=new_user_prompt["human_labels"][this_i],
173
+ ),
174
+ )
175
+
176
+ with gr.Column(variant="default") as col:
177
+ img = gr.Image(
178
+ user_prompt.value["images"][0],
179
+ label=user_prompt.value["human_labels"][0],
180
+ interactive=False,
181
+ show_download_button=False,
182
+ show_share_button=False,
183
+ )
184
+ image_components.append(img)
185
+ btn = gr.Button(get_bilingual_string("Change"), variant="secondary")
186
+
187
+ btn.click(
188
+ lambda *args: change_image(0, *args),
189
+ inputs=[user_prompt],
190
+ outputs=[user_prompt, img, img],
191
+ preprocess=True,
192
+ postprocess=True,
193
+ )
194
+ with gr.Column(variant="default") as col:
195
+ img = gr.Image(
196
+ user_prompt.value["images"][1],
197
+ label=user_prompt.value["human_labels"][1],
198
+ interactive=False,
199
+ show_download_button=False,
200
+ show_share_button=False,
201
+ )
202
+ image_components.append(img)
203
+ btn = gr.Button(get_bilingual_string("Change"), variant="secondary")
204
+
205
+ btn.click(
206
+ lambda *args: change_image(1, *args),
207
+ inputs=[user_prompt],
208
+ outputs=[user_prompt, img, img],
209
+ preprocess=True,
210
+ postprocess=True,
211
+ )
212
+ with gr.Column(variant="default") as col:
213
+ img = gr.Image(
214
+ user_prompt.value["images"][2],
215
+ label=user_prompt.value["human_labels"][2],
216
+ interactive=False,
217
+ show_download_button=False,
218
+ show_share_button=False,
219
+ )
220
+ image_components.append(img)
221
+ btn = gr.Button(get_bilingual_string("Change"), variant="secondary")
222
+
223
+ btn.click(
224
+ lambda *args: change_image(2, *args),
225
+ inputs=[user_prompt],
226
+ outputs=[user_prompt, img, img],
227
+ preprocess=True,
228
+ postprocess=True,
229
+ )
230
 
231
  user_answer_object = gr.Textbox(
232
  autofocus=True,
233
+ placeholder=get_bilingual_string("(example): An digital electronic guitar"),
234
+ label=get_bilingual_string("What would you build with these 3 things?"),
235
+ info=get_bilingual_string("For example, if you have a roll of string, a camera, and a loudspeaker, you could build an electronic guitar. If you can write in Danish, that's great!")
236
  )
237
  user_answer_explanation = gr.TextArea(
238
  autofocus=True,
239
+ label=get_bilingual_string("How would you build it?"),
240
  # The example uses a roll of string, a camera, and a loudspeaker to build an electronic guitar.
241
+ placeholder=get_bilingual_string(
242
+ """I would use the roll of string to create the strings of the guitar, and the camera to analyze the hand movements. Then, I would use an AI model to predict the chords and play the sound through the loudspeaker."""
243
+ ),
244
+ info=get_bilingual_string("It doesn't need to be a very long explanation, just a few sentences to help the bot understand your idea.")
 
 
 
245
  )
246
 
247
  csv_writer.setup(
 
253
  flagging_dir="user_data_hf",
254
  )
255
 
256
+ submit_btn = gr.Button(get_bilingual_string("Submit"), variant="primary")
257
 
258
  def log_results(prompt, object, explanation):
259
  logger.info(f"logging - Prompt: {prompt}")
260
+ # csv_writer.flag(
261
+ # [
262
+ # {
263
+ # "machine_labels": prompt["machine_labels"],
264
+ # "human_labels": prompt["human_labels"],
265
+ # },
266
+ # object,
267
+ # explanation,
268
+ # ]
269
+ # )
270
  hf_writer.flag(
271
  [
272
  {
 
277
  explanation,
278
  ]
279
  )
280
+ return ["", ""] # Clear the textboxes
281
 
282
  submit_btn.click(
283
  log_results,
284
  inputs=[user_prompt, user_answer_object, user_answer_explanation],
285
+ outputs=[user_answer_object, user_answer_explanation],
286
  preprocess=True,
287
  )
288
 
289
+ # def renew_prompt(image_components):
290
+ # new_prompt = get_user_prompt()
291
+ # for i in range(len(new_prompt["images"])):
292
+ # image_components[i].update(
293
+ # url=new_prompt["images"][i],
294
+ # label=new_prompt["human_labels"][i],
295
+ # )
296
+ # return new_prompt
297
+
298
+ # new_prompt_btn = gr.Button(get_bilingual_string("New Prompt"), variant="secondary")
299
+ # new_prompt_btn.click(
300
+ # renew_prompt,
301
+ # inputs=image_components,
302
+ # outputs=[user_prompt],
303
+ # # preprocess=True,
304
+ # )
305
 
306
  gr.Markdown(
307
+ get_bilingual_string(
308
+ "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."
309
+ )
310
  )
 
311
  if __name__ == "__main__":
312
  demo.launch()
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  datasets==2.19.0
2
  python-dotenv==1.0.1
 
 
1
  datasets==2.19.0
2
  python-dotenv==1.0.1
3
+ gradio==4.28.0