Spaces:
Running
Running
Commit
·
d8c3809
1
Parent(s):
13e7831
add txtbox
Browse files
app.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
import re
|
|
|
2 |
import gradio as gr
|
3 |
|
4 |
-
# Your
|
5 |
ner = [
|
6 |
{
|
7 |
'start': 12,
|
@@ -12,6 +13,7 @@ ner = [
|
|
12 |
}
|
13 |
]
|
14 |
|
|
|
15 |
relations = {
|
16 |
'Home Visits Survey': [
|
17 |
{'source': 'Home Visits Survey', 'relation': 'data geography', 'target': 'Jordan', 'score': 0.6180844902992249},
|
@@ -25,12 +27,8 @@ relations = {
|
|
25 |
]
|
26 |
}
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
"Programme between November 2013 and September 2014. Through in-home visits to Syrian refugee "
|
31 |
-
"households in Jordan, it gathered detailed socio-economic, health, and protection data—each "
|
32 |
-
"household tagged with a unique ID to allow longitudinal tracking."
|
33 |
-
)
|
34 |
|
35 |
def highlight_text(text):
|
36 |
entities = []
|
@@ -42,13 +40,11 @@ def highlight_text(text):
|
|
42 |
"end": ent["end"],
|
43 |
})
|
44 |
# 2) RE spans
|
45 |
-
for
|
46 |
-
for r in
|
47 |
-
|
48 |
-
target = r["target"]
|
49 |
-
for m in re.finditer(re.escape(target), text):
|
50 |
entities.append({
|
51 |
-
"entity":
|
52 |
"start": m.start(),
|
53 |
"end": m.end(),
|
54 |
})
|
@@ -56,26 +52,20 @@ def highlight_text(text):
|
|
56 |
|
57 |
with gr.Blocks() as demo:
|
58 |
gr.Markdown("## Data Use Detector\n"
|
59 |
-
"
|
60 |
|
61 |
-
txt_in
|
62 |
-
btn
|
63 |
-
txt_out
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
#
|
66 |
btn.click(fn=highlight_text, inputs=txt_in, outputs=txt_out)
|
67 |
|
68 |
-
gr.Markdown("""
|
69 |
-
**Legend**
|
70 |
-
- **named dataset** → Home Visits Survey
|
71 |
-
- **data geography** → Jordan
|
72 |
-
- **version** → Round II
|
73 |
-
- **acronym** → HV
|
74 |
-
- **author** → UNHCR, World Food Programme
|
75 |
-
- **reference year** → 2013
|
76 |
-
- **publication year** → 2014
|
77 |
-
- **data description** → detailed socio-economic, health, and protection data
|
78 |
-
""")
|
79 |
-
|
80 |
if __name__ == "__main__":
|
81 |
demo.launch()
|
|
|
1 |
import re
|
2 |
+
import json
|
3 |
import gradio as gr
|
4 |
|
5 |
+
# Your model’s raw NER output (we trust these start/end indices)
|
6 |
ner = [
|
7 |
{
|
8 |
'start': 12,
|
|
|
13 |
}
|
14 |
]
|
15 |
|
16 |
+
# Your model’s raw RE output
|
17 |
relations = {
|
18 |
'Home Visits Survey': [
|
19 |
{'source': 'Home Visits Survey', 'relation': 'data geography', 'target': 'Jordan', 'score': 0.6180844902992249},
|
|
|
27 |
]
|
28 |
}
|
29 |
|
30 |
+
# Exact sample text
|
31 |
+
SAMPLE_TEXT = """The Jordan Home Visits Survey, Round II (HV), was carried out by UNHCR and the World Food Programme between November 2013 and September 2014. Through in-home visits to Syrian refugee households in Jordan, it gathered detailed socio-economic, health, and protection data—each household tagged with a unique ID to allow longitudinal tracking."""
|
|
|
|
|
|
|
|
|
32 |
|
33 |
def highlight_text(text):
|
34 |
entities = []
|
|
|
40 |
"end": ent["end"],
|
41 |
})
|
42 |
# 2) RE spans
|
43 |
+
for rel_list in relations.values():
|
44 |
+
for r in rel_list:
|
45 |
+
for m in re.finditer(re.escape(r["target"]), text):
|
|
|
|
|
46 |
entities.append({
|
47 |
+
"entity": r["relation"],
|
48 |
"start": m.start(),
|
49 |
"end": m.end(),
|
50 |
})
|
|
|
52 |
|
53 |
with gr.Blocks() as demo:
|
54 |
gr.Markdown("## Data Use Detector\n"
|
55 |
+
"Paste or edit the sample text, then click **Highlight** to run inference.")
|
56 |
|
57 |
+
txt_in = gr.Textbox(label="Input Text", lines=4, value=SAMPLE_TEXT)
|
58 |
+
btn = gr.Button("Highlight")
|
59 |
+
txt_out = gr.HighlightedText(label="Annotated Entities")
|
60 |
+
ner_rel_box = gr.Textbox(
|
61 |
+
label="Model Predictions (JSON)",
|
62 |
+
value=json.dumps({"ner": ner, "relations": relations}, indent=2),
|
63 |
+
lines=15,
|
64 |
+
interactive=True
|
65 |
+
)
|
66 |
|
67 |
+
# Only trigger on button click
|
68 |
btn.click(fn=highlight_text, inputs=txt_in, outputs=txt_out)
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
if __name__ == "__main__":
|
71 |
demo.launch()
|