huzey commited on
Commit
4fa6c93
1 Parent(s): 241e797

add image profile

Browse files
Files changed (1) hide show
  1. app.py +279 -53
app.py CHANGED
@@ -1,8 +1,10 @@
1
  # Author: Huzheng Yang
2
  # %%
3
  import copy
 
4
  from functools import partial
5
  from io import BytesIO
 
6
  import os
7
  import uuid
8
 
@@ -1152,14 +1154,6 @@ def run_fn(
1152
  raise gr.Error(message, duration=0)
1153
 
1154
 
1155
-
1156
- def make_input_images_section(rows=1, cols=3, height="auto"):
1157
- gr.Markdown('### Input Images')
1158
- input_gallery = gr.Gallery(value=None, label="Select images", show_label=True, elem_id="images", columns=[cols], rows=[rows], object_fit="contain", height=height, type="pil", show_share_button=False)
1159
- submit_button = gr.Button("🔴 RUN", elem_id="submit_button", variant='primary')
1160
- clear_images_button = gr.Button("🗑️Clear", elem_id='clear_button', variant='stop')
1161
- return input_gallery, submit_button, clear_images_button
1162
-
1163
  def make_input_video_section():
1164
  # gr.Markdown('### Input Video')
1165
  input_gallery = gr.Video(value=None, label="Select video", elem_id="video-input", height="auto", show_share_button=False, interactive=True)
@@ -1170,8 +1164,33 @@ def make_input_video_section():
1170
  clear_images_button = gr.Button("🗑️Clear", elem_id='clear_button', variant='stop')
1171
  return input_gallery, submit_button, clear_images_button, max_frames_number
1172
 
1173
- def make_dataset_images_section(advanced=False, is_random=False):
1174
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1175
  gr.Markdown('### Load Datasets')
1176
  load_images_button = gr.Button("🔴 Load Images", elem_id="load-images-button", variant='primary')
1177
  advanced_radio = gr.Radio(["Basic", "Advanced"], label="Datasets", value="Advanced" if advanced else "Basic", elem_id="advanced-radio", show_label=True)
@@ -1204,7 +1223,67 @@ def make_dataset_images_section(advanced=False, is_random=False):
1204
  filter_by_class_text = gr.Textbox(label="Class to select", value="0,33,99", elem_id="filter_by_class_text", info=f"e.g. `0,1,2`. (1000 classes)", visible=False)
1205
  is_random_checkbox = gr.Checkbox(label="Random shuffle", value=True, elem_id="random_seed_checkbox")
1206
  random_seed_slider = gr.Slider(0, 1000, step=1, label="Random seed", value=42, elem_id="random_seed", visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1208
 
1209
  if advanced:
1210
  advanced_block.visible = True
@@ -1259,8 +1338,7 @@ def make_dataset_images_section(advanced=False, is_random=False):
1259
  key = list(dataset.keys())[0]
1260
  dataset = dataset[key]
1261
  except Exception as e:
1262
- gr.Error(f"Error loading dataset {dataset_name}: {e}")
1263
- return None
1264
  if num_images > len(dataset):
1265
  num_images = len(dataset)
1266
 
@@ -1310,15 +1388,188 @@ def make_dataset_images_section(advanced=False, is_random=False):
1310
  return img
1311
  images = [center_crop_image(image) for image in images]
1312
 
1313
- return images
1314
-
1315
- load_images_button.click(load_dataset_images,
1316
- inputs=[advanced_radio, dataset_dropdown, num_images_slider,
 
 
 
 
 
 
 
 
 
 
 
1317
  filter_by_class_checkbox, filter_by_class_text,
1318
  is_random_checkbox, random_seed_slider],
1319
  outputs=[input_gallery])
1320
 
1321
- return dataset_dropdown, num_images_slider, random_seed_slider, load_images_button
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1322
 
1323
 
1324
  # def random_rotate_rgb_gallery(images):
@@ -1501,8 +1752,7 @@ with demo:
1501
 
1502
  with gr.Row():
1503
  with gr.Column(scale=5, min_width=200):
1504
- input_gallery, submit_button, clear_images_button = make_input_images_section()
1505
- dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_dataset_images_section()
1506
  num_images_slider.value = 30
1507
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information", autofocus=False, autoscroll=False)
1508
 
@@ -1516,10 +1766,7 @@ with demo:
1516
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1517
  sampling_method_dropdown, ncut_metric_dropdown, positive_prompt, negative_prompt
1518
  ] = make_parameters_section()
1519
- num_eig_slider.value = 30
1520
 
1521
- clear_images_button.click(lambda x: ([], [], []), outputs=[input_gallery, output_gallery, cluster_gallery])
1522
-
1523
  false_placeholder = gr.Checkbox(label="False", value=False, elem_id="false_placeholder", visible=False)
1524
  no_prompt = gr.Textbox("", label="", elem_id="empty_placeholder", type="text", placeholder="", visible=False)
1525
 
@@ -1542,8 +1789,7 @@ with demo:
1542
 
1543
  with gr.Row():
1544
  with gr.Column(scale=5, min_width=200):
1545
- input_gallery, submit_button, clear_images_button = make_input_images_section()
1546
- dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_dataset_images_section()
1547
  num_images_slider.value = 30
1548
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information", autofocus=False, autoscroll=False, lines=20)
1549
 
@@ -1559,8 +1805,6 @@ with demo:
1559
  sampling_method_dropdown, ncut_metric_dropdown, positive_prompt, negative_prompt
1560
  ] = make_parameters_section()
1561
  num_eig_slider.value = 30
1562
-
1563
- clear_images_button.click(lambda x: ([], [], [], []), outputs=[input_gallery, output_gallery, cluster_gallery, norm_gallery])
1564
 
1565
  false_placeholder = gr.Checkbox(label="False", value=False, elem_id="false_placeholder", visible=False)
1566
  no_prompt = gr.Textbox("", label="", elem_id="empty_placeholder", type="text", placeholder="", visible=False)
@@ -1604,8 +1848,7 @@ with demo:
1604
 
1605
  with gr.Row():
1606
  with gr.Column(scale=5, min_width=200):
1607
- input_gallery, submit_button, clear_images_button = make_input_images_section()
1608
- dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_dataset_images_section()
1609
 
1610
  with gr.Column(scale=5, min_width=200):
1611
  output_gallery = make_output_images_section()
@@ -1624,7 +1867,6 @@ with demo:
1624
  # logging text box
1625
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
1626
 
1627
- clear_images_button.click(lambda x: ([], []), outputs=[input_gallery, output_gallery])
1628
  false_placeholder = gr.Checkbox(label="False", value=False, elem_id="false_placeholder", visible=False)
1629
  no_prompt = gr.Textbox("", label="", elem_id="empty_placeholder", type="text", placeholder="", visible=False)
1630
 
@@ -1651,8 +1893,7 @@ with demo:
1651
 
1652
  with gr.Row():
1653
  with gr.Column(scale=5, min_width=200):
1654
- input_gallery, submit_button, clear_images_button = make_input_images_section()
1655
- dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_dataset_images_section(advanced=True)
1656
  num_images_slider.value = 100
1657
  clear_images_button.visible = False
1658
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
@@ -1695,7 +1936,6 @@ with demo:
1695
  false_placeholder.visible = False
1696
  number_placeholder = gr.Number(0, label="Number placeholder", elem_id="number_placeholder")
1697
  number_placeholder.visible = False
1698
- clear_images_button.click(lambda x: ([],), outputs=[input_gallery])
1699
  no_prompt = gr.Textbox("", label="", elem_id="empty_placeholder", type="text", placeholder="", visible=False)
1700
 
1701
  submit_button.click(
@@ -1719,8 +1959,7 @@ with demo:
1719
 
1720
  with gr.Row():
1721
  with gr.Column(scale=5, min_width=200):
1722
- input_gallery, submit_button, clear_images_button = make_input_images_section(rows=5, cols=3, height='auto')
1723
- dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_dataset_images_section(advanced=True)
1724
  num_images_slider.value = 100
1725
  clear_images_button.visible = False
1726
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information", lines=20)
@@ -1769,7 +2008,6 @@ with demo:
1769
  false_placeholder.visible = False
1770
  number_placeholder = gr.Number(0, label="Number placeholder", elem_id="number_placeholder")
1771
  number_placeholder.visible = False
1772
- clear_images_button.click(lambda x: ([],), outputs=[input_gallery])
1773
  no_prompt = gr.Textbox("", label="", elem_id="empty_placeholder", type="text", placeholder="", visible=False)
1774
 
1775
  submit_button.click(
@@ -1859,9 +2097,7 @@ with demo:
1859
 
1860
  with gr.Row():
1861
  with gr.Column(scale=5, min_width=200):
1862
- input_gallery, submit_button, clear_images_button = make_input_images_section()
1863
- dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_dataset_images_section(advanced=False)
1864
- clear_images_button.click(lambda x: ([], [], [], []), outputs=[input_gallery, l1_gallery, l2_gallery, l3_gallery])
1865
 
1866
  with gr.Column(scale=5, min_width=200):
1867
  [
@@ -1898,8 +2134,7 @@ with demo:
1898
  gr.Markdown('---')
1899
  with gr.Row():
1900
  with gr.Column(scale=5, min_width=200):
1901
- input_gallery, submit_button, clear_images_button = make_input_images_section()
1902
- dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_dataset_images_section(advanced=True, is_random=True)
1903
  num_images_slider.value = 100
1904
 
1905
 
@@ -1926,9 +2161,7 @@ with demo:
1926
  num_sample_tsne_slider.value = 1000
1927
  # logging text box
1928
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
1929
-
1930
- clear_images_button.click(lambda x: ([], []), outputs=[input_gallery, output_gallery])
1931
-
1932
  false_placeholder = gr.Checkbox(label="False", value=False, elem_id="false_placeholder", visible=False)
1933
  no_prompt = gr.Textbox("", label="", elem_id="empty_placeholder", type="text", placeholder="", visible=False)
1934
 
@@ -1980,8 +2213,7 @@ with demo:
1980
 
1981
  with gr.Row():
1982
  with gr.Column(scale=5, min_width=200):
1983
- input_gallery, submit_button, clear_images_button = make_input_images_section()
1984
- dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_dataset_images_section(advanced=True, is_random=True)
1985
  num_images_slider.value = 100
1986
 
1987
 
@@ -2014,9 +2246,7 @@ with demo:
2014
  num_sample_tsne_slider.value = 1000
2015
  # logging text box
2016
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
2017
-
2018
- clear_images_button.click(lambda x: ([],), outputs=[input_gallery])
2019
-
2020
  true_placeholder = gr.Checkbox(label="True placeholder", value=True, elem_id="true_placeholder")
2021
  true_placeholder.visible = False
2022
  false_placeholder = gr.Checkbox(label="False placeholder", value=False, elem_id="false_placeholder")
@@ -2078,10 +2308,8 @@ with demo:
2078
 
2079
  with gr.Row():
2080
  with gr.Column(scale=5, min_width=200):
2081
- input_gallery, submit_button, clear_images_button = make_input_images_section()
2082
- clear_images_button.click(lambda x: [], outputs=[input_gallery])
2083
  submit_button.visible = False
2084
- dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_dataset_images_section(advanced=True)
2085
 
2086
 
2087
  for i in range(3):
@@ -2153,10 +2381,8 @@ with demo:
2153
 
2154
  with gr.Row():
2155
  with gr.Column(scale=5, min_width=200):
2156
- input_gallery, submit_button, clear_images_button = make_input_images_section()
2157
- clear_images_button.click(lambda x: [], outputs=[input_gallery])
2158
  submit_button.visible = False
2159
- dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_dataset_images_section(advanced=True)
2160
 
2161
 
2162
  for i in range(3):
 
1
  # Author: Huzheng Yang
2
  # %%
3
  import copy
4
+ import pickle
5
  from functools import partial
6
  from io import BytesIO
7
+ import json
8
  import os
9
  import uuid
10
 
 
1154
  raise gr.Error(message, duration=0)
1155
 
1156
 
 
 
 
 
 
 
 
 
1157
  def make_input_video_section():
1158
  # gr.Markdown('### Input Video')
1159
  input_gallery = gr.Video(value=None, label="Select video", elem_id="video-input", height="auto", show_share_button=False, interactive=True)
 
1164
  clear_images_button = gr.Button("🗑️Clear", elem_id='clear_button', variant='stop')
1165
  return input_gallery, submit_button, clear_images_button, max_frames_number
1166
 
 
1167
 
1168
+ def make_input_images_section(rows=1, cols=3, height="auto", advanced=False, is_random=False):
1169
+ gr.Markdown('### Input Images')
1170
+ input_gallery = gr.Gallery(value=None, label="Input images", show_label=True, elem_id="input_images", columns=[cols], rows=[rows], object_fit="contain", height=height, type="pil", show_share_button=False)
1171
+
1172
+ submit_button = gr.Button("🔴 RUN", elem_id="submit_button", variant='primary')
1173
+ with gr.Row():
1174
+ clear_images_button = gr.Button("🗑️Clear", elem_id='clear_button', variant='stop')
1175
+ clear_images_button.click(fn=lambda: gr.update(value=None), outputs=[input_gallery])
1176
+ upload_button = gr.UploadButton(elem_id="upload_button", label="⬆️Upload", variant='secondary', file_types=["image"], file_count="multiple")
1177
+
1178
+ def convert_to_pil_and_append(images, new_images):
1179
+ if images is None:
1180
+ images = []
1181
+ if new_images is None:
1182
+ return images
1183
+ if isinstance(new_images, Image.Image):
1184
+ images.append(new_images)
1185
+ if isinstance(new_images, list):
1186
+ images += [Image.open(new_image) for new_image in new_images]
1187
+ if isinstance(new_images, str):
1188
+ images.append(Image.open(new_images))
1189
+ new_images = None
1190
+ return images
1191
+ upload_button.upload(convert_to_pil_and_append, inputs=[input_gallery, upload_button], outputs=[input_gallery])
1192
+
1193
+
1194
  gr.Markdown('### Load Datasets')
1195
  load_images_button = gr.Button("🔴 Load Images", elem_id="load-images-button", variant='primary')
1196
  advanced_radio = gr.Radio(["Basic", "Advanced"], label="Datasets", value="Advanced" if advanced else "Basic", elem_id="advanced-radio", show_label=True)
 
1223
  filter_by_class_text = gr.Textbox(label="Class to select", value="0,33,99", elem_id="filter_by_class_text", info=f"e.g. `0,1,2`. (1000 classes)", visible=False)
1224
  is_random_checkbox = gr.Checkbox(label="Random shuffle", value=True, elem_id="random_seed_checkbox")
1225
  random_seed_slider = gr.Slider(0, 1000, step=1, label="Random seed", value=42, elem_id="random_seed", visible=True)
1226
+
1227
+ # add functionality, save and load images to profile
1228
+ with gr.Accordion("Saved Image Profiles", open=False) as profile_accordion:
1229
+ with gr.Row():
1230
+ profile_text = gr.Textbox(label="Profile name", placeholder="Type here: Profile name to save/load/delete", elem_id="profile-name", scale=6, show_label=False)
1231
+ list_profiles_button = gr.Button("📋 List", elem_id="list-profile-button", variant='secondary', scale=3)
1232
+ with gr.Row():
1233
+ save_profile_button = gr.Button("💾 Save", elem_id="save-profile-button", variant='secondary')
1234
+ load_profile_button = gr.Button("📂 Load", elem_id="load-profile-button", variant='secondary')
1235
+ delete_profile_button = gr.Button("🗑️ Delete", elem_id="delete-profile-button", variant='secondary')
1236
+
1237
+ class OnDiskProfiles:
1238
+ def __init__(self, profile_dir="demo_profiles"):
1239
+ if not os.path.exists(profile_dir):
1240
+ os.makedirs(profile_dir)
1241
+ self.profile_dir = profile_dir
1242
+
1243
+ def list_profiles(self):
1244
+ profiles = os.listdir(self.profile_dir)
1245
+ # remove hidden files
1246
+ profiles = [p for p in profiles if not p.startswith(".")]
1247
+ if len(profiles) == 0:
1248
+ return "No profiles found."
1249
+ profile_text = "</br>".join(profiles)
1250
+ n_files = len(profiles)
1251
+ profile_text = f"Number of profiles: {n_files}</br>---------</br>" + profile_text
1252
+ return profile_text
1253
+
1254
+ def save_profile(self, profile_name, images):
1255
+ profile_path = os.path.join(self.profile_dir, profile_name)
1256
+ if os.path.exists(profile_path):
1257
+ raise gr.Error(f"Profile {profile_name} already exists.")
1258
+ with open(profile_path, "wb") as f:
1259
+ pickle.dump(images, f)
1260
+ gr.Info(f"Profile {profile_name} saved.")
1261
+ return profile_path
1262
 
1263
+ def load_profile(self, profile_name):
1264
+ profile_path = os.path.join(self.profile_dir, profile_name)
1265
+ if not os.path.exists(profile_path):
1266
+ raise gr.Error(f"Profile {profile_name} not found.")
1267
+ with open(profile_path, "rb") as f:
1268
+ images = pickle.load(f)
1269
+ gr.Info(f"Profile {profile_name} loaded.")
1270
+ return images
1271
+
1272
+ def delete_profile(self, profile_name):
1273
+ profile_path = os.path.join(self.profile_dir, profile_name)
1274
+ os.remove(profile_path)
1275
+ gr.Info(f"Profile {profile_name} deleted.")
1276
+ return profile_path
1277
+
1278
+ home_dir = os.path.expanduser("~")
1279
+ defualt_dir = os.path.join(home_dir, ".cache")
1280
+ cache_dir = os.environ.get("DEMO_PROFILE_CACHE_DIR", defualt_dir)
1281
+ cache_dir = os.path.join(cache_dir, "demo_profiles")
1282
+ on_disk_profiles = OnDiskProfiles(cache_dir)
1283
+ save_profile_button.click(fn=lambda name, images: on_disk_profiles.save_profile(name, images), inputs=[profile_text, input_gallery])
1284
+ load_profile_button.click(fn=lambda name: gr.update(value=on_disk_profiles.load_profile(name)), inputs=profile_text, outputs=[input_gallery])
1285
+ delete_profile_button.click(fn=lambda name: on_disk_profiles.delete_profile(name), inputs=profile_text)
1286
+ list_profiles_button.click(fn=lambda: gr.Info(on_disk_profiles.list_profiles(), duration=0))
1287
 
1288
  if advanced:
1289
  advanced_block.visible = True
 
1338
  key = list(dataset.keys())[0]
1339
  dataset = dataset[key]
1340
  except Exception as e:
1341
+ raise gr.Error(f"Error loading dataset {dataset_name}: {e}")
 
1342
  if num_images > len(dataset):
1343
  num_images = len(dataset)
1344
 
 
1388
  return img
1389
  images = [center_crop_image(image) for image in images]
1390
 
1391
+ return images
1392
+
1393
+ def load_and_append(existing_images, *args, **kwargs):
1394
+ new_images = load_dataset_images(*args, **kwargs)
1395
+ if new_images is None:
1396
+ return existing_images
1397
+ if len(new_images) == 0:
1398
+ return existing_images
1399
+ if existing_images is None:
1400
+ existing_images = []
1401
+ existing_images += new_images
1402
+ return existing_images
1403
+
1404
+ load_images_button.click(load_and_append,
1405
+ inputs=[input_gallery, advanced_radio, dataset_dropdown, num_images_slider,
1406
  filter_by_class_checkbox, filter_by_class_text,
1407
  is_random_checkbox, random_seed_slider],
1408
  outputs=[input_gallery])
1409
 
1410
+
1411
+
1412
+ return input_gallery, submit_button, clear_images_button, dataset_dropdown, num_images_slider, random_seed_slider, load_images_button
1413
+
1414
+
1415
+
1416
+ # def make_input_images_section(rows=1, cols=3, height="auto"):
1417
+ # gr.Markdown('### Input Images')
1418
+ # input_gallery = gr.Gallery(value=None, label="Select images", show_label=True, elem_id="images", columns=[cols], rows=[rows], object_fit="contain", height=height, type="pil", show_share_button=False)
1419
+ # submit_button = gr.Button("🔴 RUN", elem_id="submit_button", variant='primary')
1420
+ # clear_images_button = gr.Button("🗑️Clear", elem_id='clear_button', variant='stop')
1421
+ # return input_gallery, submit_button, clear_images_button
1422
+
1423
+
1424
+ # def make_dataset_images_section(advanced=False, is_random=False):
1425
+
1426
+ # gr.Markdown('### Load Datasets')
1427
+ # load_images_button = gr.Button("🔴 Load Images", elem_id="load-images-button", variant='primary')
1428
+ # advanced_radio = gr.Radio(["Basic", "Advanced"], label="Datasets", value="Advanced" if advanced else "Basic", elem_id="advanced-radio", show_label=True)
1429
+ # with gr.Column() as basic_block:
1430
+ # example_gallery = gr.Gallery(value=example_items, label="Example Images", show_label=True, columns=[3], rows=[2], object_fit="scale-down", height="200px", show_share_button=False, elem_id="example-gallery")
1431
+ # with gr.Column() as advanced_block:
1432
+ # # dataset_names = DATASET_NAMES
1433
+ # # dataset_classes = DATASET_CLASSES
1434
+ # dataset_categories = list(DATASETS.keys())
1435
+ # defualt_cat = dataset_categories[0]
1436
+ # def get_choices(cat):
1437
+ # return [tup[0] for tup in DATASETS[cat]]
1438
+ # defualt_choices = get_choices(defualt_cat)
1439
+ # with gr.Row():
1440
+ # dataset_radio = gr.Radio(dataset_categories, label="Dataset Category", value=defualt_cat, elem_id="dataset-radio", show_label=True, min_width=600)
1441
+ # # dataset_dropdown = gr.Dropdown(dataset_names, label="Dataset name", value="mrm8488/ImageNet1K-val", elem_id="dataset", min_width=300)
1442
+ # dataset_dropdown = gr.Dropdown(defualt_choices, label="Dataset name", value=defualt_choices[0], elem_id="dataset", min_width=400)
1443
+ # dataset_radio.change(fn=lambda x: gr.update(choices=get_choices(x), value=get_choices(x)[0]), inputs=dataset_radio, outputs=dataset_dropdown)
1444
+ # # num_images_slider = gr.Number(10, label="Number of images", elem_id="num_images")
1445
+ # num_images_slider = gr.Slider(1, 1000, step=1, label="Number of images", value=10, elem_id="num_images", min_width=200)
1446
+ # if not is_random:
1447
+ # filter_by_class_checkbox = gr.Checkbox(label="Filter by class", value=True, elem_id="filter_by_class_checkbox")
1448
+ # filter_by_class_text = gr.Textbox(label="Class to select", value="0,33,99", elem_id="filter_by_class_text", info=f"e.g. `0,1,2`. (1000 classes)", visible=True)
1449
+ # # is_random_checkbox = gr.Checkbox(label="Random shuffle", value=False, elem_id="random_seed_checkbox")
1450
+ # # random_seed_slider = gr.Slider(0, 1000, step=1, label="Random seed", value=1, elem_id="random_seed", visible=False)
1451
+ # is_random_checkbox = gr.Checkbox(label="Random shuffle", value=True, elem_id="random_seed_checkbox")
1452
+ # random_seed_slider = gr.Slider(0, 1000, step=1, label="Random seed", value=1, elem_id="random_seed", visible=True)
1453
+ # if is_random:
1454
+ # filter_by_class_checkbox = gr.Checkbox(label="Filter by class", value=False, elem_id="filter_by_class_checkbox")
1455
+ # filter_by_class_text = gr.Textbox(label="Class to select", value="0,33,99", elem_id="filter_by_class_text", info=f"e.g. `0,1,2`. (1000 classes)", visible=False)
1456
+ # is_random_checkbox = gr.Checkbox(label="Random shuffle", value=True, elem_id="random_seed_checkbox")
1457
+ # random_seed_slider = gr.Slider(0, 1000, step=1, label="Random seed", value=42, elem_id="random_seed", visible=True)
1458
+
1459
+
1460
+ # if advanced:
1461
+ # advanced_block.visible = True
1462
+ # basic_block.visible = False
1463
+ # else:
1464
+ # advanced_block.visible = False
1465
+ # basic_block.visible = True
1466
+
1467
+ # # change visibility
1468
+ # advanced_radio.change(fn=lambda x: gr.update(visible=x=="Advanced"), inputs=advanced_radio, outputs=[advanced_block])
1469
+ # advanced_radio.change(fn=lambda x: gr.update(visible=x=="Basic"), inputs=advanced_radio, outputs=[basic_block])
1470
+
1471
+ # def find_num_classes(dataset_name):
1472
+ # num_classes = None
1473
+ # for cat, datasets in DATASETS.items():
1474
+ # datasets = [tup[0] for tup in datasets]
1475
+ # if dataset_name in datasets:
1476
+ # num_classes = DATASETS[cat][datasets.index(dataset_name)][1]
1477
+ # break
1478
+ # return num_classes
1479
+
1480
+ # def change_filter_options(dataset_name):
1481
+ # num_classes = find_num_classes(dataset_name)
1482
+ # if num_classes is None:
1483
+ # return (gr.Checkbox(label="Filter by class", value=False, elem_id="filter_by_class_checkbox", visible=False),
1484
+ # gr.Textbox(label="Class to select", value="0,1,2", elem_id="filter_by_class_text", info="e.g. `0,1,2`. This dataset has no class label", visible=False))
1485
+ # return (gr.Checkbox(label="Filter by class", value=True, elem_id="filter_by_class_checkbox", visible=True),
1486
+ # gr.Textbox(label="Class to select", value="0,1,2", elem_id="filter_by_class_text", info=f"e.g. `0,1,2`. ({num_classes} classes)", visible=True))
1487
+ # dataset_dropdown.change(fn=change_filter_options, inputs=dataset_dropdown, outputs=[filter_by_class_checkbox, filter_by_class_text])
1488
+
1489
+ # def change_filter_by_class(is_filter, dataset_name):
1490
+ # num_classes = find_num_classes(dataset_name)
1491
+ # return gr.Textbox(label="Class to select", value="0,1,2", elem_id="filter_by_class_text", info=f"e.g. `0,1,2`. ({num_classes} classes)", visible=is_filter)
1492
+ # filter_by_class_checkbox.change(fn=change_filter_by_class, inputs=[filter_by_class_checkbox, dataset_dropdown], outputs=filter_by_class_text)
1493
+
1494
+ # def change_random_seed(is_random):
1495
+ # return gr.Slider(0, 1000, step=1, label="Random seed", value=1, elem_id="random_seed", visible=is_random)
1496
+ # is_random_checkbox.change(fn=change_random_seed, inputs=is_random_checkbox, outputs=random_seed_slider)
1497
+
1498
+
1499
+ # def load_dataset_images(is_advanced, dataset_name, num_images=10,
1500
+ # is_filter=True, filter_by_class_text="0,1,2",
1501
+ # is_random=False, seed=1):
1502
+ # progress = gr.Progress()
1503
+ # progress(0, desc="Loading Images")
1504
+ # if is_advanced == "Basic":
1505
+ # gr.Info("Loaded images from Ego-Exo4D")
1506
+ # return default_images
1507
+ # try:
1508
+ # progress(0.5, desc="Downloading Dataset")
1509
+ # dataset = load_dataset(dataset_name, trust_remote_code=True)
1510
+ # key = list(dataset.keys())[0]
1511
+ # dataset = dataset[key]
1512
+ # except Exception as e:
1513
+ # gr.Error(f"Error loading dataset {dataset_name}: {e}")
1514
+ # return None
1515
+ # if num_images > len(dataset):
1516
+ # num_images = len(dataset)
1517
+
1518
+ # if is_filter:
1519
+ # progress(0.8, desc="Filtering Images")
1520
+ # classes = [int(i) for i in filter_by_class_text.split(",")]
1521
+ # labels = np.array(dataset['label'])
1522
+ # unique_labels = np.unique(labels)
1523
+ # valid_classes = [i for i in classes if i in unique_labels]
1524
+ # invalid_classes = [i for i in classes if i not in unique_labels]
1525
+ # if len(invalid_classes) > 0:
1526
+ # gr.Warning(f"Classes {invalid_classes} not found in the dataset.")
1527
+ # if len(valid_classes) == 0:
1528
+ # gr.Error(f"Classes {classes} not found in the dataset.")
1529
+ # return None
1530
+ # # shuffle each class
1531
+ # chunk_size = num_images // len(valid_classes)
1532
+ # image_idx = []
1533
+ # for i in valid_classes:
1534
+ # idx = np.where(labels == i)[0]
1535
+ # if is_random:
1536
+ # idx = np.random.RandomState(seed).choice(idx, chunk_size, replace=False)
1537
+ # else:
1538
+ # idx = idx[:chunk_size]
1539
+ # image_idx.extend(idx.tolist())
1540
+ # if not is_filter:
1541
+ # if is_random:
1542
+ # image_idx = np.random.RandomState(seed).choice(len(dataset), num_images, replace=False).tolist()
1543
+ # else:
1544
+ # image_idx = list(range(num_images))
1545
+ # key = 'image' if 'image' in dataset[0] else list(dataset[0].keys())[0]
1546
+ # images = [dataset[i][key] for i in image_idx]
1547
+ # gr.Info(f"Loaded {len(images)} images from {dataset_name}")
1548
+ # del dataset
1549
+
1550
+ # if dataset_name in CENTER_CROP_DATASETS:
1551
+ # def center_crop_image(img):
1552
+ # # image: PIL image
1553
+ # w, h = img.size
1554
+ # min_hw = min(h, w)
1555
+ # # center crop
1556
+ # left = (w - min_hw) // 2
1557
+ # top = (h - min_hw) // 2
1558
+ # right = left + min_hw
1559
+ # bottom = top + min_hw
1560
+ # img = img.crop((left, top, right, bottom))
1561
+ # return img
1562
+ # images = [center_crop_image(image) for image in images]
1563
+
1564
+ # return images
1565
+
1566
+ # load_images_button.click(load_dataset_images,
1567
+ # inputs=[advanced_radio, dataset_dropdown, num_images_slider,
1568
+ # filter_by_class_checkbox, filter_by_class_text,
1569
+ # is_random_checkbox, random_seed_slider],
1570
+ # outputs=[input_gallery])
1571
+
1572
+ # return dataset_dropdown, num_images_slider, random_seed_slider, load_images_button
1573
 
1574
 
1575
  # def random_rotate_rgb_gallery(images):
 
1752
 
1753
  with gr.Row():
1754
  with gr.Column(scale=5, min_width=200):
1755
+ input_gallery, submit_button, clear_images_button, dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_input_images_section()
 
1756
  num_images_slider.value = 30
1757
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information", autofocus=False, autoscroll=False)
1758
 
 
1766
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1767
  sampling_method_dropdown, ncut_metric_dropdown, positive_prompt, negative_prompt
1768
  ] = make_parameters_section()
 
1769
 
 
 
1770
  false_placeholder = gr.Checkbox(label="False", value=False, elem_id="false_placeholder", visible=False)
1771
  no_prompt = gr.Textbox("", label="", elem_id="empty_placeholder", type="text", placeholder="", visible=False)
1772
 
 
1789
 
1790
  with gr.Row():
1791
  with gr.Column(scale=5, min_width=200):
1792
+ input_gallery, submit_button, clear_images_button, dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_input_images_section()
 
1793
  num_images_slider.value = 30
1794
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information", autofocus=False, autoscroll=False, lines=20)
1795
 
 
1805
  sampling_method_dropdown, ncut_metric_dropdown, positive_prompt, negative_prompt
1806
  ] = make_parameters_section()
1807
  num_eig_slider.value = 30
 
 
1808
 
1809
  false_placeholder = gr.Checkbox(label="False", value=False, elem_id="false_placeholder", visible=False)
1810
  no_prompt = gr.Textbox("", label="", elem_id="empty_placeholder", type="text", placeholder="", visible=False)
 
1848
 
1849
  with gr.Row():
1850
  with gr.Column(scale=5, min_width=200):
1851
+ input_gallery, submit_button, clear_images_button, dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_input_images_section()
 
1852
 
1853
  with gr.Column(scale=5, min_width=200):
1854
  output_gallery = make_output_images_section()
 
1867
  # logging text box
1868
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
1869
 
 
1870
  false_placeholder = gr.Checkbox(label="False", value=False, elem_id="false_placeholder", visible=False)
1871
  no_prompt = gr.Textbox("", label="", elem_id="empty_placeholder", type="text", placeholder="", visible=False)
1872
 
 
1893
 
1894
  with gr.Row():
1895
  with gr.Column(scale=5, min_width=200):
1896
+ input_gallery, submit_button, clear_images_button, dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_input_images_section()
 
1897
  num_images_slider.value = 100
1898
  clear_images_button.visible = False
1899
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
 
1936
  false_placeholder.visible = False
1937
  number_placeholder = gr.Number(0, label="Number placeholder", elem_id="number_placeholder")
1938
  number_placeholder.visible = False
 
1939
  no_prompt = gr.Textbox("", label="", elem_id="empty_placeholder", type="text", placeholder="", visible=False)
1940
 
1941
  submit_button.click(
 
1959
 
1960
  with gr.Row():
1961
  with gr.Column(scale=5, min_width=200):
1962
+ input_gallery, submit_button, clear_images_button, dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_input_images_section()
 
1963
  num_images_slider.value = 100
1964
  clear_images_button.visible = False
1965
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information", lines=20)
 
2008
  false_placeholder.visible = False
2009
  number_placeholder = gr.Number(0, label="Number placeholder", elem_id="number_placeholder")
2010
  number_placeholder.visible = False
 
2011
  no_prompt = gr.Textbox("", label="", elem_id="empty_placeholder", type="text", placeholder="", visible=False)
2012
 
2013
  submit_button.click(
 
2097
 
2098
  with gr.Row():
2099
  with gr.Column(scale=5, min_width=200):
2100
+ input_gallery, submit_button, clear_images_button, dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_input_images_section()
 
 
2101
 
2102
  with gr.Column(scale=5, min_width=200):
2103
  [
 
2134
  gr.Markdown('---')
2135
  with gr.Row():
2136
  with gr.Column(scale=5, min_width=200):
2137
+ input_gallery, submit_button, clear_images_button, dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_input_images_section()
 
2138
  num_images_slider.value = 100
2139
 
2140
 
 
2161
  num_sample_tsne_slider.value = 1000
2162
  # logging text box
2163
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
2164
+
 
 
2165
  false_placeholder = gr.Checkbox(label="False", value=False, elem_id="false_placeholder", visible=False)
2166
  no_prompt = gr.Textbox("", label="", elem_id="empty_placeholder", type="text", placeholder="", visible=False)
2167
 
 
2213
 
2214
  with gr.Row():
2215
  with gr.Column(scale=5, min_width=200):
2216
+ input_gallery, submit_button, clear_images_button, dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_input_images_section()
 
2217
  num_images_slider.value = 100
2218
 
2219
 
 
2246
  num_sample_tsne_slider.value = 1000
2247
  # logging text box
2248
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
2249
+
 
 
2250
  true_placeholder = gr.Checkbox(label="True placeholder", value=True, elem_id="true_placeholder")
2251
  true_placeholder.visible = False
2252
  false_placeholder = gr.Checkbox(label="False placeholder", value=False, elem_id="false_placeholder")
 
2308
 
2309
  with gr.Row():
2310
  with gr.Column(scale=5, min_width=200):
2311
+ input_gallery, submit_button, clear_images_button, dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_input_images_section()
 
2312
  submit_button.visible = False
 
2313
 
2314
 
2315
  for i in range(3):
 
2381
 
2382
  with gr.Row():
2383
  with gr.Column(scale=5, min_width=200):
2384
+ input_gallery, submit_button, clear_images_button, dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_input_images_section()
 
2385
  submit_button.visible = False
 
2386
 
2387
 
2388
  for i in range(3):