Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ import pyarrow.parquet as pq
|
|
| 10 |
|
| 11 |
# Configuration
|
| 12 |
chunk_size = 100 # Size of the chunks to shuffle
|
|
|
|
| 13 |
|
| 14 |
# Load the Satellogic dataset (streaming)
|
| 15 |
dataset = ev.load_dataset("satellogic", streaming=True)
|
|
@@ -17,11 +18,15 @@ data_iter = iter(dataset)
|
|
| 17 |
shuffled_chunk = [] # Initialize an empty list to hold the current chunk
|
| 18 |
chunk_iter = None # Initialize the chunk iterator
|
| 19 |
|
| 20 |
-
# Initialize
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def get_next_image():
|
| 24 |
-
global data_iter,
|
| 25 |
|
| 26 |
while True:
|
| 27 |
# If we don't have a current chunk or it's exhausted, get a new one
|
|
@@ -48,71 +53,70 @@ def get_next_image():
|
|
| 48 |
google_maps_link = utils.get_google_map_link(sample, "satellogic")
|
| 49 |
image_id = str(bounds)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
if not any(label["image_id"] == image_id for label in labels_list):
|
| 53 |
return image, image_id, bounds, google_maps_link
|
| 54 |
except StopIteration:
|
| 55 |
# Current chunk is exhausted, reset chunk variables to get a new one in the next iteration
|
| 56 |
shuffled_chunk = []
|
| 57 |
chunk_iter = None
|
| 58 |
|
| 59 |
-
def rate_image(image_id, bounds, rating
|
| 60 |
-
global
|
| 61 |
|
| 62 |
-
|
| 63 |
{
|
| 64 |
-
"image_id": image_id,
|
| 65 |
-
"bounds": bounds,
|
| 66 |
-
"rating": rating,
|
| 67 |
-
"google_maps_link":
|
| 68 |
}
|
| 69 |
)
|
|
|
|
|
|
|
| 70 |
|
| 71 |
next_image, next_image_id, next_bounds, next_google_maps_link = get_next_image()
|
| 72 |
return next_image, next_image_id, next_bounds, next_google_maps_link
|
| 73 |
|
| 74 |
-
def
|
| 75 |
-
global
|
| 76 |
-
if
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
else:
|
| 81 |
return None
|
| 82 |
|
| 83 |
# Gradio interface
|
| 84 |
with gr.Blocks() as iface:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
with gr.Row():
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
bounds_out = gr.Textbox(label="Bounds", visible=False)
|
| 90 |
-
google_maps_link_out = gr.Textbox(label="Google Maps Link", visible=True)
|
| 91 |
-
with gr.Column():
|
| 92 |
-
rating_radio = gr.Radio(["Cool", "Not Cool"], label="Rating")
|
| 93 |
-
with gr.Row():
|
| 94 |
-
submit_button = gr.Button("Submit Rating")
|
| 95 |
-
download_button = gr.Button("Download Labels")
|
| 96 |
-
download_file = gr.File(label="Download")
|
| 97 |
|
| 98 |
submit_button.click(
|
| 99 |
-
rate_image,
|
| 100 |
-
inputs=[image_id_out, bounds_out, rating_radio
|
| 101 |
-
outputs=[image_out, image_id_out, bounds_out, google_maps_link_out]
|
| 102 |
)
|
| 103 |
|
| 104 |
download_button.click(
|
| 105 |
-
|
| 106 |
-
inputs=
|
| 107 |
-
outputs=[
|
| 108 |
)
|
| 109 |
|
| 110 |
-
|
| 111 |
initial_image, initial_image_id, initial_bounds, initial_google_maps_link = get_next_image()
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
| 117 |
|
| 118 |
iface.launch(share=True)
|
|
|
|
| 10 |
|
| 11 |
# Configuration
|
| 12 |
chunk_size = 100 # Size of the chunks to shuffle
|
| 13 |
+
label_file = os.path.join(os.path.dirname(__file__), "labels.csv") # Save CSV in the same directory as the script
|
| 14 |
|
| 15 |
# Load the Satellogic dataset (streaming)
|
| 16 |
dataset = ev.load_dataset("satellogic", streaming=True)
|
|
|
|
| 18 |
shuffled_chunk = [] # Initialize an empty list to hold the current chunk
|
| 19 |
chunk_iter = None # Initialize the chunk iterator
|
| 20 |
|
| 21 |
+
# Initialize or load labels DataFrame (make it global for the download function)
|
| 22 |
+
labels_df = None
|
| 23 |
+
if os.path.exists(label_file):
|
| 24 |
+
labels_df = pd.read_csv(label_file)
|
| 25 |
+
else:
|
| 26 |
+
labels_df = pd.DataFrame(columns=["image_id", "bounds", "rating", "google_maps_link"])
|
| 27 |
|
| 28 |
def get_next_image():
|
| 29 |
+
global data_iter, labels_df, shuffled_chunk, chunk_iter
|
| 30 |
|
| 31 |
while True:
|
| 32 |
# If we don't have a current chunk or it's exhausted, get a new one
|
|
|
|
| 53 |
google_maps_link = utils.get_google_map_link(sample, "satellogic")
|
| 54 |
image_id = str(bounds)
|
| 55 |
|
| 56 |
+
if image_id not in labels_df["image_id"].values:
|
|
|
|
| 57 |
return image, image_id, bounds, google_maps_link
|
| 58 |
except StopIteration:
|
| 59 |
# Current chunk is exhausted, reset chunk variables to get a new one in the next iteration
|
| 60 |
shuffled_chunk = []
|
| 61 |
chunk_iter = None
|
| 62 |
|
| 63 |
+
def rate_image(image_id, bounds, rating):
|
| 64 |
+
global labels_df
|
| 65 |
|
| 66 |
+
new_row = pd.DataFrame(
|
| 67 |
{
|
| 68 |
+
"image_id": [image_id],
|
| 69 |
+
"bounds": [bounds],
|
| 70 |
+
"rating": [rating],
|
| 71 |
+
"google_maps_link": [""], # this isn't necessary to pass to the function since we aren't updating it here.
|
| 72 |
}
|
| 73 |
)
|
| 74 |
+
labels_df = pd.concat([labels_df, new_row], ignore_index=True)
|
| 75 |
+
labels_df.to_csv(label_file, index=False)
|
| 76 |
|
| 77 |
next_image, next_image_id, next_bounds, next_google_maps_link = get_next_image()
|
| 78 |
return next_image, next_image_id, next_bounds, next_google_maps_link
|
| 79 |
|
| 80 |
+
def save_labels_parquet():
|
| 81 |
+
global labels_df
|
| 82 |
+
if labels_df is not None and not labels_df.empty:
|
| 83 |
+
table = pa.Table.from_pandas(labels_df)
|
| 84 |
+
pq.write_table(table, 'labeled_data.parquet')
|
| 85 |
+
return 'labeled_data.parquet'
|
| 86 |
else:
|
| 87 |
return None
|
| 88 |
|
| 89 |
# Gradio interface
|
| 90 |
with gr.Blocks() as iface:
|
| 91 |
+
image_out = gr.Image(label="Satellite Image")
|
| 92 |
+
image_id_out = gr.Textbox(label="Image ID", visible=False)
|
| 93 |
+
bounds_out = gr.Textbox(label="Bounds", visible=False)
|
| 94 |
+
google_maps_link_out = gr.Textbox(label="Google Maps Link", visible=True)
|
| 95 |
+
rating_radio = gr.Radio(["Cool", "Not Cool"], label="Rating")
|
| 96 |
with gr.Row():
|
| 97 |
+
submit_button = gr.Button("Submit Rating")
|
| 98 |
+
download_button = gr.Button("Download Labels (Parquet)")
|
| 99 |
+
download_output = gr.File(label="Download Labeled Data")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
submit_button.click(
|
| 102 |
+
fn=rate_image,
|
| 103 |
+
inputs=[image_id_out, bounds_out, rating_radio],
|
| 104 |
+
outputs=[image_out, image_id_out, bounds_out, google_maps_link_out],
|
| 105 |
)
|
| 106 |
|
| 107 |
download_button.click(
|
| 108 |
+
fn=save_labels_parquet,
|
| 109 |
+
inputs=[],
|
| 110 |
+
outputs=[download_output],
|
| 111 |
)
|
| 112 |
|
| 113 |
+
# Load the first image
|
| 114 |
initial_image, initial_image_id, initial_bounds, initial_google_maps_link = get_next_image()
|
| 115 |
|
| 116 |
+
# Set initial values
|
| 117 |
+
if initial_image:
|
| 118 |
+
iface.load(lambda: (initial_image, initial_image_id, initial_bounds, initial_google_maps_link),
|
| 119 |
+
inputs=None,
|
| 120 |
+
outputs=[image_out, image_id_out, bounds_out, google_maps_link_out])
|
| 121 |
|
| 122 |
iface.launch(share=True)
|