File size: 1,857 Bytes
fd0fe48
3b9fb2c
 
ab9767d
fd0fe48
 
ab9767d
 
 
fd0fe48
c35975c
fd0fe48
 
ab9767d
fd0fe48
 
 
ab9767d
 
fd0fe48
ab9767d
 
fd0fe48
 
 
3d53082
fd0fe48
 
ab9767d
 
fd0fe48
 
 
 
306e33b
ab9767d
 
 
 
 
 
 
 
fd0fe48
 
ab9767d
fd0fe48
 
 
 
 
306e33b
fd0fe48
aa04021
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import re
import gradio as gr

# Simulated GLiNER output
SIMULATED_ENTITIES = {
    "Household Survey Data": "named dataset",
    "Ethiopia":             "data geography",
    "2020":                 "reference year",
    "World Bank":           "publisher"
}

def highlight_text(text):
    """
    Return a dict with the original text and a list of start/end/label spans.
    """
    entities = []
    for phrase, label in SIMULATED_ENTITIES.items():
        # find all occurrences of phrase
        for m in re.finditer(re.escape(phrase), text):
            entities.append({
                "start": m.start(),
                "end":   m.end(),
                "label": label
            })
    return {"text": text, "entities": entities}

with gr.Blocks() as demo:
    gr.Markdown("## Simulated GLiNER Entity Highlighter\n"
                "Type a sentence (or use the default) then hit **Highlight Entities** or press Enter.")
    
    text_input = gr.Textbox(
        label="Input Text",
        lines=3,
        value="The Household Survey Data for Ethiopia in 2020 was published by the World Bank."
    )
    highlight_btn = gr.Button("Highlight Entities")
    highlighted   = gr.HighlightedText(label="Highlighted Entities")
    
    # Wire up both the button and pressing Enter to our highlighter
    highlight_btn.click(fn=highlight_text, inputs=text_input, outputs=highlighted)
    text_input.submit(fn=highlight_text, inputs=text_input, outputs=highlighted)
    # Also do an initial run on load so you immediately see highlights
    demo.load(fn=highlight_text, inputs=text_input, outputs=highlighted)
    
    gr.Markdown("""
**Entity Legend**  
- **named dataset**: Household Survey Data  
- **data geography**: Ethiopia  
- **reference year**: 2020  
- **publisher**: World Bank
""")

if __name__ == "__main__":
    demo.launch(inline=True)