liujch1998 commited on
Commit
50d13b1
Β·
1 Parent(s): 25f66ac

Fix decode; Replace slider with tabs

Browse files
Files changed (1) hide show
  1. app.py +37 -35
app.py CHANGED
@@ -36,9 +36,8 @@ def creativity(index_desc, query):
36
  latency = '' if 'latency' not in result else f'{result["latency"]:.3f}'
37
  if 'error' in result:
38
  ci = result['error']
39
- ngram_len = NGRAM_LEN_DEFAULT
40
- html = ''
41
- return latency, ci, ngram_len, html
42
 
43
  rs = result['rs']
44
  tokens = result['tokens']
@@ -59,34 +58,35 @@ def creativity(index_desc, query):
59
  ci = sum(uniqueness_by_n.values()) / len(uniqueness_by_n)
60
  ci = f'{ci:.2%}'
61
 
62
- ngram_len = NGRAM_LEN_DEFAULT
63
-
64
- html = ''
65
- highlighted = highlighteds_by_n[ngram_len]
66
- line_len = 0
67
- for i, (token, highlighted) in enumerate(zip(tokens, highlighteds)):
68
- if line_len >= 100 and token.startswith('Ġ') and token != 'Ċ':
69
- html += '<br/>'
70
- line_len = 0
71
- color = '0, 0, 255, 0.5'
72
- if token == 'Ċ':
73
- disp_token = '\\n'
74
- is_linebreak = True
75
- else:
76
- disp_token = token.replace('Δ ', '&nbsp;')
77
- is_linebreak = False
78
- if highlighted:
79
- html += f'<span id="hldoc-token-{i}" style="background-color: rgba{color};" class="background-color: rgba{color};">{disp_token}</span>'
80
- else:
81
- html += disp_token
82
- if is_linebreak:
83
- html += '<br/>'
84
- line_len = 0
85
- else:
86
- line_len += len(token)
87
- html = '<div><p id="hldoc" style="font-size: 16px;">' + html.strip(' ') + '</p></div>'
 
88
 
89
- return latency, ci, ngram_len, html
90
 
91
  with gr.Blocks() as demo:
92
  with gr.Column():
@@ -102,7 +102,7 @@ with gr.Blocks() as demo:
102
  index_desc = gr.Radio(choices=INDEX_DESCS, label='Corpus', value=INDEX_DESCS[0])
103
 
104
  with gr.Column(scale=3):
105
- creativity_query = gr.Textbox(placeholder='Enter a piece of text here', label='Query', interactive=True, lines=10)
106
  with gr.Row():
107
  creativity_clear = gr.ClearButton(value='Clear', variant='secondary', visible=True)
108
  creativity_submit = gr.Button(value='Submit', variant='primary', visible=True)
@@ -110,11 +110,13 @@ with gr.Blocks() as demo:
110
 
111
  with gr.Column(scale=4):
112
  creativity_ci = gr.Label(value='', label='Creativity Index')
113
- creativity_ngram_len = gr.Slider(minimum=NGRAM_LEN_MIN, maximum=NGRAM_LEN_MAX, value=NGRAM_LEN_DEFAULT, step=1, label='Length of n-gram')
114
- creativity_html = gr.HTML(value='', label='Coverage')
 
 
115
 
116
- creativity_clear.add([creativity_query, creativity_latency, creativity_ci, creativity_html])
117
- creativity_submit.click(creativity, inputs=[index_desc, creativity_query], outputs=[creativity_latency, creativity_ci, creativity_ngram_len, creativity_html], api_name=False)
118
 
119
  demo.queue(
120
  default_concurrency_limit=DEFAULT_CONCURRENCY_LIMIT,
 
36
  latency = '' if 'latency' not in result else f'{result["latency"]:.3f}'
37
  if 'error' in result:
38
  ci = result['error']
39
+ htmls = [''] * (NGRAM_LEN_MAX - NGRAM_LEN_MIN + 1)
40
+ return tuple([latency, ci] + htmls)
 
41
 
42
  rs = result['rs']
43
  tokens = result['tokens']
 
58
  ci = sum(uniqueness_by_n.values()) / len(uniqueness_by_n)
59
  ci = f'{ci:.2%}'
60
 
61
+ htmls = []
62
+ for n in range(NGRAM_LEN_MIN, NGRAM_LEN_MAX + 1):
63
+ html = ''
64
+ highlighted = highlighteds_by_n[n]
65
+ line_len = 0
66
+ for i, (token, highlighted) in enumerate(zip(tokens, highlighteds)):
67
+ if line_len >= 100 and token.startswith('▁'):
68
+ html += '<br/>'
69
+ line_len = 0
70
+ color = '0, 0, 255, 0.5'
71
+ if token == '<0x0A>':
72
+ disp_token = '\\n'
73
+ is_linebreak = True
74
+ else:
75
+ disp_token = token.replace('▁', '&nbsp;')
76
+ is_linebreak = False
77
+ if highlighted:
78
+ html += f'<span id="hldoc-token-{i}" style="background-color: rgba{color};" class="background-color: rgba{color};">{disp_token}</span>'
79
+ else:
80
+ html += disp_token
81
+ if is_linebreak:
82
+ html += '<br/>'
83
+ line_len = 0
84
+ else:
85
+ line_len += len(token)
86
+ html = '<div><p id="hldoc" style="font-size: 16px;">' + html.strip(' ') + '</p></div>'
87
+ htmls.append(html)
88
 
89
+ return tuple([latency, ci] + htmls)
90
 
91
  with gr.Blocks() as demo:
92
  with gr.Column():
 
102
  index_desc = gr.Radio(choices=INDEX_DESCS, label='Corpus', value=INDEX_DESCS[0])
103
 
104
  with gr.Column(scale=3):
105
+ creativity_query = gr.Textbox(placeholder='Enter a piece of text here', label='Input', interactive=True, lines=10)
106
  with gr.Row():
107
  creativity_clear = gr.ClearButton(value='Clear', variant='secondary', visible=True)
108
  creativity_submit = gr.Button(value='Submit', variant='primary', visible=True)
 
110
 
111
  with gr.Column(scale=4):
112
  creativity_ci = gr.Label(value='', label='Creativity Index')
113
+ creativity_htmls = []
114
+ for n in range(NGRAM_LEN_MIN, NGRAM_LEN_MAX + 1):
115
+ with gr.Tab(label=f'n={n}'):
116
+ creativity_htmls.append(gr.HTML(value='', label=f'n={n}'))
117
 
118
+ creativity_clear.add([creativity_query, creativity_latency, creativity_ci] + creativity_htmls)
119
+ creativity_submit.click(creativity, inputs=[index_desc, creativity_query], outputs=[creativity_latency, creativity_ci] + creativity_htmls, api_name=False)
120
 
121
  demo.queue(
122
  default_concurrency_limit=DEFAULT_CONCURRENCY_LIMIT,