Update app.py
Browse files
app.py
CHANGED
@@ -13,8 +13,8 @@ try:
|
|
13 |
except:
|
14 |
spacy.cli.download("en_core_web_sm")
|
15 |
nlp = spacy.load("en_core_web_sm")
|
16 |
-
wh_words = ['what', 'who', 'how', 'when', 'which']
|
17 |
|
|
|
18 |
def get_concepts(text):
|
19 |
text = text.lower()
|
20 |
doc = nlp(text)
|
@@ -38,12 +38,14 @@ def get_passages(text, k=100):
|
|
38 |
passage = sen.text
|
39 |
passage_len = len(sen)
|
40 |
continue
|
|
|
41 |
elif i==(len(sents)-1):
|
42 |
passage+=" "+sen.text
|
43 |
passages.append(passage)
|
44 |
passage = ""
|
45 |
passage_len = 0
|
46 |
continue
|
|
|
47 |
passage+=" "+sen.text
|
48 |
return passages
|
49 |
|
@@ -56,8 +58,10 @@ def get_dicts_for_dpr(concepts, n_results=20, k=100):
|
|
56 |
try:
|
57 |
html_page = wikipedia.page(title = wiki, auto_suggest = False)
|
58 |
except DisambiguationError:
|
59 |
-
continue
|
|
|
60 |
htmlResults=html_page.content
|
|
|
61 |
passages = get_passages(htmlResults, k=k)
|
62 |
for passage in passages:
|
63 |
i_dicts = {}
|
@@ -86,13 +90,11 @@ def extracted_passage_embeddings(processed_passages, max_length=156):
|
|
86 |
max_length=max_length,
|
87 |
return_token_type_ids=True
|
88 |
)
|
89 |
-
passage_embeddings = passage_encoder.predict(
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
verbose=1
|
95 |
-
)
|
96 |
return passage_embeddings
|
97 |
|
98 |
def extracted_query_embeddings(queries, max_length=64):
|
@@ -104,57 +106,65 @@ def extracted_query_embeddings(queries, max_length=64):
|
|
104 |
max_length=max_length,
|
105 |
return_token_type_ids=True
|
106 |
)
|
107 |
-
query_embeddings = query_encoder.predict(
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
verbose=1
|
113 |
-
)
|
114 |
return query_embeddings
|
115 |
|
116 |
-
#
|
117 |
|
118 |
def get_pagetext(page):
|
119 |
s=str(page).replace("/t","")
|
|
|
120 |
return s
|
121 |
|
122 |
def get_wiki_summary(search):
|
123 |
wiki_wiki = wikipediaapi.Wikipedia('en')
|
124 |
page = wiki_wiki.page(search)
|
|
|
125 |
isExist = page.exists()
|
126 |
if not isExist:
|
127 |
return isExist, "Not found", "Not found", "Not found", "Not found"
|
|
|
128 |
pageurl = page.fullurl
|
129 |
pagetitle = page.title
|
130 |
pagesummary = page.summary[0:60]
|
131 |
pagetext = get_pagetext(page.text)
|
|
|
132 |
backlinks = page.backlinks
|
133 |
linklist = ""
|
134 |
for link in backlinks.items():
|
135 |
pui = link[0]
|
136 |
linklist += pui + " , "
|
137 |
a=1
|
|
|
138 |
categories = page.categories
|
139 |
categorylist = ""
|
140 |
for category in categories.items():
|
141 |
pui = category[0]
|
142 |
categorylist += pui + " , "
|
143 |
a=1
|
|
|
144 |
links = page.links
|
145 |
linklist2 = ""
|
146 |
for link in links.items():
|
147 |
pui = link[0]
|
148 |
linklist2 += pui + " , "
|
149 |
a=1
|
|
|
150 |
sections = page.sections
|
|
|
151 |
ex_dic = {
|
152 |
'Entity' : ["URL","Title","Summary", "Text", "Backlinks", "Links", "Categories"],
|
153 |
'Value': [pageurl, pagetitle, pagesummary, pagetext, linklist,linklist2, categorylist ]
|
154 |
}
|
|
|
155 |
df = pd.DataFrame(ex_dic)
|
156 |
-
return df
|
157 |
|
|
|
|
|
158 |
def search(question):
|
159 |
concepts = get_concepts(question)
|
160 |
print("concepts: ",concepts)
|
@@ -171,12 +181,12 @@ def search(question):
|
|
171 |
prob, index = faiss_index.search(query_embeddings.pooler_output, k=lendicts)
|
172 |
return pd.DataFrame([dicts[i] for i in index[0]])
|
173 |
|
174 |
-
# AI UI SOTA -
|
175 |
with gr.Blocks() as demo: # Block documentation on event listeners, start here: https://gradio.app/blocks_and_event_listeners/
|
176 |
gr.Markdown("<h1><center>🍰 Ultimate Wikipedia AI 🎨</center></h1>")
|
177 |
gr.Markdown("""<div align="center">Search and Find Anything Then Use in AI! <a href="https://www.mediawiki.org/wiki/API:Main_page">MediaWiki - API for Wikipedia</a>. <a href="https://paperswithcode.com/datasets?q=wikipedia&v=lst&o=newest">Papers,Code,Datasets for SOTA w/ Wikipedia</a>""")
|
178 |
with gr.Row(): # inputs and buttons
|
179 |
-
inp = gr.Textbox(lines=1, default="
|
180 |
with gr.Row(): # inputs and buttons
|
181 |
b3 = gr.Button("Search AI Summaries")
|
182 |
b4 = gr.Button("Search Web Live")
|
|
|
13 |
except:
|
14 |
spacy.cli.download("en_core_web_sm")
|
15 |
nlp = spacy.load("en_core_web_sm")
|
|
|
16 |
|
17 |
+
wh_words = ['what', 'who', 'how', 'when', 'which']
|
18 |
def get_concepts(text):
|
19 |
text = text.lower()
|
20 |
doc = nlp(text)
|
|
|
38 |
passage = sen.text
|
39 |
passage_len = len(sen)
|
40 |
continue
|
41 |
+
|
42 |
elif i==(len(sents)-1):
|
43 |
passage+=" "+sen.text
|
44 |
passages.append(passage)
|
45 |
passage = ""
|
46 |
passage_len = 0
|
47 |
continue
|
48 |
+
|
49 |
passage+=" "+sen.text
|
50 |
return passages
|
51 |
|
|
|
58 |
try:
|
59 |
html_page = wikipedia.page(title = wiki, auto_suggest = False)
|
60 |
except DisambiguationError:
|
61 |
+
continue
|
62 |
+
|
63 |
htmlResults=html_page.content
|
64 |
+
|
65 |
passages = get_passages(htmlResults, k=k)
|
66 |
for passage in passages:
|
67 |
i_dicts = {}
|
|
|
90 |
max_length=max_length,
|
91 |
return_token_type_ids=True
|
92 |
)
|
93 |
+
passage_embeddings = passage_encoder.predict([np.array(passage_inputs['input_ids']),
|
94 |
+
np.array(passage_inputs['attention_mask']),
|
95 |
+
np.array(passage_inputs['token_type_ids'])],
|
96 |
+
batch_size=64,
|
97 |
+
verbose=1)
|
|
|
|
|
98 |
return passage_embeddings
|
99 |
|
100 |
def extracted_query_embeddings(queries, max_length=64):
|
|
|
106 |
max_length=max_length,
|
107 |
return_token_type_ids=True
|
108 |
)
|
109 |
+
query_embeddings = query_encoder.predict([np.array(query_inputs['input_ids']),
|
110 |
+
np.array(query_inputs['attention_mask']),
|
111 |
+
np.array(query_inputs['token_type_ids'])],
|
112 |
+
batch_size=1,
|
113 |
+
verbose=1)
|
|
|
|
|
114 |
return query_embeddings
|
115 |
|
116 |
+
#Wikipedia API:
|
117 |
|
118 |
def get_pagetext(page):
|
119 |
s=str(page).replace("/t","")
|
120 |
+
|
121 |
return s
|
122 |
|
123 |
def get_wiki_summary(search):
|
124 |
wiki_wiki = wikipediaapi.Wikipedia('en')
|
125 |
page = wiki_wiki.page(search)
|
126 |
+
|
127 |
isExist = page.exists()
|
128 |
if not isExist:
|
129 |
return isExist, "Not found", "Not found", "Not found", "Not found"
|
130 |
+
|
131 |
pageurl = page.fullurl
|
132 |
pagetitle = page.title
|
133 |
pagesummary = page.summary[0:60]
|
134 |
pagetext = get_pagetext(page.text)
|
135 |
+
|
136 |
backlinks = page.backlinks
|
137 |
linklist = ""
|
138 |
for link in backlinks.items():
|
139 |
pui = link[0]
|
140 |
linklist += pui + " , "
|
141 |
a=1
|
142 |
+
|
143 |
categories = page.categories
|
144 |
categorylist = ""
|
145 |
for category in categories.items():
|
146 |
pui = category[0]
|
147 |
categorylist += pui + " , "
|
148 |
a=1
|
149 |
+
|
150 |
links = page.links
|
151 |
linklist2 = ""
|
152 |
for link in links.items():
|
153 |
pui = link[0]
|
154 |
linklist2 += pui + " , "
|
155 |
a=1
|
156 |
+
|
157 |
sections = page.sections
|
158 |
+
|
159 |
ex_dic = {
|
160 |
'Entity' : ["URL","Title","Summary", "Text", "Backlinks", "Links", "Categories"],
|
161 |
'Value': [pageurl, pagetitle, pagesummary, pagetext, linklist,linklist2, categorylist ]
|
162 |
}
|
163 |
+
|
164 |
df = pd.DataFrame(ex_dic)
|
|
|
165 |
|
166 |
+
return df
|
167 |
+
|
168 |
def search(question):
|
169 |
concepts = get_concepts(question)
|
170 |
print("concepts: ",concepts)
|
|
|
181 |
prob, index = faiss_index.search(query_embeddings.pooler_output, k=lendicts)
|
182 |
return pd.DataFrame([dicts[i] for i in index[0]])
|
183 |
|
184 |
+
# AI UI SOTA - Gradio blocks with UI formatting, and event driven UI
|
185 |
with gr.Blocks() as demo: # Block documentation on event listeners, start here: https://gradio.app/blocks_and_event_listeners/
|
186 |
gr.Markdown("<h1><center>🍰 Ultimate Wikipedia AI 🎨</center></h1>")
|
187 |
gr.Markdown("""<div align="center">Search and Find Anything Then Use in AI! <a href="https://www.mediawiki.org/wiki/API:Main_page">MediaWiki - API for Wikipedia</a>. <a href="https://paperswithcode.com/datasets?q=wikipedia&v=lst&o=newest">Papers,Code,Datasets for SOTA w/ Wikipedia</a>""")
|
188 |
with gr.Row(): # inputs and buttons
|
189 |
+
inp = gr.Textbox(lines=1, default="Syd Mead", label="Question")
|
190 |
with gr.Row(): # inputs and buttons
|
191 |
b3 = gr.Button("Search AI Summaries")
|
192 |
b4 = gr.Button("Search Web Live")
|