Hakyung Sung commited on
Commit
fd84879
·
1 Parent(s): 08b406d

Correct app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -14
app.py CHANGED
@@ -29,8 +29,7 @@ color_scheme = {
29
  def get_highlighted_text(doc):
30
  """
31
  Wraps detected ASCs in each sentence with a span tag containing an inline style.
32
- The inline style sets a custom background color based on the tag.
33
- Additionally, the entity's tag name is appended as a small superscript to make it always visible.
34
  """
35
  highlighted_sentences = []
36
  for sent in doc.sents:
@@ -38,19 +37,19 @@ def get_highlighted_text(doc):
38
  # Find all entities fully contained within this sentence.
39
  ents_in_sent = [ent for ent in doc.ents if ent.start_char >= sent.start_char and ent.end_char <= sent.end_char]
40
  if ents_in_sent:
41
- # Process entities in reverse order so character offsets remain valid.
42
  ents_in_sent = sorted(ents_in_sent, key=lambda x: x.start_char, reverse=True)
43
  for ent in ents_in_sent:
44
  ent_start = ent.start_char - sent.start_char
45
  ent_end = ent.end_char - sent.start_char
46
- # Get the color from our scheme; use default if not defined.
47
  color = color_scheme.get(ent.label_, "#cccccc")
48
- # Wrap the entity with inline style and append its label as a superscript.
49
  s = (
50
  s[:ent_start]
51
  + f'<span style="background-color: {color}; font-weight: bold;" title="{ent.label_}">'
52
  + s[ent_start:ent_end]
53
- + f'<sup style="font-size:0.6em; margin-left:2px;">{ent.label_}</sup>'
54
  + '</span>'
55
  + s[ent_end:]
56
  )
@@ -95,7 +94,7 @@ sample_text = (
95
  "music to everyone again.' "
96
  "That evening, as Lyla set up by the village fountain to play, something miraculous happened. Her old, broken lute began to transform, glowing just "
97
  "like the bread had. In her hands now was the most beautiful lute she had ever seen, shimmering with gold and silver strings. When she strummed it, "
98
- "the music that flowed out wasn’t just sound—it was pure joy."
99
  "The village gathered around her, drawn by the enchanting melodies that seemed to fill their hearts with warmth and happiness. People laughed, danced, "
100
  "and even sang along, forgetting their worries. "
101
  "Oliver watched from his bakery window, smiling to himself. He had no idea what Lyla had wished for, but he could see its magic at work. He knew his "
@@ -104,7 +103,7 @@ sample_text = (
104
  "distant lands just to hear her play, and with every note, the village grew a little brighter, a little happier. "
105
  "As for Oliver, he continued to bake with joy in his heart, knowing that sometimes the smallest acts of kindness—like baking a loaf of bread—could "
106
  "make the biggest difference in someone’s life. "
107
- "And so, the village became known not just for its delicious bread, but for its music, magic, and the happiness that lingered in every corner. The end. "
108
  "(Generated by ChatGPT 4o 🤖)"
109
  )
110
 
@@ -114,19 +113,17 @@ def fill_sample_text():
114
  # Build the Gradio interface.
115
  with gr.Blocks() as demo:
116
  gr.Markdown("# ASC tagger demo")
117
- gr.Markdown("Enter some text to have ASCs tagged with a custom inline color scheme. Use the button below to fill in a sample text.")
118
 
119
  input_textbox = gr.Textbox(lines=10, label="Input Text", placeholder="Enter text here...")
120
  output_html = gr.HTML(label="Tagged Text")
121
- # Button to fill sample text into the input textbox.
122
- fill_btn = gr.Button("Fill Sample Text")
123
- # Button to analyze the text.
124
  tag_btn = gr.Button("Tag ASCs")
125
-
 
 
126
  # When "Fill Sample Text" is clicked, populate the textbox.
127
  fill_btn.click(fn=fill_sample_text, inputs=[], outputs=input_textbox)
128
  # When "Tag ASCs" is clicked, process the input text.
129
- tag_btn.click(fn=process_text, inputs=input_textbox, outputs=output_html)
130
 
131
  if __name__ == "__main__":
132
  demo.launch()
 
29
  def get_highlighted_text(doc):
30
  """
31
  Wraps detected ASCs in each sentence with a span tag containing an inline style.
32
+ In addition, appends the entity's tag name in an extra span so the label is always visible.
 
33
  """
34
  highlighted_sentences = []
35
  for sent in doc.sents:
 
37
  # Find all entities fully contained within this sentence.
38
  ents_in_sent = [ent for ent in doc.ents if ent.start_char >= sent.start_char and ent.end_char <= sent.end_char]
39
  if ents_in_sent:
40
+ # Process entities in reverse order so the character offsets remain valid.
41
  ents_in_sent = sorted(ents_in_sent, key=lambda x: x.start_char, reverse=True)
42
  for ent in ents_in_sent:
43
  ent_start = ent.start_char - sent.start_char
44
  ent_end = ent.end_char - sent.start_char
45
+ # Get the color based on the tag; default to light gray if not found.
46
  color = color_scheme.get(ent.label_, "#cccccc")
47
+ # Wrap the entity text with an inline style and add the tag label in a separate span.
48
  s = (
49
  s[:ent_start]
50
  + f'<span style="background-color: {color}; font-weight: bold;" title="{ent.label_}">'
51
  + s[ent_start:ent_end]
52
+ + f'<span style="display:inline-block; vertical-align: super; font-size: 0.8em; margin-left: 2px; color: black;">{ent.label_}</span>'
53
  + '</span>'
54
  + s[ent_end:]
55
  )
 
94
  "music to everyone again.' "
95
  "That evening, as Lyla set up by the village fountain to play, something miraculous happened. Her old, broken lute began to transform, glowing just "
96
  "like the bread had. In her hands now was the most beautiful lute she had ever seen, shimmering with gold and silver strings. When she strummed it, "
97
+ "the music that flowed out wasn’t just sound—it was pure joy. "
98
  "The village gathered around her, drawn by the enchanting melodies that seemed to fill their hearts with warmth and happiness. People laughed, danced, "
99
  "and even sang along, forgetting their worries. "
100
  "Oliver watched from his bakery window, smiling to himself. He had no idea what Lyla had wished for, but he could see its magic at work. He knew his "
 
103
  "distant lands just to hear her play, and with every note, the village grew a little brighter, a little happier. "
104
  "As for Oliver, he continued to bake with joy in his heart, knowing that sometimes the smallest acts of kindness—like baking a loaf of bread—could "
105
  "make the biggest difference in someone’s life. "
106
+ "And so, the village became known not just for its delicious bread, but for its music, magic, and the happiness that lingered in every corner. "
107
  "(Generated by ChatGPT 4o 🤖)"
108
  )
109
 
 
113
  # Build the Gradio interface.
114
  with gr.Blocks() as demo:
115
  gr.Markdown("# ASC tagger demo")
116
+ gr.Markdown("Enter some text to have ASCs tagged with a custom inline color scheme. Use the button below to fill in sample text.")
117
 
118
  input_textbox = gr.Textbox(lines=10, label="Input Text", placeholder="Enter text here...")
119
  output_html = gr.HTML(label="Tagged Text")
 
 
 
120
  tag_btn = gr.Button("Tag ASCs")
121
+ fill_btn = gr.Button("Fill Sample Text")
122
+
123
+ tag_btn.click(fn=process_text, inputs=input_textbox, outputs=output_html)
124
  # When "Fill Sample Text" is clicked, populate the textbox.
125
  fill_btn.click(fn=fill_sample_text, inputs=[], outputs=input_textbox)
126
  # When "Tag ASCs" is clicked, process the input text.
 
127
 
128
  if __name__ == "__main__":
129
  demo.launch()