Wesamalnabki-bsc commited on
Commit
0cbd051
·
verified ·
1 Parent(s): 876b314

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -119
app.py CHANGED
@@ -1,119 +1,118 @@
1
- from typing import Dict, Union
2
- from gliner import GLiNER
3
- import gradio as gr
4
-
5
- model = GLiNER.from_pretrained("BSC-NLP4BIA/chagas-ner",
6
- revision="gliner-bi-small-v1.0_2025-02-11_15-59-05",
7
- load_tokenizer=True)
8
-
9
- examples = [
10
- [
11
- """
12
- Sexo: Femenino
13
- País de Nacimiento: Paraguay
14
- Fecha de Nacimiento: XXXX
15
- Resultado Chagas: Positivo
16
- Informe: Seguimiento de consultas externas
17
- Visita: Primera visita
18
- Fecha: 20 de julio XXXX
19
- Paciente paraguaya de 32 años diagnosticada con enfermedad de Chagas tras cribado comunitario. Sin antecedentes transfusionales ni de familiares afectados. Refiere buen estado general.
20
- EF:
21
- Buen estado general.
22
- AC: rítmico, sin soplos.
23
- AP: MVC sin hallazgos.
24
- Abdomen blando, sin masas.
25
- PLAN:
26
- Solicitar estudio inicial de Chagas con ECG, ETT y serología.
27
- Informe: Seguimiento de consultas externas
28
- Visita: Seguimiento
29
- Fecha: 10 de diciembre XXXX
30
- Asintomática. PCR para T.cruzi negativa. ECG muestra ritmo sinusal con bloqueo de rama derecha. ETT sin alteraciones funcionales.
31
- EF:
32
- Sin cambios en exploración física.
33
- PLAN:
34
- Seguimiento anual con ECG y serología.
35
- """,
36
- "ENFERMEDAD, SINTOMA",
37
- 0.4,
38
- False,
39
- ],
40
- ]
41
-
42
-
43
- def ner(
44
- text, labels: str, threshold: float, nested_ner: bool
45
- ) -> Dict[str, Union[str, int, float]]:
46
- labels = labels.split(",")
47
- return {
48
- "text": text,
49
- "entities": [
50
- {
51
- "entity": entity["label"],
52
- "word": entity["text"],
53
- "start": entity["start"],
54
- "end": entity["end"],
55
- "score": 0,
56
- }
57
- for entity in model.predict_entities(
58
- text, labels, flat_ner=not nested_ner, threshold=threshold
59
- )
60
- ],
61
- }
62
-
63
-
64
- with gr.Blocks(title="GLiNER for Chagas detection - NER ") as demo:
65
-
66
- input_text = gr.Textbox(
67
- value=examples[0][0], label="Text input", placeholder="Enter your text here"
68
- )
69
- with gr.Row() as row:
70
- labels = gr.Textbox(
71
- value=examples[0][1],
72
- label="Labels",
73
- placeholder="Enter your labels here (comma separated)",
74
- scale=2,
75
- )
76
- threshold = gr.Slider(
77
- 0,
78
- 1,
79
- value=0.3,
80
- step=0.01,
81
- label="Threshold",
82
- info="Lower the threshold to increase how many entities get predicted.",
83
- scale=1,
84
- )
85
- nested_ner = gr.Checkbox(
86
- value=examples[0][2],
87
- label="Nested NER",
88
- info="Allow for nested NER?",
89
- scale=0,
90
- )
91
- output = gr.HighlightedText(label="Predicted Entities")
92
- submit_btn = gr.Button("Submit")
93
- examples = gr.Examples(
94
- examples,
95
- fn=ner,
96
- inputs=[input_text, labels, threshold, nested_ner],
97
- outputs=output,
98
- cache_examples=True,
99
- )
100
-
101
- # Submitting
102
- input_text.submit(
103
- fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
104
- )
105
- labels.submit(
106
- fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
107
- )
108
- threshold.release(
109
- fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
110
- )
111
- submit_btn.click(
112
- fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
113
- )
114
- nested_ner.change(
115
- fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
116
- )
117
-
118
- demo.queue()
119
- demo.launch(debug=True)
 
1
+ from typing import Dict, Union
2
+ from gliner import GLiNER
3
+ import gradio as gr
4
+
5
+ model = GLiNER.from_pretrained("BSC-NLP4BIA/chagas-ner",
6
+ # revision="gliner-bi-small-v1.0_2025-02-11_15-59-05",
7
+ revision="NuNER_Zero_2025-02-13_13-59-43_1"
8
+ load_tokenizer=True)
9
+
10
+ examples = [
11
+ [
12
+ """Sexo: Femenino
13
+ País de Nacimiento: Paraguay
14
+ Fecha de Nacimiento: XXXX
15
+ Resultado Chagas: Positivo
16
+ Informe: Seguimiento de consultas externas
17
+ Visita: Primera visita
18
+ Fecha: 20 de julio XXXX
19
+ Paciente paraguaya de 32 años diagnosticada con enfermedad de Chagas tras cribado comunitario. Sin antecedentes transfusionales ni de familiares afectados. Refiere buen estado general.
20
+ EF:
21
+ Buen estado general.
22
+ AC: rítmico, sin soplos.
23
+ AP: MVC sin hallazgos.
24
+ Abdomen blando, sin masas.
25
+ PLAN:
26
+ Solicitar estudio inicial de Chagas con ECG, ETT y serología.
27
+ Informe: Seguimiento de consultas externas
28
+ Visita: Seguimiento
29
+ Fecha: 10 de diciembre XXXX
30
+ Asintomática. PCR para T.cruzi negativa. ECG muestra ritmo sinusal con bloqueo de rama derecha. ETT sin alteraciones funcionales.
31
+ EF:
32
+ Sin cambios en exploración física.
33
+ PLAN:
34
+ Seguimiento anual con ECG y serología.""",
35
+ "ENFERMEDAD, SINTOMA",
36
+ 0.5,
37
+ False,
38
+ ],
39
+ ]
40
+
41
+
42
+ def ner(
43
+ text, labels: str, threshold: float, nested_ner: bool
44
+ ) -> Dict[str, Union[str, int, float]]:
45
+ labels = labels.split(",")
46
+ return {
47
+ "text": text,
48
+ "entities": [
49
+ {
50
+ "entity": entity["label"],
51
+ "word": entity["text"],
52
+ "start": entity["start"],
53
+ "end": entity["end"],
54
+ "score": 0,
55
+ }
56
+ for entity in model.predict_entities(
57
+ text, labels, flat_ner=not nested_ner, threshold=threshold
58
+ )
59
+ ],
60
+ }
61
+
62
+
63
+ with gr.Blocks(title="GLiNER for Chagas detection - NER ") as demo:
64
+
65
+ input_text = gr.Textbox(
66
+ value=examples[0][0], label="Text input", placeholder="Enter your text here"
67
+ )
68
+ with gr.Row() as row:
69
+ labels = gr.Textbox(
70
+ value=examples[0][1],
71
+ label="Labels",
72
+ placeholder="Enter your labels here (comma separated)",
73
+ scale=2,
74
+ )
75
+ threshold = gr.Slider(
76
+ 0,
77
+ 1,
78
+ value=0.5,
79
+ step=0.01,
80
+ label="Threshold",
81
+ info="Lower the threshold to increase how many entities get predicted.",
82
+ scale=1,
83
+ )
84
+ nested_ner = gr.Checkbox(
85
+ value=examples[0][2],
86
+ label="Nested NER",
87
+ info="Allow for nested NER?",
88
+ scale=0,
89
+ )
90
+ output = gr.HighlightedText(label="Predicted Entities")
91
+ submit_btn = gr.Button("Submit")
92
+ examples = gr.Examples(
93
+ examples,
94
+ fn=ner,
95
+ inputs=[input_text, labels, threshold, nested_ner],
96
+ outputs=output,
97
+ cache_examples=True,
98
+ )
99
+
100
+ # Submitting
101
+ input_text.submit(
102
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
103
+ )
104
+ labels.submit(
105
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
106
+ )
107
+ threshold.release(
108
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
109
+ )
110
+ submit_btn.click(
111
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
112
+ )
113
+ nested_ner.change(
114
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
115
+ )
116
+
117
+ demo.queue()
118
+ demo.launch(debug=True)