File size: 5,278 Bytes
70303d6
 
390b2d8
70303d6
 
 
 
 
 
 
 
 
 
 
 
 
ba0e651
70303d6
 
 
 
 
 
 
 
ba0e651
70303d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba0e651
70303d6
 
 
 
ba0e651
 
 
70303d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba0e651
 
70303d6
 
 
 
 
 
 
 
 
ba0e651
 
 
 
 
 
 
 
 
 
 
70303d6
 
 
 
 
b541492
 
 
ba0e651
 
 
 
 
 
 
70303d6
 
ba0e651
 
904febf
ba0e651
 
 
 
 
 
 
70303d6
 
376bc0c
 
70303d6
 
 
 
fb03159
 
 
 
 
ba0e651
 
70303d6
 
 
ba0e651
 
 
 
 
 
 
 
 
 
70303d6
 
 
376bc0c
70303d6
1dff574
4ba755d
70303d6
 
 
 
 
390b2d8
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
from logging import disable
from pkg_resources import EggMetadata
import streamlit as st
import streamlit.components.v1 as components
import networkx as nx
import matplotlib.pyplot as plt
from pyvis.network import Network
from streamlit.state.session_state import SessionState
from streamlit.type_util import Key
import rebel
import wikipedia

network_filename = "test.html"

state_variables = {
    'has_run':False,
    'wiki_suggestions': [],
    'wiki_text' : [],
    'nodes':[]
}

for k, v in state_variables.items():
    if k not in st.session_state:
        st.session_state[k] = v

def clip_text(t, lenght = 10):
    return ".".join(t.split(".")[:lenght]) + "."


def generate_graph():
    if 'wiki_text' not in st.session_state:
        return
    if len(st.session_state['wiki_text']) == 0:
        st.error("please enter a topic and select a wiki page first")
        return
    with st.spinner(text="Generating graph..."):
        texts = st.session_state['wiki_text']
        nodes = rebel.generate_knowledge_graph(texts, network_filename)
        st.session_state['nodes'] = nodes
        st.session_state['has_run'] = True
    st.success('Done!')

def show_suggestion():
    st.session_state['wiki_suggestions'] = []
    with st.spinner(text="fetching wiki topics..."):
        if st.session_state['input_method'] == "wikipedia":
            text = st.session_state.text
            if text is not None:
                subjects = text.split(",")
                for subj in subjects:
                    st.session_state['wiki_suggestions'] += wikipedia.search(subj, results = 3)

def show_wiki_text(page_title):
    with st.spinner(text="fetching wiki page..."):
        try:
            page = wikipedia.page(title=page_title, auto_suggest=False)
            st.session_state['wiki_text'].append(clip_text(page.summary))
        except wikipedia.DisambiguationError as e:
            with st.spinner(text="Woops, ambigious term, recalculating options..."):
                st.session_state['wiki_suggestions'].remove(page_title)
                temp = st.session_state['wiki_suggestions'] + e.options[:3]
                st.session_state['wiki_suggestions'] = list(set(temp))

def add_text(term):
    try:
        extra_text = clip_text(wikipedia.page(title=term, auto_suggest=True).summary)
        st.session_state['wiki_text'].append(extra_text)
    except wikipedia.WikipediaException:
        st.error("Woops, no wikipedia page for this node")
        st.session_state["nodes"].remove(term)

def reset_session():
    for k in state_variables:
        del st.session_state[k]

st.title('REBELious knowledge graph generation')
st.session_state['input_method'] = "wikipedia"

st.sidebar.markdown(
"""

# how to

- Enter wikipedia search terms, separated by comma's

- Choose one or more of the suggested pages

- Click generate!

"""
)

st.sidebar.button("Reset", on_click=reset_session, key="reset_key")

# st.selectbox(
#      'input method',
#      ('wikipedia', 'free text'),  key="input_method")

if st.session_state['input_method'] != "wikipedia":
    # st.text_area("Your text", key="text")
    pass
else:
    cols = st.columns([8, 1])
    with cols[0]:
        st.text_input("wikipedia search term", on_change=show_suggestion, key="text")
    with cols[1]:
        st.text('')
        st.text('')
        st.button("Search", on_click=show_suggestion, key="show_suggestion_key")

if len(st.session_state['wiki_suggestions']) != 0:

    num_buttons = len(st.session_state['wiki_suggestions'])
    num_cols = num_buttons if num_buttoms < 7 else 7
    columns = st.columns([1] * num_cols + [1])
    print(st.session_state['wiki_suggestions'])

    for q in range(1 + num_buttons//num_cols):
        for i, (c, s) in enumerate(zip(columns, st.session_state['wiki_suggestions'][q*num_cols: (q+1)*num_cols])):
            with c:
                st.button(s, on_click=show_wiki_text, args=(s,), key=str(i)+s)

if len(st.session_state['wiki_text']) != 0:
    for i, t in enumerate(st.session_state['wiki_text']):
        new_expander = st.expander(label=t[:30] + "...", expanded=(i==0))
        with new_expander:
            st.markdown(t)

if st.session_state['input_method'] != "wikipedia":
    # st.button("find wiki pages")
    # if "wiki_suggestions" in st.session_state:
    #         st.button("generate", on_click=generate_graph, key="gen_graph")
    pass
else:
    if len(st.session_state['wiki_text']) > 0:
        st.button("Generate", on_click=generate_graph, key="gen_graph")


if st.session_state['has_run']:
    st.sidebar.markdown(
    """

# How to expand the graph

- Click a button on the right to expand that node

- Only nodes that have wiki pages will be expanded

- Hit the Generate button again to expand your graph!

"""
)

    cols = st.columns([5, 1])
    with cols[0]:
        HtmlFile = open(network_filename, 'r', encoding='utf-8')
        source_code = HtmlFile.read()
        components.html(source_code, height=2000,width=2000)
    with cols[1]:
        for i,s in enumerate(st.session_state["nodes"]):
            st.button(s, on_click=add_text, args=(s,), key=s+str(i))