bjorn-hommel's picture
refactor
28183db
import streamlit as st
import plotly.graph_objects as go
import plotly.express as px
def indicator_plot(value, title, value_range, domain):
plot = go.Indicator(
mode = 'gauge+delta',
value = value,
domain = domain,
title = title,
delta = {
'reference': 0,
'decreasing': {'color': '#ec4899'},
'increasing': {'color': '#36def1'}
},
gauge = {
'axis': {'range': value_range, 'tickwidth': 1, 'tickcolor': 'black'},
'bar': {'color': '#4361ee'},
'bgcolor': 'white',
'borderwidth': 2,
'bordercolor': '#efefef',
'steps': [
{'range': [value_range[0], 0], 'color': '#efefef'},
{'range': [0, value_range[1]], 'color': '#efefef'}
],
'threshold': {
'line': {'color': '#4361ee', 'width': 8},
'thickness': 0.75,
'value': value
}
}
)
return plot
def scatter_plot(df, group_var):
colors = ['#36def1', '#4361ee'] if group_var else ['#4361ee']
plot = px.scatter(
df,
x='Machine-ratings',
y='Human-ratings',
color=group_var,
facet_col='x_group',
facet_col_wrap=2,
trendline='ols',
trendline_scope='trace',
hover_data={
'Text': df.text,
'Language': False,
'x_group': False,
'Human-ratings': ':.2f',
'Machine-ratings': ':.2f',
'Study': df.study,
'Instrument': df.instrument,
},
width=400,
height=400,
color_discrete_sequence=colors
)
plot.for_each_annotation(lambda a: a.update(text=a.text.split('=')[-1]))
plot.update_layout(
legend={
'orientation':'h',
'yanchor': 'bottom',
'y': -.30
})
plot.update_xaxes(title_standoff = 0)
return plot
def show_scores(sentiment, desirability, input_text):
with st.container():
p1 = indicator_plot(
value=sentiment,
title=f'Item Sentiment',
value_range=[-1, 1],
domain={'x': [0, .45], 'y': [0, .5]},
)
p2 = indicator_plot(
value=desirability,
title=f'Item Desirability',
value_range=[-4, 4],
domain={'x': [.55, 1], 'y': [0, .5]}
)
fig = go.Figure()
fig.add_trace(p1)
fig.add_trace(p2)
fig.update_layout(
title=dict(text=f'"{input_text}"', font=dict(size=36),yref='paper'),
paper_bgcolor = 'white',
font = {'color': 'black', 'family': 'Arial'})
st.plotly_chart(fig, theme=None, use_container_width=True)
st.markdown("""
Item sentiment: Absolute differences between positive and negative sentiment.
Item desirability: z-transformed values, 0 indicated "neutral".
""")