Commit
·
010edb7
1
Parent(s):
74e9a4d
Add views
Browse files
views.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import vec2text
|
3 |
+
import torch
|
4 |
+
from umap import UMAP
|
5 |
+
import plotly.express as px
|
6 |
+
import numpy as np
|
7 |
+
from streamlit_plotly_events import plotly_events
|
8 |
+
from resources import reduce_embeddings
|
9 |
+
import utils
|
10 |
+
import pandas as pd
|
11 |
+
|
12 |
+
|
13 |
+
def diffs(embeddings: np.ndarray, corrector):
|
14 |
+
st.text(f"Embedding shape: {embeddings.shape}")
|
15 |
+
st.html('<a href="https://www.flaticon.com/free-icons/array" title="array icons">Array icons created by Voysla - Flaticon</a>')
|
16 |
+
|
17 |
+
def plot(df: pd.DataFrame, embeddings: np.ndarray, vectors_2d, reducer, corrector):
|
18 |
+
|
19 |
+
|
20 |
+
# Add a scatter plot using Plotly
|
21 |
+
fig = px.scatter(
|
22 |
+
x=vectors_2d[:, 0],
|
23 |
+
y=vectors_2d[:, 1],
|
24 |
+
opacity=0.6,
|
25 |
+
hover_data={"Title": df["title"]},
|
26 |
+
labels={'x': 'UMAP Dimension 1', 'y': 'UMAP Dimension 2'},
|
27 |
+
title="UMAP Scatter Plot of Reddit Titles",
|
28 |
+
color_discrete_sequence=["#ff504c"] # Set default blue color for points
|
29 |
+
)
|
30 |
+
|
31 |
+
# Customize the layout to adapt to browser settings (light/dark mode)
|
32 |
+
fig.update_layout(
|
33 |
+
template=None, # Let Plotly adapt automatically based on user settings
|
34 |
+
plot_bgcolor="rgba(0, 0, 0, 0)",
|
35 |
+
paper_bgcolor="rgba(0, 0, 0, 0)"
|
36 |
+
)
|
37 |
+
|
38 |
+
x, y = 0.0, 0.0
|
39 |
+
vec = np.array([x, y]).astype("float32")
|
40 |
+
|
41 |
+
# Add a card container to the right of the content with Streamlit columns
|
42 |
+
col1, col2 = st.columns([0.6, 0.4]) # Adjusting ratio to allocate space for the card container
|
43 |
+
|
44 |
+
with col1:
|
45 |
+
# Main content stays here (scatterplot, form, etc.)
|
46 |
+
selected_points = plotly_events(fig, click_event=True, hover_event=False, #override_height=600, override_width="100%"
|
47 |
+
)
|
48 |
+
with st.form(key="form1_main"):
|
49 |
+
if selected_points:
|
50 |
+
clicked_point = selected_points[0]
|
51 |
+
x_coord = x = clicked_point['x']
|
52 |
+
y_coord = y = clicked_point['y']
|
53 |
+
|
54 |
+
x = st.number_input("X Coordinate", value=x, format="%.10f")
|
55 |
+
y = st.number_input("Y Coordinate", value=y, format="%.10f")
|
56 |
+
vec = np.array([x, y]).astype("float32")
|
57 |
+
|
58 |
+
|
59 |
+
submit_button = st.form_submit_button("Submit")
|
60 |
+
|
61 |
+
if selected_points or submit_button:
|
62 |
+
inferred_embedding = reducer.inverse_transform(np.array([[x, y]]) if not isinstance(reducer, UMAP) else np.array([[x, y]]))
|
63 |
+
inferred_embedding = inferred_embedding.astype("float32")
|
64 |
+
|
65 |
+
output = vec2text.invert_embeddings(
|
66 |
+
embeddings=torch.tensor(inferred_embedding).cuda(),
|
67 |
+
corrector=corrector,
|
68 |
+
num_steps=20,
|
69 |
+
)
|
70 |
+
|
71 |
+
st.text(str(output))
|
72 |
+
st.text(str(inferred_embedding))
|
73 |
+
else:
|
74 |
+
st.text("Click on a point in the scatterplot to see its coordinates.")
|
75 |
+
|
76 |
+
with col2:
|
77 |
+
closest_sentence_index = utils.find_exact_match(vectors_2d, vec, decimals=3)
|
78 |
+
st.markdown(
|
79 |
+
f"### Selected text:\n```console\n{df.title.iloc[closest_sentence_index] if closest_sentence_index > -1 else '[no selected text]'}\n```"
|
80 |
+
)
|