File size: 3,907 Bytes
20fe0c2
 
 
 
c4cfb37
c0ad028
 
65f9712
c0ad028
20fe0c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
463cba4
20fe0c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import spacy_streamlit
from spacy.symbols import *
import streamlit as st
import html
import spacy
import subprocess
import sys

subprocess.run([f"{sys.executable}", "python -m spacy download en_core_web_sm"])

from htbuilder import H, HtmlElement, styles
from htbuilder.units import unit

# Only works in 3.7+: from htbuilder import div, span
div = H.div
span = H.span

# Only works in 3.7+: from htbuilder.units import px, rem, em
px = unit.px
rem = unit.rem
em = unit.em

# Colors from the Streamlit palette.
# These are red-70, orange-70, ..., violet-70, gray-70.
PALETTE = [
    "#ff4b4b",
    "#ffa421",
    "#ffe312",
    "#21c354",
    "#00d4b1",
    "#00c0f2",
    "#1c83e1",
    "#803df5",
    "#808495",
]

OPACITIES = [
    "33", "66",
]

DEFAULT_TEXT = """AI has reached superhuman levels in various areas such as playing complex strategic and video games, calculating protein folding, and visual recognition. Are we close to superhuman levels in conversational AI as well?"""

spacy_model = "en_core_web_sm"

replacement_dict= {
"superhuman levels" : "high accuracy",
"conversational AI" : "language generation"
}


def annotation(body, label="", background=None, color=None, **style):
    """
    from https://github.com/tvst/st-annotated-text/blob/master/annotated_text/util.py
    """

    color_style = {}

    if color:
        color_style['color'] = color

    if not background:
        label_sum = sum(ord(c) for c in label)
        background_color = PALETTE[label_sum % len(PALETTE)]
        background_opacity = OPACITIES[label_sum % len(OPACITIES)]
        background = background_color + background_opacity

    return (
        span(
            style=styles(
                background=background,
                border_radius=rem(0.33),
                padding=(rem(0.125), rem(0.5)),
                overflow="hidden",
                **color_style,
                **style,
            ))(

            html.escape(body),

            span(
                style=styles(
                    padding_left=rem(0.5),
                    text_transform="uppercase",
                ))(
                span(
                    style=styles(
                        font_size=em(0.67),
                        opacity=0.5,
                    ))(
                    html.escape(label),
                ),
            ),
        )
    )


def get_annotated_html(*args):

    out = div()

    for arg in args:
        if isinstance(arg, str):
            out(html.escape(arg))

        elif isinstance(arg, HtmlElement):
            out(arg)

        elif isinstance(arg, tuple):
            out(annotation(*arg))

        elif isinstance(arg,list):
            for el in arg:
                if isinstance(el, str):
                    out(html.escape(el))

                elif isinstance(el, HtmlElement):
                    out(el)

                elif isinstance(el, tuple):
                    out(annotation(*el))
        else:
            raise Exception("Oh noes!")

    return str(out)

st.title("AI Hype Checker")
text = st.text_area("Paste your over-hyped text here:", DEFAULT_TEXT, height=100)
doc = spacy_streamlit.process_text(spacy_model, text)

for chunk in doc.noun_chunks:
    if chunk.text in replacement_dict.keys():
        text= text.replace(chunk.text, replacement_dict[chunk.text])
    else:
        continue

text = st.text_area("Fixed it! See below:", text, height=100)

st.markdown("## Here are the terms that we flagged:")
chunks= [chunk.text for chunk in doc.noun_chunks]
flagged_chunks=[]
for i in range(len(chunks)):
    if chunks[i] in replacement_dict.keys():
        flagged_chunks.append((chunks[i], replacement_dict[chunks[i]]))

flagged_chunks = list(set(flagged_chunks))
for f in flagged_chunks:
    st.markdown(
        get_annotated_html(f),
        unsafe_allow_html=True,
    )


#st.text(f"Analyzed using spaCy model {spacy_model}")