Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +65 -38
src/streamlit_app.py
CHANGED
@@ -1,40 +1,67 @@
|
|
1 |
-
import
|
2 |
-
import numpy as np
|
3 |
-
import pandas as pd
|
4 |
import streamlit as st
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
|
|
|
|
2 |
import streamlit as st
|
3 |
|
4 |
+
from segments import SegmentsClient
|
5 |
+
|
6 |
+
|
7 |
+
def update_label_on_platform(target_uuid: str, label_data: dict, user_api_key: str, labelset: str = "ground-truth") -> str:
|
8 |
+
"""
|
9 |
+
Update the label on Segments.ai for the given target_uuid with the provided label_data.
|
10 |
+
This overwrites the current label on the platform.
|
11 |
+
|
12 |
+
Depending on the structure of the uploaded label file, we determine whether the label data is nested
|
13 |
+
under an "attributes" key. If it is, we use that; otherwise, we assume the entire JSON is the label data.
|
14 |
+
|
15 |
+
Parameters:
|
16 |
+
target_uuid (str): The UUID of the target sample.
|
17 |
+
label_data (dict): The label data (JSON) to upload.
|
18 |
+
user_api_key (str): The API key provided by the user.
|
19 |
+
|
20 |
+
Returns:
|
21 |
+
str: A success message upon successful update.
|
22 |
+
"""
|
23 |
+
client = SegmentsClient(user_api_key)
|
24 |
+
try:
|
25 |
+
# Check if the JSON has an "attributes" key.
|
26 |
+
if isinstance(label_data, dict) and "attributes" in label_data:
|
27 |
+
# If your label JSON is nested under "attributes", use this:
|
28 |
+
# client.update_label(target_uuid, labelset=labelset, attributes=label_data["attributes"])
|
29 |
+
attributes_to_update = label_data["attributes"]
|
30 |
+
else:
|
31 |
+
# If your label JSON is flat, use this:
|
32 |
+
# client.update_label(target_uuid, labelset=labelset, attributes=label_data)
|
33 |
+
attributes_to_update = label_data
|
34 |
+
|
35 |
+
client.update_label(target_uuid, labelset=labelset, attributes=attributes_to_update)
|
36 |
+
return "Label updated successfully on Segments.ai."
|
37 |
+
except Exception as e:
|
38 |
+
raise Exception(f"Error updating target label on Segments.ai: {e}")
|
39 |
+
|
40 |
+
|
41 |
+
# ---------------------- Streamlit UI ----------------------
|
42 |
+
|
43 |
+
st.title("Upload and Overwrite Label on Segments.ai")
|
44 |
+
st.markdown(
|
45 |
+
"Select a label file (JSON) from your local machine, enter your API key, and specify the target UUID where the label should be updated."
|
46 |
+
)
|
47 |
+
|
48 |
+
user_api_key = st.text_input("Enter your API key", type="password", value="")
|
49 |
+
uploaded_file = st.file_uploader("Choose a label file", type=["json"])
|
50 |
+
target_uuid = st.text_input("Target UUID", value="")
|
51 |
+
|
52 |
+
if st.button("Upload Label"):
|
53 |
+
if not user_api_key:
|
54 |
+
st.error("Please enter your API key.")
|
55 |
+
elif uploaded_file is None:
|
56 |
+
st.error("Please upload a label file.")
|
57 |
+
elif not target_uuid:
|
58 |
+
st.error("Please enter a target UUID.")
|
59 |
+
else:
|
60 |
+
try:
|
61 |
+
# Load label data from the uploaded file.
|
62 |
+
label_data = json.load(uploaded_file)
|
63 |
+
# Update label on the platform using the user-provided API key.
|
64 |
+
result_message = update_label_on_platform(target_uuid, label_data, user_api_key)
|
65 |
+
st.success(result_message)
|
66 |
+
except Exception as e:
|
67 |
+
st.error(f"An error occurred: {e}")
|