maxspad commited on
Commit
f58c9c5
1 Parent(s): 5cc9296

finally figured out altair

Browse files
Files changed (5) hide show
  1. app.py +13 -6
  2. fullreport.py +45 -0
  3. overview.py +2 -3
  4. plots.py +19 -0
  5. requirements.txt +2 -1
app.py CHANGED
@@ -3,12 +3,13 @@ import transformers as tf
3
  import pandas as pd
4
 
5
  from overview import NQDOverview
 
6
 
7
 
8
  # Function to load and cache models
9
  @st.experimental_singleton(show_spinner=False)
10
  def load_model(username, prefix, model_name):
11
- p = tf.pipeline('text-classification', f'{username}/{prefix}-{model_name}')
12
  return p
13
 
14
  @st.experimental_singleton(show_spinner=False)
@@ -17,9 +18,11 @@ def load_pickle(f):
17
 
18
  def get_results(model, c):
19
  res = model(c)[0]
20
- label = float(res['label'].split('_')[1])
21
- score = res['score']
22
- return {'label': label, 'score': score}
 
 
23
 
24
  def run_models(model_names, models, c):
25
  results = {}
@@ -80,10 +83,14 @@ with st.form('comment_form'):
80
  st.experimental_rerun()
81
 
82
  results = run_models(models_to_load, models, st.session_state['comment'])
83
-
84
- tab_titles = ['Overview', 'Q1 - Level of Detail', 'Q2 - Suggestion Given', 'Q3 - Suggestion Linked', 'About']
 
85
  tabs = st.tabs(tab_titles)
86
 
87
  with tabs[0]:
88
  overview = NQDOverview(st, results)
89
  overview.draw()
 
 
 
 
3
  import pandas as pd
4
 
5
  from overview import NQDOverview
6
+ from fullreport import NQDFullReport
7
 
8
 
9
  # Function to load and cache models
10
  @st.experimental_singleton(show_spinner=False)
11
  def load_model(username, prefix, model_name):
12
+ p = tf.pipeline('text-classification', f'{username}/{prefix}-{model_name}', return_all_scores=True)
13
  return p
14
 
15
  @st.experimental_singleton(show_spinner=False)
 
18
 
19
  def get_results(model, c):
20
  res = model(c)[0]
21
+ scores = [r['score'] for r in res]
22
+ label = max(range(len(scores)), key=lambda i: scores[i])
23
+ # label = float(res['label'].split('_')[1])
24
+ # scores = res['score']
25
+ return {'label': label, 'scores': scores}
26
 
27
  def run_models(model_names, models, c):
28
  results = {}
 
83
  st.experimental_rerun()
84
 
85
  results = run_models(models_to_load, models, st.session_state['comment'])
86
+ st.write(results)
87
+ # tab_titles = ['Overview', 'Q1 - Level of Detail', 'Q2 - Suggestion Given', 'Q3 - Suggestion Linked', 'About']
88
+ tab_titles = ['Overview', 'Full Report']
89
  tabs = st.tabs(tab_titles)
90
 
91
  with tabs[0]:
92
  overview = NQDOverview(st, results)
93
  overview.draw()
94
+ with tabs[1]:
95
+ fullrep = NQDFullReport(st, results)
96
+ fullrep.draw()
fullreport.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import altair as alt
3
+ import pandas as pd
4
+ from plots import altair_gauge
5
+
6
+ md_about_qual = '''
7
+ The Quality of Assessment for Learning (QuAL) score measures three
8
+ components of high-quality feedback via three subscores:
9
+
10
+ 1. A detailed description of the behavior observed (rated 0-3 depending on detail level)
11
+ 2. A suggestion for improvement is present (rated no = 0, yes = 1)
12
+ 3. Linkage between the behavior and the suggestion is present (rated no = 0, yes = 1)
13
+
14
+ The final QuAL score is the sum of these subscores, so it ranges from 0 (lowest quality)
15
+ to 5 (highest quality).
16
+ '''
17
+ class NQDFullReport(object):
18
+
19
+ def __init__(self, parent : st, results : dict):
20
+ self.p = parent
21
+ self.results = results
22
+
23
+ def draw(self):
24
+ st = self.p
25
+ st.header('Understand Your Score')
26
+ st.subheader('About the QuAL Score')
27
+ # with st.expander('About the QuAL Score', True):
28
+ st.markdown(md_about_qual)
29
+
30
+ st.subheader('Your Level of Detail')
31
+
32
+ gauge = altair_gauge(self.results['q1']['label'], 3, 'Level of Detail')
33
+ c1, c2 = st.columns(2)
34
+ with c1:
35
+ st.altair_chart(gauge, use_container_width=True)
36
+ with c2:
37
+ # st.write(self.results)
38
+ bar_df = (pd.DataFrame(self.results['q1']['scores'])
39
+ .reset_index()
40
+ .rename(columns={'index': 'Rating', 0: 'Score'}))
41
+ bar = alt.Chart(bar_df).mark_bar().encode(
42
+ x='Rating:O', y='Score',
43
+ color=alt.Color('Rating', scale=alt.Scale(scheme='redyellowgreen'), legend=None)
44
+ ).properties(height=225, title='Prediction Scores')
45
+ st.altair_chart(bar, use_container_width=True)
overview.py CHANGED
@@ -20,10 +20,9 @@ class NQDOverview(object):
20
  value = self.results['qual']['label'],
21
  mode = "gauge+number",
22
  title = {'text': "QuAL"},
23
- gauge = {'axis': {'range': [None, 5]},
24
  'bgcolor': 'lightgray',
25
- 'bar': {'color': color, 'thickness': 1.0},
26
-
27
  }
28
  ),
29
  layout=go.Layout(margin=dict(t=0, b=135))
 
20
  value = self.results['qual']['label'],
21
  mode = "gauge+number",
22
  title = {'text': "QuAL"},
23
+ gauge = {'axis': {'range': [None, 5], 'showticklabels': True, 'ticks': ""},
24
  'bgcolor': 'lightgray',
25
+ 'bar': {'color': color, 'thickness': 1.0}
 
26
  }
27
  ),
28
  layout=go.Layout(margin=dict(t=0, b=135))
plots.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import altair as alt
2
+ import pandas as pd
3
+ import matplotlib.cm as cm
4
+
5
+ def altair_gauge(score, max_score, title):
6
+ source = pd.DataFrame({"category": [1,2], "value": [4,3], "tt": ['hello!',None]})
7
+ gauge_theta2 = -1 * 2 * 3.14 * 0.25 + 3.14 * score / float(max_score)
8
+ c = alt.layer(
9
+ alt.Chart(source).mark_arc(innerRadius=100, theta=0, thetaOffset=(-1*2*3.14*0.25), theta2=(-1*2*3.14*0.25 + 3.14), color='lightgray', tooltip=None),
10
+ alt.Chart(source).mark_arc(innerRadius=100, theta=0, thetaOffset=(-1*2*3.14*0.25), theta2=gauge_theta2, tooltip=f'{title}: {int(score)}', color=get_color(score, max_score)),
11
+ alt.Chart(source).mark_text(text='%.1d' % score, size=80, font='Calibri', dy=-30)
12
+ ).properties(title=title)
13
+ return c
14
+
15
+ def get_color(score, max_score):
16
+ cmap = cm.get_cmap('RdYlGn')
17
+ color = cmap(score / float(max_score))
18
+ color = f'rgba({int(color[0]*256)}, {int(color[1]*256)}, {int(color[2]*256)}, {int(color[3]*256)})'
19
+ return color
requirements.txt CHANGED
@@ -4,4 +4,5 @@ torchaudio
4
  transformers
5
  plotly==5.11.0
6
  pandas
7
- spacy
 
 
4
  transformers
5
  plotly==5.11.0
6
  pandas
7
+ spacy
8
+ altair