If anyone needs it… Its kinda of hard to find the right colours so that it’s readable and “nice” to see…
I used Color library to generate the gradient:
from colour import Color
green = Color("#56ff33")
colors = list(green.range_to(Color("#ff6e6e"),10))
print(colors)
which provided me this list of colours:
colorList = ["#56ff33", “#84ff3a”, “#aeff40”, “#d7ff47”, “#fcff4d”, “#ffdf54”, “#ffbf5a”, “#ffa161”, “#ff8667”, “#ff6e6e”]
the code for the color legend is :
#Colour legend
st.write('Colours Legend')
legend = '<div style="display: table;"><div style="display: table-row">'
for color in colorList:
if color == colorList[0]:
legendText = 'Machine is sure'
elif color == colorList[len(colorList)-1]:
legendText = 'Machine is not so sure'
else:
legendText = ' '
legend = legend + '<div style="background-color: ' + color + '; padding: 4px 3px; display: table-cell; width: min-content;">' + legendText + '</div>'
legend = legend + '</div></div>'
st.markdown(legend, unsafe_allow_html=True)
For the implementation with the predict score… you need to build a custom formula that generate a score from 0 to 9 and then place the sentence in a with the coresponding index in the colour list.
something like this (I’m using dataframe, so just adjust it to your structure…):
'<span style="background-color: ' + colorList[int(min(round(abs(x['PredictScore']),0), len(colorList)-1))] + '">' + x['Target'] + '</span>'
in this case i’m using the predict score directly, but i’m going to change that to use the normalisez predict score option from ctranslate2. Otherwise, long sentence always comeout red.