Commit
·
1988995
1
Parent(s):
2b22bff
Make on click event
Browse files
app.py
CHANGED
|
@@ -89,7 +89,7 @@ fig = px.scatter(
|
|
| 89 |
hover_data={"Title": df["title"]},
|
| 90 |
labels={'x': 'UMAP Dimension 1', 'y': 'UMAP Dimension 2'},
|
| 91 |
title="UMAP Scatter Plot of Reddit Titles",
|
| 92 |
-
color_discrete_sequence=["#
|
| 93 |
)
|
| 94 |
|
| 95 |
# Customize the layout to adapt to browser settings (light/dark mode)
|
|
@@ -98,27 +98,32 @@ fig.update_layout(
|
|
| 98 |
plot_bgcolor="rgba(0, 0, 0, 0)",
|
| 99 |
paper_bgcolor="rgba(0, 0, 0, 0)"
|
| 100 |
)
|
| 101 |
-
|
| 102 |
# Display the scatterplot and capture click events
|
| 103 |
selected_points = plotly_events(fig, click_event=True, hover_event=False, override_height=600, override_width="100%")
|
| 104 |
-
|
| 105 |
-
# If a point is clicked, handle the embedding inversion
|
| 106 |
-
if selected_points:
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
hover_data={"Title": df["title"]},
|
| 90 |
labels={'x': 'UMAP Dimension 1', 'y': 'UMAP Dimension 2'},
|
| 91 |
title="UMAP Scatter Plot of Reddit Titles",
|
| 92 |
+
color_discrete_sequence=["#ff504c"] # Set default blue color for points
|
| 93 |
)
|
| 94 |
|
| 95 |
# Customize the layout to adapt to browser settings (light/dark mode)
|
|
|
|
| 98 |
plot_bgcolor="rgba(0, 0, 0, 0)",
|
| 99 |
paper_bgcolor="rgba(0, 0, 0, 0)"
|
| 100 |
)
|
| 101 |
+
x, y = 0, 0
|
| 102 |
# Display the scatterplot and capture click events
|
| 103 |
selected_points = plotly_events(fig, click_event=True, hover_event=False, override_height=600, override_width="100%")
|
| 104 |
+
with st.form(key="form1"):
|
| 105 |
+
# If a point is clicked, handle the embedding inversion
|
| 106 |
+
if selected_points:
|
| 107 |
+
clicked_point = selected_points[0]
|
| 108 |
+
x_coord = x = clicked_point['x']
|
| 109 |
+
y_coord = y = clicked_point['y']
|
| 110 |
+
|
| 111 |
+
x = st.number_input("X Coordinate", value=x, format="%.10f")
|
| 112 |
+
y = st.number_input("Y Coordinate", value=y, format="%.10f")
|
| 113 |
+
|
| 114 |
+
submit_button = st.form_submit_button("Submit")
|
| 115 |
+
|
| 116 |
+
if selected_points or submit_button:
|
| 117 |
+
inferred_embedding = reducer.inverse_transform(np.array([[x, y]]) if not isinstance(reducer, UMAP) else np.array([[x, y]]))
|
| 118 |
+
inferred_embedding = inferred_embedding.astype("float32")
|
| 119 |
+
|
| 120 |
+
output = vec2text.invert_embeddings(
|
| 121 |
+
embeddings=torch.tensor(inferred_embedding).cuda(),
|
| 122 |
+
corrector=corrector,
|
| 123 |
+
num_steps=20,
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
st.text(str(output))
|
| 127 |
+
st.text(str(inferred_embedding))
|
| 128 |
+
else:
|
| 129 |
+
st.text("Click on a point in the scatterplot to see its coordinates.")
|