Spaces:
son9john
/
Runtime error

son9john commited on
Commit
8217f9e
1 Parent(s): 00a86c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -19
app.py CHANGED
@@ -36,40 +36,159 @@ API_KEY = os.environ["API_KEY"]
36
 
37
 
38
  nlp = spacy.load("en_core_web_sm")
 
39
  def process_nlp(system_message):
40
  # Colorize the system message text
41
  colorized_text = colorize_text(system_message['content'])
42
  return colorized_text
43
 
44
- def colorize_text(text):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  colorized_text = ""
46
  lines = text.split("\n")
47
-
 
 
 
 
 
48
  for line in lines:
 
49
  doc = nlp(line)
 
50
  for token in doc:
 
51
  if token.ent_type_:
52
- colorized_text += f'**{token.text_with_ws}**'
53
- elif token.pos_ == 'NOUN':
54
- colorized_text += f'<span style="color: #FF3300; background-color: transparent;">{token.text_with_ws}</span>'
55
- elif token.pos_ == 'VERB':
56
- colorized_text += f'<span style="color: #FFFF00; background-color: transparent;">{token.text_with_ws}</span>'
57
- elif token.pos_ == 'ADJ':
58
- colorized_text += f'<span style="color: #00CC00; background-color: transparent;">{token.text_with_ws}</span>'
59
- elif token.pos_ == 'ADV':
60
- colorized_text += f'<span style="color: #FF6600; background-color: transparent;">{token.text_with_ws}</span>'
61
- elif token.is_digit:
62
- colorized_text += f'<span style="color: #9900CC; background-color: transparent;">{token.text_with_ws}</span>'
63
- elif token.is_punct:
64
- colorized_text += f'<span style="color: #8B4513; background-color: transparent;">{token.text_with_ws}</span>'
65
- elif token.is_quote:
66
- colorized_text += f'<span style="color: #008080; background-color: transparent;">{token.text_with_ws}</span>'
 
 
 
 
 
 
 
 
 
 
 
67
  else:
68
- colorized_text += token.text_with_ws
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  colorized_text += "<br>"
70
-
71
  return colorized_text
72
 
 
73
  def colorize_and_update(system_message, submit_update):
74
  colorized_system_message = colorize_text(system_message['content'])
75
  submit_update(None, colorized_system_message) # Pass the colorized_system_message as the second output
 
36
 
37
 
38
  nlp = spacy.load("en_core_web_sm")
39
+
40
  def process_nlp(system_message):
41
  # Colorize the system message text
42
  colorized_text = colorize_text(system_message['content'])
43
  return colorized_text
44
 
45
+ from colour import Color
46
+
47
+ # define color combinations for different parts of speech
48
+ COLORS = {
49
+ "NOUN": "#000000", # Black
50
+ "VERB": "#ff6936", # Orange
51
+ "ADJ": "#4363d8", # Blue
52
+ "ADV": "#228b22", # Green
53
+ "digit": "#9a45d6", # Purple
54
+ "punct": "#ffcc00", # Yellow
55
+ "quote": "#b300b3" # Magenta
56
+ }
57
+
58
+ # define color combinations for individuals with dyslexia and color vision deficiencies
59
+ DYSLEXIA_COLORS = {
60
+ "NOUN": "#000000",
61
+ "VERB": "#ff6936",
62
+ "ADJ": "#4363d8",
63
+ "ADV": "#228b22",
64
+ "digit": "#9a45d6",
65
+ "punct": "#ffcc00",
66
+ "quote": "#b300b3",
67
+ }
68
+ RED_GREEN_COLORS = {
69
+ "NOUN": "#000000",
70
+ "VERB": "#fe642e", # Lighter orange
71
+ "ADJ": "#2e86c1", # Lighter blue
72
+ "ADV": "#82e0aa", # Lighter green
73
+ "digit": "#aa6c39", # Brown
74
+ "punct": "#f0b27a", # Lighter yellow
75
+ "quote": "#9932cc" # Darker magenta
76
+ }
77
+
78
+ # define a muted background color
79
+ BACKGROUND_COLOR = "#ffffff" # White
80
+
81
+ # define font and size
82
+ FONT = "OpenDyslexic"
83
+ FONT_SIZE = "18px"
84
+
85
+ def colorize_text(text, colors=DYSLEXIA_COLORS, background_color=None, font=FONT, font_size=FONT_SIZE):
86
+ if colors is None:
87
+ colors = COLORS
88
  colorized_text = ""
89
  lines = text.split("\n")
90
+
91
+ # set background color
92
+ if background_color is None:
93
+ background_color = BACKGROUND_COLOR
94
+
95
+ # iterate over the lines in the text
96
  for line in lines:
97
+ # parse the line with the language model
98
  doc = nlp(line)
99
+ # iterate over the tokens in the line
100
  for token in doc:
101
+ # check if the token is an entity
102
  if token.ent_type_:
103
+ # use dyslexia colors for entity if available
104
+ if colors == COLORS:
105
+ color = DYSLEXIA_COLORS.get(token.pos_, None)
106
+ else:
107
+ color = colors.get(token.pos_, None)
108
+ # check if a color is available for the token
109
+ if color is not None:
110
+ colorized_text += (
111
+ f'<span style="color: {color}; '
112
+ f'background-color: {background_color}; '
113
+ f'font-family: {font}; '
114
+ f'font-size: {font_size}; '
115
+ f'font-weight: bold; '
116
+ f'text-decoration: none; '
117
+ f'padding-right: 0.5em;">'
118
+ f"{token.text}</span>"
119
+ )
120
+ else:
121
+ colorized_text += (
122
+ f'<span style="font-family: {font}; '
123
+ f'font-size: {font_size}; '
124
+ f'font-weight: bold; '
125
+ f'text-decoration: none; '
126
+ f'padding-right: 0.5em;">'
127
+ f"{token.text}</span>"
128
+ )
129
  else:
130
+ # check if a color is available for the token
131
+ color = colors.get(token.pos_, None)
132
+ if color is not None:
133
+ colorized_text += (
134
+ f'<span style="color: {color}; '
135
+ f'background-color: {background_color}; '
136
+ f'font-family: {font}; '
137
+ f'font-size: {font_size}; '
138
+ f'font-weight: bold; '
139
+ f'text-decoration: none; '
140
+ f'padding-right: 0.5em;">'
141
+ f"{token.text}</span>"
142
+ )
143
+ elif token.is_digit:
144
+ colorized_text += (
145
+ f'<span style="color: {colors["digit"]}; '
146
+ f'background-color: {background_color}; '
147
+ f'font-family: {font}; '
148
+ f'font-size: {font_size}; '
149
+ f'font-weight: bold; '
150
+ f'text-decoration: none; '
151
+ f'padding-right: 0.5em;">'
152
+ f"{token.text}</span>"
153
+ )
154
+ elif token.is_punct:
155
+ colorized_text += (
156
+ f'<span style="color: {colors["punct"]}; '
157
+ f'background-color: {background_color}; '
158
+ f'font-family: {font}; '
159
+ f'font-size: {font_size}; '
160
+ f'font-weight: bold; '
161
+ f'text-decoration: none; '
162
+ f'padding-right: 0.5em;">'
163
+ f"{token.text}</span>"
164
+ )
165
+ elif token.is_quote:
166
+ colorized_text += (
167
+ f'<span style="color: {colors["quote"]}; '
168
+ f'background-color: {background_color}; '
169
+ f'font-family: {font}; '
170
+ f'font-size: {font_size}; '
171
+ f'text-decoration: none; '
172
+ f'padding-right: 0.5em;">'
173
+ f"{token.text}</span>"
174
+ )
175
+ else:
176
+ # use larger font size for specific parts of speech, such as nouns and verbs
177
+ font_size = FONT_SIZE
178
+ if token.pos_ in ["NOUN", "VERB"]:
179
+ font_size = "22px"
180
+ colorized_text += (
181
+ f'<span style="font-family: {font}; '
182
+ f'font-size: {font_size}; '
183
+ f'font-weight: bold; '
184
+ f'text-decoration: none; '
185
+ f'padding-right: 0.5em;">'
186
+ f"{token.text}</span>"
187
+ )
188
  colorized_text += "<br>"
 
189
  return colorized_text
190
 
191
+
192
  def colorize_and_update(system_message, submit_update):
193
  colorized_system_message = colorize_text(system_message['content'])
194
  submit_update(None, colorized_system_message) # Pass the colorized_system_message as the second output