Update app.py
Browse files
app.py
CHANGED
@@ -42,75 +42,139 @@ def process_nlp(system_message):
|
|
42 |
colorized_text = colorize_text(system_message['content'])
|
43 |
return colorized_text
|
44 |
|
45 |
-
#
|
46 |
COLORS = {
|
47 |
-
"NOUN": "#
|
48 |
-
"VERB": "#
|
49 |
-
"ADJ": "#
|
50 |
-
"ADV": "#
|
51 |
-
"digit": "#
|
52 |
-
"punct": "#
|
53 |
-
"quote": "#
|
54 |
}
|
55 |
|
56 |
-
#
|
57 |
DYSLEXIA_COLORS = {
|
58 |
-
"NOUN": "#
|
59 |
-
"VERB": "#
|
60 |
-
"ADJ": "#
|
61 |
-
"ADV": "#
|
62 |
-
"digit": "#
|
63 |
-
"punct": "#
|
64 |
-
"quote": "#
|
65 |
}
|
66 |
|
67 |
-
#
|
68 |
-
|
69 |
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
if colors is None:
|
72 |
colors = COLORS
|
73 |
-
|
74 |
colorized_text = ""
|
75 |
lines = text.split("\n")
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
77 |
for line in lines:
|
|
|
78 |
doc = nlp(line)
|
79 |
-
|
80 |
for token in doc:
|
|
|
81 |
if token.ent_type_:
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
else:
|
|
|
84 |
color = colors.get(token.pos_, None)
|
85 |
if color is not None:
|
86 |
colorized_text += (
|
87 |
f'<span style="color: {color}; '
|
88 |
-
f'background-color:
|
89 |
-
f
|
|
|
|
|
|
|
|
|
|
|
90 |
)
|
91 |
elif token.is_digit:
|
92 |
colorized_text += (
|
93 |
f'<span style="color: {colors["digit"]}; '
|
94 |
-
f'background-color:
|
95 |
-
f
|
|
|
|
|
|
|
|
|
|
|
96 |
)
|
97 |
elif token.is_punct:
|
98 |
colorized_text += (
|
99 |
f'<span style="color: {colors["punct"]}; '
|
100 |
-
f'background-color:
|
101 |
-
f
|
|
|
|
|
|
|
|
|
|
|
102 |
)
|
103 |
elif token.is_quote:
|
104 |
colorized_text += (
|
105 |
f'<span style="color: {colors["quote"]}; '
|
106 |
-
f'background-color:
|
107 |
-
f
|
|
|
|
|
|
|
|
|
108 |
)
|
109 |
else:
|
110 |
-
colorized_text +=
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
colorized_text += "<br>"
|
113 |
-
|
114 |
return colorized_text
|
115 |
|
116 |
|
|
|
42 |
colorized_text = colorize_text(system_message['content'])
|
43 |
return colorized_text
|
44 |
|
45 |
+
# define color combinations for different parts of speech
|
46 |
COLORS = {
|
47 |
+
"NOUN": "#5e5e5e", # Dark gray
|
48 |
+
"VERB": "#ff6936", # Orange
|
49 |
+
"ADJ": "#4363d8", # Blue
|
50 |
+
"ADV": "#228b22", # Green
|
51 |
+
"digit": "#9a45d6", # Purple
|
52 |
+
"punct": "#ffcc00", # Yellow
|
53 |
+
"quote": "#b300b3" # Magenta
|
54 |
}
|
55 |
|
56 |
+
# define color combinations for individuals with dyslexia
|
57 |
DYSLEXIA_COLORS = {
|
58 |
+
"NOUN": "#5e5e5e",
|
59 |
+
"VERB": "#ff6936",
|
60 |
+
"ADJ": "#4363d8",
|
61 |
+
"ADV": "#228b22",
|
62 |
+
"digit": "#9a45d6",
|
63 |
+
"punct": "#ffcc00",
|
64 |
+
"quote": "#b300b3"
|
65 |
}
|
66 |
|
67 |
+
# define a muted background color
|
68 |
+
BACKGROUND_COLOR = "#f5f5f5" # Light gray
|
69 |
|
70 |
+
# define font and size
|
71 |
+
FONT = "Arial"
|
72 |
+
FONT_SIZE = "14px"
|
73 |
+
|
74 |
+
# load the English language model
|
75 |
+
nlp = spacy.load('en_core_web_sm')
|
76 |
+
|
77 |
+
def colorize_text(text, colors=None, background_color=None):
|
78 |
if colors is None:
|
79 |
colors = COLORS
|
|
|
80 |
colorized_text = ""
|
81 |
lines = text.split("\n")
|
82 |
+
|
83 |
+
# set background color
|
84 |
+
if background_color is None:
|
85 |
+
background_color = BACKGROUND_COLOR
|
86 |
+
|
87 |
+
# iterate over the lines in the text
|
88 |
for line in lines:
|
89 |
+
# parse the line with the language model
|
90 |
doc = nlp(line)
|
91 |
+
# iterate over the tokens in the line
|
92 |
for token in doc:
|
93 |
+
# check if the token is an entity
|
94 |
if token.ent_type_:
|
95 |
+
# use dyslexia colors for entity if available
|
96 |
+
if colors == COLORS:
|
97 |
+
color = DYSLEXIA_COLORS.get(token.pos_, None)
|
98 |
+
else:
|
99 |
+
color = colors.get(token.pos_, None)
|
100 |
+
# check if a color is available for the token
|
101 |
+
if color is not None:
|
102 |
+
colorized_text += (
|
103 |
+
f'<span style="color: {color}; '
|
104 |
+
f'background-color: {background_color}; '
|
105 |
+
f'font-family: {FONT}; '
|
106 |
+
f'font-size: {FONT_SIZE}; '
|
107 |
+
f'font-weight: bold; '
|
108 |
+
f'text-decoration: none; '
|
109 |
+
f'padding-right: 0.5em;">' # Add space between tokens
|
110 |
+
f"{token.text}</span>"
|
111 |
+
)
|
112 |
+
else:
|
113 |
+
colorized_text += (
|
114 |
+
f'<span style="font-family: {FONT}; '
|
115 |
+
f'font-size: {FONT_SIZE}; '
|
116 |
+
f'font-weight: bold; '
|
117 |
+
f'text-decoration: none; '
|
118 |
+
f'padding-right: 0.5em;">' # Add space between tokens
|
119 |
+
f"{token.text}</span>"
|
120 |
+
)
|
121 |
else:
|
122 |
+
# check if a color is available for the token
|
123 |
color = colors.get(token.pos_, None)
|
124 |
if color is not None:
|
125 |
colorized_text += (
|
126 |
f'<span style="color: {color}; '
|
127 |
+
f'background-color: {background_color}; '
|
128 |
+
f'font-family: {FONT}; '
|
129 |
+
f'font-size: {FONT_SIZE}; '
|
130 |
+
f'font-weight: bold; '
|
131 |
+
f'text-decoration: none; '
|
132 |
+
f'padding-right: 0.5em;">' # Add space between tokens
|
133 |
+
f"{token.text}</span>"
|
134 |
)
|
135 |
elif token.is_digit:
|
136 |
colorized_text += (
|
137 |
f'<span style="color: {colors["digit"]}; '
|
138 |
+
f'background-color: {background_color}; '
|
139 |
+
f'font-family: {FONT}; '
|
140 |
+
f'font-size: {FONT_SIZE}; '
|
141 |
+
f'font-weight: bold; '
|
142 |
+
f'text-decoration: none; '
|
143 |
+
f'padding-right: 0.5em;">' # Add space between tokens
|
144 |
+
f"{token.text}</span>"
|
145 |
)
|
146 |
elif token.is_punct:
|
147 |
colorized_text += (
|
148 |
f'<span style="color: {colors["punct"]}; '
|
149 |
+
f'background-color: {background_color}; '
|
150 |
+
f'font-family: {FONT}; '
|
151 |
+
f'font-size: {FONT_SIZE}; '
|
152 |
+
f'font-weight: bold; '
|
153 |
+
f'text-decoration: none; '
|
154 |
+
f'padding-right: 0.5em;">' # Add space between tokens
|
155 |
+
f"{token.text}</span>"
|
156 |
)
|
157 |
elif token.is_quote:
|
158 |
colorized_text += (
|
159 |
f'<span style="color: {colors["quote"]}; '
|
160 |
+
f'background-color: {background_color}; '
|
161 |
+
f'font-family: {FONT}; '
|
162 |
+
f'font-size: {FONT_SIZE}; '
|
163 |
+
f'text-decoration: none; '
|
164 |
+
f'padding-right: 0.5em;">' # Add space between tokens
|
165 |
+
f"{token.text}</span>"
|
166 |
)
|
167 |
else:
|
168 |
+
colorized_text += (
|
169 |
+
f'<span style="font-family: {FONT}; '
|
170 |
+
f'font-size: {FONT_SIZE}; '
|
171 |
+
f'font-weight: bold; '
|
172 |
+
f'text-decoration: none; '
|
173 |
+
f'padding-right: 0.5em;">' # Add space between tokens
|
174 |
+
f"{token.text}</span>"
|
175 |
+
)
|
176 |
colorized_text += "<br>"
|
177 |
+
|
178 |
return colorized_text
|
179 |
|
180 |
|