kiyer commited on
Commit
d1fa2c0
β€’
1 Parent(s): 9c7a7db

major upgrade to v2.0

Browse files
absts/.DS_Store DELETED
Binary file (6.15 kB)
 
app.py CHANGED
@@ -1,62 +1,990 @@
1
  import streamlit as st
 
2
 
3
- st.set_page_config(
4
- page_title="Pathfinder",
5
- page_icon="πŸ‘‹",
6
- )
 
 
 
 
 
7
 
8
- # st.write("# Welcome to Pathfinder! πŸ‘‹")
9
- st.image('local_files/pathfinder_logo.png',caption="Pathfinder: LLM enabled literature search")
10
- st.sidebar.success("Select a function above.")
11
- st.sidebar.markdown("Current functions include visualizing papers in the arxiv embedding, searching for similar papers to an input paper or prompt phrase, or answering quick questions.")
12
 
13
- st.markdown(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  """
15
- Pathfinder (formerly called arXiv+GPT) is a framework for searching and
16
- visualizing papers on the [arXiv](https://arxiv.org/) using the context
17
- sensitivity from modern large language models (LLMs) to better link paper contexts.
18
-
19
- **πŸ‘ˆ Select a tool from the sidebar** to see some examples
20
- of what this framework can do!
21
-
22
- ### Tool summary:
23
- - `Paper search` looks for relevant papers given an arxiv id or a question.
24
- - `Arxiv embedding` shows the landscape of current galaxy evolution papers (astro-ph.GA)
25
- - `Answering questions` brings it all together using RAG to give concise answers to questions with primary sources and relevant papers.
26
- - `Author search` uses a list of authors for the papers to visualize trajectories of individual researchers or groups over time.
27
- - `Research hotspots` uses paper ages to visualize excess research at a particular time in the past in different parts of the embedding space.
28
-
29
- This is not meant to be a replacement to existing tools like the
30
- [ADS](https://ui.adsabs.harvard.edu/),
31
- [arxivsorter](https://www.arxivsorter.org/), but rather a supplement to find papers
32
- that otherwise might be missed during a literature survey.
33
- It is also only trained on astro-ph.GA (astrophysics of galaxies) papers currently,
34
- if you are interested in extending it please reach out!
35
-
36
- The image below shows a representation of all the astro-ph.GA papers that can be explored in more detail
37
- using the `Arxiv embedding` page. The papers tend to cluster together by similarity, and result in an
38
- atlas that shows well studied (forests) and currently uncharted areas (water).
39
  """
40
- )
41
 
42
- # st.image('https://drive.google.com/uc?id=1yQQCdlgnFzi-_yOMplGIqEyPKJhIsZpO&export=download')
43
- st.image('local_files/galaxy_worldmap_kiyer-min.png')
 
 
 
 
 
 
 
44
 
45
- st.markdown(
 
 
 
 
 
 
 
 
 
 
 
 
46
  """
47
- ### Coming soon:
48
- - [AstroLLaMA](https://huggingface.co/spaces/universeTBD/astrollama) embeddings!
49
- - export results
50
- - daily updates to repo
51
- - other fields apart from `astro-ph.GA`
52
-
53
- ### Want to learn more?
54
- - Check out `AstroLLaMA` [paper](https://huggingface.co/papers/2309.06126)
55
- - Check out `chaotic_neural` [(link)](http://chaotic-neural.readthedocs.io/)
56
- - Jump into our [documentation](https://docs.streamlit.io)
57
- - Contribute!
58
-
59
- Pathfinder is developed and maintained by [UniverseTBD](https://universetbd.org/). Updates on [huggingface](https://huggingface.co/universeTBD) or [twitter](https://twitter.com/universe_tbd).
60
-
61
- """
62
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ st.set_page_config(layout="wide")
3
 
4
+ import numpy as np
5
+ from abc import ABC, abstractmethod
6
+ from typing import List, Dict, Any, Tuple
7
+ from collections import defaultdict
8
+ from tqdm import tqdm
9
+ import pandas as pd
10
+ from datetime import datetime, date
11
+ from datasets import load_dataset, load_from_disk
12
+ from collections import Counter
13
 
14
+ import yaml, json, requests, sys, os, time
15
+ import concurrent.futures
 
 
16
 
17
+ from langchain import hub
18
+ from langchain_openai import ChatOpenAI as openai_llm
19
+ from langchain_openai import OpenAIEmbeddings
20
+ from langchain_core.runnables import RunnableConfig, RunnablePassthrough, RunnableParallel
21
+ from langchain_core.prompts import PromptTemplate
22
+ from langchain_community.callbacks import StreamlitCallbackHandler
23
+ from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
24
+ from langchain_community.vectorstores import Chroma
25
+ from langchain_community.document_loaders import TextLoader
26
+ from langchain.agents import create_react_agent, Tool, AgentExecutor
27
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
28
+ from langchain_core.output_parsers import StrOutputParser
29
+ from langchain.callbacks import FileCallbackHandler
30
+ from langchain.callbacks.manager import CallbackManager
31
+
32
+ import instructor
33
+ from pydantic import BaseModel, Field
34
+ from typing import List, Literal
35
+
36
+ from nltk.corpus import stopwords
37
+ import nltk
38
+ from openai import OpenAI
39
+ # import anthropic
40
+ import cohere
41
+ import faiss
42
+
43
+ import spacy
44
+ from string import punctuation
45
+ import pytextrank
46
+
47
+ from bokeh.plotting import figure
48
+ from bokeh.models import ColumnDataSource
49
+ from bokeh.io import output_notebook
50
+ from bokeh.palettes import Spectral5
51
+ from bokeh.transform import linear_cmap
52
+
53
+ ts = time.time()
54
+ st.session_state.ts = ts
55
+
56
+ openai_key = st.secrets["openai_key"]
57
+ # cohere_key = st.secrets['cohere_key']
58
+ cohere_key = 'Of1MjzFjGmvzBAqdvNHTQLkAjecPcOKpiIPAnFMn'
59
+
60
+ if 'nlp' not in st.session_state:
61
+ nlp = spacy.load("en_core_web_sm")
62
+ nlp.add_pipe("textrank")
63
+ st.session_state.nlp = nlp
64
+
65
+ try:
66
+ stopwords.words('english')
67
+ except:
68
+ nltk.download('stopwords')
69
+ stopwords.words('english')
70
+
71
+ st.session_state.gen_llm = openai_llm(temperature=0,
72
+ model_name='gpt-4o-mini',
73
+ openai_api_key = openai_key)
74
+ st.session_state.consensus_client = instructor.patch(OpenAI(api_key=openai_key))
75
+ st.session_state.embed_client = OpenAI(api_key = openai_key)
76
+ embed_model = "text-embedding-3-small"
77
+ st.session_state.embeddings = OpenAIEmbeddings(model = embed_model, api_key = openai_key)
78
+
79
+ st.image('local_files/pathfinder_logo.png')
80
+
81
+ st.expander("What is Pathfinder / How do I use it?", expanded=False).write(
82
+ """
83
+ Pathfinder v2.0 is a framework for searching and visualizing astronomy papers on the [arXiv](https://arxiv.org/) and [ADS](https://ui.adsabs.harvard.edu/) using the context
84
+ sensitivity from modern large language models (LLMs) to better parse patterns in paper contexts.
85
+
86
+ This tool was built during the [JSALT workshop](https://www.clsp.jhu.edu/2024-jelinek-summer-workshop-on-speech-and-language-technology/) to do awesome things.
87
+
88
+ **πŸ‘ˆ Use the sidebar to tweak the search parameters to get better results**.
89
+
90
+ ### Tool summary:
91
+ - Please wait while the initial data loads and compiles, this takes about a minute initially.
92
+
93
+ This is not meant to be a replacement to existing tools like the
94
+ [ADS](https://ui.adsabs.harvard.edu/),
95
+ [arxivsorter](https://www.arxivsorter.org/), semantic search or google scholar, but rather a supplement to find papers
96
+ that otherwise might be missed during a literature survey.
97
+ It is trained on astro-ph (astrophysics of galaxies) papers up to last-year-ish mined from arxiv and supplemented with ADS metadata,
98
+ if you are interested in extending it please reach out!
99
+
100
+ Also add: feedback form, socials, literature, contact us, copyright, collaboration, etc.
101
+
102
+ The image below shows a representation of all the astro-ph.GA papers that can be explored in more detail
103
+ using the `Arxiv embedding` page. The papers tend to cluster together by similarity, and result in an
104
+ atlas that shows well studied (forests) and currently uncharted areas (water).
105
+ """
106
+ )
107
+
108
+
109
+ st.sidebar.header("Fine-tune the search")
110
+ top_k = st.sidebar.slider("Number of papers to retrieve:", 3, 30, 10)
111
+ extra_keywords = st.sidebar.text_input("Enter extra keywords (comma-separated):")
112
+
113
+ st.sidebar.subheader("Toggles")
114
+ toggle_a = st.sidebar.toggle("Weight by keywords", value = False)
115
+ toggle_b = st.sidebar.toggle("Weight by date", value = False)
116
+ toggle_c = st.sidebar.toggle("Weight by citations", value = False)
117
+
118
+ method = st.sidebar.radio("Retrieval method:", ["Semantic search", "Semantic search + HyDE", "Semantic search + HyDE + CoHERE"], index=2)
119
+
120
+ method2 = st.sidebar.radio("Generation complexity:", ["Basic RAG","ReAct Agent"])
121
+
122
+ question_type = st.sidebar.selectbox("Select question type:", ["Multi-paper (Default)", "Single-paper", "Bibliometric", "Broad but nuanced"])
123
+ st.session_state.question_type = question_type
124
+ # store_output = st.sidebar.button("Save output")
125
+
126
+ query = st.text_input("Ask me anything:")
127
+ submit_button = st.button("Run pathfinder!")
128
+
129
+ search_text_list = ['rooting around in the paper pile...','looking for clarity...','scanning the event horizon...','peering into the abyss...','potatoes power this ongoing search...']
130
+
131
+ if 'arxiv_corpus' not in st.session_state:
132
+ with st.spinner('loading data (please wait for this to finish before querying)...'):
133
+ # try:
134
+ arxiv_corpus = load_from_disk('data/')
135
+ # except:
136
+ # st.write('downloading data')
137
+ # arxiv_corpus = load_dataset('kiyer/pathfinder_arxiv_data',split='train')
138
+ # # arxiv_corpus = load_dataset('kiyer/pathfinder_arxiv_data_galaxy',split='train')
139
+ # arxiv_corpus.save_to_disk('data/')
140
+ arxiv_corpus.add_faiss_index('embed')
141
+ st.session_state.arxiv_corpus = arxiv_corpus
142
+ st.toast('loaded arxiv corpus')
143
+
144
+ if 'ids' not in st.session_state:
145
+ with st.spinner('making the LLM talk to the astro papers...'):
146
+ st.session_state.ids = st.session_state.arxiv_corpus['ads_id']
147
+ st.session_state.titles = st.session_state.arxiv_corpus['title']
148
+ st.session_state.abstracts = st.session_state.arxiv_corpus['abstract']
149
+ st.session_state.authors = st.session_state.arxiv_corpus['authors']
150
+ st.session_state.cites = st.session_state.arxiv_corpus['cites']
151
+ st.session_state.years = st.session_state.arxiv_corpus['date']
152
+ st.session_state.kws = st.session_state.arxiv_corpus['keywords']
153
+ st.session_state.ads_kws = st.session_state.arxiv_corpus['ads_keywords']
154
+ st.session_state.bibcode = st.session_state.arxiv_corpus['bibcode']
155
+ st.session_state.umap_x = st.session_state.arxiv_corpus['umap_x']
156
+ st.session_state.umap_y = st.session_state.arxiv_corpus['umap_y']
157
+ st.toast('done caching. time taken: %.2f sec' %(time.time()-ts))
158
+
159
+ def get_keywords(text):
160
+ result = []
161
+ pos_tag = ['PROPN', 'ADJ', 'NOUN']
162
+ doc = st.session_state.nlp(text.lower())
163
+ for token in doc:
164
+ if(token.text in st.session_state.nlp.Defaults.stop_words or token.text in punctuation):
165
+ continue
166
+ if(token.pos_ in pos_tag):
167
+ result.append(token.text)
168
+ return result
169
+
170
+ def parse_doc(text, nret = 10):
171
+ local_kws = []
172
+ doc = st.session_state.nlp(text)
173
+ # examine the top-ranked phrases in the document
174
+ for phrase in doc._.phrases[:nret]:
175
+ # print(phrase.text)
176
+ local_kws.append(phrase.text)
177
+ return local_kws
178
+
179
+ class EmbeddingRetrievalSystem():
180
+
181
+ def __init__(self, weight_citation = False, weight_date = False, weight_keywords = False):
182
+
183
+ self.ids = st.session_state.ids
184
+ self.years = st.session_state.years
185
+ self.abstract = st.session_state.abstracts
186
+ self.client = OpenAI(api_key = openai_key)
187
+ self.embed_model = "text-embedding-3-small"
188
+ self.dataset = st.session_state.arxiv_corpus
189
+ self.kws = st.session_state.kws
190
+ self.cites = st.session_state.cites
191
+
192
+ self.weight_citation = weight_citation
193
+ self.weight_date = weight_date
194
+ self.weight_keywords = weight_keywords
195
+ self.id_to_index = {self.ids[i]: i for i in range(len(self.ids))}
196
+
197
+ # self.citation_filter = CitationFilter(self.dataset)
198
+ # self.date_filter = DateFilter(self.dataset['date'])
199
+ # self.keyword_filter = KeywordFilter(corpus=self.dataset, remove_capitals=True)
200
+
201
+ def parse_date(self, id):
202
+ # indexval = np.where(self.ids == id)[0][0]
203
+ indexval = id
204
+ return self.years[indexval]
205
+
206
+ def make_embedding(self, text):
207
+ str_embed = self.client.embeddings.create(input = [text], model = self.embed_model).data[0].embedding
208
+ return str_embed
209
+
210
+ def embed_batch(self, texts: List[str]) -> List[np.ndarray]:
211
+ embeddings = self.client.embeddings.create(input=texts, model=self.embed_model).data
212
+ return [np.array(embedding.embedding, dtype=np.float32) for embedding in embeddings]
213
+
214
+ def get_query_embedding(self, query):
215
+ return self.make_embedding(query)
216
+
217
+ def analyze_temporal_query(self, query):
218
+ return
219
+
220
+ def calc_faiss(self, query_embedding, top_k = 100):
221
+ # xq = query_embedding.reshape(-1,1).T.astype('float32')
222
+ # D, I = self.index.search(xq, top_k)
223
+ # return I[0], D[0]
224
+ tmp = self.dataset.search('embed', query_embedding, k=top_k)
225
+ return [tmp.indices, tmp.scores]
226
+
227
+ def rank_and_filter(self, query, query_embedding, query_date, top_k = 10, return_scores=False, time_result=None):
228
+
229
+ # st.write('status')
230
+
231
+ # st.write('toggles', self.toggles)
232
+ # st.write('question_type', self.question_type)
233
+ # st.write('rag method', self.rag_method)
234
+ # st.write('gen method', self.gen_method)
235
+
236
+ self.weight_keywords = self.toggles["Keyword weighting"]
237
+ self.weight_date = self.toggles["Time weighting"]
238
+ self.weight_citation = self.toggles["Citation weighting"]
239
+
240
+ topk_indices, similarities = self.calc_faiss(np.array(query_embedding), top_k = 1000)
241
+ similarities = 1/similarities # converting from a distance (less is better) to a similarity (more is better)
242
+
243
+ query_kws = get_keywords(query)
244
+ input_kws = self.query_input_keywords
245
+ query_kws = query_kws + input_kws
246
+ self.query_kws = query_kws
247
+
248
+ if self.weight_keywords == True:
249
+ sub_kws = [self.kws[i] for i in topk_indices]
250
+ kw_weight = np.zeros((len(topk_indices),)) + 0.1
251
+
252
+ for k in query_kws:
253
+ for i in (range(len(topk_indices))):
254
+ for j in range(len(sub_kws[i])):
255
+ if k.lower() in sub_kws[i][j].lower():
256
+ kw_weight[i] = kw_weight[i] + 0.1
257
+ # print(i, k, sub_kws[i][j])
258
+
259
+ # kw_weight = kw_weight**0.36 / np.amax(kw_weight**0.36)
260
+ kw_weight = kw_weight / np.amax(kw_weight)
261
+ else:
262
+ kw_weight = np.ones((len(topk_indices),))
263
+
264
+ if self.weight_date == True:
265
+ sub_dates = [self.years[i] for i in topk_indices]
266
+ date = datetime.now().date()
267
+ date_diff = np.array([((date - i).days / 365.) for i in sub_dates])
268
+ # age_weight = (1 + np.exp(date_diff/2.1))**(-1) + 0.5
269
+ age_weight = (1 + np.exp(date_diff/0.7))**(-1)
270
+ age_weight = age_weight / np.amax(age_weight)
271
+ else:
272
+ age_weight = np.ones((len(topk_indices),))
273
+
274
+ if self.weight_citation == True:
275
+ # st.write('weighting by citations')
276
+ sub_cites = np.array([self.cites[i] for i in topk_indices])
277
+ temp = sub_cites.copy()
278
+ temp[sub_cites > 300] = 300.
279
+ cite_weight = (1 + np.exp((300-temp)/42.0))**(-1.)
280
+ cite_weight = cite_weight / np.amax(cite_weight)
281
+ else:
282
+ cite_weight = np.ones((len(topk_indices),))
283
+
284
+ similarities = similarities * (kw_weight) * (age_weight) * (cite_weight)
285
+
286
+ filtered_results = [[topk_indices[i], similarities[i]] for i in range(len(similarities))]
287
+ top_results = sorted(filtered_results, key=lambda x: x[1], reverse=True)[:top_k]
288
+
289
+ if return_scores:
290
+ return {doc[0]: doc[1] for doc in top_results}
291
+
292
+ # Only keep the document IDs
293
+ top_results = [doc[0] for doc in top_results]
294
+ return top_results
295
+
296
+ def retrieve(self, query, top_k, time_result=None, query_date = None, return_scores = False):
297
+
298
+ query_embedding = self.get_query_embedding(query)
299
+
300
+ # Judge time relevance
301
+ if time_result is None:
302
+ if self.weight_date:
303
+ time_result, time_taken = self.analyze_temporal_query(query, self.anthropic_client)
304
+ else:
305
+ time_result = {'has_temporal_aspect': False, 'expected_year_filter': None, 'expected_recency_weight': None}
306
+
307
+ top_results = self.rank_and_filter(query,
308
+ query_embedding,
309
+ query_date,
310
+ top_k,
311
+ return_scores = return_scores,
312
+ time_result = time_result)
313
+
314
+ return top_results
315
+
316
+ class HydeRetrievalSystem(EmbeddingRetrievalSystem):
317
+ def __init__(self, generation_model: str = "claude-3-haiku-20240307",
318
+ embedding_model: str = "text-embedding-3-small",
319
+ temperature: float = 0.5,
320
+ max_doclen: int = 500,
321
+ generate_n: int = 1,
322
+ embed_query = True,
323
+ conclusion = False, **kwargs):
324
+
325
+ # Handle the kwargs for the superclass init -- filters/citation weighting
326
+ super().__init__(**kwargs)
327
+
328
+ if max_doclen * generate_n > 8191:
329
+ raise ValueError("Too many tokens. Please reduce max_doclen or generate_n.")
330
+
331
+ self.embedding_model = embedding_model
332
+ self.generation_model = generation_model
333
+
334
+ # HYPERPARAMETERS
335
+ self.temperature = temperature # generation temperature
336
+ self.max_doclen = max_doclen # max tokens for generation
337
+ self.generate_n = generate_n # how many documents
338
+ self.embed_query = embed_query # embed the query vector?
339
+ self.conclusion = conclusion # generate conclusion as well?
340
+
341
+ # self.anthropic_key = anthropic_key
342
+ # self.generation_client = anthropic.Anthropic(api_key = self.anthropic_key)
343
+ self.generation_client = openai_llm(temperature=0,model_name='gpt-4o-mini', openai_api_key = openai_key)
344
+
345
+ def retrieve(self, query: str, top_k: int = 10, return_scores = False, time_result = None) -> List[Tuple[str, str, float]]:
346
+ if time_result is None:
347
+ if self.weight_date: time_result, time_taken = analyze_temporal_query(query, self.anthropic_client)
348
+ else: time_result = {'has_temporal_aspect': False, 'expected_year_filter': None, 'expected_recency_weight': None}
349
+
350
+ docs = self.generate_docs(query)
351
+ st.expander('Abstract generated with hyde', expanded=False).write(docs)
352
+
353
+ doc_embeddings = self.embed_docs(docs)
354
+
355
+ if self.embed_query:
356
+ query_emb = self.embed_docs([query])[0]
357
+ doc_embeddings.append(query_emb)
358
+
359
+ embedding = np.mean(np.array(doc_embeddings), axis = 0)
360
+
361
+ top_results = self.rank_and_filter(query, embedding, query_date=None, top_k = top_k, return_scores = return_scores, time_result = time_result)
362
+
363
+ return top_results
364
+
365
+ def generate_doc(self, query: str):
366
+ prompt = """You are an expert astronomer. Given a scientific query, generate the abstract of an expert-level research paper
367
+ that answers the question. Stick to a maximum length of {} tokens and return just the text of the abstract and conclusion.
368
+ Do not include labels for any section. Use research-specific jargon.""".format(self.max_doclen)
369
+ # st.write('invoking hyde generation')
370
+
371
+ # message = self.generation_client.messages.create(
372
+ # model = self.generation_model,
373
+ # max_tokens = self.max_doclen,
374
+ # temperature = self.temperature,
375
+ # system = prompt,
376
+ # messages=[{ "role": "user",
377
+ # "content": [{"type": "text", "text": query,}] }]
378
+ # )
379
+ # return message.content[0].text
380
+
381
+ messages = [("system",prompt,),("human", query),]
382
+ return self.generation_client.invoke(messages).content
383
+
384
+
385
+
386
+ def generate_docs(self, query: str):
387
+ docs = []
388
+ for i in range(self.generate_n):
389
+ docs.append(self.generate_doc(query))
390
+ return docs
391
+
392
+ def embed_docs(self, docs: List[str]):
393
+ return self.embed_batch(docs)
394
+
395
+ class HydeCohereRetrievalSystem(HydeRetrievalSystem):
396
+ def __init__(self, **kwargs):
397
+ super().__init__(**kwargs)
398
+
399
+ self.cohere_key = cohere_key
400
+ self.cohere_client = cohere.Client(self.cohere_key)
401
+
402
+ def retrieve(self, query: str,
403
+ top_k: int = 10,
404
+ rerank_top_k: int = 250,
405
+ return_scores = False, time_result = None,
406
+ reweight = False) -> List[Tuple[str, str, float]]:
407
+
408
+ if time_result is None:
409
+ if self.weight_date: time_result, time_taken = analyze_temporal_query(query, self.anthropic_client)
410
+ else: time_result = {'has_temporal_aspect': False, 'expected_year_filter': None, 'expected_recency_weight': None}
411
+
412
+ top_results = super().retrieve(query, top_k = rerank_top_k, time_result = time_result)
413
+
414
+ # doc_texts = self.get_document_texts(top_results)
415
+ # docs_for_rerank = [f"Abstract: {doc['abstract']}\nConclusions: {doc['conclusions']}" for doc in doc_texts]
416
+ docs_for_rerank = [self.abstract[i] for i in top_results]
417
+
418
+ if len(docs_for_rerank) == 0:
419
+ return []
420
+
421
+ reranked_results = self.cohere_client.rerank(
422
+ query=query,
423
+ documents=docs_for_rerank,
424
+ model='rerank-english-v3.0',
425
+ top_n=top_k
426
+ )
427
+
428
+ final_results = []
429
+ for result in reranked_results.results:
430
+ doc_id = top_results[result.index]
431
+ doc_text = docs_for_rerank[result.index]
432
+ score = float(result.relevance_score)
433
+ final_results.append([doc_id, "", score])
434
+
435
+ if reweight:
436
+ if time_result['has_temporal_aspect']:
437
+ final_results = self.date_filter.filter(final_results, time_score = time_result['expected_recency_weight'])
438
+
439
+ if self.weight_citation: self.citation_filter.filter(final_results)
440
+
441
+ if return_scores:
442
+ return {result[0]: result[2] for result in final_results}
443
+
444
+ return [doc[0] for doc in final_results]
445
+
446
+ def embed_docs(self, docs: List[str]):
447
+ return self.embed_batch(docs)
448
+
449
+ # --------- other fns ------------------
450
+
451
+ def get_topk(query, top_k):
452
+ print('running retrieval')
453
+ rs = st.session_state.ec.retrieve(query, top_k, return_scores=True)
454
+ return rs
455
+
456
+ def Library(query, top_k = 7):
457
+ rs = get_topk(query, top_k = top_k)
458
+ op_docs = ''
459
+ for paperno, i in enumerate(rs):
460
+ op_docs = op_docs + 'Paper %.0f:' %(paperno+1) +' (published in '+st.session_state.bibcode[i][0:4] + ') ' + st.session_state.titles[i] + '\n' + st.session_state.abstracts[i] + '\n\n'
461
+
462
+ return op_docs
463
+
464
+ def Library2(query, top_k = 7):
465
+ rs = get_topk(query, top_k = top_k)
466
+ absts, fnames = [], []
467
+ for paperno, i in enumerate(rs):
468
+ absts.append(st.session_state.abstracts[i])
469
+ fnames.append(st.session_state.bibcode[i])
470
+ return absts, fnames, rs
471
+
472
+ def get_paper_df(ids):
473
+
474
+ papers, scores, yrs, links, cites, kws, authors, absts = [], [], [], [], [], [], [], []
475
+ for i in ids:
476
+ papers.append(st.session_state.titles[i])
477
+ scores.append(ids[i])
478
+ links.append('https://ui.adsabs.harvard.edu/abs/'+st.session_state.bibcode[i]+'/abstract')
479
+ yrs.append(st.session_state.bibcode[i][0:4])
480
+ cites.append(st.session_state.cites[i])
481
+ authors.append(st.session_state.authors[i][0])
482
+ kws.append(st.session_state.ads_kws[i])
483
+ absts.append(st.session_state.abstracts[i])
484
+
485
+ return pd.DataFrame({
486
+ 'Title': papers,
487
+ 'Relevance': scores,
488
+ 'Lead author': authors,
489
+ 'Year': yrs,
490
+ 'ADS Link': links,
491
+ 'Citations': cites,
492
+ 'Keywords': kws,
493
+ 'Abstract': absts
494
+ })
495
+
496
+ def extract_keywords(question, ec):
497
+ # Simulated keyword extraction (replace with actual logic)
498
+ return ['keyword1', 'keyword2', 'keyword3']
499
+
500
+ # Function to estimate consensus (replace with actual implementation)
501
+ def estimate_consensus():
502
+ # Simulated consensus estimation (replace with actual calculation)
503
+ return 0.75
504
+
505
+
506
+ def run_agent_qa(query, top_k):
507
+
508
+ # define tools
509
+ search = DuckDuckGoSearchAPIWrapper()
510
+ tools = [
511
+ Tool(
512
+ name="Library",
513
+ func=Library,
514
+ description="A source of information pertinent to your question. Do not answer a question without consulting this!"
515
+ ),
516
+ Tool(
517
+ name="Search",
518
+ func=search.run,
519
+ description="useful for when you need to look up knowledge about common topics or current events",
520
+ )
521
+ ]
522
+
523
+ if 'tools' not in st.session_state:
524
+ st.session_state.tools = tools
525
+
526
+ # define prompt
527
+
528
+ # for another question type:
529
+ # First, find the quotes from the document that are most relevant to answering the question, and then print them in numbered order.
530
+ # Quotes should be relatively short. If there are no relevant quotes, write β€œNo relevant quotes” instead.
531
+
532
+
533
+ template = """You are an expert astronomer and cosmologist.
534
+ Answer the following question as best you can using information from the library, but speaking in a concise and factual manner.
535
+ If you can not come up with an answer, say you do not know.
536
+ Try to break the question down into smaller steps and solve it in a logical manner.
537
+
538
+ You have access to the following tools:
539
+
540
+ {tools}
541
+
542
+ Use the following format:
543
+
544
+ Question: the input question you must answer
545
+ Thought: you should always think about what to do
546
+ Action: the action to take, should be one of [{tool_names}]
547
+ Action Input: the input to the action
548
+ Observation: the result of the action
549
+ ... (this Thought/Action/Action Input/Observation can repeat N times)
550
+ Thought: I now know the final answer
551
+ Final Answer: the final answer to the original input question. provide information about how you arrived at the answer, and any nuances or uncertainties the reader should be aware of
552
+
553
+ Begin! Remember to speak in a pedagogical and factual manner."
554
+
555
+ Question: {input}
556
+ Thought:{agent_scratchpad}"""
557
+
558
+ prompt = hub.pull("hwchase17/react")
559
+ prompt.template=template
560
+
561
+ # path to write intermediate trace to
562
+
563
+ file_path = "agent_trace.txt"
564
+ try:
565
+ os.remove(file_path)
566
+ except:
567
+ pass
568
+ file_handler = FileCallbackHandler(file_path)
569
+ callback_manager=CallbackManager([file_handler])
570
+
571
+ # define and execute agent
572
+
573
+ tool_names = [tool.name for tool in st.session_state.tools]
574
+ if 'agent' not in st.session_state:
575
+ # agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names)
576
+ agent = create_react_agent(llm=st.session_state.gen_llm, tools=tools, prompt=prompt)
577
+ st.session_state.agent = agent
578
+
579
+ if 'agent_executor' not in st.session_state:
580
+ agent_executor = AgentExecutor(agent=st.session_state.agent, tools=st.session_state.tools, verbose=True, handle_parsing_errors=True, callbacks=CallbackManager([file_handler]))
581
+ st.session_state.agent_executor = agent_executor
582
+
583
+ answer = st.session_state.agent_executor.invoke({"input": query,})
584
+ return answer
585
+
586
+ regular_prompt = """You are an expert astronomer and cosmologist.
587
+ Answer the following question as best you can using information from the library, but speaking in a concise and factual manner.
588
+ If you can not come up with an answer, say you do not know.
589
+ Try to break the question down into smaller steps and solve it in a logical manner.
590
+
591
+ Provide information about how you arrived at the answer, and any nuances or uncertainties the reader should be aware of.
592
+
593
+ Begin! Remember to speak in a pedagogical and factual manner."
594
+
595
+ Relevant documents:{context}
596
+
597
+ Question: {question}
598
+ Answer:"""
599
+
600
+ bibliometric_prompt = """You are an AI assistant with expertise in astronomy and astrophysics literature. Your task is to assist with relevant bibliometric information in response to a user question. The user question may consist of identifying key papers, authors, or trends in a specific area of astronomical research.
601
+
602
+ Depending on what the user wants, direct them to consult the NASA Astrophysics Data System (ADS) at https://ui.adsabs.harvard.edu/. Provide them with the recommended ADS query depending on their question.
603
+
604
+ Here's a more detailed guide on how to use NASA ADS for various types of queries:
605
+
606
+ Basic topic search: Enter keywords in the search bar, e.g., "exoplanets". Use quotation marks for exact phrases, e.g., "dark energy”
607
+ Author search: Use the syntax author:"Last Name, First Name", e.g., author:"Hawking, S". For papers by multiple authors, use AND, e.g., author:"Hawking, S" AND author:"Ellis, G"
608
+ Date range: Use year:YYYY-YYYY, e.g., year:2010-2020. For papers since a certain year, use year:YYYY-, e.g., year:2015-
609
+ 4.Combining search terms: Use AND, OR, NOT operators, e.g., "black holes" AND (author:"Hawking, S" OR author:"Penrose, R")
610
+ Filtering results: Use the left sidebar to filter by publication year, article type, or astronomy database
611
+ Sorting results: Use the "Sort" dropdown menu to order by options like citation count, publication date, or relevance
612
+ Advanced searches: Click on the "Search" dropdown menu and select "Classic Form" for field-specific searchesUse bibcode:YYYY for a specific journal/year, e.g., bibcode:2020ApJ to find all Astrophysical Journal papers from 2020
613
+ Finding review articles: Wrap the query in the reviews() operator (e.g. reviews(β€œdark energy”))
614
+ Excluding preprints: Add NOT doctype:"eprint" to your search
615
+ Citation metrics: Click on the citation count of a paper to see its citation history and who has cited it
616
+
617
+ Some examples:
618
+
619
+ Example 1:
620
+ β€œHow many papers published in 2022 used data from MAST missions?”
621
+ Your response should be: year:2022 data:"MAST"
622
+
623
+ Example 2:
624
+ β€œWhat are the most cited papers on spiral galaxy halos measured in X-rays, with publication date from 2010 to 2023?
625
+ Your response should be: "spiral galaxy halos" AND "x-ray" year:2010-2024
626
+
627
+ Example 3:
628
+ β€œCan you list 3 papers published by β€œ< name>” as first author?”
629
+ Your response should be: author: β€œ^X”
630
+
631
+ Example 4:
632
+ β€œBased on papers with β€œ<name>” as an author or co-author, can you suggest the five most recent astro-ph papers that would be relevant?”
633
+ Your response should be:
634
+
635
+ Remember to advise users that while these examples cover many common scenarios, NASA ADS has many more advanced features that can be explored through its documentation.
636
+
637
+ Relevant documents:{context}
638
+ Question: {question}
639
+
640
+ Response:"""
641
+
642
+ single_paper_prompt = """You are an astronomer with access to a vast database of astronomical facts and figures. Your task is to provide a concise, accurate answer to the following specific factual question about astronomy or astrophysics.
643
+ Provide the requested information clearly and directly. If relevant, include the source of your information or any recent updates to this fact. If there's any uncertainty or variation in the accepted value, briefly explain why.
644
+ If the question can't be answered with a single fact, provide a short, focused explanation. Always prioritize accuracy over speculation.
645
+ Relevant documents:{context}
646
+ Question: {question}
647
+ Response:"""
648
+
649
+ deep_knowledge_prompt = """You are an expert astronomer with deep knowledge across various subfields of astronomy and astrophysics. Your task is to provide a comprehensive and nuanced answer to the following question, which involves an unresolved topic or requires broad, common-sense understanding.
650
+ Consider multiple perspectives and current debates in the field. Explain any uncertainties or ongoing research. If relevant, mention how this topic connects to other areas of astronomy.
651
+ Provide your response in a clear, pedagogical manner, breaking down complex concepts for easier understanding. If appropriate, suggest areas where further research might be needed.
652
+ After formulating your initial response, take a moment to reflect on your answer. Consider:
653
+ 1. Have you addressed all aspects of the question?
654
+ 2. Are there any potential biases or assumptions in your explanation?
655
+ 3. Is your explanation clear and accessible to someone with a general science background?
656
+ 4. Have you adequately conveyed the uncertainties or debates surrounding this topic?
657
+ Based on this reflection, refine your answer as needed.
658
+ Remember, while you have extensive knowledge, it's okay to acknowledge the limits of current scientific understanding. If parts of the question cannot be answered definitively, explain why.
659
+ Relevant documents:{context}
660
+
661
+ Question: {question}
662
+
663
+ Initial Response:
664
+ [Your initial response here]
665
+
666
+ Reflection and Refinement:
667
+ [Your reflections and any refinements to your answer here]
668
+
669
+ Final Response:
670
+ [Your final, refined answer here]"""
671
+
672
+ def make_rag_qa_answer(query, top_k = 10):
673
+
674
+
675
+ # try:
676
+ absts, fhdrs, rs = Library2(query, top_k = top_k)
677
+
678
+ temp_abst = ''
679
+ loaders = []
680
+ for i in range(len(absts)):
681
+ temp_abst = absts[i]
682
+
683
+ try:
684
+ text_file = open("absts/"+fhdrs[i]+".txt", "w")
685
+ except:
686
+ os.mkdir('absts')
687
+ text_file = open("absts/"+fhdrs[i]+".txt", "w")
688
+ n = text_file.write(temp_abst)
689
+ text_file.close()
690
+ loader = TextLoader("absts/"+fhdrs[i]+".txt")
691
+ loaders.append(loader)
692
+
693
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=150, chunk_overlap=50, add_start_index=True)
694
+
695
+ splits = text_splitter.split_documents([loader.load()[0] for loader in loaders])
696
+ vectorstore = Chroma.from_documents(documents=splits, embedding=st.session_state.embeddings, collection_name='retdoc4')
697
+ # retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 6, "fetch_k": len(splits)})
698
+ retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 6})
699
+
700
+ for i in range(len(absts)):
701
+ os.remove("absts/"+fhdrs[i]+".txt")
702
+
703
+ if st.session_state.question_type == 'Bibliometric':
704
+ template = bibliometric_prompt
705
+ elif st.session_state.question_type == 'Single-paper':
706
+ template = single_paper_prompt
707
+ elif st.session_state.question_type == 'Broad but nuanced':
708
+ template = deep_knowledge_prompt
709
+ else:
710
+ template = regular_prompt
711
+ prompt = PromptTemplate.from_template(template)
712
+
713
+ def format_docs(docs):
714
+ return "\n\n".join(doc.page_content for doc in docs)
715
+
716
+
717
+ rag_chain_from_docs = (
718
+ RunnablePassthrough.assign(context=(lambda x: format_docs(x["context"])))
719
+ | prompt
720
+ | st.session_state.gen_llm
721
+ | StrOutputParser()
722
+ )
723
+
724
+ rag_chain_with_source = RunnableParallel(
725
+ {"context": retriever, "question": RunnablePassthrough()}
726
+ ).assign(answer=rag_chain_from_docs)
727
+
728
+ rag_answer = rag_chain_with_source.invoke(query, )
729
+
730
+ vectorstore.delete_collection()
731
+
732
+ # except:
733
+ # st.write('heavy load! please wait 10 seconds and try again.')
734
+
735
+ return rag_answer, rs
736
+
737
+ def guess_question_type(query: str):
738
+ categorization_prompt = """You are an expert astrophysicist and computer scientist specializing in linguistics and semantics. Your task is to categorize a given query into one of the following categories:
739
+
740
+ 1. Summarization
741
+ 2. Single-paper factual
742
+ 3. Multi-paper factual
743
+ 4. Named entity recognition
744
+ 5. Jargon-specific questions / overloaded words
745
+ 6. Time-sensitive
746
+ 7. Consensus evaluation
747
+ 8. What-ifs and counterfactuals
748
+ 9. Compositional
749
+
750
+ Analyze the query carefully, considering its content, structure, and implications. Then, determine which of the above categories best fits the query.
751
+
752
+ In your analysis, consider the following:
753
+ - Does the query ask for a well-known datapoint or mechanism?
754
+ - Can it be answered by a single paper or does it require multiple sources?
755
+ - Does it involve proper nouns or specific scientific terms?
756
+ - Is it time-dependent or likely to change in the near future?
757
+ - Does it require evaluating consensus across multiple sources?
758
+ - Is it a hypothetical or counterfactual question?
759
+ - Does it need to be broken down into sub-queries (i.e. compositional)?
760
+
761
+ After your analysis, categorize the query into one of the nine categories listed above.
762
+
763
+ Provide a brief explanation for your categorization, highlighting the key aspects of the query that led to your decision.
764
+
765
+ Present your final answer in the following format:
766
+
767
+ <categorization>
768
+ Category: [Selected category]
769
+ Explanation: [Your explanation for the categorization]
770
+ </categorization>"""
771
+ # st.write('invoking hyde generation')
772
+
773
+ # message = self.generation_client.messages.create(
774
+ # model = self.generation_model,
775
+ # max_tokens = self.max_doclen,
776
+ # temperature = self.temperature,
777
+ # system = prompt,
778
+ # messages=[{ "role": "user",
779
+ # "content": [{"type": "text", "text": query,}] }]
780
+ # )
781
+ # return message.content[0].text
782
+
783
+ messages = [("system",categorization_prompt,),("human", query),]
784
+ return st.session_state.ec.generation_client.invoke(messages).content
785
+
786
+ class OverallConsensusEvaluation(BaseModel):
787
+ consensus: Literal["Strong Agreement", "Moderate Agreement", "Weak Agreement", "No Clear Consensus", "Weak Disagreement", "Moderate Disagreement", "Strong Disagreement"] = Field(
788
+ ...,
789
+ description="The overall level of consensus between the query and the abstracts"
790
+ )
791
+ explanation: str = Field(
792
+ ...,
793
+ description="A detailed explanation of the consensus evaluation"
794
+ )
795
+ relevance_score: float = Field(
796
+ ...,
797
+ description="A score from 0 to 1 indicating how relevant the abstracts are to the query overall",
798
+ ge=0,
799
+ le=1
800
+ )
801
+
802
+ def evaluate_overall_consensus(query: str, abstracts: List[str]) -> OverallConsensusEvaluation:
803
+ """
804
+ Evaluates the overall consensus of the abstracts in relation to the query in a single LLM call.
805
+ """
806
+ prompt = f"""
807
+ Query: {query}
808
+
809
+ You will be provided with {len(abstracts)} scientific abstracts. Your task is to:
810
+ 1. Evaluate the overall consensus between the query and the abstracts.
811
+ 2. Provide a detailed explanation of your consensus evaluation.
812
+ 3. Assign an overall relevance score from 0 to 1, where 0 means completely irrelevant and 1 means highly relevant.
813
+
814
+ For the consensus evaluation, use one of the following levels:
815
+ Strong Agreement, Moderate Agreement, Weak Agreement, No Clear Consensus, Weak Disagreement, Moderate Disagreement, Strong Disagreement
816
+
817
+ Here are the abstracts:
818
+
819
+ {' '.join([f"Abstract {i+1}: {abstract}" for i, abstract in enumerate(abstracts)])}
820
+
821
+ Provide your evaluation in a structured format.
822
+ """
823
+
824
+ response = st.session_state.consensus_client.chat.completions.create(
825
+ model="gpt-4o-mini", # used to be "gpt-4",
826
+ response_model=OverallConsensusEvaluation,
827
+ messages=[
828
+ {"role": "system", "content": """You are an assistant with expertise in astrophysics for question-answering tasks.
829
+ Evaluate the overall consensus of the retrieved scientific abstracts in relation to a given query.
830
+ If you don't know the answer, just say that you don't know.
831
+ Use six sentences maximum and keep the answer concise."""},
832
+ {"role": "user", "content": prompt}
833
+ ],
834
+ temperature=0
835
+ )
836
+
837
+ return response
838
+
839
+ def create_embedding_plot(rs):
840
  """
841
+ function to create embedding plot
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
842
  """
 
843
 
844
+ pltsource = ColumnDataSource(data=dict(
845
+ x=st.session_state.umap_x,
846
+ y=st.session_state.umap_y,
847
+ title=st.session_state.titles,
848
+ link=st.session_state.bibcode,
849
+ ))
850
+
851
+ rsflag = np.zeros((len(st.session_state.ids),))
852
+ rsflag[np.array([k for k in rs])] = 1
853
 
854
+ # outflag = np.zeros((len(st.session_state.ids),))
855
+ # outflag[np.array([k for k in find_outliers(rs)])] = 1
856
+ pltsource.data['colors'] = rsflag * 0.8 + 0.1
857
+ # pltsource.data['colors'][outflag] = 0.5
858
+ pltsource.data['sizes'] = (rsflag + 1)**5 / 100
859
+
860
+ TOOLTIPS = """
861
+ <div style="width:300px;">
862
+ ID: $index
863
+ ($x, $y)
864
+ @title <br>
865
+ @link <br> <br>
866
+ </div>
867
  """
868
+
869
+ mapper = linear_cmap(field_name="colors", palette=Spectral5, low=0., high=1.)
870
+
871
+ p = figure(width=700, height=900, tooltips=TOOLTIPS, x_range=(0, 20), y_range=(-4.2,18),
872
+ title="UMAP projection of embeddings for the astro-ph corpus")
873
+
874
+ p.axis.visible=False
875
+ p.grid.visible=False
876
+ p.outline_line_alpha = 0.
877
+
878
+ p.circle('x', 'y', radius='sizes', source=pltsource, alpha=0.3, fill_color=mapper, fill_alpha='colors', line_color="lightgrey",line_alpha=0.1)
879
+
880
+ return p
881
+
882
+ if submit_button:
883
+
884
+ keywords = [kw.strip() for kw in extra_keywords.split(',')] if extra_keywords else []
885
+ toggles = {'Keyword weighting': toggle_a, 'Time weighting': toggle_b, 'Citation weighting': toggle_c}
886
+
887
+ if (method == "Semantic search"):
888
+ with st.spinner('set retrieval method to'+ method):
889
+ st.session_state.ec = EmbeddingRetrievalSystem()
890
+ elif (method == "Semantic search + HyDE"):
891
+ with st.spinner('set retrieval method to'+ method):
892
+ st.session_state.ec = HydeRetrievalSystem()
893
+ elif (method == "Semantic search + HyDE + CoHERE"):
894
+ with st.spinner('set retrieval method to'+ method):
895
+ st.session_state.ec = HydeCohereRetrievalSystem()
896
+ st.toast('loaded retrieval system')
897
+
898
+ with st.spinner(search_text_list[np.random.choice(len(search_text_list))]):
899
+
900
+ st.session_state.ec.query_input_keywords = keywords
901
+ st.session_state.ec.toggles = toggles
902
+ st.session_state.ec.question_type = question_type
903
+ st.session_state.ec.rag_method = method
904
+ st.session_state.ec.gen_method = method2
905
+
906
+ if method2 == "Basic RAG":
907
+ st.session_state.gen_method = 'rag'
908
+ elif method2 == "ReAct Agent":
909
+ st.session_state.gen_method = 'agent'
910
+
911
+ if st.session_state.gen_method == 'agent':
912
+ answer = run_agent_qa(query, top_k)
913
+ rs = get_topk(query, top_k)
914
+
915
+ answer_text = answer['output']
916
+ st.write(answer_text)
917
+
918
+ file_path = "agent_trace.txt"
919
+ with open(file_path, 'r') as file:
920
+ intermediate_steps = file.read()
921
+ st.expander('Intermediate steps', expanded=False).write(intermediate_steps)
922
+
923
+ elif st.session_state.gen_method == 'rag':
924
+ answer, rs = make_rag_qa_answer(query, top_k)
925
+ answer_text = answer['answer']
926
+ st.write(answer_text)
927
+
928
+ triggered_keywords = st.session_state.ec.query_kws
929
+
930
+ with st.spinner('compiling top-k papers'+ method):
931
+ papers_df = get_paper_df(rs)
932
+
933
+ with st.expander("Relevant papers", expanded=True):
934
+ # st.dataframe(papers_df, hide_index=True)
935
+ st.data_editor(papers_df, column_config = {'ADS Link':st.column_config.LinkColumn(display_text= 'https://ui.adsabs.harvard.edu/abs/(.*?)/abstract')})
936
+
937
+ st.write('**Triggered keywords:** `'+ "`, `".join(triggered_keywords)+'`')
938
+
939
+ col1, col2 = st.columns(2)
940
+
941
+ with col1:
942
+ with st.expander("Evaluating question type", expanded=True):
943
+ st.subheader("Question type suggestion")
944
+ question_type_gen = guess_question_type(query)
945
+ if '<categorization>' in question_type_gen:
946
+ question_type_gen = question_type_gen.split('<categorization>')[1]
947
+ if '</categorization>' in question_type_gen:
948
+ question_type_gen = question_type_gen.split('</categorization>')[0]
949
+ question_type_gen = question_type_gen.replace('\n',' \n')
950
+ st.markdown(question_type_gen)
951
+
952
+ with col2:
953
+ with st.expander("Evaluating abstract consensus", expanded=True):
954
+ consensus_answer = evaluate_overall_consensus(query, [st.session_state.abstracts[i] for i in rs])
955
+ st.subheader("Consensus: "+consensus_answer.consensus)
956
+ st.markdown(consensus_answer.explanation)
957
+ st.markdown('Relevance of retrieved papers to answer: %.1f' %consensus_answer.relevance_score)
958
+
959
+ session_vars = {
960
+ "runtime": "pathfinder_v1_online",
961
+ "query": query,
962
+ "question_type": question_type,
963
+ 'Keyword weighting': toggle_a,
964
+ 'Time weighting': toggle_b,
965
+ 'Citation weighting': toggle_c,
966
+ "rag_method" : method,
967
+ "gen_method" : method2,
968
+ "answer" : answer_text,
969
+ "topk" : ['%.0f' %i for i in rs],
970
+ "topk_scores" : ['%.6f' %rs[i] for i in rs],
971
+ "topk_papers": list(papers_df['ADS Link']),
972
+ }
973
+
974
+ @st.fragment()
975
+ def download_op(data):
976
+ json_string = json.dumps(data)
977
+ st.download_button(
978
+ label='Download output',
979
+ file_name="pathfinder_data.json",
980
+ mime="application/json",
981
+ data=json_string,)
982
+
983
+ with st.sidebar:
984
+ download_op(session_vars)
985
+
986
+ embedding_plot = create_embedding_plot(rs)
987
+ st.bokeh_chart(embedding_plot)
988
+
989
+ else:
990
+ st.info("Use the sidebar to tweak the search parameters to get better results.")
local_files/astro_ph_ga_feeds_ada_embedding_27-Jun-2023.pkl β†’ data/data-00000-of-00012.arrow RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:655af27d3c033e15ded2051d0b3a668dd429f64ec3bed5ceac3eacd969e618dd
3
- size 407961763
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b4bbd812e330ce29cf46abbb701ff70b5c25047753922fcc6dd347cd96944ca7
3
+ size 481016544
local_files/astro_ph_ga_embedding_16-Jun-2024.pkl β†’ data/data-00001-of-00012.arrow RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:e8149e23eb9102bdaa41019eb0ed33ec0fb5fcd8f1868cd0a5a12cac52538a99
3
- size 400163
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8b95552c0290335c946c519766b99b356425672d692dfb550789cb10e574fb63
3
+ size 475735248
local_files/astro_ph_ga_embedding_27-Jun-2023.pkl β†’ data/data-00002-of-00012.arrow RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:476fb3dc5155a733d6427b9f3cc126134d8be881cb0d598df2876f9a61dd672d
3
- size 265762
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ea8ca6e7ac9b5c7e7fc4ffeb7b304d6cf48fbd97ee0e43ffff4291360a96ecdb
3
+ size 477037032
local_files/astro_ph_ga_feeds_ada_embedding_16-Jun-2024.pkl β†’ data/data-00003-of-00012.arrow RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:7142d6cbd1eed73405990fa80b791d231da401208200ab1987a9b61d861f6c17
3
- size 614400163
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6b38c307368b75a5352aeb678b105d8349ed64c0bf42846d9d8c05b88f3f86ee
3
+ size 480337696
data/data-00004-of-00012.arrow ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:77e6a294bc709aaa1fa103903f372516a86e303a9de36471c930af9d3d45ed81
3
+ size 475570280
data/data-00005-of-00012.arrow ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:abe6acc1a93e5c445a1b790d5e6f81032d57d28fae840e1672b013b7e00b6ebc
3
+ size 474685320
data/data-00006-of-00012.arrow ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6d1186e8f83b60d78efca8ee4aaa526a4a25d1c99108befd904a27927df3721a
3
+ size 452749528
data/data-00007-of-00012.arrow ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:475be1db95279824c32d170c39ae664485c911c0b499f3f287eb76cb8ffa3672
3
+ size 456206336
data/data-00008-of-00012.arrow ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8b407b33b433a6eb9d67071b605a637456ebaf0dfbb758eeab746755646448a4
3
+ size 467900584
data/data-00009-of-00012.arrow ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:015f809f2197e9fe98b4bf7d851f52bbbc29bd25f2a6264d7bbac6ca0c4029d3
3
+ size 479707864
data/data-00010-of-00012.arrow ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3d41cff04251d57fd742310815c5cbc9a8527e9c57775971dd36f262d20d1d5b
3
+ size 466979224
data/data-00011-of-00012.arrow ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8d5634d0dc5cb54e6b859176c86d0bae7eec0b4a4a487e9478d2e49b780a6338
3
+ size 486948696
data/dataset_info.json ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "builder_name": "parquet",
3
+ "citation": "",
4
+ "config_name": "default",
5
+ "dataset_name": "pathfinder_arxiv_data",
6
+ "dataset_size": 5770056875,
7
+ "description": "",
8
+ "download_checksums": {
9
+ "hf://datasets/kiyer/pathfinder_arxiv_data@66fc52fb3d7d82779c3d73b0cb0c14218cb02e63/data/train-00000-of-00012.parquet": {
10
+ "num_bytes": 384481705,
11
+ "checksum": null
12
+ },
13
+ "hf://datasets/kiyer/pathfinder_arxiv_data@66fc52fb3d7d82779c3d73b0cb0c14218cb02e63/data/train-00001-of-00012.parquet": {
14
+ "num_bytes": 383347319,
15
+ "checksum": null
16
+ },
17
+ "hf://datasets/kiyer/pathfinder_arxiv_data@66fc52fb3d7d82779c3d73b0cb0c14218cb02e63/data/train-00002-of-00012.parquet": {
18
+ "num_bytes": 383133689,
19
+ "checksum": null
20
+ },
21
+ "hf://datasets/kiyer/pathfinder_arxiv_data@66fc52fb3d7d82779c3d73b0cb0c14218cb02e63/data/train-00003-of-00012.parquet": {
22
+ "num_bytes": 384399351,
23
+ "checksum": null
24
+ },
25
+ "hf://datasets/kiyer/pathfinder_arxiv_data@66fc52fb3d7d82779c3d73b0cb0c14218cb02e63/data/train-00004-of-00012.parquet": {
26
+ "num_bytes": 382810245,
27
+ "checksum": null
28
+ },
29
+ "hf://datasets/kiyer/pathfinder_arxiv_data@66fc52fb3d7d82779c3d73b0cb0c14218cb02e63/data/train-00005-of-00012.parquet": {
30
+ "num_bytes": 382870394,
31
+ "checksum": null
32
+ },
33
+ "hf://datasets/kiyer/pathfinder_arxiv_data@66fc52fb3d7d82779c3d73b0cb0c14218cb02e63/data/train-00006-of-00012.parquet": {
34
+ "num_bytes": 364849142,
35
+ "checksum": null
36
+ },
37
+ "hf://datasets/kiyer/pathfinder_arxiv_data@66fc52fb3d7d82779c3d73b0cb0c14218cb02e63/data/train-00007-of-00012.parquet": {
38
+ "num_bytes": 363965178,
39
+ "checksum": null
40
+ },
41
+ "hf://datasets/kiyer/pathfinder_arxiv_data@66fc52fb3d7d82779c3d73b0cb0c14218cb02e63/data/train-00008-of-00012.parquet": {
42
+ "num_bytes": 376639054,
43
+ "checksum": null
44
+ },
45
+ "hf://datasets/kiyer/pathfinder_arxiv_data@66fc52fb3d7d82779c3d73b0cb0c14218cb02e63/data/train-00009-of-00012.parquet": {
46
+ "num_bytes": 384035100,
47
+ "checksum": null
48
+ },
49
+ "hf://datasets/kiyer/pathfinder_arxiv_data@66fc52fb3d7d82779c3d73b0cb0c14218cb02e63/data/train-00010-of-00012.parquet": {
50
+ "num_bytes": 355126903,
51
+ "checksum": null
52
+ },
53
+ "hf://datasets/kiyer/pathfinder_arxiv_data@66fc52fb3d7d82779c3d73b0cb0c14218cb02e63/data/train-00011-of-00012.parquet": {
54
+ "num_bytes": 359912183,
55
+ "checksum": null
56
+ }
57
+ },
58
+ "download_size": 4505570263,
59
+ "features": {
60
+ "ads_id": {
61
+ "dtype": "string",
62
+ "_type": "Value"
63
+ },
64
+ "arxiv_id": {
65
+ "dtype": "string",
66
+ "_type": "Value"
67
+ },
68
+ "title": {
69
+ "dtype": "string",
70
+ "_type": "Value"
71
+ },
72
+ "abstract": {
73
+ "dtype": "string",
74
+ "_type": "Value"
75
+ },
76
+ "embed": {
77
+ "feature": {
78
+ "dtype": "float32",
79
+ "_type": "Value"
80
+ },
81
+ "_type": "Sequence"
82
+ },
83
+ "umap_x": {
84
+ "dtype": "float32",
85
+ "_type": "Value"
86
+ },
87
+ "umap_y": {
88
+ "dtype": "float32",
89
+ "_type": "Value"
90
+ },
91
+ "date": {
92
+ "dtype": "date32",
93
+ "_type": "Value"
94
+ },
95
+ "cites": {
96
+ "dtype": "int64",
97
+ "_type": "Value"
98
+ },
99
+ "bibcode": {
100
+ "dtype": "string",
101
+ "_type": "Value"
102
+ },
103
+ "keywords": {
104
+ "feature": {
105
+ "dtype": "string",
106
+ "_type": "Value"
107
+ },
108
+ "_type": "Sequence"
109
+ },
110
+ "ads_keywords": {
111
+ "feature": {
112
+ "dtype": "string",
113
+ "_type": "Value"
114
+ },
115
+ "_type": "Sequence"
116
+ },
117
+ "read_count": {
118
+ "dtype": "int64",
119
+ "_type": "Value"
120
+ },
121
+ "doi": {
122
+ "feature": {
123
+ "dtype": "string",
124
+ "_type": "Value"
125
+ },
126
+ "_type": "Sequence"
127
+ },
128
+ "authors": {
129
+ "feature": {
130
+ "dtype": "string",
131
+ "_type": "Value"
132
+ },
133
+ "_type": "Sequence"
134
+ },
135
+ "aff": {
136
+ "feature": {
137
+ "dtype": "string",
138
+ "_type": "Value"
139
+ },
140
+ "_type": "Sequence"
141
+ },
142
+ "cite_bibcodes": {
143
+ "feature": {
144
+ "dtype": "string",
145
+ "_type": "Value"
146
+ },
147
+ "_type": "Sequence"
148
+ },
149
+ "ref_bibcodes": {
150
+ "feature": {
151
+ "dtype": "string",
152
+ "_type": "Value"
153
+ },
154
+ "_type": "Sequence"
155
+ }
156
+ },
157
+ "homepage": "",
158
+ "license": "",
159
+ "size_in_bytes": 10275627138,
160
+ "splits": {
161
+ "train": {
162
+ "name": "train",
163
+ "num_bytes": 5770056875,
164
+ "num_examples": 499142,
165
+ "shard_lengths": [
166
+ 42596,
167
+ 43596,
168
+ 43595,
169
+ 42595,
170
+ 43595,
171
+ 43595,
172
+ 46595,
173
+ 44595,
174
+ 43595,
175
+ 43595,
176
+ 43595,
177
+ 17595
178
+ ],
179
+ "dataset_name": "pathfinder_arxiv_data"
180
+ }
181
+ },
182
+ "version": {
183
+ "version_str": "0.0.0",
184
+ "major": 0,
185
+ "minor": 0,
186
+ "patch": 0
187
+ }
188
+ }
data/state.json ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_data_files": [
3
+ {
4
+ "filename": "data-00000-of-00012.arrow"
5
+ },
6
+ {
7
+ "filename": "data-00001-of-00012.arrow"
8
+ },
9
+ {
10
+ "filename": "data-00002-of-00012.arrow"
11
+ },
12
+ {
13
+ "filename": "data-00003-of-00012.arrow"
14
+ },
15
+ {
16
+ "filename": "data-00004-of-00012.arrow"
17
+ },
18
+ {
19
+ "filename": "data-00005-of-00012.arrow"
20
+ },
21
+ {
22
+ "filename": "data-00006-of-00012.arrow"
23
+ },
24
+ {
25
+ "filename": "data-00007-of-00012.arrow"
26
+ },
27
+ {
28
+ "filename": "data-00008-of-00012.arrow"
29
+ },
30
+ {
31
+ "filename": "data-00009-of-00012.arrow"
32
+ },
33
+ {
34
+ "filename": "data-00010-of-00012.arrow"
35
+ },
36
+ {
37
+ "filename": "data-00011-of-00012.arrow"
38
+ }
39
+ ],
40
+ "_fingerprint": "10a80a75c30e04f8",
41
+ "_format_columns": null,
42
+ "_format_kwargs": {},
43
+ "_format_type": null,
44
+ "_output_all_columns": false,
45
+ "_split": "train"
46
+ }
local_files/astro_ph_ga_feeds_upto_16-Jun-2024.pkl DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:89114c7ff34595e424f1585d32aec5665a07f26399e75bb8b40b4de7737ac2d0
3
- size 134799303
 
 
 
 
local_files/astro_ph_ga_feeds_upto_27-Jun-2023.pkl DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:29237e0e973a5fcd4df826c09432a069e6a471d5725fdfc9a0f8c7c62b69e188
3
- size 89228171
 
 
 
 
pages/.ipynb_checkpoints/Untitled-checkpoint.ipynb DELETED
@@ -1,6 +0,0 @@
1
- {
2
- "cells": [],
3
- "metadata": {},
4
- "nbformat": 4,
5
- "nbformat_minor": 5
6
- }
 
 
 
 
 
 
 
pages/1_arxiv_embedding_explorer.py DELETED
@@ -1,121 +0,0 @@
1
- import streamlit as st
2
- import pandas as pd
3
- import numpy as np
4
- import matplotlib.pyplot as plt
5
- import pickle
6
- from bokeh.palettes import OrRd
7
- from bokeh.plotting import figure, show
8
- from bokeh.plotting import ColumnDataSource, figure, output_notebook, show
9
- import cloudpickle as cp
10
- import pickle
11
- from scipy import stats
12
- from urllib.request import urlopen
13
-
14
- @st.cache_data
15
- def get_feeds_data(url):
16
- # data = cp.load(urlopen(url))
17
- with open(url, "rb") as fp:
18
- data = pickle.load(fp)
19
- st.sidebar.success("Fetched data from API!")
20
- return data
21
-
22
- # embeddings = OpenAIEmbeddings()
23
-
24
- dateval = "27-Jun-2023"
25
- feeds_link = "local_files/astro_ph_ga_feeds_upto_"+dateval+".pkl"
26
- embed_link = "local_files/astro_ph_ga_feeds_ada_embedding_"+dateval+".pkl"
27
- gal_feeds = get_feeds_data(feeds_link)
28
- arxiv_ada_embeddings = get_feeds_data(embed_link)
29
-
30
- @st.cache_data
31
- def get_embedding_data(url):
32
- # data = cp.load(urlopen(url))
33
- with open(url, "rb") as fp:
34
- data = pickle.load(fp)
35
- st.sidebar.success("Fetched data from API!")
36
- return data
37
-
38
- url = "local_files/astro_ph_ga_embedding_"+dateval+".pkl"
39
- # e2d, _, _, _, _ = get_embedding_data(url)
40
- embedding = get_embedding_data(url)
41
-
42
- st.title("ArXiv+GPT3 embedding explorer")
43
- st.markdown('[Includes papers up to: `'+dateval+'`]')
44
- st.markdown("This is an explorer for astro-ph.GA papers on the arXiv (up to Apt 18th, 2023). The papers have been preprocessed with `chaotic_neural` [(link)](http://chaotic-neural.readthedocs.io/) after which the collected abstracts are run through `text-embedding-ada-002` with [langchain](https://python.langchain.com/en/latest/ecosystem/openai.html) to generate a unique vector correpsonding to each paper. These are then compressed using [umap](https://umap-learn.readthedocs.io/en/latest/) and shown here, and can be used for similarity searches with methods like [faiss](https://github.com/facebookresearch/faiss). The scatterplot here can be paired with a heatmap for more targeted searches looking at a specific topic or area (see sidebar). Upgrade to chaotic neural suggested by Jo Ciucă, thank you! More to come (hopefully) with GPT-4 and its applications!")
45
- st.markdown("Interpreting the UMAP plot: the algorithm creates a 2d embedding from the high-dim vector space that tries to conserve as much similarity information as possible. Nearby points in UMAP space are similar, and grow dissimiliar as you move farther away. The axes do not have any physical meaning.")
46
-
47
- from tqdm import tqdm
48
- ctr = -1
49
- num_chunks = len(gal_feeds)
50
- all_text = []
51
- all_titles = []
52
- all_arxivid = []
53
- all_links = []
54
-
55
- for nc in tqdm(range(num_chunks)):
56
- for i in range(len(gal_feeds[nc].entries)):
57
- text = gal_feeds[nc].entries[i].summary
58
- text = text.replace('\n', ' ')
59
- text = text.replace('\\', '')
60
- all_text.append(text)
61
- all_titles.append(gal_feeds[nc].entries[i].title)
62
- all_arxivid.append(gal_feeds[nc].entries[i].id.split('/')[-1][0:-2])
63
- all_links.append(gal_feeds[nc].entries[i].links[1].href)
64
-
65
-
66
- def density_estimation(m1, m2, xmin=0, ymin=0, xmax=15, ymax=15):
67
- X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
68
- positions = np.vstack([X.ravel(), Y.ravel()])
69
- values = np.vstack([m1, m2])
70
- kernel = stats.gaussian_kde(values)
71
- Z = np.reshape(kernel(positions).T, X.shape)
72
- return X, Y, Z
73
-
74
- st.sidebar.markdown('This is a widget that allows you to look for papers containing specific phrases in the dataset and show it as a heatmap. Enter the phrase of interest, then change the size and opacity of the heatmap as desired to find the high-density regions. Hover over blue points to see the details of individual papers.')
75
- st.sidebar.markdown('`Note`: (i) if you enter a query that is not in the corpus of abstracts, it will return an error. just enter a different query in that case. (ii) there are some empty tooltips when you hover, these correspond to the underlying hexbins, and can be ignored.')
76
-
77
- st.sidebar.text_input("Search query", key="phrase", value="Quenching")
78
- alpha_value = st.sidebar.slider("Pick the hexbin opacity",0.0,1.0,0.81)
79
- size_value = st.sidebar.slider("Pick the hexbin gridsize",10,50,20)
80
-
81
- phrase=st.session_state.phrase
82
-
83
- phrase_flags = np.zeros((len(all_text),))
84
- for i in range(len(all_text)):
85
- if phrase.lower() in all_text[i].lower():
86
- phrase_flags[i] = 1
87
-
88
-
89
- source = ColumnDataSource(data=dict(
90
- x=embedding[0:,0],
91
- y=embedding[0:,1],
92
- title=all_titles,
93
- link=all_links,
94
- ))
95
-
96
- TOOLTIPS = """
97
- <div style="width:300px;">
98
- ID: $index
99
- ($x, $y)
100
- @title <br>
101
- @link <br> <br>
102
- </div>
103
- """
104
-
105
- p = figure(width=700, height=583, tooltips=TOOLTIPS, x_range=(0, 15), y_range=(2.5,15),
106
- title="UMAP projection of embeddings for the astro-ph.GA corpus"+phrase)
107
-
108
- # p.hexbin(em