masharpe commited on
Commit
fad10f4
1 Parent(s): 66fa394

Move style to CSS string

Browse files
Files changed (1) hide show
  1. app.py +24 -8
app.py CHANGED
@@ -29,11 +29,30 @@ logger = logging.getLogger(__name__)
29
  # gr.DataFrame is currently bugged for updating values,
30
  # so we must use raw HTML.
31
  # https://github.com/gradio-app/gradio/issues/8160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  def make_html_table(headers, data):
33
- rows = ['<tr>' + ''.join(f'<th style="width: 50%">{h}</th>' for h in headers) + '</tr>\n']
34
  for row in data:
35
- rows.append('<tr>' + ''.join(f'<td style="width: 50%; font-family: monospace; white-space: pre-wrap; text-align: left; vertical-align: top;">{v}</td>' for v in row) + '</tr>\n')
36
- return '<table style="width: 100%; table-layout: fixed">\n' + ''.join(rows) + '</table>\n'
37
 
38
  def highlight_prefix(tokens, prefix_len):
39
  prefix_tokens = tokens[:prefix_len]
@@ -46,11 +65,7 @@ def highlight_prefix(tokens, prefix_len):
46
  prefix_html = html.escape(s[:s_lcp_len])
47
  suffix_html = html.escape(s[s_lcp_len:])
48
 
49
- #highlight_style = 'background-color: #FFFFAE;'
50
- #highlight_style = 'text-decoration: underline;'
51
- highlight_style = 'background-color: #90FF90;'
52
-
53
- return f'<span style="{highlight_style}">{prefix_html}</span>{suffix_html}'
54
 
55
  def format_response_pair(tokens_a, tokens_b):
56
  # This is slightly convoluted, so as to properly handle grapheme clusters that span token boundaries.
@@ -176,6 +191,7 @@ demo = gr.Interface(
176
  outputs=[
177
  gr.HTML(),
178
  ],
 
179
  title='All-Prefix-Optimal Coupling',
180
  description='Try similar prompts to see the effect of the difference between them. '
181
  f'Model: `{repo_id}`.'
 
29
  # gr.DataFrame is currently bugged for updating values,
30
  # so we must use raw HTML.
31
  # https://github.com/gradio-app/gradio/issues/8160
32
+ css = '''
33
+ .response-table {
34
+ width: 100%;
35
+ table-layout: fixed;
36
+ }
37
+ .response-table th, .response-table td {
38
+ width: 50%;
39
+ }
40
+ .response-table td {
41
+ font-family: monospace;
42
+ white-space: pre-wrap;
43
+ text-align: left;
44
+ vertical-align: top;
45
+ }
46
+ .highlight {
47
+ background-color: #90FF90;
48
+ }
49
+ '''
50
+
51
  def make_html_table(headers, data):
52
+ rows = ['<tr>' + ''.join(f'<th>{h}</th>' for h in headers) + '</tr>\n']
53
  for row in data:
54
+ rows.append('<tr>' + ''.join(f'<td>{v}</td>' for v in row) + '</tr>\n')
55
+ return '<table class="response-table">\n' + ''.join(rows) + '</table>\n'
56
 
57
  def highlight_prefix(tokens, prefix_len):
58
  prefix_tokens = tokens[:prefix_len]
 
65
  prefix_html = html.escape(s[:s_lcp_len])
66
  suffix_html = html.escape(s[s_lcp_len:])
67
 
68
+ return f'<span class="highlight">{prefix_html}</span>{suffix_html}'
 
 
 
 
69
 
70
  def format_response_pair(tokens_a, tokens_b):
71
  # This is slightly convoluted, so as to properly handle grapheme clusters that span token boundaries.
 
191
  outputs=[
192
  gr.HTML(),
193
  ],
194
+ css=css,
195
  title='All-Prefix-Optimal Coupling',
196
  description='Try similar prompts to see the effect of the difference between them. '
197
  f'Model: `{repo_id}`.'