Update app.py
Browse files
app.py
CHANGED
|
@@ -42,84 +42,206 @@ def process_nlp(system_message):
|
|
| 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": "#
|
| 48 |
-
"VERB": "#
|
| 49 |
-
"ADJ": "#
|
| 50 |
-
"ADV": "#
|
| 51 |
-
"digit": "#
|
| 52 |
-
"punct": "#
|
| 53 |
-
"quote": "#
|
| 54 |
}
|
| 55 |
|
| 56 |
# define color combinations for individuals with dyslexia
|
| 57 |
DYSLEXIA_COLORS = {
|
| 58 |
-
"NOUN": "#
|
| 59 |
-
"VERB": "#
|
| 60 |
-
"ADJ": "#
|
| 61 |
-
"ADV": "#
|
| 62 |
-
"digit": "#
|
| 63 |
-
"punct": "#
|
| 64 |
-
"quote": "#
|
| 65 |
}
|
| 66 |
|
| 67 |
# define a muted background color
|
| 68 |
-
BACKGROUND_COLOR = "#
|
| 69 |
|
| 70 |
# define font and size
|
| 71 |
-
FONT = "
|
| 72 |
-
FONT_SIZE = "
|
| 73 |
-
|
| 74 |
-
# load the English language model
|
| 75 |
-
nlp = spacy.load('en_core_web_sm')
|
| 76 |
|
| 77 |
-
def colorize_text(text, colors=
|
| 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'
|
| 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'
|
| 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 += (
|
|
@@ -127,9 +249,7 @@ def colorize_text(text, colors=DYSLEXIA_COLORS, background_color=None):
|
|
| 127 |
f'background-color: {background_color}; '
|
| 128 |
f'font-family: {FONT}; '
|
| 129 |
f'font-size: {FONT_SIZE}; '
|
| 130 |
-
f'
|
| 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:
|
|
@@ -138,9 +258,7 @@ def colorize_text(text, colors=DYSLEXIA_COLORS, background_color=None):
|
|
| 138 |
f'background-color: {background_color}; '
|
| 139 |
f'font-family: {FONT}; '
|
| 140 |
f'font-size: {FONT_SIZE}; '
|
| 141 |
-
f'
|
| 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:
|
|
@@ -149,9 +267,7 @@ def colorize_text(text, colors=DYSLEXIA_COLORS, background_color=None):
|
|
| 149 |
f'background-color: {background_color}; '
|
| 150 |
f'font-family: {FONT}; '
|
| 151 |
f'font-size: {FONT_SIZE}; '
|
| 152 |
-
f'
|
| 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:
|
|
@@ -160,25 +276,20 @@ def colorize_text(text, colors=DYSLEXIA_COLORS, background_color=None):
|
|
| 160 |
f'background-color: {background_color}; '
|
| 161 |
f'font-family: {FONT}; '
|
| 162 |
f'font-size: {FONT_SIZE}; '
|
| 163 |
-
f'text-decoration:
|
| 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'
|
| 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 |
-
|
| 181 |
-
|
| 182 |
# def colorize_text(text):
|
| 183 |
# colorized_text = ""
|
| 184 |
# lines = text.split("\n")
|
|
@@ -300,7 +411,7 @@ def transcribe(audio, text, submit_update=None):
|
|
| 300 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 301 |
prompt = [{"text": f"{message['role']}: {message['content']}\n\n"} for message in messages]
|
| 302 |
system_message = openai.ChatCompletion.create(
|
| 303 |
-
model="gpt-
|
| 304 |
messages=messages,
|
| 305 |
max_tokens=2000
|
| 306 |
)["choices"][0]["message"]
|
|
|
|
| 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=DYSLEXIA_COLORS, 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 |
# define color combinations for different parts of speech
|
| 181 |
COLORS = {
|
| 182 |
+
"NOUN": "#FF3300",
|
| 183 |
+
"VERB": "#008000",
|
| 184 |
+
"ADJ": "#1E90FF",
|
| 185 |
+
"ADV": "#FF8C00",
|
| 186 |
+
"digit": "#FF1493",
|
| 187 |
+
"punct": "#8B0000",
|
| 188 |
+
"quote": "#800080",
|
| 189 |
}
|
| 190 |
|
| 191 |
# define color combinations for individuals with dyslexia
|
| 192 |
DYSLEXIA_COLORS = {
|
| 193 |
+
"NOUN": "#1E90FF",
|
| 194 |
+
"VERB": "#006400",
|
| 195 |
+
"ADJ": "#00CED1",
|
| 196 |
+
"ADV": "#FF8C00",
|
| 197 |
+
"digit": "#FF1493",
|
| 198 |
+
"punct": "#A0522D",
|
| 199 |
+
"quote": "#800080",
|
| 200 |
}
|
| 201 |
|
| 202 |
# define a muted background color
|
| 203 |
+
BACKGROUND_COLOR = "#EAEAEA"
|
| 204 |
|
| 205 |
# define font and size
|
| 206 |
+
FONT = "Georgia"
|
| 207 |
+
FONT_SIZE = "18px"
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
+
def colorize_text(text, colors=None, background_color=None):
|
| 210 |
if colors is None:
|
| 211 |
colors = COLORS
|
| 212 |
colorized_text = ""
|
| 213 |
lines = text.split("\n")
|
| 214 |
+
|
| 215 |
# set background color
|
| 216 |
if background_color is None:
|
| 217 |
background_color = BACKGROUND_COLOR
|
| 218 |
+
|
|
|
|
| 219 |
for line in lines:
|
|
|
|
| 220 |
doc = nlp(line)
|
|
|
|
| 221 |
for token in doc:
|
|
|
|
| 222 |
if token.ent_type_:
|
| 223 |
# use dyslexia colors for entity if available
|
| 224 |
if colors == COLORS:
|
| 225 |
color = DYSLEXIA_COLORS.get(token.pos_, None)
|
| 226 |
else:
|
| 227 |
color = colors.get(token.pos_, None)
|
|
|
|
| 228 |
if color is not None:
|
| 229 |
colorized_text += (
|
| 230 |
f'<span style="color: {color}; '
|
| 231 |
f'background-color: {background_color}; '
|
| 232 |
f'font-family: {FONT}; '
|
| 233 |
f'font-size: {FONT_SIZE}; '
|
| 234 |
+
f'text-decoration: underline;">'
|
|
|
|
|
|
|
| 235 |
f"{token.text}</span>"
|
| 236 |
)
|
| 237 |
else:
|
| 238 |
colorized_text += (
|
| 239 |
f'<span style="font-family: {FONT}; '
|
| 240 |
f'font-size: {FONT_SIZE}; '
|
| 241 |
+
f'text-decoration: underline;">'
|
|
|
|
|
|
|
| 242 |
f"{token.text}</span>"
|
| 243 |
)
|
| 244 |
else:
|
|
|
|
| 245 |
color = colors.get(token.pos_, None)
|
| 246 |
if color is not None:
|
| 247 |
colorized_text += (
|
|
|
|
| 249 |
f'background-color: {background_color}; '
|
| 250 |
f'font-family: {FONT}; '
|
| 251 |
f'font-size: {FONT_SIZE}; '
|
| 252 |
+
f'text-decoration: underline;">'
|
|
|
|
|
|
|
| 253 |
f"{token.text}</span>"
|
| 254 |
)
|
| 255 |
elif token.is_digit:
|
|
|
|
| 258 |
f'background-color: {background_color}; '
|
| 259 |
f'font-family: {FONT}; '
|
| 260 |
f'font-size: {FONT_SIZE}; '
|
| 261 |
+
f'text-decoration: underline;">'
|
|
|
|
|
|
|
| 262 |
f"{token.text}</span>"
|
| 263 |
)
|
| 264 |
elif token.is_punct:
|
|
|
|
| 267 |
f'background-color: {background_color}; '
|
| 268 |
f'font-family: {FONT}; '
|
| 269 |
f'font-size: {FONT_SIZE}; '
|
| 270 |
+
f'text-decoration: underline;">'
|
|
|
|
|
|
|
| 271 |
f"{token.text}</span>"
|
| 272 |
)
|
| 273 |
elif token.is_quote:
|
|
|
|
| 276 |
f'background-color: {background_color}; '
|
| 277 |
f'font-family: {FONT}; '
|
| 278 |
f'font-size: {FONT_SIZE}; '
|
| 279 |
+
f'text-decoration: underline;">'
|
|
|
|
| 280 |
f"{token.text}</span>"
|
| 281 |
)
|
| 282 |
else:
|
| 283 |
colorized_text += (
|
| 284 |
f'<span style="font-family: {FONT}; '
|
| 285 |
f'font-size: {FONT_SIZE}; '
|
| 286 |
+
f'text-decoration: underline;">'
|
|
|
|
|
|
|
| 287 |
f"{token.text}</span>"
|
| 288 |
)
|
| 289 |
+
colorized_text += " "
|
| 290 |
colorized_text += "<br>"
|
|
|
|
| 291 |
return colorized_text
|
| 292 |
|
|
|
|
|
|
|
| 293 |
# def colorize_text(text):
|
| 294 |
# colorized_text = ""
|
| 295 |
# lines = text.split("\n")
|
|
|
|
| 411 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 412 |
prompt = [{"text": f"{message['role']}: {message['content']}\n\n"} for message in messages]
|
| 413 |
system_message = openai.ChatCompletion.create(
|
| 414 |
+
model="gpt-4",
|
| 415 |
messages=messages,
|
| 416 |
max_tokens=2000
|
| 417 |
)["choices"][0]["message"]
|