I’m working on making an entity name recognition application using BERT Transformer where the input and visualization process is done using Plotly Dash. But I get an error when I want to display the output of the NER prediction in the form of a bar graph or a pie graph. I’m still having trouble solving this problem. Someone who can help me in solving the problem will be appreciated.
My Code for layout:
# Define app
app = JupyterDash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
server = app.server
app.config['suppress_callback_exceptions']= True
colors = {"background": "#2D2D2D", "background_div": "white", 'text': 'white'}
app.layout = html.Div(style={'backgroundColor': colors['background']},
children=[
html.Div([
html.H2('Analyzing NER Indonesian Language (BERT Transformer)',
style={
'paddingTop': '30px',
'marginBottom': '30px',
'textAlign': 'center',
'color': colors['text']
}),
]),
html.Div(
dcc.Textarea(
id='text-input',
placeholder='Enter your text to be analyzed',
className='textarea',
style={
'width': '80%', 'height': 250, 'verticalAlign': 'top',
'fontFamily': 'Arial', 'fontColor': '#515151',
}
),
style={'display': 'flex', 'justifyContent': 'center'}
),
html.Div(
[html.Button(id='submit', n_clicks=0, children='Submit')],
style={'display': 'flex', 'justifyContent': 'center', 'marginTop': '20px'}),
# html.Br(),
# html.Div([
# dcc.Loading(
# id='loading-progress-1',
# children=[
# html.Div([
# dcc.Store(id='ner-bert-data'),
# ]),
# ], type="circle")
# ]),
html.Br(),
html.Div(
className="row",
children=[
html.Div(
className="six columns",
style={"width":600, "margin": 10, "marginLeft": '30px', 'display': 'inline-block'},
children=[
html.Div(
children=
dcc.Graph(id='pie-charts')
)
]
),
html.Div(
className="six columns",
style={"width":700, "margin": 10, 'display': 'inline-block'},
children=html.Div(
children=
dcc.Graph(id='bar-charts'),
)
)
]
),
]
)
My Code from display output:
@app.callback(Output('pie-charts', 'figure'),
[Input('submit', 'n_clicks')],
[State('text-input', 'value')])
def update_ner_data(n_clicks, text_input):
ctx = dash.callback_context
if text_input is None or text_input == "":
return "", "Did not run"
tokenized_sentence = tokenizer.encode(text_input)
input_ids = torch.tensor([tokenized_sentence]).cuda()
with torch.no_grad():
output = model(input_ids)
label_indices = np.argmax(output[0].to('cpu').numpy(),
axis=2)
# join split tokens
tokens = tokenizer.convert_ids_to_tokens(input_ids.to('cpu').numpy()[0])
new_tokens, new_labels = [], []
for token, label_idx in zip(tokens, label_indices[0]):
if token.startswith("##"):
new_tokens[-1] = new_tokens[-1] + token[2:]
else:
new_labels.append(tags_vals[label_idx])
new_tokens.append(token)
ner_bert_data = {'Words':new_tokens,'Labels':new_labels}
fig = px.pie(nert_bert_data.Labels.value_counts(), names=labels)
return fig