tomsoderlund commited on
Commit
98314f0
·
1 Parent(s): 9cb5903

str() not JSON

Browse files
Files changed (1) hide show
  1. app.py +5 -6
app.py CHANGED
@@ -1,19 +1,18 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import json
4
 
5
  def process_swedish_text(text):
6
  # Models from https://huggingface.co/models
7
  # https://huggingface.co/KBLab/bert-base-swedish-cased-ner
8
  nlp = pipeline('ner', model='KBLab/bert-base-swedish-cased-ner', tokenizer='KBLab/bert-base-swedish-cased-ner')
9
  # Run NER
10
- pipeline_results = nlp(text)
11
- print('NER results:', pipeline_results)
12
  # Fix TypeError("'numpy.float32' object is not iterable")
13
- pipeline_results_adjusted = map(lambda entity: entity | { 'score': float(entity['score']) }, pipeline_results)
14
- print(pipeline_results_adjusted)
15
  # Return values
16
- return json.dumps({'entities': list(pipeline_results_adjusted)})
17
 
18
  gradio_interface = gr.Interface(fn=process_swedish_text, inputs="text", outputs="json")
19
  gradio_interface.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
  def process_swedish_text(text):
5
  # Models from https://huggingface.co/models
6
  # https://huggingface.co/KBLab/bert-base-swedish-cased-ner
7
  nlp = pipeline('ner', model='KBLab/bert-base-swedish-cased-ner', tokenizer='KBLab/bert-base-swedish-cased-ner')
8
  # Run NER
9
+ nlp_results = nlp(text)
10
+ print('nlp_results:', nlp_results)
11
  # Fix TypeError("'numpy.float32' object is not iterable")
12
+ nlp_results_adjusted = map(lambda entity: entity | { 'score': float(entity['score']) }, nlp_results)
13
+ print('nlp_results_adjusted:', nlp_results_adjusted)
14
  # Return values
15
+ return str({'entities': list(nlp_results_adjusted)})
16
 
17
  gradio_interface = gr.Interface(fn=process_swedish_text, inputs="text", outputs="json")
18
  gradio_interface.launch()