avsolatorio commited on
Commit
d09cece
·
verified ·
1 Parent(s): cc602ba

Update app.py

Browse files

Annotate query

Files changed (1) hide show
  1. app.py +27 -8
app.py CHANGED
@@ -73,12 +73,29 @@ def parse_query(query: str, labels: Union[str, list], threshold: float = 0.3, ne
73
  entities.append(entity)
74
  else:
75
  entities.append(entity)
76
-
77
  payload = {"query": query, "entities": entities}
78
  print(f"{datetime.now()} :: parse_query :: {json.dumps(payload)}\n")
79
 
80
  return payload
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
  # Initialize model here.
84
  print("Initializing models...")
@@ -132,27 +149,29 @@ with gr.Blocks(title="GLiNER-query-parser") as demo:
132
  scale=0,
133
  )
134
 
135
- output = gr.JSON(label="Extracted entities")
 
 
136
  submit_btn = gr.Button("Submit")
137
 
138
  # Submitting
139
  query.submit(
140
- fn=parse_query, inputs=[query, entities, threshold, is_nested, model_name], outputs=output
141
  )
142
  entities.submit(
143
- fn=parse_query, inputs=[query, entities, threshold, is_nested, model_name], outputs=output
144
  )
145
  threshold.release(
146
- fn=parse_query, inputs=[query, entities, threshold, is_nested, model_name], outputs=output
147
  )
148
  submit_btn.click(
149
- fn=parse_query, inputs=[query, entities, threshold, is_nested, model_name], outputs=output
150
  )
151
  is_nested.change(
152
- fn=parse_query, inputs=[query, entities, threshold, is_nested, model_name], outputs=output
153
  )
154
  model_name.change(
155
- fn=parse_query, inputs=[query, entities, threshold, is_nested, model_name], outputs=output
156
  )
157
 
158
  demo.queue(default_concurrency_limit=5)
 
73
  entities.append(entity)
74
  else:
75
  entities.append(entity)
76
+
77
  payload = {"query": query, "entities": entities}
78
  print(f"{datetime.now()} :: parse_query :: {json.dumps(payload)}\n")
79
 
80
  return payload
81
 
82
+ def annotate_query(query: str, labels: Union[str, list], threshold: float = 0.3, nested_ner: bool = False, model_name: str = None) -> Dict[str, Union[str, list]]:
83
+ payload = parse_query(query, labels, threshold, nested_ner, model_name)
84
+
85
+ return {
86
+ "text": query,
87
+ "entities": [
88
+ {
89
+ "entity": entity["label"],
90
+ "word": entity["text"],
91
+ "start": entity["start"],
92
+ "end": entity["end"],
93
+ "score": entity["score"],
94
+ }
95
+ for entity in payload["entities"]
96
+ ],
97
+ }
98
+
99
 
100
  # Initialize model here.
101
  print("Initializing models...")
 
149
  scale=0,
150
  )
151
 
152
+ # output = gr.JSON(label="Extracted entities")
153
+
154
+ output = gr.HighlightedText(label="Annotated entities")
155
  submit_btn = gr.Button("Submit")
156
 
157
  # Submitting
158
  query.submit(
159
+ fn=annotate_query, inputs=[query, entities, threshold, is_nested, model_name], outputs=output
160
  )
161
  entities.submit(
162
+ fn=annotate_query, inputs=[query, entities, threshold, is_nested, model_name], outputs=output
163
  )
164
  threshold.release(
165
+ fn=annotate_query, inputs=[query, entities, threshold, is_nested, model_name], outputs=output
166
  )
167
  submit_btn.click(
168
+ fn=annotate_query, inputs=[query, entities, threshold, is_nested, model_name], outputs=output
169
  )
170
  is_nested.change(
171
+ fn=annotate_query, inputs=[query, entities, threshold, is_nested, model_name], outputs=output
172
  )
173
  model_name.change(
174
+ fn=annotate_query, inputs=[query, entities, threshold, is_nested, model_name], outputs=output
175
  )
176
 
177
  demo.queue(default_concurrency_limit=5)