Spaces:
Paused
Paused
derek-thomas
commited on
Commit
Β·
ea72d75
1
Parent(s):
b8cbe49
Reformatting
Browse files- app/Top2Vec.py +21 -21
- app/pages/01_Topic_Explorer_π.py +16 -16
- app/pages/02_Document_Explorer_π.py +22 -20
- app/pages/03_Semantic_Search_π.py +33 -30
app/Top2Vec.py
CHANGED
|
@@ -1,26 +1,26 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
-
st.set_page_config(page_title="Top2Vec", layout="wide")
|
| 4 |
|
| 5 |
st.markdown(
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
"""
|
| 26 |
-
)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
+
st.set_page_config(page_title="Top2Vec", layout="wide")
|
| 4 |
|
| 5 |
st.markdown(
|
| 6 |
+
"""
|
| 7 |
+
# Introduction
|
| 8 |
+
This is [space](https://huggingface.co/spaces) dedicated to using [top2vec](https://github.com/ddangelov/Top2Vec) and showing what features are available for semantic searching and topic modeling.
|
| 9 |
+
Please check out this [readme](https://github.com/ddangelov/Top2Vec#how-does-it-work) to better understand how it works.
|
| 10 |
+
|
| 11 |
+
> Top2Vec is an algorithm for **topic modeling** and **semantic search**. It automatically detects topics present in text and generates jointly embedded topic, document and word vectors.
|
| 12 |
|
| 13 |
+
|
| 14 |
+
# Setup
|
| 15 |
+
I used the [20 NewsGroups](https://huggingface.co/datasets/SetFit/20_newsgroups) dataset with `top2vec`.
|
| 16 |
+
I fit on the dataset and reduced the topics to 20.
|
| 17 |
+
The topics are created from top2vec, not the labels.
|
| 18 |
+
No analysis on the top 20 topics vs labels is provided.
|
| 19 |
+
|
| 20 |
+
# Usage
|
| 21 |
+
Check out
|
| 22 |
+
- The [Topic Explorer](/Topic_Explorer) page to understand what topic were detected
|
| 23 |
+
- The [Document Explorer](/Document_Explorer) page to visually explore documents
|
| 24 |
+
- The [Semantic Search](/Semantic_Search) page to search by meaning
|
| 25 |
+
"""
|
| 26 |
+
)
|
|
|
|
|
|
app/pages/01_Topic_Explorer_π.py
CHANGED
|
@@ -1,12 +1,10 @@
|
|
| 1 |
from logging import getLogger
|
| 2 |
from pathlib import Path
|
| 3 |
-
import joblib
|
| 4 |
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
-
import plotly.express as px
|
| 7 |
import plotly.graph_objects as go
|
| 8 |
import streamlit as st
|
| 9 |
-
|
| 10 |
from top2vec import Top2Vec
|
| 11 |
|
| 12 |
|
|
@@ -22,18 +20,18 @@ def initialize_state():
|
|
| 22 |
st.session_state.umap_model = joblib.load(proj_dir / 'models' / 'umap.sav')
|
| 23 |
logger.info("loading data...")
|
| 24 |
|
| 25 |
-
data = pd.read_csv(proj_dir/'data'/'data.csv')
|
| 26 |
data['topic_id'] = data['topic_id'].apply(lambda x: f'{x:02d}')
|
| 27 |
st.session_state.data = data
|
| 28 |
|
| 29 |
-
topics = pd.read_csv(proj_dir/'data'/'topics.csv')
|
| 30 |
topics['topic_id'] = topics['topic_id'].apply(lambda x: f'{x:02d}')
|
| 31 |
|
| 32 |
st.session_state.topics = topics
|
| 33 |
|
| 34 |
if 'data' not in st.session_state:
|
| 35 |
logger.info("loading data...")
|
| 36 |
-
data = pd.read_csv(proj_dir/'data'/'data.csv')
|
| 37 |
data['topic_id'] = data['topic_id'].apply(lambda x: f'{x:02d}')
|
| 38 |
st.session_state.data = data
|
| 39 |
st.session_state.selected_data = data
|
|
@@ -41,10 +39,11 @@ def initialize_state():
|
|
| 41 |
|
| 42 |
if 'topics' not in st.session_state:
|
| 43 |
logger.info("loading topics...")
|
| 44 |
-
topics = pd.read_csv(proj_dir/'data'/'topics.csv')
|
| 45 |
topics['topic_id'] = topics['topic_id'].apply(lambda x: f'{x:02d}')
|
| 46 |
st.session_state.topics = topics
|
| 47 |
|
|
|
|
| 48 |
def main():
|
| 49 |
st.write("""
|
| 50 |
A way to dive into each topic. Use the slider on the left to choose the topic.
|
|
@@ -53,17 +52,18 @@ def main():
|
|
| 53 |
|
| 54 |
topic_num = st.sidebar.slider("Topic Number", 0, 19, value=0)
|
| 55 |
fig = go.Figure(go.Bar(
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
fig.update_layout(
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
st.plotly_chart(fig, True)
|
| 66 |
|
|
|
|
| 67 |
if __name__ == "__main__":
|
| 68 |
# Setting up Logger and proj_dir
|
| 69 |
logger = getLogger(__name__)
|
|
@@ -73,10 +73,10 @@ if __name__ == "__main__":
|
|
| 73 |
pd.set_option('display.max_colwidth', 0)
|
| 74 |
|
| 75 |
# Streamlit settings
|
| 76 |
-
st.set_page_config(layout="wide")
|
| 77 |
md_title = "# Topic Explorer π"
|
| 78 |
st.markdown(md_title)
|
| 79 |
st.sidebar.markdown(md_title)
|
| 80 |
|
| 81 |
initialize_state()
|
| 82 |
-
main()
|
|
|
|
| 1 |
from logging import getLogger
|
| 2 |
from pathlib import Path
|
|
|
|
| 3 |
|
| 4 |
+
import joblib
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
import plotly.graph_objects as go
|
| 7 |
import streamlit as st
|
|
|
|
| 8 |
from top2vec import Top2Vec
|
| 9 |
|
| 10 |
|
|
|
|
| 20 |
st.session_state.umap_model = joblib.load(proj_dir / 'models' / 'umap.sav')
|
| 21 |
logger.info("loading data...")
|
| 22 |
|
| 23 |
+
data = pd.read_csv(proj_dir / 'data' / 'data.csv')
|
| 24 |
data['topic_id'] = data['topic_id'].apply(lambda x: f'{x:02d}')
|
| 25 |
st.session_state.data = data
|
| 26 |
|
| 27 |
+
topics = pd.read_csv(proj_dir / 'data' / 'topics.csv')
|
| 28 |
topics['topic_id'] = topics['topic_id'].apply(lambda x: f'{x:02d}')
|
| 29 |
|
| 30 |
st.session_state.topics = topics
|
| 31 |
|
| 32 |
if 'data' not in st.session_state:
|
| 33 |
logger.info("loading data...")
|
| 34 |
+
data = pd.read_csv(proj_dir / 'data' / 'data.csv')
|
| 35 |
data['topic_id'] = data['topic_id'].apply(lambda x: f'{x:02d}')
|
| 36 |
st.session_state.data = data
|
| 37 |
st.session_state.selected_data = data
|
|
|
|
| 39 |
|
| 40 |
if 'topics' not in st.session_state:
|
| 41 |
logger.info("loading topics...")
|
| 42 |
+
topics = pd.read_csv(proj_dir / 'data' / 'topics.csv')
|
| 43 |
topics['topic_id'] = topics['topic_id'].apply(lambda x: f'{x:02d}')
|
| 44 |
st.session_state.topics = topics
|
| 45 |
|
| 46 |
+
|
| 47 |
def main():
|
| 48 |
st.write("""
|
| 49 |
A way to dive into each topic. Use the slider on the left to choose the topic.
|
|
|
|
| 52 |
|
| 53 |
topic_num = st.sidebar.slider("Topic Number", 0, 19, value=0)
|
| 54 |
fig = go.Figure(go.Bar(
|
| 55 |
+
x=st.session_state.model.topic_word_scores_reduced[topic_num][::-1],
|
| 56 |
+
y=st.session_state.model.topic_words_reduced[topic_num][::-1],
|
| 57 |
+
orientation='h'))
|
| 58 |
fig.update_layout(
|
| 59 |
+
title=f'Words for Topic {topic_num}',
|
| 60 |
+
yaxis_title='Top 20 topic words',
|
| 61 |
+
xaxis_title='Distance to topic centroid'
|
| 62 |
+
)
|
| 63 |
|
| 64 |
st.plotly_chart(fig, True)
|
| 65 |
|
| 66 |
+
|
| 67 |
if __name__ == "__main__":
|
| 68 |
# Setting up Logger and proj_dir
|
| 69 |
logger = getLogger(__name__)
|
|
|
|
| 73 |
pd.set_option('display.max_colwidth', 0)
|
| 74 |
|
| 75 |
# Streamlit settings
|
| 76 |
+
st.set_page_config(layout="wide")
|
| 77 |
md_title = "# Topic Explorer π"
|
| 78 |
st.markdown(md_title)
|
| 79 |
st.sidebar.markdown(md_title)
|
| 80 |
|
| 81 |
initialize_state()
|
| 82 |
+
main()
|
app/pages/02_Document_Explorer_π.py
CHANGED
|
@@ -1,15 +1,12 @@
|
|
| 1 |
-
from distutils.fancy_getopt import wrap_text
|
| 2 |
from logging import getLogger
|
| 3 |
from pathlib import Path
|
| 4 |
-
import joblib
|
| 5 |
|
|
|
|
| 6 |
import pandas as pd
|
| 7 |
import plotly.express as px
|
| 8 |
-
import plotly.graph_objects as go
|
| 9 |
import streamlit as st
|
| 10 |
from st_aggrid import AgGrid, ColumnsAutoSizeMode, GridOptionsBuilder
|
| 11 |
from streamlit_plotly_events import plotly_events
|
| 12 |
-
|
| 13 |
from top2vec import Top2Vec
|
| 14 |
|
| 15 |
|
|
@@ -25,18 +22,18 @@ def initialize_state():
|
|
| 25 |
st.session_state.umap_model = joblib.load(proj_dir / 'models' / 'umap.sav')
|
| 26 |
logger.info("loading data...")
|
| 27 |
|
| 28 |
-
data = pd.read_csv(proj_dir/'data'/'data.csv')
|
| 29 |
data['topic_id'] = data['topic_id'].apply(lambda x: f'{x:02d}')
|
| 30 |
st.session_state.data = data
|
| 31 |
|
| 32 |
-
topics = pd.read_csv(proj_dir/'data'/'topics.csv')
|
| 33 |
topics['topic_id'] = topics['topic_id'].apply(lambda x: f'{x:02d}')
|
| 34 |
|
| 35 |
st.session_state.topics = topics
|
| 36 |
|
| 37 |
if 'data' not in st.session_state:
|
| 38 |
logger.info("loading data...")
|
| 39 |
-
data = pd.read_csv(proj_dir/'data'/'data.csv')
|
| 40 |
data['topic_id'] = data['topic_id'].apply(lambda x: f'{x:02d}')
|
| 41 |
st.session_state.data = data
|
| 42 |
st.session_state.selected_data = data
|
|
@@ -44,10 +41,11 @@ def initialize_state():
|
|
| 44 |
|
| 45 |
if 'topics' not in st.session_state:
|
| 46 |
logger.info("loading topics...")
|
| 47 |
-
topics = pd.read_csv(proj_dir/'data'/'topics.csv')
|
| 48 |
topics['topic_id'] = topics['topic_id'].apply(lambda x: f'{x:02d}')
|
| 49 |
st.session_state.topics = topics
|
| 50 |
|
|
|
|
| 51 |
def reset():
|
| 52 |
logger.info("Resetting...")
|
| 53 |
st.session_state.selected_data = st.session_state.data
|
|
@@ -61,12 +59,12 @@ def filter_df():
|
|
| 61 |
logger.info(f"Updates selected_data: {len(st.session_state.selected_data)}")
|
| 62 |
else:
|
| 63 |
logger.info(f"Lame")
|
| 64 |
-
|
| 65 |
|
| 66 |
def reset():
|
| 67 |
st.session_state.selected_data = st.session_state.data
|
| 68 |
st.session_state.selected_points = []
|
| 69 |
-
|
| 70 |
|
| 71 |
def main():
|
| 72 |
st.write("""
|
|
@@ -76,11 +74,13 @@ def main():
|
|
| 76 |
|
| 77 |
Zoom in and explore a topic of your choice. You can see the documents you select with the `lasso` or `box`
|
| 78 |
tool below in the corresponding tabs."""
|
| 79 |
-
|
| 80 |
|
| 81 |
st.button("Reset", help="Will Reset the selected points and the selected topics", on_click=reset)
|
| 82 |
-
data_to_model = st.session_state.data.sort_values(by='topic_id',
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
st.session_state.selected_points = plotly_events(fig, select_event=True, click_event=False)
|
| 85 |
filter_df()
|
| 86 |
|
|
@@ -94,18 +94,18 @@ def main():
|
|
| 94 |
builder = GridOptionsBuilder.from_dataframe(data)
|
| 95 |
builder.configure_pagination()
|
| 96 |
go = builder.build()
|
| 97 |
-
AgGrid(st.session_state.selected_data[cols], theme='streamlit', gridOptions=go,
|
|
|
|
| 98 |
else:
|
| 99 |
st.markdown('Select points in the graph with the `lasso` or `box` select tools to populate this table.')
|
| 100 |
|
| 101 |
def get_topics_counts() -> pd.DataFrame:
|
| 102 |
topic_counts = st.session_state.selected_data["topic_id"].value_counts().to_frame()
|
| 103 |
merged = topic_counts.merge(st.session_state.topics, left_index=True, right_on='topic_id')
|
| 104 |
-
cleaned = merged.drop(['topic_id_y'], axis=1).rename({'topic_id_x':'topic_count'}, axis=1)
|
| 105 |
cols = ['topic_id'] + [col for col in cleaned.columns if col != 'topic_id']
|
| 106 |
return cleaned[cols]
|
| 107 |
-
|
| 108 |
-
|
| 109 |
with tab2:
|
| 110 |
if st.session_state.selected_points:
|
| 111 |
filter_df()
|
|
@@ -116,10 +116,12 @@ def main():
|
|
| 116 |
builder.configure_pagination()
|
| 117 |
builder.configure_column('topic_0', header_name='Topic Word', wrap_text=True)
|
| 118 |
go = builder.build()
|
| 119 |
-
AgGrid(topic_counts.loc[:,cols], theme='streamlit', gridOptions=go,
|
|
|
|
| 120 |
else:
|
| 121 |
st.markdown('Select points in the graph with the `lasso` or `box` select tools to populate this table.')
|
| 122 |
|
|
|
|
| 123 |
if __name__ == "__main__":
|
| 124 |
# Setting up Logger and proj_dir
|
| 125 |
logger = getLogger(__name__)
|
|
@@ -129,10 +131,10 @@ if __name__ == "__main__":
|
|
| 129 |
pd.set_option('display.max_colwidth', 0)
|
| 130 |
|
| 131 |
# Streamlit settings
|
| 132 |
-
st.set_page_config(layout="wide")
|
| 133 |
md_title = "# Document Explorer π"
|
| 134 |
st.markdown(md_title)
|
| 135 |
st.sidebar.markdown(md_title)
|
| 136 |
|
| 137 |
initialize_state()
|
| 138 |
-
main()
|
|
|
|
|
|
|
| 1 |
from logging import getLogger
|
| 2 |
from pathlib import Path
|
|
|
|
| 3 |
|
| 4 |
+
import joblib
|
| 5 |
import pandas as pd
|
| 6 |
import plotly.express as px
|
|
|
|
| 7 |
import streamlit as st
|
| 8 |
from st_aggrid import AgGrid, ColumnsAutoSizeMode, GridOptionsBuilder
|
| 9 |
from streamlit_plotly_events import plotly_events
|
|
|
|
| 10 |
from top2vec import Top2Vec
|
| 11 |
|
| 12 |
|
|
|
|
| 22 |
st.session_state.umap_model = joblib.load(proj_dir / 'models' / 'umap.sav')
|
| 23 |
logger.info("loading data...")
|
| 24 |
|
| 25 |
+
data = pd.read_csv(proj_dir / 'data' / 'data.csv')
|
| 26 |
data['topic_id'] = data['topic_id'].apply(lambda x: f'{x:02d}')
|
| 27 |
st.session_state.data = data
|
| 28 |
|
| 29 |
+
topics = pd.read_csv(proj_dir / 'data' / 'topics.csv')
|
| 30 |
topics['topic_id'] = topics['topic_id'].apply(lambda x: f'{x:02d}')
|
| 31 |
|
| 32 |
st.session_state.topics = topics
|
| 33 |
|
| 34 |
if 'data' not in st.session_state:
|
| 35 |
logger.info("loading data...")
|
| 36 |
+
data = pd.read_csv(proj_dir / 'data' / 'data.csv')
|
| 37 |
data['topic_id'] = data['topic_id'].apply(lambda x: f'{x:02d}')
|
| 38 |
st.session_state.data = data
|
| 39 |
st.session_state.selected_data = data
|
|
|
|
| 41 |
|
| 42 |
if 'topics' not in st.session_state:
|
| 43 |
logger.info("loading topics...")
|
| 44 |
+
topics = pd.read_csv(proj_dir / 'data' / 'topics.csv')
|
| 45 |
topics['topic_id'] = topics['topic_id'].apply(lambda x: f'{x:02d}')
|
| 46 |
st.session_state.topics = topics
|
| 47 |
|
| 48 |
+
|
| 49 |
def reset():
|
| 50 |
logger.info("Resetting...")
|
| 51 |
st.session_state.selected_data = st.session_state.data
|
|
|
|
| 59 |
logger.info(f"Updates selected_data: {len(st.session_state.selected_data)}")
|
| 60 |
else:
|
| 61 |
logger.info(f"Lame")
|
| 62 |
+
|
| 63 |
|
| 64 |
def reset():
|
| 65 |
st.session_state.selected_data = st.session_state.data
|
| 66 |
st.session_state.selected_points = []
|
| 67 |
+
|
| 68 |
|
| 69 |
def main():
|
| 70 |
st.write("""
|
|
|
|
| 74 |
|
| 75 |
Zoom in and explore a topic of your choice. You can see the documents you select with the `lasso` or `box`
|
| 76 |
tool below in the corresponding tabs."""
|
| 77 |
+
)
|
| 78 |
|
| 79 |
st.button("Reset", help="Will Reset the selected points and the selected topics", on_click=reset)
|
| 80 |
+
data_to_model = st.session_state.data.sort_values(by='topic_id',
|
| 81 |
+
ascending=True) # to make legend sorted https://bioinformatics.stackexchange.com/a/18847
|
| 82 |
+
fig = px.scatter(data_to_model, x='x', y='y', color='topic_id', template='plotly_dark',
|
| 83 |
+
hover_data=['id', 'topic_id', 'x', 'y'])
|
| 84 |
st.session_state.selected_points = plotly_events(fig, select_event=True, click_event=False)
|
| 85 |
filter_df()
|
| 86 |
|
|
|
|
| 94 |
builder = GridOptionsBuilder.from_dataframe(data)
|
| 95 |
builder.configure_pagination()
|
| 96 |
go = builder.build()
|
| 97 |
+
AgGrid(st.session_state.selected_data[cols], theme='streamlit', gridOptions=go,
|
| 98 |
+
columns_auto_size_mode=ColumnsAutoSizeMode.FIT_CONTENTS)
|
| 99 |
else:
|
| 100 |
st.markdown('Select points in the graph with the `lasso` or `box` select tools to populate this table.')
|
| 101 |
|
| 102 |
def get_topics_counts() -> pd.DataFrame:
|
| 103 |
topic_counts = st.session_state.selected_data["topic_id"].value_counts().to_frame()
|
| 104 |
merged = topic_counts.merge(st.session_state.topics, left_index=True, right_on='topic_id')
|
| 105 |
+
cleaned = merged.drop(['topic_id_y'], axis=1).rename({'topic_id_x': 'topic_count'}, axis=1)
|
| 106 |
cols = ['topic_id'] + [col for col in cleaned.columns if col != 'topic_id']
|
| 107 |
return cleaned[cols]
|
| 108 |
+
|
|
|
|
| 109 |
with tab2:
|
| 110 |
if st.session_state.selected_points:
|
| 111 |
filter_df()
|
|
|
|
| 116 |
builder.configure_pagination()
|
| 117 |
builder.configure_column('topic_0', header_name='Topic Word', wrap_text=True)
|
| 118 |
go = builder.build()
|
| 119 |
+
AgGrid(topic_counts.loc[:, cols], theme='streamlit', gridOptions=go,
|
| 120 |
+
columns_auto_size_mode=ColumnsAutoSizeMode.FIT_ALL_COLUMNS_TO_VIEW)
|
| 121 |
else:
|
| 122 |
st.markdown('Select points in the graph with the `lasso` or `box` select tools to populate this table.')
|
| 123 |
|
| 124 |
+
|
| 125 |
if __name__ == "__main__":
|
| 126 |
# Setting up Logger and proj_dir
|
| 127 |
logger = getLogger(__name__)
|
|
|
|
| 131 |
pd.set_option('display.max_colwidth', 0)
|
| 132 |
|
| 133 |
# Streamlit settings
|
| 134 |
+
st.set_page_config(layout="wide")
|
| 135 |
md_title = "# Document Explorer π"
|
| 136 |
st.markdown(md_title)
|
| 137 |
st.sidebar.markdown(md_title)
|
| 138 |
|
| 139 |
initialize_state()
|
| 140 |
+
main()
|
app/pages/03_Semantic_Search_π.py
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
-
from
|
| 2 |
-
from
|
|
|
|
| 3 |
import joblib
|
| 4 |
-
import streamlit as st
|
| 5 |
import pandas as pd
|
| 6 |
-
from pathlib import Path
|
| 7 |
import plotly.express as px
|
| 8 |
-
import
|
| 9 |
-
from
|
| 10 |
-
from
|
| 11 |
-
from logging import getLogger
|
| 12 |
|
| 13 |
|
| 14 |
@st.cache(show_spinner=False)
|
|
@@ -22,19 +20,19 @@ def initialize_state():
|
|
| 22 |
st.session_state.model = model
|
| 23 |
st.session_state.umap_model = joblib.load(proj_dir / 'models' / 'umap.sav')
|
| 24 |
logger.info("loading data...")
|
| 25 |
-
|
| 26 |
-
data = pd.read_csv(proj_dir/'data'/'data.csv')
|
| 27 |
data['topic_id'] = data['topic_id'].apply(lambda x: f'{x:02d}')
|
| 28 |
st.session_state.data = data
|
| 29 |
|
| 30 |
-
topics = pd.read_csv(proj_dir/'data'/'topics.csv')
|
| 31 |
topics['topic_id'] = topics['topic_id'].apply(lambda x: f'{x:02d}')
|
| 32 |
|
| 33 |
st.session_state.topics = topics
|
| 34 |
|
| 35 |
if 'data' not in st.session_state:
|
| 36 |
logger.info("loading data...")
|
| 37 |
-
data = pd.read_csv(proj_dir/'data'/'data.csv')
|
| 38 |
data['topic_id'] = data['topic_id'].apply(lambda x: f'{x:02d}')
|
| 39 |
st.session_state.data = data
|
| 40 |
st.session_state.selected_data = data
|
|
@@ -42,14 +40,14 @@ def initialize_state():
|
|
| 42 |
|
| 43 |
if 'topics' not in st.session_state:
|
| 44 |
logger.info("loading topics...")
|
| 45 |
-
topics = pd.read_csv(proj_dir/'data'/'topics.csv')
|
| 46 |
topics['topic_id'] = topics['topic_id'].apply(lambda x: f'{x:02d}')
|
| 47 |
st.session_state.topics = topics
|
| 48 |
|
| 49 |
st.session_state.selected_points = []
|
| 50 |
|
| 51 |
-
def main():
|
| 52 |
|
|
|
|
| 53 |
max_docs = st.sidebar.slider("# docs", 10, 100, value=50)
|
| 54 |
to_search = st.text_input("Write your query here", "") or ""
|
| 55 |
with st.spinner('Embedding Query...'):
|
|
@@ -57,19 +55,23 @@ def main():
|
|
| 57 |
with st.spinner('Dimension Reduction...'):
|
| 58 |
point = st.session_state.umap_model.transform(vector.reshape(1, -1))
|
| 59 |
|
| 60 |
-
documents, document_scores, document_ids = st.session_state.model.search_documents_by_vector(vector.flatten(),
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
-
st.session_state.data_to_model = st.session_state.data.merge(st.session_state.search_raw_df, left_on='id',
|
| 64 |
-
|
| 65 |
-
st.session_state.data_to_model
|
|
|
|
|
|
|
|
|
|
| 66 |
st.session_state.data_to_model_with_point = st.session_state.data_to_model
|
| 67 |
st.session_state.data_to_model_without_point = st.session_state.data_to_model.iloc[:-1]
|
| 68 |
|
| 69 |
def get_topics_counts() -> pd.DataFrame:
|
| 70 |
topic_counts = st.session_state.data_to_model_without_point["topic_id"].value_counts().to_frame()
|
| 71 |
merged = topic_counts.merge(st.session_state.topics, left_index=True, right_on='topic_id')
|
| 72 |
-
cleaned = merged.drop(['topic_id_y'], axis=1).rename({'topic_id_x':'topic_count'}, axis=1)
|
| 73 |
cols = ['topic_id'] + [col for col in cleaned.columns if col != 'topic_id']
|
| 74 |
return cleaned[cols]
|
| 75 |
|
|
@@ -83,25 +85,25 @@ def main():
|
|
| 83 |
|
| 84 |
The Query is shown with the documents in yellow.
|
| 85 |
"""
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
df = st.session_state.data_to_model_with_point.sort_values(by='topic_id', ascending=True)
|
| 90 |
-
fig = px.scatter(df.iloc[:-1], x='x', y='y', color='topic_id', template='plotly_dark',
|
|
|
|
| 91 |
fig.add_traces(px.scatter(df.tail(1), x="x", y="y").update_traces(marker_size=10, marker_color="yellow").data)
|
| 92 |
st.plotly_chart(fig, use_container_width=True)
|
| 93 |
tab1, tab2 = st.tabs(["Docs", "Topics"])
|
| 94 |
|
| 95 |
-
|
| 96 |
with tab1:
|
| 97 |
cols = ['id', 'document_scores', 'topic_id', 'documents']
|
| 98 |
builder = GridOptionsBuilder.from_dataframe(st.session_state.data_to_model_without_point.loc[:, cols])
|
| 99 |
builder.configure_pagination()
|
| 100 |
-
builder.configure_column('document_scores', type=["numericColumn","numberColumnFilter","customNumericFormat"],
|
|
|
|
| 101 |
go = builder.build()
|
| 102 |
-
AgGrid(st.session_state.data_to_model_without_point.loc[:,cols], theme='streamlit', gridOptions=go,
|
|
|
|
| 103 |
|
| 104 |
-
|
| 105 |
with tab2:
|
| 106 |
cols = ['topic_id', 'topic_count', 'topic_0']
|
| 107 |
topic_counts = get_topics_counts()
|
|
@@ -109,7 +111,8 @@ def main():
|
|
| 109 |
builder.configure_pagination()
|
| 110 |
builder.configure_column('topic_0', header_name='Topic Word', wrap_text=True)
|
| 111 |
go = builder.build()
|
| 112 |
-
AgGrid(topic_counts.loc[:,cols], theme='streamlit', gridOptions=go,
|
|
|
|
| 113 |
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
|
@@ -121,10 +124,10 @@ if __name__ == "__main__":
|
|
| 121 |
pd.set_option('display.max_colwidth', 0)
|
| 122 |
|
| 123 |
# Streamlit settings
|
| 124 |
-
st.set_page_config(layout="wide")
|
| 125 |
md_title = "# Semantic Search π"
|
| 126 |
st.markdown(md_title)
|
| 127 |
st.sidebar.markdown(md_title)
|
| 128 |
|
| 129 |
initialize_state()
|
| 130 |
-
main()
|
|
|
|
| 1 |
+
from logging import getLogger
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
import joblib
|
|
|
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
import plotly.express as px
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from st_aggrid import AgGrid, ColumnsAutoSizeMode, GridOptionsBuilder
|
| 9 |
+
from top2vec import Top2Vec
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
@st.cache(show_spinner=False)
|
|
|
|
| 20 |
st.session_state.model = model
|
| 21 |
st.session_state.umap_model = joblib.load(proj_dir / 'models' / 'umap.sav')
|
| 22 |
logger.info("loading data...")
|
| 23 |
+
|
| 24 |
+
data = pd.read_csv(proj_dir / 'data' / 'data.csv')
|
| 25 |
data['topic_id'] = data['topic_id'].apply(lambda x: f'{x:02d}')
|
| 26 |
st.session_state.data = data
|
| 27 |
|
| 28 |
+
topics = pd.read_csv(proj_dir / 'data' / 'topics.csv')
|
| 29 |
topics['topic_id'] = topics['topic_id'].apply(lambda x: f'{x:02d}')
|
| 30 |
|
| 31 |
st.session_state.topics = topics
|
| 32 |
|
| 33 |
if 'data' not in st.session_state:
|
| 34 |
logger.info("loading data...")
|
| 35 |
+
data = pd.read_csv(proj_dir / 'data' / 'data.csv')
|
| 36 |
data['topic_id'] = data['topic_id'].apply(lambda x: f'{x:02d}')
|
| 37 |
st.session_state.data = data
|
| 38 |
st.session_state.selected_data = data
|
|
|
|
| 40 |
|
| 41 |
if 'topics' not in st.session_state:
|
| 42 |
logger.info("loading topics...")
|
| 43 |
+
topics = pd.read_csv(proj_dir / 'data' / 'topics.csv')
|
| 44 |
topics['topic_id'] = topics['topic_id'].apply(lambda x: f'{x:02d}')
|
| 45 |
st.session_state.topics = topics
|
| 46 |
|
| 47 |
st.session_state.selected_points = []
|
| 48 |
|
|
|
|
| 49 |
|
| 50 |
+
def main():
|
| 51 |
max_docs = st.sidebar.slider("# docs", 10, 100, value=50)
|
| 52 |
to_search = st.text_input("Write your query here", "") or ""
|
| 53 |
with st.spinner('Embedding Query...'):
|
|
|
|
| 55 |
with st.spinner('Dimension Reduction...'):
|
| 56 |
point = st.session_state.umap_model.transform(vector.reshape(1, -1))
|
| 57 |
|
| 58 |
+
documents, document_scores, document_ids = st.session_state.model.search_documents_by_vector(vector.flatten(),
|
| 59 |
+
num_docs=max_docs)
|
| 60 |
+
st.session_state.search_raw_df = pd.DataFrame({'document_ids': document_ids, 'document_scores': document_scores})
|
| 61 |
|
| 62 |
+
st.session_state.data_to_model = st.session_state.data.merge(st.session_state.search_raw_df, left_on='id',
|
| 63 |
+
right_on='document_ids').drop(['document_ids'], axis=1)
|
| 64 |
+
st.session_state.data_to_model = st.session_state.data_to_model.sort_values(by='document_scores',
|
| 65 |
+
ascending=False) # to make legend sorted https://bioinformatics.stackexchange.com/a/18847
|
| 66 |
+
st.session_state.data_to_model.loc[len(st.session_state.data_to_model.index)] = ['Point', *point[0].tolist(),
|
| 67 |
+
to_search, 'Query', 0]
|
| 68 |
st.session_state.data_to_model_with_point = st.session_state.data_to_model
|
| 69 |
st.session_state.data_to_model_without_point = st.session_state.data_to_model.iloc[:-1]
|
| 70 |
|
| 71 |
def get_topics_counts() -> pd.DataFrame:
|
| 72 |
topic_counts = st.session_state.data_to_model_without_point["topic_id"].value_counts().to_frame()
|
| 73 |
merged = topic_counts.merge(st.session_state.topics, left_index=True, right_on='topic_id')
|
| 74 |
+
cleaned = merged.drop(['topic_id_y'], axis=1).rename({'topic_id_x': 'topic_count'}, axis=1)
|
| 75 |
cols = ['topic_id'] + [col for col in cleaned.columns if col != 'topic_id']
|
| 76 |
return cleaned[cols]
|
| 77 |
|
|
|
|
| 85 |
|
| 86 |
The Query is shown with the documents in yellow.
|
| 87 |
"""
|
| 88 |
+
)
|
|
|
|
| 89 |
|
| 90 |
df = st.session_state.data_to_model_with_point.sort_values(by='topic_id', ascending=True)
|
| 91 |
+
fig = px.scatter(df.iloc[:-1], x='x', y='y', color='topic_id', template='plotly_dark',
|
| 92 |
+
hover_data=['id', 'topic_id', 'x', 'y'])
|
| 93 |
fig.add_traces(px.scatter(df.tail(1), x="x", y="y").update_traces(marker_size=10, marker_color="yellow").data)
|
| 94 |
st.plotly_chart(fig, use_container_width=True)
|
| 95 |
tab1, tab2 = st.tabs(["Docs", "Topics"])
|
| 96 |
|
|
|
|
| 97 |
with tab1:
|
| 98 |
cols = ['id', 'document_scores', 'topic_id', 'documents']
|
| 99 |
builder = GridOptionsBuilder.from_dataframe(st.session_state.data_to_model_without_point.loc[:, cols])
|
| 100 |
builder.configure_pagination()
|
| 101 |
+
builder.configure_column('document_scores', type=["numericColumn", "numberColumnFilter", "customNumericFormat"],
|
| 102 |
+
precision=2)
|
| 103 |
go = builder.build()
|
| 104 |
+
AgGrid(st.session_state.data_to_model_without_point.loc[:, cols], theme='streamlit', gridOptions=go,
|
| 105 |
+
columns_auto_size_mode=ColumnsAutoSizeMode.FIT_CONTENTS)
|
| 106 |
|
|
|
|
| 107 |
with tab2:
|
| 108 |
cols = ['topic_id', 'topic_count', 'topic_0']
|
| 109 |
topic_counts = get_topics_counts()
|
|
|
|
| 111 |
builder.configure_pagination()
|
| 112 |
builder.configure_column('topic_0', header_name='Topic Word', wrap_text=True)
|
| 113 |
go = builder.build()
|
| 114 |
+
AgGrid(topic_counts.loc[:, cols], theme='streamlit', gridOptions=go,
|
| 115 |
+
columns_auto_size_mode=ColumnsAutoSizeMode.FIT_ALL_COLUMNS_TO_VIEW)
|
| 116 |
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
|
|
|
| 124 |
pd.set_option('display.max_colwidth', 0)
|
| 125 |
|
| 126 |
# Streamlit settings
|
| 127 |
+
st.set_page_config(layout="wide")
|
| 128 |
md_title = "# Semantic Search π"
|
| 129 |
st.markdown(md_title)
|
| 130 |
st.sidebar.markdown(md_title)
|
| 131 |
|
| 132 |
initialize_state()
|
| 133 |
+
main()
|