WesScivetti commited on
Commit
bb48658
·
2 Parent(s): dec77c7 8a036b1

Merge branch 'main' of https://huggingface.co/spaces/WesScivetti/SNACS_English_Demo

Browse files
Files changed (1) hide show
  1. app.py +11 -3
app.py CHANGED
@@ -52,6 +52,12 @@ EXAMPLES = [
52
  ]
53
  EXAMPLE_LABELS = ['Harry Potter (en)', 'Through the Looking Glass (en)', 'Fresh Prince of Bel-Air (en)', 'Don Quixote (es)']
54
 
 
 
 
 
 
 
55
  class MyPipeline(TokenClassificationPipeline):
56
  """Custom Pipeline class with custom postprocess function, designed to output proability distribution in addition to top scores
57
  Inherits from HF TokenClassificationPipeline"""
@@ -246,10 +252,12 @@ def classify_tokens(text: str, use_canned=False):
246
  )
247
 
248
  # tagged spans
249
- results_spans = pipe(text, aggregation_strategy="simple").sort(key=lambda x: x["start"])
 
250
 
251
  # per-token + probabilities
252
- results_tokens = pipe(text, aggregation_strategy="none", ignore_labels=[]).sort(key=lambda x: x["start"])
 
253
  print(results_tokens)
254
  else: # canned example to test the output display
255
  text = "fox in socks"
@@ -353,7 +361,7 @@ def classify_tokens(text: str, use_canned=False):
353
  styled_html2 = f"<div style='font-family:sans-serif;line-height:1.6;'>{output2}</div>"
354
 
355
  simple_output_data = {"text": text, "entities": [{**e} | {"entity_group": display_label(e["entity_group"])} for e in results_spans]}
356
- return simple_output_data, json.dumps(results_spans), json.dumps(results_tokens), styled_html1, table_html, styled_html2
357
  # except Exception as e:
358
  # # Force the real error into the Space logs
359
  # import traceback, sys
 
52
  ]
53
  EXAMPLE_LABELS = ['Harry Potter (en)', 'Through the Looking Glass (en)', 'Fresh Prince of Bel-Air (en)', 'Don Quixote (es)']
54
 
55
+ class FloatEncoder(json.JSONEncoder):
56
+ def default(self, obj):
57
+ if isinstance(obj, np.float32):
58
+ return float(obj)
59
+ return json.JSONEncoder.default(self, obj)
60
+
61
  class MyPipeline(TokenClassificationPipeline):
62
  """Custom Pipeline class with custom postprocess function, designed to output proability distribution in addition to top scores
63
  Inherits from HF TokenClassificationPipeline"""
 
252
  )
253
 
254
  # tagged spans
255
+ results_spans = pipe(text, aggregation_strategy="simple")
256
+ results_spans.sort(key=lambda x: x["start"])
257
 
258
  # per-token + probabilities
259
+ results_tokens = pipe(text, aggregation_strategy="none", ignore_labels=[])
260
+ results_tokens.sort(key=lambda x: x["start"])
261
  print(results_tokens)
262
  else: # canned example to test the output display
263
  text = "fox in socks"
 
361
  styled_html2 = f"<div style='font-family:sans-serif;line-height:1.6;'>{output2}</div>"
362
 
363
  simple_output_data = {"text": text, "entities": [{**e} | {"entity_group": display_label(e["entity_group"])} for e in results_spans]}
364
+ return simple_output_data, json.dumps(results_spans, cls=FloatEncoder), json.dumps(results_tokens, cls=FloatEncoder), styled_html1, table_html, styled_html2
365
  # except Exception as e:
366
  # # Force the real error into the Space logs
367
  # import traceback, sys