WesScivetti commited on
Commit
8a036b1
·
verified ·
1 Parent(s): 0bbfce0

Update app.py

Browse files

add float32 json fix

Files changed (1) hide show
  1. app.py +7 -1
app.py CHANGED
@@ -50,6 +50,12 @@ EXAMPLES = [
50
  ]
51
  EXAMPLE_LABELS = ['Harry Potter (en)', 'Through the Looking Glass (en)', 'Fresh Prince of Bel-Air (en)', 'Don Quixote (es)']
52
 
 
 
 
 
 
 
53
  class MyPipeline(TokenClassificationPipeline):
54
  """Custom Pipeline class with custom postprocess function, designed to output proability distribution in addition to top scores
55
  Inherits from HF TokenClassificationPipeline"""
@@ -353,7 +359,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
 
50
  ]
51
  EXAMPLE_LABELS = ['Harry Potter (en)', 'Through the Looking Glass (en)', 'Fresh Prince of Bel-Air (en)', 'Don Quixote (es)']
52
 
53
+ class FloatEncoder(json.JSONEncoder):
54
+ def default(self, obj):
55
+ if isinstance(obj, np.float32):
56
+ return float(obj)
57
+ return json.JSONEncoder.default(self, obj)
58
+
59
  class MyPipeline(TokenClassificationPipeline):
60
  """Custom Pipeline class with custom postprocess function, designed to output proability distribution in addition to top scores
61
  Inherits from HF TokenClassificationPipeline"""
 
359
  styled_html2 = f"<div style='font-family:sans-serif;line-height:1.6;'>{output2}</div>"
360
 
361
  simple_output_data = {"text": text, "entities": [{**e} | {"entity_group": display_label(e["entity_group"])} for e in results_spans]}
362
+ return simple_output_data, json.dumps(results_spans, cls=FloatEncoder), json.dumps(results_tokens, cls=FloatEncoder), styled_html1, table_html, styled_html2
363
  # except Exception as e:
364
  # # Force the real error into the Space logs
365
  # import traceback, sys