Commit ·
8246b3c
1
Parent(s): 2e98766
Fix app?
Browse files12 unmodified lines131415161617181920 unmodified lines4041424343444546474853 unmodified lines10210310410310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814910510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015112 unmodified linesfrom streamlit_plotly_events import plotly_eventsimport plotly.graph_objects as goimport logging
import utils# Activate tqdm with pandastqdm.pandas()
20 unmodified lines@st.cache_resourcedef vector_compressor_from_config(): # Return UMAP with 2 components for dimensionality reduction return UMAP(n_components=2) # return UMAP(n_components=2) return PCA(n_components=2)
# Caching the dataframe since loading from an external source can be time-consuming@st.cache_data53 unmodified lines)
x, y = 0.0, 0.0
# Display the scatterplot and capture click eventsselected_points = plotly_events(fig, click_event=True, hover_event=False, override_height=600, override_width="100%")
# Sidebar for additional informationst.sidebar.header("Scatter Plot Info")st.sidebar.write("""This scatter plot visualizes the UMAP dimensionality reduction of Reddit post titles. Each point represents a post, with similar titles being positioned closer together.""")st.sidebar.write("Use the form below to select coordinates or click on a point in the scatter plot.")st.sidebar.markdown("---")
st.sidebar.header("How to Use")st.sidebar.write("""1. **Click a point** in the scatter plot to see the corresponding coordinates.2. **Adjust the coordinates** using the form inputs if needed.3. **Submit** to see the reconstructed text output.""")
# Form for inputting coordinateswith st.form(key="form1"): # If a point is clicked, handle the embedding inversion if selected_points: clicked_point = selected_points[0] x_coord = x = clicked_point['x'] y_coord = y = clicked_point['y']
x = st.number_input("X Coordinate", value=x, format="%.10f") y = st.number_input("Y Coordinate", value=y, format="%.10f")
submit_button = st.form_submit_button("Submit")
if selected_points or submit_button: inferred_embedding = reducer.inverse_transform(np.array([[x, y]]) if not isinstance(reducer, UMAP) else np.array([[x, y]])) inferred_embedding = inferred_embedding.astype("float32")
output = vec2text.invert_embeddings( embeddings=torch.tensor(inferred_embedding).cuda(), corrector=corrector, num_steps=20, )
st.text(str(output)) st.text(str(inferred_embedding)) else: st.text("Click on a point in the scatterplot to see its coordinates.")vec = np.array([x, y]).astype("float32")
# Add a card container to the right of the content with Streamlit columnscol1, col2 = st.columns([3, 1]) # Adjusting ratio to allocate space for the card container
with col1: # Main content stays here (scatterplot, form, etc.) selected_points = plotly_events(fig, click_event=True, hover_event=False, #override_height=600, override_width="100%" ) with st.form(key="form1_main"): if selected_points: clicked_point = selected_points[0] x_coord = x = clicked_point['x'] y_coord = y = clicked_point['y']
x = st.number_input("X Coordinate", value=x, format="%.10f") y = st.number_input("Y Coordinate", value=y, format="%.10f") vec = np.array([x, y]).astype("float32")
submit_button = st.form_submit_button("Submit")
if selected_points or submit_button: inferred_embedding = reducer.inverse_transform(np.array([[x, y]]) if not isinstance(reducer, UMAP) else np.array([[x, y]])) inferred_embedding = inferred_embedding.astype("float32")
output = vec2text.invert_embeddings( embeddings=torch.tensor(inferred_embedding).cuda(), corrector=corrector, num_steps=20, )
st.text(str(output)) st.text(str(inferred_embedding)) else: st.text("Click on a point in the scatterplot to see its coordinates.")
with col2: closest_sentence_index = utils.find_exact_match(vectors_2d, vec, decimals=3) st.write(f"{vectors_2d.dtype} {vec.dtype}") if closest_sentence_index > -1: st.write(df["title"].iloc[closest_sentence_index]) # Card content st.markdown("## Card Container") st.write("This is an additional card container to the right of the main content.") st.write("You can use this space to show additional information, actions, or insights.") st.button("Card Button")app.py+51-49