Spaces:
Sleeping
Sleeping
File size: 12,543 Bytes
0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a 0df5fcd ddadb1a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
import requests, wikipedia, re
from rank_bm25 import BM25Okapi
import streamlit as st
import pandas as pd
import spacy
####################################
## Streamlit app helper functions ##
####################################
def get_examples():
"""
Function for loading example questions
and contexts from examples.csv
Parameters: None
-----------
Returns:
--------
ex_queries, ex_questions, ex_contexts : list(str), list(list(str)), list(str)
Example search query, question, and context strings
(each entry of ex_questions is a list of three question strings)
"""
examples = pd.read_csv('examples.csv')
ex_questions = [q.split(':') for q in list(examples['question'])]
ex_contexts = list(examples['context'])
ex_queries = list(examples['query'])
return ex_queries, ex_questions, ex_contexts
def basic_clear_boxes():
"""
Clears the question, context, response
"""
for field in ['question','context','response']:
st.session_state['basic'][field] = ''
def basic_ex_click(examples, i):
"""
Fills in the chosen example
"""
st.session_state['basic']['question'] = examples[1][i][0]
st.session_state['basic']['context'] = examples[2][i]
def semi_clear_query():
"""
Clears the search query field
and page options list
"""
st.session_state['semi']['query'] = ''
for field in ['selected_pages','page_options']:
st.session_state['semi'][field] = []
def semi_clear_question():
"""
Clears the question and response field
and selected pages list
"""
for field in ['question','response']:
st.session_state['semi'][field] = ''
def semi_ex_query_click(examples,i):
"""
Fills in the query example and
populates question examples when query
example button is clicked
"""
st.session_state['semi']['query'] = examples[0][i]
st.session_state['semi']['ex_questions'] = examples[1][i]
def semi_ex_question_click(i):
"""
Fills in the question example
"""
st.session_state['semi']['question'] = st.session_state['semi']['ex_questions'][i]
def auto_clear_boxes():
"""
Clears the response and question fields
"""
for field in ['question','response']:
st.session_state['auto'][field]=''
def auto_ex_click(examples,i):
"""
Fills in the chosen example question
"""
st.session_state['auto']['question'] = examples[1][i][0]
###########################
## Query helper function ##
###########################
def generate_query(nlp,text):
"""
Process text into a search query,
only retaining nouns, proper nouns,
numerals, verbs, and adjectives
Parameters:
-----------
nlp : spacy.Pipe
spaCy pipeline for processing search query
text : str
The input text to be processed
Returns:
--------
query : str
The condensed search query
"""
tokens = nlp(text)
keep = {'PROPN', 'NUM', 'VERB', 'NOUN', 'ADJ'}
query = ' '.join(token.text for token in tokens \
if token.pos_ in keep)
return query
##############################
## Document retriever class ##
##############################
class ContextRetriever:
"""
Retrieves documents from Wikipedia based on a query,
and prepared context paragraphs for a RoBERTa model
"""
def __init__(self,url='https://en.wikipedia.org/w/api.php'):
self.url = url
self.pageids = None
self.pages = None
self.paragraphs = None
def get_pageids(self,query,topn = None):
"""
Retrieve page ids corresponding to a search query
Parameters:
-----------
query : str
A query to use for Wikipedia page search
topn : int or None
If topn is provided, will only return pageids
for topn search results
Returns: None, but stores:
--------
self.pageids : list(tuple(int,str))
A list of Wikipedia (pageid,title) tuples resulting
from the search
"""
params = {
'action':'query',
'list':'search',
'srsearch':query,
'format':'json',
}
results = requests.get(self.url, params=params).json()
pageids = [(page['pageid'],page['title']) for page in results['query']['search']]
pageids = pageids[:topn]
self.pageids = pageids
def ids_to_pages(self,ids):
"""
Use MediaWiki API to retrieve page content corresponding to
a list of pageids
Parameters:
-----------
ids : list(tuple(int,str))
A list of Wikipedia (pageid,title) tuples
Returns: None, but stores
--------
pages : list(tuple(str,str))
The k-th enry is a tuple consisting of the title and page content
of the page corresponding to the k-th entry of ids
"""
pages = []
for pageid in ids:
try:
page = wikipedia.page(pageid=pageid[0],auto_suggest=False)
pages.append((page.title, page.content))
except wikipedia.DisambiguationError:
continue
return pages
def get_all_pages(self):
"""
Use MediaWiki API to retrieve page content corresponding to
the list of pageids in self.pageids
Parameters: None
-----------
Returns: None, but stores
--------
self.pages : list(tuple(str,str))
The k-th enry is a tuple consisting of the title and page content
of the page corresponding to the k-th entry of self.pageids
"""
assert self.pageids is not None, "No pageids exist. Get pageids first using self.get_pageids"
self.pages = self.ids_to_pages(self.pageids)
def pages_to_paragraphs(self,pages):
"""
Process a list of pages into a list of paragraphs from those pages
Parameters:
-----------
pages : list(str)
A list of Wikipedia page content dumps, as strings
Returns:
--------
paragraphs : dict
keys are titles of pages from pages (as strings)
paragraphs[page] is a list of paragraphs (as strings)
extracted from page
"""
# Content from WikiMedia has these headings. We only grab content appearing
# before the first instance of any of these
pattern = '|'.join([
'== References ==',
'== Further reading ==',
'== External links',
'== See also ==',
'== Sources ==',
'== Notes ==',
'== Further references ==',
'== Footnotes ==',
'=== Notes ===',
'=== Sources ===',
'=== Citations ===',
])
pattern = re.compile(pattern)
paragraphs = {}
for page in pages:
# Truncate page to the first index of the start of a matching heading,
# or the end of the page if no matches exist
title, content = page
idx = min([match.start() for match in pattern.finditer(content)]+[len(content)])
content = content[:idx]
# Split into paragraphs, omitting lines with headings (start with '='),
# empty lines, or lines like '\t\t' or '\t\t\t' which sometimes appear
paragraphs[title] = [
p for p in content.split('\n') if p \
and not p.startswith('=') \
and not p.startswith('\t\t') \
and not p.startswith(' ')
]
return paragraphs
def get_all_paragraphs(self):
"""
Process self.pages into list of paragraphs from pages
Parameters: None
-----------
Returns: None, but stores
--------
self.paragraphs : dict
keys are titles of pages from self.pages (as strings)
self.paragraphs[page] is a list of paragraphs (as strings)
extracted from page
"""
assert self.pages is not None, "No page content exists. Get pages first using self.get_pages"
# Content from WikiMedia has these headings. We only grab content appearing
# before the first instance of any of these
self.paragraphs = self.pages_to_paragraphs(self.pages)
def rank_paragraphs(self,paragraphs,query,topn=10):
"""
Ranks the elements of paragraphs in descending order
by relevance to query using BM25 Okapi, and returns top
topn results
Parameters:
-----------
paragraphs : dict
keys are titles of pages (as strings)
paragraphs[page] is a list of paragraphs (as strings)
extracted from page
query : str
The query to use in ranking paragraphs by relevance
topn : int or None
The number of most relevant paragraphs to return
If None, will return roughly the top 1/4 of the
paragraphs
Returns:
--------
best_paragraphs : list(list(str,str))
The k-th entry is a list [title,paragraph] for the k-th
most relevant paragraph, where title is the title of the
Wikipedia article from which that paragraph was sourced
"""
corpus, titles, page_nums = [],[],[]
# Compile paragraphs into corpus
for i,page in enumerate(paragraphs):
titles.append(page)
paras = paragraphs[page]
corpus += paras
page_nums += len(paras)*[i]
# Tokenize corpus and query and initialize bm25 object
tokenized_corpus = [p.split(" ") for p in corpus]
bm25 = BM25Okapi(tokenized_corpus)
tokenized_query = query.split(" ")
# Compute scores and compile tuples (paragraph number, score, page number)
# before sorting tuples by score
bm_scores = bm25.get_scores(tokenized_query)
paragraph_data = [[i,score,page_nums[i]] for i,score in enumerate(bm_scores)]
paragraph_data.sort(reverse=True,key=lambda p:p[1])
# Grab topn best [title,paragraph] pairs sorted by bm25 score
topn = len(paragraph_data)//4+1 if topn is None else min(topn,len(paragraph_data))
best_paragraphs = [[titles[p[2]],corpus[p[0]]] for p in paragraph_data[:topn]]
return best_paragraphs
def generate_answer(pipeline,paragraphs, question):
"""
Generate an answer using a question-answer pipeline
Parameters:
-----------
pipeline : transformers.QuestionAnsweringPipeline
The question answering pipeline object
paragraphs : list(list(str,str))
The k-th entry is a list [title,paragraph] consisting
of a context paragraph and the title of the page from which the
paragraph was sourced
question : str
A question that is to be answered based on context given
in the entries of paragraphs
Returns:
--------
response : str
A response indicating the answer that was discovered,
or indicating that no answer could be found.
"""
# For each paragraph, format input to QA pipeline...
for paragraph in paragraphs:
input = {
'context':paragraph[1],
'question':question,
}
# ...and pass to QA pipeline
output = pipeline(**input)
# Append answers and scores. Report score of
# zero for paragraphs without answer, so they are
# deprioritized when the max is taken below
if output['answer']!='':
paragraph += [output['answer'],output['score']]
else:
paragraph += ['',0]
# Get paragraph with max confidence score and collect data
best_paragraph = max(paragraphs,key = lambda x:x[3])
best_answer = best_paragraph[2]
best_context_page = best_paragraph[0]
best_context = best_paragraph[1]
# Update response in session state
if best_answer == "":
response = "I cannot find the answer to your question."
else:
response = f"""
My answer is: {best_answer}
...and here's where I found it:
Page title: {best_context_page}
Paragraph containing answer:
{best_context}
"""
return response |