mattupson commited on
Commit
169138c
Β·
unverified Β·
1 Parent(s): 4ec7fd7

new: Add sidebar and explanation

Browse files
Files changed (1) hide show
  1. app.py +39 -15
app.py CHANGED
@@ -18,31 +18,55 @@ HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; borde
18
 
19
  def render_entities(doc, colors: dict, options: dict) -> str:
20
  """
21
- Takes a SpaCy doc
22
  """
23
 
24
- #if isinstance(doc, spacy.tokens.doc.Doc):
25
- # doc = doc.to_json()
26
-
27
  html = spacy.displacy.render(doc, style="ent", options=options)
28
  html = html.replace("\n", " ")
29
 
30
  return html
31
 
32
 
33
- st.header("Location Entity Recognition Demo πŸ”ŽπŸŒ†πŸŒ")
 
 
 
 
 
34
 
35
- st.subheader("Look for Locations")
 
 
 
36
 
37
- if st.button("Show new example", key="text"):
38
- sample = random.choice(grants)
39
- doc = nlp(sample["text"])
40
  html = render_entities(doc, colors, options)
41
- text = st.text_area("Text input", value=sample["text"], height=200)
42
  st.write(HTML_WRAPPER.format(html), unsafe_allow_html=True)
43
- else:
 
 
 
 
44
  sample = random.choice(grants)
45
- doc = nlp(sample["text"])
46
- html = render_entities(doc, colors, options)
47
- text = st.text_area("Text input", value=sample["text"], height=200, help="Enter text here and click the 'Find Locations' button to search for entities.")
48
- st.write(HTML_WRAPPER.format(html), unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  def render_entities(doc, colors: dict, options: dict) -> str:
20
  """
21
+ Takes a SpaCy doc and renders the entities with the given colors.
22
  """
23
 
 
 
 
24
  html = spacy.displacy.render(doc, style="ent", options=options)
25
  html = html.replace("\n", " ")
26
 
27
  return html
28
 
29
 
30
+ st.sidebar.header("Location Recognition Demo πŸ”ŽπŸŒ†πŸŒ")
31
+ st.sidebar.markdown(
32
+ """
33
+ This example application accompanies the blog post: [Extracting useful information from documents with Named Entity Recognition]().
34
+ It uses a pre-trained Named Entity Recognition (NER) model from the [spaCy](https://spacy.io/) library to extract locations from your own examples, or a sample of grant applications from The Wellcome Trust.
35
+ The application will extract the following types of location entity:
36
 
37
+ * __GPE__: Geopolitical entities (countries, cities, states)
38
+ * __LOC__: Locations (mountains, rivers, lakes)
39
+ """
40
+ )
41
 
42
+
43
+ def show_example(text):
 
44
  html = render_entities(doc, colors, options)
 
45
  st.write(HTML_WRAPPER.format(html), unsafe_allow_html=True)
46
+
47
+ return text
48
+
49
+
50
+ if st.button("Show Wellcome example", key="text"):
51
  sample = random.choice(grants)
52
+ text = st.text_area(
53
+ "Add your own text or click the button to see a Wellcome example",
54
+ value=sample["text"],
55
+ height=200,
56
+ help="Enter your own text and press CTRL + ENTER to search for entities",
57
+ )
58
+ doc = nlp(text)
59
+ show_example(doc.text)
60
+ else:
61
+ text = st.text_area(
62
+ "Add your own text or click the button to see a Wellcome example",
63
+ value="Enter your text here",
64
+ height=200,
65
+ help="Enter your own text and press CTRL + ENTER to search for entities",
66
+ )
67
+ doc = nlp(text)
68
+ show_example(doc.text)
69
+
70
+ st.markdown(
71
+ "Examples from The Wellcome Trust are taken from data that are publishes openly at [360 Giving](https://data.threesixtygiving.org/). They are published under a [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) license."
72
+ )