Update app.py
Browse files
app.py
CHANGED
@@ -188,84 +188,206 @@ from colour import Color
|
|
188 |
# colorized_text += "<br>"
|
189 |
# return colorized_text
|
190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
# define color combinations for different parts of speech
|
192 |
COLORS = {
|
193 |
-
"NOUN": "#
|
194 |
-
"VERB": "#
|
195 |
-
"ADJ": "#
|
196 |
-
"ADV": "#
|
197 |
-
"digit": "#
|
198 |
-
"punct": "#
|
199 |
-
"quote": "#
|
200 |
}
|
201 |
|
202 |
# define color combinations for individuals with dyslexia
|
203 |
DYSLEXIA_COLORS = {
|
204 |
-
"NOUN": "#
|
205 |
-
"VERB": "#
|
206 |
-
"ADJ": "#
|
207 |
-
"ADV": "#
|
208 |
-
"digit": "#
|
209 |
-
"punct": "#
|
210 |
-
"quote": "#
|
211 |
}
|
212 |
|
213 |
# define a muted background color
|
214 |
-
BACKGROUND_COLOR = "#
|
215 |
|
216 |
# define font and size
|
217 |
-
FONT = "
|
218 |
-
FONT_SIZE = "
|
219 |
-
|
220 |
-
# load the English language model
|
221 |
-
nlp = spacy.load('en_core_web_sm')
|
222 |
|
223 |
-
def colorize_text(text, colors=
|
224 |
if colors is None:
|
225 |
colors = COLORS
|
226 |
colorized_text = ""
|
227 |
lines = text.split("\n")
|
228 |
-
|
229 |
# set background color
|
230 |
if background_color is None:
|
231 |
background_color = BACKGROUND_COLOR
|
232 |
-
|
233 |
-
# iterate over the lines in the text
|
234 |
for line in lines:
|
235 |
-
# parse the line with the language model
|
236 |
doc = nlp(line)
|
237 |
-
# iterate over the tokens in the line
|
238 |
for token in doc:
|
239 |
-
# check if the token is an entity
|
240 |
if token.ent_type_:
|
241 |
# use dyslexia colors for entity if available
|
242 |
if colors == COLORS:
|
243 |
color = DYSLEXIA_COLORS.get(token.pos_, None)
|
244 |
else:
|
245 |
color = colors.get(token.pos_, None)
|
246 |
-
# check if a color is available for the token
|
247 |
if color is not None:
|
248 |
colorized_text += (
|
249 |
f'<span style="color: {color}; '
|
250 |
f'background-color: {background_color}; '
|
251 |
f'font-family: {FONT}; '
|
252 |
f'font-size: {FONT_SIZE}; '
|
253 |
-
f'
|
254 |
-
f'text-decoration: none; '
|
255 |
-
f'padding-right: 0.5em;">' # Add space between tokens
|
256 |
f"{token.text}</span>"
|
257 |
)
|
258 |
else:
|
259 |
colorized_text += (
|
260 |
f'<span style="font-family: {FONT}; '
|
261 |
f'font-size: {FONT_SIZE}; '
|
262 |
-
f'
|
263 |
-
f'text-decoration: none; '
|
264 |
-
f'padding-right: 0.5em;">' # Add space between tokens
|
265 |
f"{token.text}</span>"
|
266 |
)
|
267 |
else:
|
268 |
-
# check if a color is available for the token
|
269 |
color = colors.get(token.pos_, None)
|
270 |
if color is not None:
|
271 |
colorized_text += (
|
@@ -273,9 +395,7 @@ def colorize_text(text, colors=DYSLEXIA_COLORS, background_color=None):
|
|
273 |
f'background-color: {background_color}; '
|
274 |
f'font-family: {FONT}; '
|
275 |
f'font-size: {FONT_SIZE}; '
|
276 |
-
f'
|
277 |
-
f'text-decoration: none; '
|
278 |
-
f'padding-right: 0.5em;">' # Add space between tokens
|
279 |
f"{token.text}</span>"
|
280 |
)
|
281 |
elif token.is_digit:
|
@@ -284,9 +404,7 @@ def colorize_text(text, colors=DYSLEXIA_COLORS, background_color=None):
|
|
284 |
f'background-color: {background_color}; '
|
285 |
f'font-family: {FONT}; '
|
286 |
f'font-size: {FONT_SIZE}; '
|
287 |
-
f'
|
288 |
-
f'text-decoration: none; '
|
289 |
-
f'padding-right: 0.5em;">' # Add space between tokens
|
290 |
f"{token.text}</span>"
|
291 |
)
|
292 |
elif token.is_punct:
|
@@ -295,9 +413,7 @@ def colorize_text(text, colors=DYSLEXIA_COLORS, background_color=None):
|
|
295 |
f'background-color: {background_color}; '
|
296 |
f'font-family: {FONT}; '
|
297 |
f'font-size: {FONT_SIZE}; '
|
298 |
-
f'
|
299 |
-
f'text-decoration: none; '
|
300 |
-
f'padding-right: 0.5em;">' # Add space between tokens
|
301 |
f"{token.text}</span>"
|
302 |
)
|
303 |
elif token.is_quote:
|
@@ -306,24 +422,20 @@ def colorize_text(text, colors=DYSLEXIA_COLORS, background_color=None):
|
|
306 |
f'background-color: {background_color}; '
|
307 |
f'font-family: {FONT}; '
|
308 |
f'font-size: {FONT_SIZE}; '
|
309 |
-
f'text-decoration:
|
310 |
-
f'padding-right: 0.5em;">' # Add space between tokens
|
311 |
f"{token.text}</span>"
|
312 |
)
|
313 |
else:
|
314 |
colorized_text += (
|
315 |
f'<span style="font-family: {FONT}; '
|
316 |
f'font-size: {FONT_SIZE}; '
|
317 |
-
f'
|
318 |
-
f'text-decoration: none; '
|
319 |
-
f'padding-right: 0.5em;">' # Add space between tokens
|
320 |
f"{token.text}</span>"
|
321 |
)
|
|
|
322 |
colorized_text += "<br>"
|
323 |
-
|
324 |
return colorized_text
|
325 |
|
326 |
-
|
327 |
def colorize_and_update(system_message, submit_update):
|
328 |
colorized_system_message = colorize_text(system_message['content'])
|
329 |
submit_update(None, colorized_system_message) # Pass the colorized_system_message as the second output
|
|
|
188 |
# colorized_text += "<br>"
|
189 |
# return colorized_text
|
190 |
|
191 |
+
# # define color combinations for different parts of speech
|
192 |
+
# COLORS = {
|
193 |
+
# "NOUN": "#5e5e5e", # Dark gray
|
194 |
+
# "VERB": "#ff6936", # Orange
|
195 |
+
# "ADJ": "#4363d8", # Blue
|
196 |
+
# "ADV": "#228b22", # Green
|
197 |
+
# "digit": "#9a45d6", # Purple
|
198 |
+
# "punct": "#ffcc00", # Yellow
|
199 |
+
# "quote": "#b300b3" # Magenta
|
200 |
+
# }
|
201 |
+
|
202 |
+
# # define color combinations for individuals with dyslexia
|
203 |
+
# DYSLEXIA_COLORS = {
|
204 |
+
# "NOUN": "#5e5e5e",
|
205 |
+
# "VERB": "#ff6936",
|
206 |
+
# "ADJ": "#4363d8",
|
207 |
+
# "ADV": "#228b22",
|
208 |
+
# "digit": "#9a45d6",
|
209 |
+
# "punct": "#ffcc00",
|
210 |
+
# "quote": "#b300b3"
|
211 |
+
# }
|
212 |
+
|
213 |
+
# # define a muted background color
|
214 |
+
# BACKGROUND_COLOR = "#f5f5f5" # Light gray
|
215 |
+
|
216 |
+
# # define font and size
|
217 |
+
# FONT = "Arial"
|
218 |
+
# FONT_SIZE = "14px"
|
219 |
+
|
220 |
+
# # load the English language model
|
221 |
+
# nlp = spacy.load('en_core_web_sm')
|
222 |
+
|
223 |
+
# def colorize_text(text, colors=DYSLEXIA_COLORS, background_color=None):
|
224 |
+
# if colors is None:
|
225 |
+
# colors = COLORS
|
226 |
+
# colorized_text = ""
|
227 |
+
# lines = text.split("\n")
|
228 |
+
|
229 |
+
# # set background color
|
230 |
+
# if background_color is None:
|
231 |
+
# background_color = BACKGROUND_COLOR
|
232 |
+
|
233 |
+
# # iterate over the lines in the text
|
234 |
+
# for line in lines:
|
235 |
+
# # parse the line with the language model
|
236 |
+
# doc = nlp(line)
|
237 |
+
# # iterate over the tokens in the line
|
238 |
+
# for token in doc:
|
239 |
+
# # check if the token is an entity
|
240 |
+
# if token.ent_type_:
|
241 |
+
# # use dyslexia colors for entity if available
|
242 |
+
# if colors == COLORS:
|
243 |
+
# color = DYSLEXIA_COLORS.get(token.pos_, None)
|
244 |
+
# else:
|
245 |
+
# color = colors.get(token.pos_, None)
|
246 |
+
# # check if a color is available for the token
|
247 |
+
# if color is not None:
|
248 |
+
# colorized_text += (
|
249 |
+
# f'<span style="color: {color}; '
|
250 |
+
# f'background-color: {background_color}; '
|
251 |
+
# f'font-family: {FONT}; '
|
252 |
+
# f'font-size: {FONT_SIZE}; '
|
253 |
+
# f'font-weight: bold; '
|
254 |
+
# f'text-decoration: none; '
|
255 |
+
# f'padding-right: 0.5em;">' # Add space between tokens
|
256 |
+
# f"{token.text}</span>"
|
257 |
+
# )
|
258 |
+
# else:
|
259 |
+
# colorized_text += (
|
260 |
+
# f'<span style="font-family: {FONT}; '
|
261 |
+
# f'font-size: {FONT_SIZE}; '
|
262 |
+
# f'font-weight: bold; '
|
263 |
+
# f'text-decoration: none; '
|
264 |
+
# f'padding-right: 0.5em;">' # Add space between tokens
|
265 |
+
# f"{token.text}</span>"
|
266 |
+
# )
|
267 |
+
# else:
|
268 |
+
# # check if a color is available for the token
|
269 |
+
# color = colors.get(token.pos_, None)
|
270 |
+
# if color is not None:
|
271 |
+
# colorized_text += (
|
272 |
+
# f'<span style="color: {color}; '
|
273 |
+
# f'background-color: {background_color}; '
|
274 |
+
# f'font-family: {FONT}; '
|
275 |
+
# f'font-size: {FONT_SIZE}; '
|
276 |
+
# f'font-weight: bold; '
|
277 |
+
# f'text-decoration: none; '
|
278 |
+
# f'padding-right: 0.5em;">' # Add space between tokens
|
279 |
+
# f"{token.text}</span>"
|
280 |
+
# )
|
281 |
+
# elif token.is_digit:
|
282 |
+
# colorized_text += (
|
283 |
+
# f'<span style="color: {colors["digit"]}; '
|
284 |
+
# f'background-color: {background_color}; '
|
285 |
+
# f'font-family: {FONT}; '
|
286 |
+
# f'font-size: {FONT_SIZE}; '
|
287 |
+
# f'font-weight: bold; '
|
288 |
+
# f'text-decoration: none; '
|
289 |
+
# f'padding-right: 0.5em;">' # Add space between tokens
|
290 |
+
# f"{token.text}</span>"
|
291 |
+
# )
|
292 |
+
# elif token.is_punct:
|
293 |
+
# colorized_text += (
|
294 |
+
# f'<span style="color: {colors["punct"]}; '
|
295 |
+
# f'background-color: {background_color}; '
|
296 |
+
# f'font-family: {FONT}; '
|
297 |
+
# f'font-size: {FONT_SIZE}; '
|
298 |
+
# f'font-weight: bold; '
|
299 |
+
# f'text-decoration: none; '
|
300 |
+
# f'padding-right: 0.5em;">' # Add space between tokens
|
301 |
+
# f"{token.text}</span>"
|
302 |
+
# )
|
303 |
+
# elif token.is_quote:
|
304 |
+
# colorized_text += (
|
305 |
+
# f'<span style="color: {colors["quote"]}; '
|
306 |
+
# f'background-color: {background_color}; '
|
307 |
+
# f'font-family: {FONT}; '
|
308 |
+
# f'font-size: {FONT_SIZE}; '
|
309 |
+
# f'text-decoration: none; '
|
310 |
+
# f'padding-right: 0.5em;">' # Add space between tokens
|
311 |
+
# f"{token.text}</span>"
|
312 |
+
# )
|
313 |
+
# else:
|
314 |
+
# colorized_text += (
|
315 |
+
# f'<span style="font-family: {FONT}; '
|
316 |
+
# f'font-size: {FONT_SIZE}; '
|
317 |
+
# f'font-weight: bold; '
|
318 |
+
# f'text-decoration: none; '
|
319 |
+
# f'padding-right: 0.5em;">' # Add space between tokens
|
320 |
+
# f"{token.text}</span>"
|
321 |
+
# )
|
322 |
+
# colorized_text += "<br>"
|
323 |
+
|
324 |
+
# return colorized_text
|
325 |
+
|
326 |
# define color combinations for different parts of speech
|
327 |
COLORS = {
|
328 |
+
"NOUN": "#FF3300",
|
329 |
+
"VERB": "#008000",
|
330 |
+
"ADJ": "#1E90FF",
|
331 |
+
"ADV": "#FF8C00",
|
332 |
+
"digit": "#FF1493",
|
333 |
+
"punct": "#8B0000",
|
334 |
+
"quote": "#800080",
|
335 |
}
|
336 |
|
337 |
# define color combinations for individuals with dyslexia
|
338 |
DYSLEXIA_COLORS = {
|
339 |
+
"NOUN": "#1E90FF",
|
340 |
+
"VERB": "#006400",
|
341 |
+
"ADJ": "#00CED1",
|
342 |
+
"ADV": "#FF8C00",
|
343 |
+
"digit": "#FF1493",
|
344 |
+
"punct": "#A0522D",
|
345 |
+
"quote": "#800080",
|
346 |
}
|
347 |
|
348 |
# define a muted background color
|
349 |
+
BACKGROUND_COLOR = "#EAEAEA"
|
350 |
|
351 |
# define font and size
|
352 |
+
FONT = "Georgia"
|
353 |
+
FONT_SIZE = "18px"
|
|
|
|
|
|
|
354 |
|
355 |
+
def colorize_text(text, colors=None, background_color=None):
|
356 |
if colors is None:
|
357 |
colors = COLORS
|
358 |
colorized_text = ""
|
359 |
lines = text.split("\n")
|
360 |
+
|
361 |
# set background color
|
362 |
if background_color is None:
|
363 |
background_color = BACKGROUND_COLOR
|
364 |
+
|
|
|
365 |
for line in lines:
|
|
|
366 |
doc = nlp(line)
|
|
|
367 |
for token in doc:
|
|
|
368 |
if token.ent_type_:
|
369 |
# use dyslexia colors for entity if available
|
370 |
if colors == COLORS:
|
371 |
color = DYSLEXIA_COLORS.get(token.pos_, None)
|
372 |
else:
|
373 |
color = colors.get(token.pos_, None)
|
|
|
374 |
if color is not None:
|
375 |
colorized_text += (
|
376 |
f'<span style="color: {color}; '
|
377 |
f'background-color: {background_color}; '
|
378 |
f'font-family: {FONT}; '
|
379 |
f'font-size: {FONT_SIZE}; '
|
380 |
+
f'text-decoration: underline;">'
|
|
|
|
|
381 |
f"{token.text}</span>"
|
382 |
)
|
383 |
else:
|
384 |
colorized_text += (
|
385 |
f'<span style="font-family: {FONT}; '
|
386 |
f'font-size: {FONT_SIZE}; '
|
387 |
+
f'text-decoration: underline;">'
|
|
|
|
|
388 |
f"{token.text}</span>"
|
389 |
)
|
390 |
else:
|
|
|
391 |
color = colors.get(token.pos_, None)
|
392 |
if color is not None:
|
393 |
colorized_text += (
|
|
|
395 |
f'background-color: {background_color}; '
|
396 |
f'font-family: {FONT}; '
|
397 |
f'font-size: {FONT_SIZE}; '
|
398 |
+
f'text-decoration: underline;">'
|
|
|
|
|
399 |
f"{token.text}</span>"
|
400 |
)
|
401 |
elif token.is_digit:
|
|
|
404 |
f'background-color: {background_color}; '
|
405 |
f'font-family: {FONT}; '
|
406 |
f'font-size: {FONT_SIZE}; '
|
407 |
+
f'text-decoration: underline;">'
|
|
|
|
|
408 |
f"{token.text}</span>"
|
409 |
)
|
410 |
elif token.is_punct:
|
|
|
413 |
f'background-color: {background_color}; '
|
414 |
f'font-family: {FONT}; '
|
415 |
f'font-size: {FONT_SIZE}; '
|
416 |
+
f'text-decoration: underline;">'
|
|
|
|
|
417 |
f"{token.text}</span>"
|
418 |
)
|
419 |
elif token.is_quote:
|
|
|
422 |
f'background-color: {background_color}; '
|
423 |
f'font-family: {FONT}; '
|
424 |
f'font-size: {FONT_SIZE}; '
|
425 |
+
f'text-decoration: underline;">'
|
|
|
426 |
f"{token.text}</span>"
|
427 |
)
|
428 |
else:
|
429 |
colorized_text += (
|
430 |
f'<span style="font-family: {FONT}; '
|
431 |
f'font-size: {FONT_SIZE}; '
|
432 |
+
f'text-decoration: underline;">'
|
|
|
|
|
433 |
f"{token.text}</span>"
|
434 |
)
|
435 |
+
colorized_text += " "
|
436 |
colorized_text += "<br>"
|
|
|
437 |
return colorized_text
|
438 |
|
|
|
439 |
def colorize_and_update(system_message, submit_update):
|
440 |
colorized_system_message = colorize_text(system_message['content'])
|
441 |
submit_update(None, colorized_system_message) # Pass the colorized_system_message as the second output
|