File size: 1,720 Bytes
d0de866
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from src.wikicontext import WikiContext
import wikipedia


def main():
    st.sidebar.title('WikiContext')
    algorithm = st.sidebar.selectbox("Algorithm that you want to use.", ["TextRank"], 
        index=0)

    max_prereqs = st.sidebar.slider("How many prerequisites do you want to see?", min_value=2, 
        max_value=10)

    if algorithm == "TextRank":
        run_the_app(algorithm, max_prereqs=max_prereqs)


def run_the_app(algorithm, params=None, max_prereqs=5):
    if params is None:
        params = {}

    query = st.text_input(label="The topic you want summarized.", value="")
    results = []
    if query:
        results = wikipedia.search(query)
        subject = st.selectbox(label="Choose from disambiguation.", options=results, index=0)

        if subject and algorithm:
            wc = WikiContext(subject, algorithm, params, max_prereqs=max_prereqs)
            st.title(subject)
            with st.spinner(text="Generating summary..."):
                st.markdown(get_main_summary(wc))
            with st.spinner(text="Generating prerequisites..."):
                prereqs = get_prereq_summary(wc)
            st.header("Prerequisites")
            for subhead in prereqs:
                st.subheader(subhead)
                st.write(prereqs[subhead])


@st.cache(show_spinner=False)
def get_main_summary(wikicontext):
    if not wikicontext.content:
        wikicontext.get_main_content()
    return wikicontext.get_main_summary()


@st.cache(show_spinner=False)
def get_prereq_summary(wikicontext):
    if not wikicontext.prereq:
        wikicontext.get_prereqs_content()
    return wikicontext.get_prereqs_summary()


if __name__ == "__main__":
    main()