Spaces:
Sleeping
Sleeping
miesnerjacob
commited on
Commit
β’
2644c54
1
Parent(s):
b4401d7
removed extra annotation files
Browse files- app.py +6 -6
- text_annotation.py +0 -51
- text_annotation_utils.py +0 -127
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import pandas as pd
|
2 |
import streamlit as st
|
3 |
-
from
|
4 |
from streamlit_option_menu import option_menu
|
5 |
from sentiment_analysis import SentimentAnalysis
|
6 |
from keyword_extraction import KeywordExtractor
|
@@ -134,7 +134,7 @@ elif page == "Sentiment Analysis":
|
|
134 |
|
135 |
text = st.text_area("Paste text here", value="")
|
136 |
|
137 |
-
if st.button('
|
138 |
with st.spinner("Loading..."):
|
139 |
preds, html = sentiment_analyzer.run(text)
|
140 |
st.success('All done!')
|
@@ -160,7 +160,7 @@ elif page == "Keyword Extraction":
|
|
160 |
|
161 |
max_keywords = st.slider('# of Keywords Max Limit', min_value=1, max_value=10, value=5, step=1)
|
162 |
|
163 |
-
if st.button('
|
164 |
with st.spinner("Loading..."):
|
165 |
annotation, keywords = keyword_extractor.generate(text, max_keywords)
|
166 |
st.success('All done!')
|
@@ -191,7 +191,7 @@ elif page == "Part of Speech Tagging":
|
|
191 |
|
192 |
text = st.text_area("Paste text here", value="")
|
193 |
|
194 |
-
if st.button('
|
195 |
with st.spinner("Loading..."):
|
196 |
preds = pos_tagger.classify(text)
|
197 |
st.success('All done!')
|
@@ -213,7 +213,7 @@ elif page == "Emotion Detection":
|
|
213 |
|
214 |
text = st.text_area("Paste text here", value="")
|
215 |
|
216 |
-
if st.button('
|
217 |
with st.spinner("Loading..."):
|
218 |
preds, html = emotion_detector.run(text)
|
219 |
st.success('All done!')
|
@@ -237,7 +237,7 @@ elif page == "Named Entity Recognition":
|
|
237 |
|
238 |
text = st.text_area("Paste text here", value="")
|
239 |
|
240 |
-
if st.button('
|
241 |
with st.spinner("Loading..."):
|
242 |
preds, ner_annotation = ner.classify(text)
|
243 |
st.success('All done!')
|
|
|
1 |
import pandas as pd
|
2 |
import streamlit as st
|
3 |
+
from annotated_text import annotated_text
|
4 |
from streamlit_option_menu import option_menu
|
5 |
from sentiment_analysis import SentimentAnalysis
|
6 |
from keyword_extraction import KeywordExtractor
|
|
|
134 |
|
135 |
text = st.text_area("Paste text here", value="")
|
136 |
|
137 |
+
if st.button('π₯ Run!'):
|
138 |
with st.spinner("Loading..."):
|
139 |
preds, html = sentiment_analyzer.run(text)
|
140 |
st.success('All done!')
|
|
|
160 |
|
161 |
max_keywords = st.slider('# of Keywords Max Limit', min_value=1, max_value=10, value=5, step=1)
|
162 |
|
163 |
+
if st.button('π₯ Run!'):
|
164 |
with st.spinner("Loading..."):
|
165 |
annotation, keywords = keyword_extractor.generate(text, max_keywords)
|
166 |
st.success('All done!')
|
|
|
191 |
|
192 |
text = st.text_area("Paste text here", value="")
|
193 |
|
194 |
+
if st.button('π₯ Run!'):
|
195 |
with st.spinner("Loading..."):
|
196 |
preds = pos_tagger.classify(text)
|
197 |
st.success('All done!')
|
|
|
213 |
|
214 |
text = st.text_area("Paste text here", value="")
|
215 |
|
216 |
+
if st.button('π₯ Run!'):
|
217 |
with st.spinner("Loading..."):
|
218 |
preds, html = emotion_detector.run(text)
|
219 |
st.success('All done!')
|
|
|
237 |
|
238 |
text = st.text_area("Paste text here", value="")
|
239 |
|
240 |
+
if st.button('π₯ Run!'):
|
241 |
with st.spinner("Loading..."):
|
242 |
preds, ner_annotation = ner.classify(text)
|
243 |
st.success('All done!')
|
text_annotation.py
DELETED
@@ -1,51 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from text_annotation_utils import *
|
3 |
-
|
4 |
-
def annotated_text(*args, type=None):
|
5 |
-
"""Writes text with annotations into your Streamlit app.
|
6 |
-
Parameters
|
7 |
-
----------
|
8 |
-
*args : str, tuple or htbuilder.HtmlElement
|
9 |
-
Arguments can be:
|
10 |
-
- strings, to draw the string as-is on the screen.
|
11 |
-
- tuples of the form (main_text, annotation_text, background, color) where
|
12 |
-
background and foreground colors are optional and should be an CSS-valid string such as
|
13 |
-
"#aabbcc" or "rgb(10, 20, 30)"
|
14 |
-
- HtmlElement objects in case you want to customize the annotations further. In particular,
|
15 |
-
you can import the `annotation()` function from this module to easily produce annotations
|
16 |
-
whose CSS you can customize via keyword arguments.
|
17 |
-
Examples
|
18 |
-
--------
|
19 |
-
# >>> annotated_text(
|
20 |
-
# ... "This ",
|
21 |
-
# ... ("is", "verb", "#8ef"),
|
22 |
-
# ... " some ",
|
23 |
-
# ... ("annotated", "adj", "#faa"),
|
24 |
-
# ... ("text", "noun", "#afa"),
|
25 |
-
# ... " for those of ",
|
26 |
-
# ... ("you", "pronoun", "#fea"),
|
27 |
-
# ... " who ",
|
28 |
-
# ... ("like", "verb", "#8ef"),
|
29 |
-
# ... " this sort of ",
|
30 |
-
# ... ("thing", "noun", "#afa"),
|
31 |
-
# ... )
|
32 |
-
# >>> annotated_text(
|
33 |
-
# ... "Hello ",
|
34 |
-
# ... annotation("world!", "noun", color="#8ef", border="1px dashed red"),
|
35 |
-
# ... )
|
36 |
-
"""
|
37 |
-
if type == 'title':
|
38 |
-
st.markdown(
|
39 |
-
'<p class="big-font">' + get_annotated_html(*args)+ '</p>',
|
40 |
-
unsafe_allow_html=True,
|
41 |
-
)
|
42 |
-
if type == 'description':
|
43 |
-
st.markdown(
|
44 |
-
'<p class="medium-font">' + get_annotated_html(*args) + '</p>',
|
45 |
-
unsafe_allow_html=True,
|
46 |
-
)
|
47 |
-
else:
|
48 |
-
st.markdown(
|
49 |
-
get_annotated_html(*args),
|
50 |
-
unsafe_allow_html=True,
|
51 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
text_annotation_utils.py
DELETED
@@ -1,127 +0,0 @@
|
|
1 |
-
import html
|
2 |
-
from htbuilder import H, HtmlElement, styles
|
3 |
-
from htbuilder.units import unit
|
4 |
-
|
5 |
-
# Only works in 3.7+: from htbuilder import div, span
|
6 |
-
div = H.div
|
7 |
-
span = H.span
|
8 |
-
|
9 |
-
# Only works in 3.7+: from htbuilder.units import px, rem, em
|
10 |
-
px = unit.px
|
11 |
-
rem = unit.rem
|
12 |
-
em = unit.em
|
13 |
-
|
14 |
-
# Colors from the Streamlit palette.
|
15 |
-
# These are red-70, orange-70, ..., violet-70, gray-70.
|
16 |
-
PALETTE = [
|
17 |
-
"#ff4b4b",
|
18 |
-
"#ffa421",
|
19 |
-
"#ffe312",
|
20 |
-
"#21c354",
|
21 |
-
"#00d4b1",
|
22 |
-
"#00c0f2",
|
23 |
-
"#1c83e1",
|
24 |
-
"#803df5",
|
25 |
-
"#808495",
|
26 |
-
]
|
27 |
-
|
28 |
-
OPACITIES = [
|
29 |
-
"33", "66",
|
30 |
-
]
|
31 |
-
|
32 |
-
def annotation(body, label="", background=None, color=None, **style):
|
33 |
-
"""Build an HtmlElement span object with the given body and annotation label.
|
34 |
-
The end result will look something like this:
|
35 |
-
[body | label]
|
36 |
-
Parameters
|
37 |
-
----------
|
38 |
-
body : string
|
39 |
-
The string to put in the "body" part of the annotation.
|
40 |
-
label : string
|
41 |
-
The string to put in the "label" part of the annotation.
|
42 |
-
background : string or None
|
43 |
-
The color to use for the background "chip" containing this annotation.
|
44 |
-
If None, will use a random color based on the label.
|
45 |
-
color : string or None
|
46 |
-
The color to use for the body and label text.
|
47 |
-
If None, will use the document's default text color.
|
48 |
-
style : dict
|
49 |
-
Any CSS you want to apply to the containing "chip". This is useful for things like
|
50 |
-
Examples
|
51 |
-
--------
|
52 |
-
Produce a simple annotation with default colors:
|
53 |
-
# >>> annotation("apple", "fruit")
|
54 |
-
Produce an annotation with custom colors:
|
55 |
-
# >>> annotation("apple", "fruit", background="#FF0", color="black")
|
56 |
-
Produce an annotation with crazy CSS:
|
57 |
-
# >>> annotation("apple", "fruit", background="#FF0", border="1px dashed red")
|
58 |
-
"""
|
59 |
-
|
60 |
-
color_style = {}
|
61 |
-
|
62 |
-
if color:
|
63 |
-
color_style['color'] = color
|
64 |
-
|
65 |
-
if not background:
|
66 |
-
label_sum = sum(ord(c) for c in label)
|
67 |
-
background_color = PALETTE[label_sum % len(PALETTE)]
|
68 |
-
background_opacity = OPACITIES[label_sum % len(OPACITIES)]
|
69 |
-
background = background_color + background_opacity
|
70 |
-
|
71 |
-
return (
|
72 |
-
span(
|
73 |
-
style=styles(
|
74 |
-
background=background,
|
75 |
-
border_radius=rem(0.33),
|
76 |
-
padding=(rem(0.125), rem(0.5)),
|
77 |
-
overflow="hidden",
|
78 |
-
**color_style,
|
79 |
-
**style,
|
80 |
-
))(
|
81 |
-
|
82 |
-
html.escape(body),
|
83 |
-
|
84 |
-
span(
|
85 |
-
style=styles(
|
86 |
-
padding_left=rem(0.5),
|
87 |
-
text_transform="uppercase",
|
88 |
-
))(
|
89 |
-
span(
|
90 |
-
style=styles(
|
91 |
-
font_size=em(0.67),
|
92 |
-
opacity=0.5,
|
93 |
-
))(
|
94 |
-
html.escape(label),
|
95 |
-
),
|
96 |
-
),
|
97 |
-
)
|
98 |
-
)
|
99 |
-
|
100 |
-
|
101 |
-
def get_annotated_html(*args):
|
102 |
-
"""Writes text with annotations into an HTML string.
|
103 |
-
Parameters
|
104 |
-
----------
|
105 |
-
*args : see annotated_text()
|
106 |
-
Returns
|
107 |
-
-------
|
108 |
-
str
|
109 |
-
An HTML string.
|
110 |
-
"""
|
111 |
-
|
112 |
-
out = div()
|
113 |
-
|
114 |
-
for arg in args:
|
115 |
-
if isinstance(arg, str):
|
116 |
-
out(html.escape(arg))
|
117 |
-
|
118 |
-
elif isinstance(arg, HtmlElement):
|
119 |
-
out(arg)
|
120 |
-
|
121 |
-
elif isinstance(arg, tuple):
|
122 |
-
out(annotation(*arg))
|
123 |
-
|
124 |
-
else:
|
125 |
-
raise Exception("Bad input")
|
126 |
-
|
127 |
-
return str(out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|