Maximax67 commited on
Commit
967bb8d
1 Parent(s): 95b2f1e

Add gradio dataframes

Browse files
Files changed (2) hide show
  1. README.md +3 -0
  2. app.py +89 -19
README.md CHANGED
@@ -6,6 +6,9 @@ colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 4.26.0
8
  app_file: app.py
 
 
 
9
  pinned: false
10
  license: mit
11
  ---
 
6
  sdk: gradio
7
  sdk_version: 4.26.0
8
  app_file: app.py
9
+ fullWidth: true
10
+ short_description: "cefrpy demo: Highlight English words according to CEFR scale"
11
+ models: ["spacy/en_core_web_sm"]
12
  pinned: false
13
  license: mit
14
  ---
app.py CHANGED
@@ -17,15 +17,28 @@ DEFAULT_ENTITY_ITEMS_TO_SKIP = [
17
  'ORG', 'FAC', 'PERSON'
18
  ]
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  DEFAULT_TEXT = """The world's oldest known recipe is for beer. It dates back to around 5,000 BC and was found in ancient Sumeria (modern-day Iraq).
21
 
22
- Did you know that the shortest war in history lasted only 38 minutes? It occurred between Britain and Zanzibar on August 27, 1896. Zanzibar surrendered after the British issued an ultimatum to end their rule.
23
 
24
- Some of the longest words in the English language can leave you tongue-tied! One example is "pneumonoultramicroscopicsilicovolcanoconiosis," which refers to a lung disease caused by inhaling very fine silica dust. With 45 letters, it's often cited as one of the longest words in English dictionaries. However, it's more of a curiosity than a practical term used in everyday language!
25
 
26
  In 2006, a Coca-Cola employee offered to sell Coca-Cola secrets to Pepsi. Pepsi responded by notifying Coca-Cola, and the FBI set up a sting operation to catch the culprit.
27
 
28
- Sir Isaac Newton, the renowned mathematician and physicist, invented the cat flap. While studying at Cambridge University, Newton had a pet cat named Spithead. He became annoyed when Spithead would interrupt his experiments, so he designed a small door in his study door through which the cat could come and go freely."""
29
 
30
  DISPLACY_RENDER_OPTIONS = {
31
  "colors": {
@@ -52,8 +65,8 @@ ABBREVIATION_MAPPING = {
52
 
53
  LINKS_HTML = """
54
  <p>
55
- &ensp;Github: <a href="https://github.com/Maximax67/cefrpy">https://github.com/Maximax67/cefrpy</a><br>
56
- &ensp;Docs: <a href="https://maximax67.github.io/cefrpy">https://maximax67.github.io/cefrpy</a><br>
57
  </p>
58
  """
59
 
@@ -63,10 +76,6 @@ h1 {
63
  text-align: center;
64
  display:block;
65
  }
66
-
67
- .hide-container, .gr-group {
68
- background: white !important;
69
- }
70
  """
71
 
72
  nlp = spacy.load(MODEL)
@@ -96,22 +105,58 @@ def get_dict_ents(text: str, tokens: list[tuple[str, str, bool, float, int, int]
96
  return dict_ents
97
 
98
 
99
- def render_visualization(text: str, ents_to_skip: list[str]) -> str:
100
  doc = nlp(text)
101
-
102
  text_analyzer = CEFRSpaCyAnalyzer(entity_types_to_skip=ents_to_skip, abbreviation_mapping=ABBREVIATION_MAPPING)
103
  tokens = text_analyzer.analize_doc(doc)
 
 
 
 
 
104
  dict_ents = get_dict_ents(text, tokens)
105
  html = displacy.render(dict_ents, manual=True, style="ent", options=DISPLACY_RENDER_OPTIONS)
106
 
107
  return html
108
 
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  demo = gr.Blocks(css=CSS)
111
 
112
  with demo:
113
- with gr.Row(variant="default"):
114
- with gr.Group():
115
  with gr.Column():
116
  with gr.Row():
117
  gr.Markdown("# Gradio Demo: cefrpy")
@@ -141,19 +186,44 @@ with demo:
141
  variant="primary"
142
  )
143
 
144
- with gr.Group():
145
  with gr.Row():
146
  gr.Markdown("# Words CEFR level visualization")
147
 
148
  with gr.Row():
149
- render_output = gr.HTML(
150
- value=render_visualization(DEFAULT_TEXT, DEFAULT_ENTITY_ITEMS_TO_SKIP),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  )
152
 
 
 
 
153
  render_button.click(
154
- render_visualization,
155
  inputs=[text_input, ent_input],
156
- outputs=render_output
 
 
 
 
 
 
 
 
157
  )
158
 
159
- demo.launch()
 
17
  'ORG', 'FAC', 'PERSON'
18
  ]
19
 
20
+ TOKEN_ATTRIBUTES = [
21
+ "Token",
22
+ "POS",
23
+ "Skipped",
24
+ "Level",
25
+ "Start",
26
+ "End"
27
+ ]
28
+
29
+ WORDLIST_HEADER = ["Word", "Pos", "CEFR", "Level"]
30
+
31
+ DEFAULT_WORDLIST_SLIDER_LEVEL = 4.0
32
+
33
  DEFAULT_TEXT = """The world's oldest known recipe is for beer. It dates back to around 5,000 BC and was found in ancient Sumeria (modern-day Iraq).
34
 
35
+ Due to thermal expansion, the iron structure of the Eiffel Tower can expand in hot weather, making the tower grow by up to 6 inches (15 centimeters) in height.
36
 
37
+ Did you know that the word "antidisestablishmentarianism" is often cited as one of the longest non-technical words in the English language? It originated in the 19th century in Britain during debates over the disestablishment of the Church of England, and it refers to the opposition to the withdrawal of state support for an established church. This word has gained notoriety for its length and has been used as a challenge for spelling bees and word enthusiasts alike.
38
 
39
  In 2006, a Coca-Cola employee offered to sell Coca-Cola secrets to Pepsi. Pepsi responded by notifying Coca-Cola, and the FBI set up a sting operation to catch the culprit.
40
 
41
+ Like humans, cows form strong social bonds and often have "best friends" within their herds. They display complex social behaviors, including grooming, playing, and even grieving when separated from their friends."""
42
 
43
  DISPLACY_RENDER_OPTIONS = {
44
  "colors": {
 
65
 
66
  LINKS_HTML = """
67
  <p>
68
+ &ensp;Github: <a href="https://github.com/Maximax67/cefrpy">link</a><br>
69
+ &ensp;Docs: <a href="https://maximax67.github.io/cefrpy">link</a><br>
70
  </p>
71
  """
72
 
 
76
  text-align: center;
77
  display:block;
78
  }
 
 
 
 
79
  """
80
 
81
  nlp = spacy.load(MODEL)
 
105
  return dict_ents
106
 
107
 
108
+ def get_cefr_tokens(text: str, ents_to_skip: list[str]) -> list[tuple[str, str, bool, float, int, int]]:
109
  doc = nlp(text)
 
110
  text_analyzer = CEFRSpaCyAnalyzer(entity_types_to_skip=ents_to_skip, abbreviation_mapping=ABBREVIATION_MAPPING)
111
  tokens = text_analyzer.analize_doc(doc)
112
+
113
+ return tokens
114
+
115
+
116
+ def get_html_visualization(text: str, tokens: list[tuple[str, str, bool, float, int, int]]) -> str:
117
  dict_ents = get_dict_ents(text, tokens)
118
  html = displacy.render(dict_ents, manual=True, style="ent", options=DISPLACY_RENDER_OPTIONS)
119
 
120
  return html
121
 
122
 
123
+ def get_wordlist_set(tokens: list[tuple[str, str, bool, float, int, int]],
124
+ min_level: float) -> set[tuple[str, str, bool, float, int, int]]:
125
+ filtered_tokens = set()
126
+ for word, pos, _, level, _, _ in tokens:
127
+ if level and level >= min_level:
128
+ filtered_tokens.add((word.lower(), pos, str(CEFRLevel(round(level))), level))
129
+
130
+ return filtered_tokens
131
+
132
+
133
+ def get_wordlist(tokens: list[tuple[str, str, bool, float, int, int]], min_level: float):
134
+ wordlist_set = get_wordlist_set(tokens, min_level)
135
+ wordlist = list(wordlist_set)
136
+ wordlist.sort()
137
+
138
+ return wordlist
139
+
140
+
141
+ def get_wordlist_from_dataframe(dataframe, min_level: float):
142
+ return get_wordlist(dataframe.values, min_level)
143
+
144
+
145
+ def process_text(text: str, ents_to_skip: list[str] | None = DEFAULT_ENTITY_ITEMS_TO_SKIP, min_level: float = DEFAULT_WORDLIST_SLIDER_LEVEL) -> tuple[list[list], str]:
146
+ tokens = get_cefr_tokens(text, ents_to_skip)
147
+ html = get_html_visualization(text, tokens)
148
+ wordlist = get_wordlist(tokens, min_level)
149
+
150
+ return tokens, wordlist, html
151
+
152
+
153
+ initial_tokens, initial_wordlist, initial_html = process_text(DEFAULT_TEXT)
154
+
155
  demo = gr.Blocks(css=CSS)
156
 
157
  with demo:
158
+ with gr.Row():
159
+ with gr.Column():
160
  with gr.Column():
161
  with gr.Row():
162
  gr.Markdown("# Gradio Demo: cefrpy")
 
186
  variant="primary"
187
  )
188
 
189
+ with gr.Column():
190
  with gr.Row():
191
  gr.Markdown("# Words CEFR level visualization")
192
 
193
  with gr.Row():
194
+ rendered_html = gr.HTML(initial_html)
195
+
196
+ with gr.Row():
197
+ with gr.Column():
198
+ with gr.Row():
199
+ tokens_output = gr.Dataframe(headers=TOKEN_ATTRIBUTES, value=initial_tokens, interactive=False)
200
+
201
+ with gr.Column():
202
+ with gr.Row():
203
+ min_level_slider = gr.Slider(
204
+ minimum=1.0,
205
+ maximum=6.0,
206
+ value=DEFAULT_WORDLIST_SLIDER_LEVEL,
207
+ step=0.02,
208
+ interactive=True,
209
+ label="Min level to generate word list"
210
  )
211
 
212
+ with gr.Row():
213
+ wordlist = gr.Dataframe(headers=WORDLIST_HEADER, value=initial_wordlist, interactive=False)
214
+
215
  render_button.click(
216
+ process_text,
217
  inputs=[text_input, ent_input],
218
+ outputs=[tokens_output, wordlist, rendered_html],
219
+ api_name="process_text"
220
+ )
221
+
222
+ min_level_slider.release(
223
+ get_wordlist_from_dataframe,
224
+ inputs=[tokens_output, min_level_slider],
225
+ outputs=[wordlist],
226
+ api_name=False
227
  )
228
 
229
+ demo.launch(show_api=True)