rafmacalaba commited on
Commit
d64d47b
·
1 Parent(s): aa04021
Files changed (1) hide show
  1. app.py +29 -32
app.py CHANGED
@@ -1,54 +1,51 @@
1
  import re
2
  import gradio as gr
3
 
4
- # Simulated GLiNER output
5
- SIMULATED_ENTITIES = {
6
  "Household Survey Data": "named dataset",
7
  "Ethiopia": "data geography",
8
  "2020": "reference year",
9
  "World Bank": "publisher"
10
  }
11
 
12
- def highlight_text(text):
13
- """
14
- Return a dict with the original text and a list of start/end/label spans.
15
- """
16
  entities = []
17
- for phrase, label in SIMULATED_ENTITIES.items():
18
- # find all occurrences of phrase
19
  for m in re.finditer(re.escape(phrase), text):
20
  entities.append({
21
- "start": m.start(),
22
- "end": m.end(),
23
- "label": label
24
  })
25
  return {"text": text, "entities": entities}
26
 
27
  with gr.Blocks() as demo:
28
- gr.Markdown("## Simulated GLiNER Entity Highlighter\n"
29
- "Type a sentence (or use the default) then hit **Highlight Entities** or press Enter.")
30
-
31
- text_input = gr.Textbox(
32
- label="Input Text",
33
  lines=3,
34
- value="The Household Survey Data for Ethiopia in 2020 was published by the World Bank."
 
 
35
  )
36
- highlight_btn = gr.Button("Highlight Entities")
37
- highlighted = gr.HighlightedText(label="Highlighted Entities")
38
-
39
- # Wire up both the button and pressing Enter to our highlighter
40
- highlight_btn.click(fn=highlight_text, inputs=text_input, outputs=highlighted)
41
- text_input.submit(fn=highlight_text, inputs=text_input, outputs=highlighted)
42
- # Also do an initial run on load so you immediately see highlights
43
- demo.load(fn=highlight_text, inputs=text_input, outputs=highlighted)
44
-
45
  gr.Markdown("""
46
- **Entity Legend**
47
- - **named dataset**: Household Survey Data
48
- - **data geography**: Ethiopia
49
- - **reference year**: 2020
50
- - **publisher**: World Bank
51
  """)
52
 
53
  if __name__ == "__main__":
54
- demo.launch(inline=True)
 
1
  import re
2
  import gradio as gr
3
 
4
+ # Simulated GLiNER‐style output
5
+ SIM_ENTITIES = {
6
  "Household Survey Data": "named dataset",
7
  "Ethiopia": "data geography",
8
  "2020": "reference year",
9
  "World Bank": "publisher"
10
  }
11
 
12
+ def annotate_text(text):
 
 
 
13
  entities = []
14
+ for phrase, label in SIM_ENTITIES.items():
 
15
  for m in re.finditer(re.escape(phrase), text):
16
  entities.append({
17
+ "entity": label, # note: 'entity' key
18
+ "start": m.start(),
19
+ "end": m.end(),
20
  })
21
  return {"text": text, "entities": entities}
22
 
23
  with gr.Blocks() as demo:
24
+ gr.Markdown("## Simulated GLiNER Highlighter\n"
25
+ "Type or paste a sentence and click **Highlight**.")
26
+
27
+ txt = gr.Textbox(
 
28
  lines=3,
29
+ value="The Household Survey Data for Ethiopia in 2020 was published by the World Bank.",
30
+ label="Input Text",
31
+ placeholder="Enter any sentence here…"
32
  )
33
+ btn = gr.Button("Highlight")
34
+ out = gr.HighlightedText(label="Annotated Entities")
35
+
36
+ # fire on button click or Enter
37
+ btn.click(fn=annotate_text, inputs=txt, outputs=out)
38
+ txt.submit(fn=annotate_text, inputs=txt, outputs=out)
39
+ # initial render
40
+ demo.load(fn=annotate_text, inputs=txt, outputs=out)
41
+
42
  gr.Markdown("""
43
+ **Legend**
44
+ - **named dataset** Household Survey Data
45
+ - **data geography** Ethiopia
46
+ - **reference year** 2020
47
+ - **publisher** World Bank
48
  """)
49
 
50
  if __name__ == "__main__":
51
+ demo.launch(debug=True)