File size: 3,069 Bytes
28183db |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
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".
""") |