rafmacalaba commited on
Commit
fd0fe48
·
1 Parent(s): a508c57
Files changed (1) hide show
  1. app.py +44 -60
app.py CHANGED
@@ -1,66 +1,50 @@
 
1
  import gradio as gr
2
- from typing import Union, Dict, Any, List
3
 
4
- # Sample pre-calculated entities
5
- sample_text = (
6
- "Recent studies on ocean currents from the Global Ocean Temperature Dataset "
7
- "(GOTD) indicate significant shifts in marine biodiversity."
8
- )
9
- sample_entities = [
10
- {"label": "named dataset", "text": "Global Ocean Temperature Dataset", "start": 29, "end": 62, "score": 0.99},
11
- {"label": "acronym", "text": "GOTD", "start": 64, "end": 68, "score": 0.98},
12
- ]
13
- rels = [
14
- 'acronym', 'author', 'data description',
15
- 'data geography', 'data source', 'data type',
16
- 'publication year', 'publisher', 'reference year', 'version'
17
- ]
18
- MODELS = ["demo-model-1", "demo-model-2"]
19
 
20
- # Annotate_query simulation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- def annotate_query(
23
- query: str,
24
- labels: Union[str, List[str]],
25
- threshold: float = 0.3,
26
- nested_ner: bool = False,
27
- model_name: str = None
28
- ) -> Dict[str, Any]:
29
- # In a real app, you'd call parse_query/inference_pipeline here.
30
- # For simulation, reuse sample_entities.
31
- return {
32
- "text": query,
33
- "entities": [
34
- {"start": ent["start"], "end": ent["end"], "label": ent["label"]}
35
- for ent in sample_entities
36
- ]
37
- }
38
-
39
- # Build Gradio UI
40
- demo = gr.Blocks()
41
- with demo:
42
- gr.Markdown(
43
- """
44
- ## Step: Annotate Query Simulation
45
- Enter text (prepopulated) and click **Annotate** to see how entities are highlighted.
46
- """
47
- )
48
- # Inputs
49
- query = gr.Textbox(lines=3, value=sample_text, label="Input Text")
50
- entities = gr.Textbox(value=", ".join(rels), label="Relations (unused in simulation)")
51
- threshold = gr.Slider(0, 1, value=0.3, step=0.01, label="Threshold")
52
- nested = gr.Checkbox(value=False, label="Nested NER")
53
- model = gr.Radio(choices=MODELS, value=MODELS[0], label="Model")
54
-
55
- # Outputs
56
- output_hl = gr.HighlightedText(label="Annotated Entities")
57
-
58
- # Button
59
- annotate_btn = gr.Button("Annotate")
60
- annotate_btn.click(
61
- fn=annotate_query,
62
- inputs=[query, entities, threshold, nested, model],
63
- outputs=[output_hl]
64
  )
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- demo.launch(debug=True)
 
 
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
46
+ - **publisher**: World Bank
47
+ """)
48
 
49
+ if __name__ == "__main__":
50
+ demo.launch()