Last active
August 9, 2022 04:59
-
-
Save eliasdabbas/2e4cda474fde5516ffb36edf97dff441 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# !pip install --upgrade transformers plotly pandas | |
import plotly.graph_objects as go | |
import pandas as pd | |
pd.options.display.max_columns = None | |
from transformers import pipeline | |
unmasker = pipeline('fill-mask', model='bert-base-uncased') | |
results = [] | |
cars = ['mercedes', 'audi', 'bmw', 'volkswagen', 'ford', 'toyota', | |
'peugeot', 'citroen', 'volvo', 'land rover', 'nissan', 'renault', 'fiat', | |
'kia', 'lexus', 'jaguar', 'hyundai', 'honda', 'porsche'] | |
for car in cars: | |
result = unmasker(f'{car.title()} is a [MASK] car.') | |
results.append(result) | |
df_list = [] | |
for car, result in zip(cars, results): | |
df = pd.json_normalize(result) | |
df['car'] = car | |
df_list.append(df) | |
car_df = pd.concat(df_list, ignore_index=True) | |
car_matrix = car_df.pivot(index='car', columns='token_str', values='score') | |
fig = px.imshow(car_matrix, color_continuous_scale='cividis', | |
title='[CAR_BRAND] is a [MASK] car.<br>Bert: please fill in the blank', | |
width=1000, height=800, template='none') | |
fig.layout.xaxis.side = 'top' | |
fig.layout.yaxis.title = '' | |
fig.layout.xaxis.title = '' | |
fig.layout.xaxis.tickangle = 300 | |
fig |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Produces this chart: