rafmacalaba commited on
Commit
9c95361
·
1 Parent(s): d64d47b
Files changed (1) hide show
  1. app.py +43 -28
app.py CHANGED
@@ -1,51 +1,66 @@
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)
 
1
  import re
2
  import gradio as gr
3
 
4
+ # Your actual GLiNER predictions
5
  SIM_ENTITIES = {
6
+ "Home Visits Survey": "named dataset",
7
+ "Jordan": "data geography",
8
+ "Round II": "version",
9
+ "HV": "acronym",
10
+ "UNHCR": "author",
11
+ "World Food Programme": "author",
12
+ "2013": "reference year",
13
+ "2014": "publication year",
14
+ "detailed socio-economic, health, and protection data": "data description",
15
  }
16
 
17
+ SAMPLE_TEXT = (
18
+ "The Jordan Home Visits Survey, Round II (HV), was carried out by UNHCR and the World Food "
19
+ "Programme between November 2013 and September 2014. Through in-home visits to Syrian refugee "
20
+ "households in Jordan, it gathered detailed socio-economic, health, and protection data—each "
21
+ "household tagged with a unique ID to allow longitudinal tracking."
22
+ )
23
+
24
+ def highlight_text(text):
25
  entities = []
26
  for phrase, label in SIM_ENTITIES.items():
27
  for m in re.finditer(re.escape(phrase), text):
28
  entities.append({
29
+ "entity": label,
30
  "start": m.start(),
31
  "end": m.end(),
32
  })
33
  return {"text": text, "entities": entities}
34
 
35
  with gr.Blocks() as demo:
36
+ gr.Markdown("## GLiNER NER + RE Highlighting\n"
37
+ "This demo uses your model’s actual predictions to annotate the sample sentence.\n\n"
38
+ "**Labels & spans** matching your `ner` and `relations` outputs will be colored below.")
39
+
40
+ txt_in = gr.Textbox(
 
41
  label="Input Text",
42
+ lines=4,
43
+ value=SAMPLE_TEXT
44
  )
45
+ btn = gr.Button("Highlight Entities")
46
+ txt_out = gr.HighlightedText(label="Annotated Entities")
47
+
48
+ # wire up
49
+ btn.click(fn=highlight_text, inputs=txt_in, outputs=txt_out)
50
+ txt_in.submit(fn=highlight_text, inputs=txt_in, outputs=txt_out)
51
+ demo.load(fn=highlight_text, inputs=txt_in, outputs=txt_out)
52
+
 
53
  gr.Markdown("""
54
  **Legend**
55
+ - **named dataset** → Home Visits Survey
56
+ - **data geography** → Jordan
57
+ - **version** → Round II
58
+ - **acronym** → HV
59
+ - **author** → UNHCR, World Food Programme
60
+ - **reference year** → 2013
61
+ - **publication year** → 2014
62
+ - **data description** → detailed socio-economic, health, and protection data
63
  """)
64
 
65
  if __name__ == "__main__":
66
+ demo.launch()