khaerens commited on
Commit
f3c6abd
1 Parent(s): 456234e

add free text and code clean up

Browse files
__pycache__/app.cpython-38.pyc CHANGED
Binary files a/__pycache__/app.cpython-38.pyc and b/__pycache__/app.cpython-38.pyc differ
 
__pycache__/rebel.cpython-38.pyc CHANGED
Binary files a/__pycache__/rebel.cpython-38.pyc and b/__pycache__/rebel.cpython-38.pyc differ
 
__pycache__/utils.cpython-38.pyc ADDED
Binary file (314 Bytes). View file
 
app.py CHANGED
@@ -9,10 +9,11 @@ from streamlit.state.session_state import SessionState
9
  from streamlit.type_util import Key
10
  import rebel
11
  import wikipedia
 
12
 
13
- network_filename = "test.html"
14
 
15
- state_variables = {
16
  'has_run':False,
17
  'wiki_suggestions': [],
18
  'wiki_text' : [],
@@ -20,15 +21,16 @@ state_variables = {
20
  "topics":[]
21
  }
22
 
23
- for k, v in state_variables.items():
24
- if k not in st.session_state:
25
- st.session_state[k] = v
26
-
27
- def clip_text(t, lenght = 10):
28
- return ".".join(t.split(".")[:lenght]) + "."
29
 
 
 
 
 
30
 
31
- def generate_graph():
32
  if 'wiki_text' not in st.session_state:
33
  return
34
  if len(st.session_state['wiki_text']) == 0:
@@ -37,7 +39,7 @@ def generate_graph():
37
  with st.spinner(text="Generating graph..."):
38
  texts = st.session_state['wiki_text']
39
  st.session_state['nodes'] = []
40
- nodes = rebel.generate_knowledge_graph(texts, network_filename)
41
  print("gen_graph", nodes)
42
  for n in nodes:
43
  n = n.lower()
@@ -46,7 +48,7 @@ def generate_graph():
46
  st.session_state['has_run'] = True
47
  st.success('Done!')
48
 
49
- def show_suggestion():
50
  st.session_state['wiki_suggestions'] = []
51
  with st.spinner(text="fetching wiki topics..."):
52
  if st.session_state['input_method'] == "wikipedia":
@@ -56,7 +58,7 @@ def show_suggestion():
56
  for subj in subjects:
57
  st.session_state['wiki_suggestions'] += wikipedia.search(subj, results = 3)
58
 
59
- def show_wiki_text(page_title):
60
  with st.spinner(text="fetching wiki page..."):
61
  try:
62
  page = wikipedia.page(title=page_title, auto_suggest=False)
@@ -68,23 +70,41 @@ def show_wiki_text(page_title):
68
  temp = st.session_state['wiki_suggestions'] + e.options[:3]
69
  st.session_state['wiki_suggestions'] = list(set(temp))
70
 
71
- def add_text(term):
72
  try:
73
- extra_text = clip_text(wikipedia.page(title=term, auto_suggest=True).summary)
74
  st.session_state['wiki_text'].append(extra_text)
75
  st.session_state['topics'].append(term.lower())
 
 
 
 
 
76
  except wikipedia.WikipediaException:
77
- st.error("Woops, no wikipedia page for this node")
78
- st.session_state["nodes"].remove(term)
79
 
80
- def reset_session():
81
- for k in state_variables:
82
  del st.session_state[k]
83
 
 
 
 
 
 
 
 
 
 
 
84
  st.title('REBELious knowledge graph generation')
85
- st.session_state['input_method'] = "wikipedia"
 
 
 
86
 
87
- st.sidebar.markdown(
 
88
  """
89
  # how to
90
  - Enter wikipedia search terms, separated by comma's
@@ -92,76 +112,86 @@ st.sidebar.markdown(
92
  - Click generate!
93
  """
94
  )
95
-
96
- st.sidebar.button("Reset", on_click=reset_session, key="reset_key")
97
-
98
- # st.selectbox(
99
- # 'input method',
100
- # ('wikipedia', 'free text'), key="input_method")
101
-
102
- if st.session_state['input_method'] != "wikipedia":
103
- # st.text_area("Your text", key="text")
104
- pass
105
- else:
106
  cols = st.columns([8, 1])
107
  with cols[0]:
108
- st.text_input("wikipedia search term", on_change=show_suggestion, key="text")
109
  with cols[1]:
110
  st.text('')
111
  st.text('')
112
- st.button("Search", on_click=show_suggestion, key="show_suggestion_key")
113
-
114
- if len(st.session_state['wiki_suggestions']) != 0:
115
- num_buttons = len(st.session_state['wiki_suggestions'])
116
- num_cols = num_buttons if num_buttons < 8 else 8
117
- columns = st.columns([1] * num_cols )
118
- for q in range(1 + num_buttons//num_cols):
119
- for i, (c, s) in enumerate(zip(columns, st.session_state['wiki_suggestions'][q*num_cols: (q+1)*num_cols])):
120
- with c:
121
- st.button(s, on_click=show_wiki_text, args=(s,), key=str(i)+s)
122
-
123
- if len(st.session_state['wiki_text']) != 0:
124
- for i, t in enumerate(st.session_state['wiki_text']):
125
- new_expander = st.expander(label=t[:30] + "...", expanded=(i==0))
126
- with new_expander:
127
- st.markdown(t)
128
-
129
- if st.session_state['input_method'] != "wikipedia":
130
- # st.button("find wiki pages")
131
- # if "wiki_suggestions" in st.session_state:
132
- # st.button("generate", on_click=generate_graph, key="gen_graph")
133
- pass
134
- else:
135
- if len(st.session_state['wiki_text']) > 0:
136
- st.button("Generate", on_click=generate_graph, key="gen_graph")
137
-
138
 
139
- if st.session_state['has_run']:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  st.sidebar.markdown(
141
- """
142
- # How to expand the graph
143
- - Click a button on the right to expand that node
144
- - Only nodes that have wiki pages will be expanded
145
- - Hit the Generate button again to expand your graph!
146
  """
147
  )
148
-
149
- HtmlFile = open(network_filename, 'r', encoding='utf-8')
150
- source_code = HtmlFile.read()
151
- components.html(source_code, width=720, height=600)
152
-
153
- num_buttons = len(st.session_state["nodes"])
154
- num_cols = num_buttons if num_buttons < 7 else 7
155
- columns = st.columns([1] * num_cols + [1])
156
- print(st.session_state["nodes"])
157
-
158
- for q in range(1 + num_buttons//num_cols):
159
- for i, (c, s) in enumerate(zip(columns, st.session_state["nodes"][q*num_cols: (q+1)*num_cols])):
160
- with c:
161
- st.button(s, on_click=add_text, args=(s,), key=str(i)+s)
162
-
163
-
164
-
165
-
 
 
 
 
166
 
167
 
 
9
  from streamlit.type_util import Key
10
  import rebel
11
  import wikipedia
12
+ from utils import clip_text
13
 
14
+ GRAPH_FILENAME = "test.html"
15
 
16
+ wiki_state_variables = {
17
  'has_run':False,
18
  'wiki_suggestions': [],
19
  'wiki_text' : [],
 
21
  "topics":[]
22
  }
23
 
24
+ free_text_state_variables = {
25
+ 'has_run':False,
26
+ }
 
 
 
27
 
28
+ def wiki_init_state_variables():
29
+ for k, v in wiki_state_variables.items():
30
+ if k not in st.session_state:
31
+ st.session_state[k] = v
32
 
33
+ def wiki_generate_graph():
34
  if 'wiki_text' not in st.session_state:
35
  return
36
  if len(st.session_state['wiki_text']) == 0:
 
39
  with st.spinner(text="Generating graph..."):
40
  texts = st.session_state['wiki_text']
41
  st.session_state['nodes'] = []
42
+ nodes = rebel.generate_knowledge_graph(texts, GRAPH_FILENAME)
43
  print("gen_graph", nodes)
44
  for n in nodes:
45
  n = n.lower()
 
48
  st.session_state['has_run'] = True
49
  st.success('Done!')
50
 
51
+ def wiki_show_suggestion():
52
  st.session_state['wiki_suggestions'] = []
53
  with st.spinner(text="fetching wiki topics..."):
54
  if st.session_state['input_method'] == "wikipedia":
 
58
  for subj in subjects:
59
  st.session_state['wiki_suggestions'] += wikipedia.search(subj, results = 3)
60
 
61
+ def wiki_show_text(page_title):
62
  with st.spinner(text="fetching wiki page..."):
63
  try:
64
  page = wikipedia.page(title=page_title, auto_suggest=False)
 
70
  temp = st.session_state['wiki_suggestions'] + e.options[:3]
71
  st.session_state['wiki_suggestions'] = list(set(temp))
72
 
73
+ def wiki_add_text(term):
74
  try:
75
+ extra_text = clip_text(wikipedia.page(title=term, auto_suggest=False).summary)
76
  st.session_state['wiki_text'].append(extra_text)
77
  st.session_state['topics'].append(term.lower())
78
+ except wikipedia.DisambiguationError as e:
79
+ with st.spinner(text="Woops, ambigious term, recalculating options..."):
80
+ st.session_state['nodes'].remove(term)
81
+ temp = st.session_state['nodes'] + e.options[:3]
82
+ st.session_state['node'] = list(set(temp))
83
  except wikipedia.WikipediaException:
84
+ st.session_state['nodes'].remove(term)
 
85
 
86
+ def wiki_reset_session():
87
+ for k in wiki_state_variables:
88
  del st.session_state[k]
89
 
90
+ def free_text_generate():
91
+ text = st.session_state['free_text']
92
+ rebel.generate_knowledge_graph([text], GRAPH_FILENAME)
93
+ st.session_state['has_run'] = True
94
+
95
+ def free_text_layout():
96
+ st.text_input("Free text", key="free_text")
97
+ st.button("Generate", on_click=free_text_generate, key="free_text_generate")
98
+
99
+
100
  st.title('REBELious knowledge graph generation')
101
+ st.selectbox(
102
+ 'input method',
103
+ ('wikipedia', 'free text'), key="input_method")
104
+
105
 
106
+ def show_wiki_hub_page():
107
+ st.sidebar.markdown(
108
  """
109
  # how to
110
  - Enter wikipedia search terms, separated by comma's
 
112
  - Click generate!
113
  """
114
  )
115
+ st.sidebar.button("Reset", on_click=wiki_reset_session, key="reset_key")
 
 
 
 
 
 
 
 
 
 
116
  cols = st.columns([8, 1])
117
  with cols[0]:
118
+ st.text_input("wikipedia search term", on_change=wiki_show_suggestion, key="text")
119
  with cols[1]:
120
  st.text('')
121
  st.text('')
122
+ st.button("Search", on_click=wiki_show_suggestion, key="show_suggestion_key")
123
+
124
+ if len(st.session_state['wiki_suggestions']) != 0:
125
+ num_buttons = len(st.session_state['wiki_suggestions'])
126
+ num_cols = num_buttons if num_buttons < 8 else 8
127
+ columns = st.columns([1] * num_cols )
128
+ for q in range(1 + num_buttons//num_cols):
129
+ for i, (c, s) in enumerate(zip(columns, st.session_state['wiki_suggestions'][q*num_cols: (q+1)*num_cols])):
130
+ with c:
131
+ st.button(s, on_click=wiki_show_text, args=(s,), key=str(i)+s)
132
+
133
+ if len(st.session_state['wiki_text']) != 0:
134
+ for i, t in enumerate(st.session_state['wiki_text']):
135
+ new_expander = st.expander(label=t[:30] + "...", expanded=(i==0))
136
+ with new_expander:
137
+ st.markdown(t)
 
 
 
 
 
 
 
 
 
 
138
 
139
+ if len(st.session_state['wiki_text']) > 0:
140
+ st.button("Generate", on_click=wiki_generate_graph, key="gen_graph")
141
+
142
+ if st.session_state['has_run']:
143
+ st.sidebar.markdown(
144
+ """
145
+ # How to expand the graph
146
+ - Click a button on the right to expand that node
147
+ - Only nodes that have wiki pages will be expanded
148
+ - Hit the Generate button again to expand your graph!
149
+ """
150
+ )
151
+
152
+ HtmlFile = open(GRAPH_FILENAME, 'r', encoding='utf-8')
153
+ source_code = HtmlFile.read()
154
+ components.html(source_code, width=720, height=600)
155
+
156
+ num_buttons = len(st.session_state["nodes"])
157
+ num_cols = num_buttons if num_buttons < 7 else 7
158
+ columns = st.columns([1] * num_cols + [1])
159
+ print(st.session_state["nodes"])
160
+
161
+ for q in range(1 + num_buttons//num_cols):
162
+ for i, (c, s) in enumerate(zip(columns, st.session_state["nodes"][q*num_cols: (q+1)*num_cols])):
163
+ with c:
164
+ st.button(s, on_click=wiki_add_text, args=(s,), key=str(i)+s)
165
+
166
+ def show_free_text_hub_page():
167
  st.sidebar.markdown(
168
+ """
169
+ # How to
170
+ - Enter a text you'd like to see as a graph.
171
+ - Click generate!
 
172
  """
173
  )
174
+ st.sidebar.button("Reset", key="reset_key")
175
+ free_text_layout()
176
+ if st.session_state['has_run']:
177
+ st.sidebar.markdown(
178
+ """
179
+ # How to expand the graph
180
+ - Click a button on the right to expand that node
181
+ - Only nodes that have wiki pages will be expanded
182
+ - Hit the Generate button again to expand your graph!
183
+ """
184
+ )
185
+
186
+ HtmlFile = open(GRAPH_FILENAME, 'r', encoding='utf-8')
187
+ source_code = HtmlFile.read()
188
+ components.html(source_code, width=720, height=600)
189
+
190
+
191
+ if st.session_state['input_method'] == "wikipedia":
192
+ wiki_init_state_variables()
193
+ show_wiki_hub_page()
194
+ else:
195
+ show_free_text_hub_page()
196
 
197
 
rebel.py CHANGED
@@ -2,7 +2,6 @@ from typing import List
2
  from transformers import pipeline
3
  from pyvis.network import Network
4
  from functools import lru_cache
5
- from app import generate_graph
6
  import spacy
7
  from spacy import displacy
8
 
 
2
  from transformers import pipeline
3
  from pyvis.network import Network
4
  from functools import lru_cache
 
5
  import spacy
6
  from spacy import displacy
7
 
utils.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ def clip_text(t, lenght = 10):
2
+ return ".".join(t.split(".")[:lenght]) + "."