gera-richarte commited on
Commit
0212ece
1 Parent(s): 69db194

no more globals, iterator is by user session. It was generating some conflicts

Browse files
Files changed (1) hide show
  1. app.py +21 -19
app.py CHANGED
@@ -23,10 +23,7 @@ sets = {
23
  }
24
  }
25
 
26
- def open_dataset(dataset, set_name, split, batch_size, shard = -1):
27
- # I should really move ds.config_name and dsi to a gr.State()
28
- global dsi, ds
29
-
30
  if shard == -1:
31
  # Trick to open the whole dataset
32
  data_files = None
@@ -53,14 +50,15 @@ def open_dataset(dataset, set_name, split, batch_size, shard = -1):
53
 
54
  dsi = iter(ds)
55
 
 
 
56
  return (
57
  gr.update(label=f"Shards (max {shards})", value=shard, maximum=shards),
58
- *get_images(batch_size)
 
59
  )
60
 
61
- def get_images(batch_size):
62
- global dsi, ds
63
-
64
  items = []
65
  metadatas = []
66
 
@@ -70,17 +68,17 @@ def get_images(batch_size):
70
  metadata = {"bounds":[[1,1,4,4]], }
71
  else:
72
  try:
73
- item = next(dsi)
74
  except StopIteration:
75
  break
76
  metadata = item["metadata"]
77
- if ds.config_name == "satellogic":
78
  # image = (np.asarray(item["1m"])).astype("uint8")
79
  # items.append(image[0,0,:,:])
80
  image = np.asarray(item["rgb"][0]).astype(np.uint8)
81
  items.append(image.transpose(1,2,0))
82
 
83
- if ds.config_name == "sentinel_1":
84
  metadata = json.loads(metadata)
85
  data = np.asarray(item["10m"])
86
  for i in range(data.shape[0]):
@@ -92,7 +90,7 @@ def get_images(batch_size):
92
  image[2] = (image[0]/(image[1]+0.1))*256
93
  items.append(image.transpose(1,2,0))
94
 
95
- if ds.config_name == "default":
96
  dataRGB = np.asarray(item["rgb"]).astype("uint8")
97
  dataCHM = np.asarray(item["chm"]).astype("uint8")
98
  data1m = np.asarray(item["1m"]).astype("uint8")
@@ -112,9 +110,13 @@ def get_images(batch_size):
112
  def update_shape(rows, columns):
113
  return gr.update(rows=rows, columns=columns)
114
 
 
 
115
 
116
  with gr.Blocks(title="Dataset Explorer", fill_height = True) as demo:
117
- gr.Markdown("# [satellogic/EarthView](https://huggingface.co/datasets/satellogic/EarthView) Dataset Viewer")
 
 
118
  batch_size = gr.Number(10, label = "Batch Size", render=False)
119
  shard = gr.Slider(label="Shard", minimum=0, maximum=10000, step=1, render=False)
120
  table = gr.DataFrame(render = False)
@@ -133,8 +135,8 @@ with gr.Blocks(title="Dataset Explorer", fill_height = True) as demo:
133
 
134
  gr.Button("Load (minutes)").click(
135
  open_dataset,
136
- inputs=[dataset, config, split, batch_size, initial_shard],
137
- outputs=[shard, gallery, table])
138
 
139
  gallery.render()
140
 
@@ -151,11 +153,11 @@ with gr.Blocks(title="Dataset Explorer", fill_height = True) as demo:
151
  shard.render()
152
  shard.release(
153
  open_dataset,
154
- inputs=[dataset, config, split, batch_size, shard],
155
- outputs=[shard, gallery, table])
156
 
157
- btn = gr.Button("Get More Images", scale=0)
158
- btn.click(get_images, [batch_size], [gallery, table])
159
  btn.click()
160
 
161
  table.render()
 
23
  }
24
  }
25
 
26
+ def open_dataset(dataset, set_name, split, batch_size, state, shard = -1):
 
 
 
27
  if shard == -1:
28
  # Trick to open the whole dataset
29
  data_files = None
 
50
 
51
  dsi = iter(ds)
52
 
53
+ state["config"] = config
54
+ state["dsi"] = dsi
55
  return (
56
  gr.update(label=f"Shards (max {shards})", value=shard, maximum=shards),
57
+ *get_images(batch_size, state),
58
+ state
59
  )
60
 
61
+ def get_images(batch_size, state):
 
 
62
  items = []
63
  metadatas = []
64
 
 
68
  metadata = {"bounds":[[1,1,4,4]], }
69
  else:
70
  try:
71
+ item = next(state["dsi"])
72
  except StopIteration:
73
  break
74
  metadata = item["metadata"]
75
+ if state["config"] == "satellogic":
76
  # image = (np.asarray(item["1m"])).astype("uint8")
77
  # items.append(image[0,0,:,:])
78
  image = np.asarray(item["rgb"][0]).astype(np.uint8)
79
  items.append(image.transpose(1,2,0))
80
 
81
+ if state["config"] == "sentinel_1":
82
  metadata = json.loads(metadata)
83
  data = np.asarray(item["10m"])
84
  for i in range(data.shape[0]):
 
90
  image[2] = (image[0]/(image[1]+0.1))*256
91
  items.append(image.transpose(1,2,0))
92
 
93
+ if state["config"] == "default":
94
  dataRGB = np.asarray(item["rgb"]).astype("uint8")
95
  dataCHM = np.asarray(item["chm"]).astype("uint8")
96
  data1m = np.asarray(item["1m"]).astype("uint8")
 
110
  def update_shape(rows, columns):
111
  return gr.update(rows=rows, columns=columns)
112
 
113
+ def new_state():
114
+ return gr.State({})
115
 
116
  with gr.Blocks(title="Dataset Explorer", fill_height = True) as demo:
117
+ state = new_state()
118
+
119
+ gr.Markdown("# Viewer for [satellogic/EarthView](https://huggingface.co/datasets/satellogic/EarthView) Dataset")
120
  batch_size = gr.Number(10, label = "Batch Size", render=False)
121
  shard = gr.Slider(label="Shard", minimum=0, maximum=10000, step=1, render=False)
122
  table = gr.DataFrame(render = False)
 
135
 
136
  gr.Button("Load (minutes)").click(
137
  open_dataset,
138
+ inputs=[dataset, config, split, batch_size, state, initial_shard],
139
+ outputs=[shard, gallery, table, state])
140
 
141
  gallery.render()
142
 
 
153
  shard.render()
154
  shard.release(
155
  open_dataset,
156
+ inputs=[dataset, config, split, batch_size, state, shard],
157
+ outputs=[shard, gallery, table, state])
158
 
159
+ btn = gr.Button("Next Batch (same shard)", scale=0)
160
+ btn.click(get_images, [batch_size, state], [gallery, table])
161
  btn.click()
162
 
163
  table.render()