rafmacalaba commited on
Commit
cd683ff
·
1 Parent(s): 9c95361

use ner and rel

Browse files
Files changed (1) hide show
  1. app.py +46 -29
app.py CHANGED
@@ -1,19 +1,31 @@
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 "
@@ -23,29 +35,34 @@ SAMPLE_TEXT = (
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)
@@ -63,4 +80,4 @@ with gr.Blocks() as demo:
63
  """)
64
 
65
  if __name__ == "__main__":
66
- demo.launch()
 
1
  import re
2
  import gradio as gr
3
 
4
+ # Your actual model outputs:
5
+ ner = [
6
+ {
7
+ 'start': 12,
8
+ 'end': 30,
9
+ 'text': 'Home Visits Survey',
10
+ 'label': 'named dataset',
11
+ 'score': 0.9947463870048523
12
+ }
13
+ ]
14
+
15
+ relations = {
16
+ 'Home Visits Survey': [
17
+ {'source': 'Home Visits Survey', 'relation': 'data geography', 'target': 'Jordan', 'score': 0.6180844902992249},
18
+ {'source': 'Home Visits Survey', 'relation': 'version', 'target': 'Round II', 'score': 0.9688164591789246},
19
+ {'source': 'Home Visits Survey', 'relation': 'acronym', 'target': 'HV', 'score': 0.9140607714653015},
20
+ {'source': 'Home Visits Survey', 'relation': 'author', 'target': 'UNHCR', 'score': 0.7762154340744019},
21
+ {'source': 'Home Visits Survey', 'relation': 'author', 'target': 'World Food Programme', 'score': 0.6582539677619934},
22
+ {'source': 'Home Visits Survey', 'relation': 'reference year', 'target': '2013', 'score': 0.524115264415741},
23
+ {'source': 'Home Visits Survey', 'relation': 'publication year', 'target': '2014', 'score': 0.6853994131088257},
24
+ {'source': 'Home Visits Survey', 'relation': 'data description', 'target': 'detailed socio-economic, health, and protection data', 'score': 0.6544178128242493},
25
+ ]
26
  }
27
 
28
+ # The sample sentence you want to highlight:
29
  SAMPLE_TEXT = (
30
  "The Jordan Home Visits Survey, Round II (HV), was carried out by UNHCR and the World Food "
31
  "Programme between November 2013 and September 2014. Through in-home visits to Syrian refugee "
 
35
 
36
  def highlight_text(text):
37
  entities = []
38
+ # 1) NER spans
39
+ for ent in ner:
40
+ entities.append({
41
+ "entity": ent["label"],
42
+ "start": ent["start"],
43
+ "end": ent["end"],
44
+ })
45
+ # 2) RE spans: annotate each target with its relation label
46
+ for src, rels in relations.items():
47
+ for r in rels:
48
+ label = r["relation"]
49
+ target = r["target"]
50
+ for m in re.finditer(re.escape(target), text):
51
+ entities.append({
52
+ "entity": label,
53
+ "start": m.start(),
54
+ "end": m.end(),
55
+ })
56
  return {"text": text, "entities": entities}
57
 
58
  with gr.Blocks() as demo:
59
+ gr.Markdown("## Data Use Detector\n"
60
+ "Input text and the model will highlight the entities it detects.")
 
61
 
62
+ txt_in = gr.Textbox(label="Input Text", lines=4, value=SAMPLE_TEXT)
63
+ btn = gr.Button("Highlight Entities")
 
 
 
 
64
  txt_out = gr.HighlightedText(label="Annotated Entities")
65
 
 
66
  btn.click(fn=highlight_text, inputs=txt_in, outputs=txt_out)
67
  txt_in.submit(fn=highlight_text, inputs=txt_in, outputs=txt_out)
68
  demo.load(fn=highlight_text, inputs=txt_in, outputs=txt_out)
 
80
  """)
81
 
82
  if __name__ == "__main__":
83
+ demo.launch()