rafmacalaba commited on
Commit
72c5156
·
1 Parent(s): 2463f9e

add caching for preds

Browse files
Files changed (1) hide show
  1. app.py +13 -10
app.py CHANGED
@@ -148,7 +148,7 @@ def highlight_text(text, ner_threshold, re_threshold):
148
  "end": end
149
  })
150
 
151
- return {"text": text, "entities": entities}
152
 
153
  # JSON output function
154
 
@@ -166,9 +166,12 @@ def get_model_predictions(text, ner_threshold, re_threshold):
166
  ner_preds, rel_preds = prune_acronym_and_self_relations(ner_preds, rel_preds)
167
  return json.dumps({"ner": ner_preds, "relations": rel_preds}, indent=2)
168
 
169
- # Build Gradio UI
170
- demo = gr.Blocks()
171
- with demo:
 
 
 
172
  gr.Markdown("## Data Use Detector\n"
173
  "Adjust the sliders below to set thresholds, then:\n"
174
  "- **Submit** to highlight entities.\n"
@@ -195,18 +198,18 @@ with demo:
195
  txt_out = gr.HighlightedText(label="Annotated Entities")
196
 
197
  get_pred_btn = gr.Button("Get Model Predictions")
198
- ner_rel_box = gr.Textbox(label="Model Predictions (JSON)", lines=15)
199
-
200
  # Wire up interactions
201
  highlight_btn.click(
202
  fn=highlight_text,
203
  inputs=[txt_in, ner_slider, re_slider],
204
- outputs=txt_out
205
  )
206
  get_pred_btn.click(
207
- fn=get_model_predictions,
208
- inputs=[txt_in, ner_slider, re_slider],
209
- outputs=ner_rel_box
210
  )
211
 
212
  # Enable queue for concurrency
 
148
  "end": end
149
  })
150
 
151
+ return {"text": text, "entities": entities}, {"ner": ner_preds, "relations": rel_preds}
152
 
153
  # JSON output function
154
 
 
166
  ner_preds, rel_preds = prune_acronym_and_self_relations(ner_preds, rel_preds)
167
  return json.dumps({"ner": ner_preds, "relations": rel_preds}, indent=2)
168
 
169
+ def _cached_predictions(state):
170
+ if not state:
171
+ return "📋 No predictions yet. Click **Submit** first."
172
+ return json.dumps(state, indent=2)
173
+
174
+ with gr.Blocks as demo:
175
  gr.Markdown("## Data Use Detector\n"
176
  "Adjust the sliders below to set thresholds, then:\n"
177
  "- **Submit** to highlight entities.\n"
 
198
  txt_out = gr.HighlightedText(label="Annotated Entities")
199
 
200
  get_pred_btn = gr.Button("Get Model Predictions")
201
+ json_out = gr.Textbox(label="Model Predictions (JSON)", lines=15)
202
+ state = gr.State()
203
  # Wire up interactions
204
  highlight_btn.click(
205
  fn=highlight_text,
206
  inputs=[txt_in, ner_slider, re_slider],
207
+ outputs=[txt_out, state]
208
  )
209
  get_pred_btn.click(
210
+ fn=_cached_predictions,
211
+ inputs=[state],
212
+ outputs=[json_out]
213
  )
214
 
215
  # Enable queue for concurrency