Tomatillo commited on
Commit
216cc4c
·
verified ·
1 Parent(s): 074a445

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +65 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,67 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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}")