wiki.py
1 """ 2 Application that queries Wikipedia API and summarizes the top result. 3 4 Requires streamlit to be installed. 5 pip install streamlit 6 """ 7 8 import os 9 import urllib.parse 10 11 import requests 12 import streamlit as st 13 14 from txtai.pipeline import Summary 15 16 17 class Application: 18 """ 19 Main application. 20 """ 21 22 SEARCH_TEMPLATE = "https://en.wikipedia.org/w/api.php?action=opensearch&search=%s&limit=1&namespace=0&format=json" 23 CONTENT_TEMPLATE = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=%s" 24 25 def __init__(self): 26 """ 27 Creates a new application. 28 """ 29 30 self.summary = Summary("sshleifer/distilbart-cnn-12-6") 31 32 def run(self): 33 """ 34 Runs a Streamlit application. 35 """ 36 37 st.title("Wikipedia") 38 st.markdown("This application queries the Wikipedia API and summarizes the top result.") 39 40 query = st.text_input("Query") 41 42 if query: 43 query = urllib.parse.quote_plus(query) 44 data = requests.get(Application.SEARCH_TEMPLATE % query).json() 45 if data and data[1]: 46 page = urllib.parse.quote_plus(data[1][0]) 47 content = requests.get(Application.CONTENT_TEMPLATE % page).json() 48 content = list(content["query"]["pages"].values())[0]["extract"] 49 50 st.write(self.summary(content)) 51 st.markdown("*Source: " + data[3][0] + "*") 52 else: 53 st.markdown("*No results found*") 54 55 56 @st.cache(allow_output_mutation=True) 57 def create(): 58 """ 59 Creates and caches a Streamlit application. 60 61 Returns: 62 Application 63 """ 64 65 return Application() 66 67 68 if __name__ == "__main__": 69 os.environ["TOKENIZERS_PARALLELISM"] = "false" 70 71 # Create and run application 72 app = create() 73 app.run()