File size: 811 Bytes
601f74f
bcd3c3e
28dfd94
601f74f
ac76be2
d4e4acc
 
 
 
 
ac76be2
 
28dfd94
 
 
d4e4acc
28dfd94
ac76be2
ac8ec3a
ee14c57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import gradio as gr
from transformers import pipeline
import json

def process_swedish_text(text):
  # Models from https://huggingface.co/models
  # https://huggingface.co/KBLab/bert-base-swedish-cased-ner
  ml_model = 'KBLab/bert-base-swedish-cased-ner'
  ner_pipeline = pipeline(model=ml_model, task='ner')
  # Run NER
  pipeline_results = ner_pipeline(text)
  print('NER results:', pipeline_results)
  # Fix TypeError("'numpy.float32' object is not iterable")
  pipeline_results_adjusted = map(lambda entity: entity | { 'score': float(entity['score']) }, pipeline_results)
  print(pipeline_results_adjusted)
  # Return values
  return json.dumps({'entities': list(pipeline_results_adjusted)})

gradio_interface = gr.Interface(fn=process_swedish_text, inputs="text", outputs="json")
gradio_interface.launch()