File size: 1,808 Bytes
79c2d9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Application that queries Wikipedia API and summarizes the top result.
"""

import os
import urllib.parse

import requests
import streamlit as st

from txtai.pipeline import Summary


class Application:
    """
    Main application.
    """

    SEARCH_TEMPLATE = "https://en.wikipedia.org/w/api.php?action=opensearch&search=%s&limit=1&namespace=0&format=json"
    CONTENT_TEMPLATE = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=%s"

    def __init__(self):
        """
        Creates a new application.
        """

        self.summary = Summary("sshleifer/distilbart-cnn-12-6")

    def run(self):
        """
        Runs a Streamlit application.
        """

        st.title("Wikipedia")
        st.markdown("This application queries the Wikipedia API and summarizes the top result.")

        query = st.text_input("Query")

        if query:
            query = urllib.parse.quote_plus(query)
            data = requests.get(Application.SEARCH_TEMPLATE % query).json()
            if data and data[1]:
                page = urllib.parse.quote_plus(data[1][0])
                content = requests.get(Application.CONTENT_TEMPLATE % page).json()
                content = list(content["query"]["pages"].values())[0]["extract"]

                st.write(self.summary(content))
                st.markdown("*Source: " + data[3][0] + "*")
            else:
                st.markdown("*No results found*")


@st.cache(allow_output_mutation=True)
def create():
    """
    Creates and caches a Streamlit application.

    Returns:
        Application
    """

    return Application()


if __name__ == "__main__":
    os.environ["TOKENIZERS_PARALLELISM"] = "false"

    # Create and run application
    app = create()
    app.run()