Hakyung Sung commited on
Commit
1c63838
·
1 Parent(s): 1b16a8e

Correct app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -69
app.py CHANGED
@@ -15,21 +15,22 @@ if 'parser' not in nlp.pipe_names and 'senter' not in nlp.pipe_names:
15
 
16
  def get_highlighted_text(doc):
17
  """
18
- Wraps detected ASCs in each sentence with a <span> tag that carries the entity tag as a
19
- data attribute. The output HTML is wrapped with a CSS <style> block that applies your desired styling.
 
20
  """
21
  highlighted_sentences = []
22
  for sent in doc.sents:
23
  text = sent.text
24
- # Find all entities that are fully contained within this sentence.
25
  ents_in_sent = [ent for ent in doc.ents if ent.start_char >= sent.start_char and ent.end_char <= sent.end_char]
26
  if ents_in_sent:
27
- # Process entities in reverse order so that character offsets remain valid.
28
  ents_in_sent = sorted(ents_in_sent, key=lambda x: x.start_char, reverse=True)
29
  for ent in ents_in_sent:
30
  ent_start = ent.start_char - sent.start_char
31
  ent_end = ent.end_char - sent.start_char
32
- # Wrap the detected entity with a span and include its tag via the data-entity attribute.
33
  text = (
34
  text[:ent_start]
35
  + f'<span class="entity" data-entity="{ent.label_}">'
@@ -40,7 +41,7 @@ def get_highlighted_text(doc):
40
  highlighted_sentences.append(text)
41
  result = "<br><br>".join(highlighted_sentences)
42
 
43
- # Embed the provided CSS for styling.
44
  style = """
45
  <style>
46
  body {
@@ -56,60 +57,7 @@ def get_highlighted_text(doc):
56
  background-color: white;
57
  border-radius: 8px;
58
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
59
- }
60
- h1 {
61
- font-size: 2rem;
62
- font-weight: 700;
63
- margin-bottom: 20px;
64
- text-align: center;
65
- }
66
- p, textarea, input {
67
- font-size: 0.9rem;
68
- font-weight: 400;
69
- }
70
- .info {
71
- font-size: 0.9rem;
72
- color: #333;
73
- }
74
- .accuracy-highlight {
75
- color: #007030;
76
- font-weight: bold;
77
- }
78
- input[type="submit"], input[type="button"] {
79
- font-size: 0.9rem;
80
- }
81
- textarea {
82
- width: 95%;
83
- max-width: 95%;
84
- padding: 20px;
85
- margin-bottom: 10px;
86
- border: 1px solid #ccc;
87
- border-radius: 4px;
88
- resize: vertical;
89
- }
90
- input[type="submit"], input[type="button"] {
91
- background-color: #007030;
92
- color: white;
93
- padding: 10px 20px;
94
- border: none;
95
- border-radius: 4px;
96
- cursor: pointer;
97
- margin-right: 10px;
98
- }
99
- input[type="submit"]:hover, input[type="button"]:hover {
100
- background-color: #45a049;
101
- }
102
- .results {
103
- margin-top: 20px;
104
- line-height: 2.5;
105
- }
106
- .tag-counts {
107
- margin-top: 2px;
108
- text-align: center;
109
- }
110
- .tag-counts h2 {
111
- margin-bottom: 5px;
112
- text-align: left;
113
  }
114
  .entity {
115
  display: inline-block;
@@ -205,7 +153,6 @@ def get_highlighted_text(doc):
205
  """
206
  return style + "<div class='container'>" + result + "</div>"
207
 
208
-
209
  def process_text(input_text):
210
  """
211
  Process input text to detect and tag ASCs.
@@ -224,24 +171,25 @@ def process_text(input_text):
224
 
225
  return get_highlighted_text(doc)
226
 
227
- # Define sample text
228
- sample_text = "Nancy sliced the tire open."
229
 
230
  def fill_sample_text():
231
  return sample_text
232
 
233
  # Build the Gradio interface.
234
  with gr.Blocks() as demo:
235
- gr.Markdown("# ASC tagger demo")
236
- gr.Markdown("This demo version of the Argument Structure Construction (ASC) tagger processes sentences and identifies nine types of ASCs. The identified ASCs are highlighted in the output text.")
237
 
238
- input_textbox = gr.Textbox(lines=5, label="Input text", placeholder="Enter text here...")
239
- output_html = gr.HTML(label="Tagged text")
240
- fill_btn = gr.Button("Fill sample sentence")
 
241
  tag_btn = gr.Button("Tag ASCs")
242
 
243
  fill_btn.click(fn=fill_sample_text, inputs=[], outputs=input_textbox)
244
  tag_btn.click(fn=process_text, inputs=input_textbox, outputs=output_html)
245
 
246
  if __name__ == "__main__":
247
- demo.launch(share=True)
 
15
 
16
  def get_highlighted_text(doc):
17
  """
18
+ Wraps detected ASCs in each sentence with a <span> tag that carries the entity tag
19
+ in the data-entity attribute. The final HTML output is prepended with a CSS block that
20
+ applies your desired styles.
21
  """
22
  highlighted_sentences = []
23
  for sent in doc.sents:
24
  text = sent.text
25
+ # Find all entities completely contained within this sentence.
26
  ents_in_sent = [ent for ent in doc.ents if ent.start_char >= sent.start_char and ent.end_char <= sent.end_char]
27
  if ents_in_sent:
28
+ # Process entities in reverse order to preserve character offsets.
29
  ents_in_sent = sorted(ents_in_sent, key=lambda x: x.start_char, reverse=True)
30
  for ent in ents_in_sent:
31
  ent_start = ent.start_char - sent.start_char
32
  ent_end = ent.end_char - sent.start_char
33
+ # Wrap the entity text with a <span> that carries the entity tag via data-entity.
34
  text = (
35
  text[:ent_start]
36
  + f'<span class="entity" data-entity="{ent.label_}">'
 
41
  highlighted_sentences.append(text)
42
  result = "<br><br>".join(highlighted_sentences)
43
 
44
+ # Embed the CSS that controls the appearance (using your tag style) and increases line spacing.
45
  style = """
46
  <style>
47
  body {
 
57
  background-color: white;
58
  border-radius: 8px;
59
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
60
+ line-height: 2em; /* Increased line spacing */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  }
62
  .entity {
63
  display: inline-block;
 
153
  """
154
  return style + "<div class='container'>" + result + "</div>"
155
 
 
156
  def process_text(input_text):
157
  """
158
  Process input text to detect and tag ASCs.
 
171
 
172
  return get_highlighted_text(doc)
173
 
174
+ # Define a simpler sample text.
175
+ sample_text = "Oliver baked magical bread. Lyla enjoyed the treat."
176
 
177
  def fill_sample_text():
178
  return sample_text
179
 
180
  # Build the Gradio interface.
181
  with gr.Blocks() as demo:
182
+ gr.Markdown("# ASC Tagger Demo")
183
+ gr.Markdown("Enter some text to have ASCs tagged. Use the button below to fill in sample text.")
184
 
185
+ # The input textbox is not modified by submission.
186
+ input_textbox = gr.Textbox(lines=5, label="Input Text", placeholder="Enter text here...")
187
+ output_html = gr.HTML(label="Tagged Text")
188
+ fill_btn = gr.Button("Fill Sample Text")
189
  tag_btn = gr.Button("Tag ASCs")
190
 
191
  fill_btn.click(fn=fill_sample_text, inputs=[], outputs=input_textbox)
192
  tag_btn.click(fn=process_text, inputs=input_textbox, outputs=output_html)
193
 
194
  if __name__ == "__main__":
195
+ demo.launch()