Spaces:
Running
Running
liujch1998
commited on
Commit
Β·
50d13b1
1
Parent(s):
25f66ac
Fix decode; Replace slider with tabs
Browse files
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 |
-
|
40 |
-
|
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 |
-
|
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 |
-
return latency, ci
|
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='
|
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 |
-
|
114 |
-
|
|
|
|
|
115 |
|
116 |
-
creativity_clear.add([creativity_query, creativity_latency, creativity_ci
|
117 |
-
creativity_submit.click(creativity, inputs=[index_desc, creativity_query], outputs=[creativity_latency, creativity_ci
|
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('β', ' ')
|
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,
|