storytelling / src /probability_emote.py
jitesh's picture
adds all emotions
bf902a7
raw
history blame
6.33 kB
import numpy as np
import plotly.graph_objects as go
import streamlit as st
from .lib import set_input
@st.cache
def get_w(f, ec=0.86, rv=0.50):
result = (rv + f-1)/(ec + f-1)
result = np.clip(result, 0, 1)
# print(f'w = {result}')
return result
@st.cache
def get_f(w, ec=0.86, rv=0.50):
result = 1+(ec*w-rv)/(1-w)
result = np.clip(result, 0, 1)
# print(f'f = {result}')
return result
@st.cache
def get_pe(w, ec=0.86, f=0.50):
result = ec*w+(1-w)*(1-f)
result = np.clip(result, 0, 1)
# print(f'f = {result}')
return result
xdata1 = np.arange(0, 1.1, step=0.01)
# rand = np.random.random_sample()
@st.cache
def proper_float(i):
return np.round(i, 2)
@st.cache
def get_text():
return '''
### Eric's proposal
> I would propose a scoring metric something like this:
> * `probability_emote = w * emotion_confidence + (1 - w) * frequency_penalty`
>
> Where:
> * emotion_confidence: a score from 0.0 to 1.0 representing the emotion model’s confidence in it’s classification results
> * frequency_penalty: a score from 0.0 to 1.0 where a high score penalizes frequent emotes
> * `frequency_penalty = 1 - emotion_frequency`
> * w: a weight from 0.0 to 1.0 that controls the balance between emotion_confidence and frequency_penalty
> * Then you generate a random number between 0.0 and 1.0 and emote if it is greater than probability_emote
> * You will have to set frequency_penalty and w through trial and error, but you can start with setting w=0.5 and giving the emotion classifier and frequency penalty equal weight.
> * Setting w=1.0 would disable the frequency penalty altogether
'''
@st.cache
def get_equation_text(w=0.5, ec=0.7, rand=None, emotion_frequency=None):
text = f'''
#### Equation
```
frequency_penalty = 1 - emotion_frequency
probability_emote = w * emotion_confidence + (1 - w) * frequency_penalty
```
**probability_emote** = {proper_float(w)} * {proper_float(ec)} + {proper_float(1-w)} * frequency_penalty
'''
if rand is not None:
frequency_penalty = proper_float(1-emotion_frequency)
probability_emote = proper_float((w)*(ec)+(1-w)*frequency_penalty)
text = f'''
#### Equation
```
frequency_penalty = 1 - emotion_frequency = 1 - {proper_float(emotion_frequency)} = {frequency_penalty}
probability_emote = w * emotion_confidence + (1 - w) * frequency_penalty
probability_emote = {proper_float(w)} * {proper_float(ec)} + {proper_float(1-w)} * {frequency_penalty}
```
**probability_emote** = {probability_emote}
```
Show_Emotion
= probability_emote > (Random value between 0 and 1)
Random value = {rand}
Show_Emotion = {probability_emote} > {rand}
```
**Show_Emotion** = {probability_emote > rand}
'''
return text
def run_probability_emote(container_param):
w = set_input(container_param,
label='Weight w', key_slider='w_slider', key_input='w_input',
min_value=0.,
max_value=1.,
value=.5,
step=.01,)
score = set_input(container_param,
label='Confidence Score', key_slider='score_slider', key_input='score_input',
min_value=0.,
max_value=1.,
value=.5,
step=.01,)
# score = container_param.slider(
# label='Confidence Score',
# min_value=0.,
# max_value=1.,
# value=.5,
# step=.01)
calculate_check = container_param.checkbox(label='Calculate', value=False)
if calculate_check:
emotion_frequency = set_input(container_param,
label='Emotion Frequency', key_slider='emotion_frequency_slider_slider', key_input='emotion_frequency_slider_input',
min_value=0.,
max_value=1.,
value=.5,
step=.01,)
rand = set_input(container_param,
label='Random Value', key_slider='rand_slider', key_input='rand_input',
min_value=0.,
max_value=1.,
value=.5,
step=.01,)
else:
emotion_frequency = 'emotion_frequency'
rand = None
st.markdown(get_equation_text(w=w, ec=score, rand=rand,
emotion_frequency=emotion_frequency))
fig = go.Figure()
# fig.add_trace(go.Scatter(x=xdata1, y=np.ones_like(xdata1)*rand,
# mode='markers', name='Random',
# line=dict(color='#ff8300', width=2)
# ))
if calculate_check:
dd = 0.01
fig.add_hline(y=rand, line_width=3,
line_dash="dash", line_color="#ff8300")
fig.add_vline(x=emotion_frequency, line_width=3,
line_dash="dash", line_color="green")
fig.add_trace(go.Scatter(
x=[emotion_frequency-dd, emotion_frequency+dd], y=[rand-dd, rand+dd],
mode='lines',
line=dict(color='#ee00ee', width=8)
),)
fig.add_trace(go.Scatter(
x=[emotion_frequency+dd, emotion_frequency-dd], y=[rand-dd, rand+dd],
mode='lines',
line=dict(color='#ee00ee', width=8)
),)
fig.add_trace(go.Scatter(
x=xdata1, y=get_pe(w=w, f=xdata1, ec=score),
mode='lines',
name='Probability-Emote',
line=dict(color='#00eeee', width=4)
),
)
fig.update_layout(
template='plotly_dark',
xaxis_range=[0., 1.],
yaxis_range=[0., 1.],
xaxis_title="Emotion Frequency",
yaxis_title="Probability Emote",
showlegend=False
)
st.plotly_chart(fig, use_container_width=True)
st.markdown(get_text())