rafmacalaba commited on
Commit
ab9767d
·
1 Parent(s): fd0fe48
Files changed (1) hide show
  1. app.py +20 -16
app.py CHANGED
@@ -1,45 +1,49 @@
1
  import re
2
  import gradio as gr
3
 
4
- # A simulated GLiNER output mapping for demonstration:
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
- Simulate GLiNER entity extraction by looking for a few known phrases
15
- and returning start/end spans + labels.
16
  """
17
  entities = []
18
  for phrase, label in SIMULATED_ENTITIES.items():
19
- for match in re.finditer(re.escape(phrase), text):
 
20
  entities.append({
21
- "start": match.start(),
22
- "end": match.end(),
23
  "label": label
24
  })
25
- # Return in the format gr.HighlightedText expects:
26
  return {"text": text, "entities": entities}
27
 
28
- # Build the Gradio interface
29
  with gr.Blocks() as demo:
30
  gr.Markdown("## Simulated GLiNER Entity Highlighter\n"
31
- "Enter any sentence (or use the default) and see simulated entity highlights.")
 
32
  text_input = gr.Textbox(
33
  label="Input Text",
34
  lines=3,
35
  value="The Household Survey Data for Ethiopia in 2020 was published by the World Bank."
36
  )
37
- highlighted = gr.HighlightedText(label="Highlighted Entities")
38
- # Trigger highlighting whenever the text changes
39
- text_input.change(fn=highlight_text, inputs=text_input, outputs=highlighted)
 
 
 
 
 
40
 
41
  gr.Markdown("""
42
- **Entity legend**
43
  - **named dataset**: Household Survey Data
44
  - **data geography**: Ethiopia
45
  - **reference year**: 2020
 
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