Commit
·
b21feb2
1
Parent(s):
c0f5faa
Run the simplest test
Browse files
app.py
CHANGED
|
@@ -1,152 +1,158 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
import
|
| 11 |
-
|
| 12 |
-
from
|
| 13 |
-
#
|
| 14 |
-
import
|
| 15 |
-
import
|
| 16 |
-
import
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
#
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
#
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
with
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
# import os
|
| 5 |
+
# import pickle
|
| 6 |
import streamlit as st
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
st.text("This is a test")
|
| 10 |
+
# import pandas as pd
|
| 11 |
+
# import vec2text
|
| 12 |
+
# import torch
|
| 13 |
+
# from transformers import AutoModel, AutoTokenizer
|
| 14 |
+
# from umap import UMAP
|
| 15 |
+
# from tqdm import tqdm
|
| 16 |
+
# import plotly.express as px
|
| 17 |
+
# import numpy as np
|
| 18 |
+
# from sklearn.decomposition import PCA
|
| 19 |
+
# # from streamlit_plotly_events import plotly_events
|
| 20 |
+
# import plotly.graph_objects as go
|
| 21 |
+
# import logging
|
| 22 |
+
# import utils
|
| 23 |
+
# # Activate tqdm with pandas
|
| 24 |
+
# tqdm.pandas()
|
| 25 |
+
|
| 26 |
+
# # Custom file cache decorator
|
| 27 |
+
# def file_cache(file_path):
|
| 28 |
+
# def decorator(func):
|
| 29 |
+
# def wrapper(*args, **kwargs):
|
| 30 |
+
# # Check if the file already exists
|
| 31 |
+
# if os.path.exists(file_path):
|
| 32 |
+
# # Load from cache
|
| 33 |
+
# with open(file_path, "rb") as f:
|
| 34 |
+
# print(f"Loading cached data from {file_path}")
|
| 35 |
+
# return pickle.load(f)
|
| 36 |
+
# else:
|
| 37 |
+
# # Compute and save to cache
|
| 38 |
+
# result = func(*args, **kwargs)
|
| 39 |
+
# with open(file_path, "wb") as f:
|
| 40 |
+
# pickle.dump(result, f)
|
| 41 |
+
# print(f"Saving new cache to {file_path}")
|
| 42 |
+
# return result
|
| 43 |
+
# return wrapper
|
| 44 |
+
# return decorator
|
| 45 |
+
|
| 46 |
+
# @st.cache_resource
|
| 47 |
+
# def vector_compressor_from_config():
|
| 48 |
+
# # Return UMAP with 2 components for dimensionality reduction
|
| 49 |
+
# # return UMAP(n_components=2)
|
| 50 |
+
# return PCA(n_components=2)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# # Caching the dataframe since loading from an external source can be time-consuming
|
| 54 |
+
# @st.cache_data
|
| 55 |
+
# def load_data():
|
| 56 |
+
# return pd.read_csv("https://huggingface.co/datasets/marksverdhei/reddit-syac-urls/resolve/main/train.csv")
|
| 57 |
+
|
| 58 |
+
# df = load_data()
|
| 59 |
+
|
| 60 |
+
# # Caching the model and tokenizer to avoid reloading
|
| 61 |
+
# # @st.cache_resource
|
| 62 |
+
# # def load_model_and_tokenizer():
|
| 63 |
+
# # encoder = AutoModel.from_pretrained("sentence-transformers/gtr-t5-base").encoder.to("cuda")
|
| 64 |
+
# # tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/gtr-t5-base")
|
| 65 |
+
# # return encoder, tokenizer
|
| 66 |
+
|
| 67 |
+
# # encoder, tokenizer = load_model_and_tokenizer()
|
| 68 |
+
|
| 69 |
+
# # Caching the vec2text corrector
|
| 70 |
+
# # @st.cache_resource
|
| 71 |
+
# # def load_corrector():
|
| 72 |
+
# # return vec2text.load_pretrained_corrector("gtr-base")
|
| 73 |
+
|
| 74 |
+
# # corrector = load_corrector()
|
| 75 |
+
|
| 76 |
+
# # Caching the precomputed embeddings since they are stored locally and large
|
| 77 |
+
# @st.cache_data
|
| 78 |
+
# def load_embeddings():
|
| 79 |
+
# return np.load("syac-title-embeddings.npy")
|
| 80 |
+
|
| 81 |
+
# embeddings = load_embeddings()
|
| 82 |
+
|
| 83 |
+
# # Custom cache the UMAP reduction using file_cache decorator
|
| 84 |
+
# @st.cache_data
|
| 85 |
+
# @file_cache(".cache/reducer_embeddings.pickle")
|
| 86 |
+
# def reduce_embeddings(embeddings):
|
| 87 |
+
# reducer = vector_compressor_from_config()
|
| 88 |
+
# return reducer.fit_transform(embeddings), reducer
|
| 89 |
+
|
| 90 |
+
# vectors_2d, reducer = reduce_embeddings(embeddings)
|
| 91 |
+
|
| 92 |
+
# # Add a scatter plot using Plotly
|
| 93 |
+
# # fig = px.scatter(
|
| 94 |
+
# # x=vectors_2d[:, 0],
|
| 95 |
+
# # y=vectors_2d[:, 1],
|
| 96 |
+
# # opacity=0.6,
|
| 97 |
+
# # hover_data={"Title": df["title"]},
|
| 98 |
+
# # labels={'x': 'UMAP Dimension 1', 'y': 'UMAP Dimension 2'},
|
| 99 |
+
# # title="UMAP Scatter Plot of Reddit Titles",
|
| 100 |
+
# # color_discrete_sequence=["#ff504c"] # Set default blue color for points
|
| 101 |
+
# # )
|
| 102 |
+
|
| 103 |
+
# # # Customize the layout to adapt to browser settings (light/dark mode)
|
| 104 |
+
# # fig.update_layout(
|
| 105 |
+
# # template=None, # Let Plotly adapt automatically based on user settings
|
| 106 |
+
# # plot_bgcolor="rgba(0, 0, 0, 0)",
|
| 107 |
+
# # paper_bgcolor="rgba(0, 0, 0, 0)"
|
| 108 |
+
# # )
|
| 109 |
+
|
| 110 |
+
# x, y = 0.0, 0.0
|
| 111 |
+
# vec = np.array([x, y]).astype("float32")
|
| 112 |
+
|
| 113 |
+
# # Add a card container to the right of the content with Streamlit columns
|
| 114 |
+
# col1, col2 = st.columns([3, 1]) # Adjusting ratio to allocate space for the card container
|
| 115 |
+
|
| 116 |
+
# with col1:
|
| 117 |
+
# # Main content stays here (scatterplot, form, etc.)
|
| 118 |
+
# # selected_points = plotly_events(fig, click_event=True, hover_event=False,
|
| 119 |
+
# # )
|
| 120 |
+
# selected_points = None
|
| 121 |
+
# with st.form(key="form1_main"):
|
| 122 |
+
# if selected_points:
|
| 123 |
+
# clicked_point = selected_points[0]
|
| 124 |
+
# x_coord = x = clicked_point['x']
|
| 125 |
+
# y_coord = y = clicked_point['y']
|
| 126 |
+
|
| 127 |
+
# x = st.number_input("X Coordinate", value=x, format="%.10f")
|
| 128 |
+
# y = st.number_input("Y Coordinate", value=y, format="%.10f")
|
| 129 |
+
# vec = np.array([x, y]).astype("float32")
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
# submit_button = st.form_submit_button("Submit")
|
| 133 |
+
|
| 134 |
+
# if selected_points or submit_button:
|
| 135 |
+
# inferred_embedding = reducer.inverse_transform(np.array([[x, y]]) if not isinstance(reducer, UMAP) else np.array([[x, y]]))
|
| 136 |
+
# inferred_embedding = inferred_embedding.astype("float32")
|
| 137 |
+
|
| 138 |
+
# output = vec2text.invert_embeddings(
|
| 139 |
+
# embeddings=torch.tensor(inferred_embedding).cuda(),
|
| 140 |
+
# corrector=corrector,
|
| 141 |
+
# num_steps=20,
|
| 142 |
+
# )
|
| 143 |
+
|
| 144 |
+
# st.text(str(output))
|
| 145 |
+
# st.text(str(inferred_embedding))
|
| 146 |
+
# else:
|
| 147 |
+
# st.text("Click on a point in the scatterplot to see its coordinates.")
|
| 148 |
+
|
| 149 |
+
# with col2:
|
| 150 |
+
# closest_sentence_index = utils.find_exact_match(vectors_2d, vec, decimals=3)
|
| 151 |
+
# st.write(f"{vectors_2d.dtype} {vec.dtype}")
|
| 152 |
+
# if closest_sentence_index > -1:
|
| 153 |
+
# st.write(df["title"].iloc[closest_sentence_index])
|
| 154 |
+
# # Card content
|
| 155 |
+
# st.markdown("## Card Container")
|
| 156 |
+
# st.write("This is an additional card container to the right of the main content.")
|
| 157 |
+
# st.write("You can use this space to show additional information, actions, or insights.")
|
| 158 |
+
# st.button("Card Button")
|