cafierom commited on
Commit
c41c60e
·
verified ·
1 Parent(s): 1a2cfea

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -8,7 +8,7 @@ model, processor = start_model()
8
  print("Model loaded.")
9
 
10
  @spaces.GPU
11
- def predict(prompt):
12
  """
13
  Generates a response and returns an HTML string with tokens highlighted by probability.
14
  """
@@ -28,16 +28,24 @@ def predict(prompt):
28
  # Construct HTML output
29
  html_output = '<div style="font-family: monospace; white-space: pre-wrap; line-height: 1.5;">'
30
 
 
 
 
 
31
  for token, score in zip(first_beam_tokens, first_beam_scores):
32
- # Replace BPE space marker with actual space for readability, but keep token boundaries
33
  display_token = token.replace(' ', ' ')
34
 
35
  if score < 0.6:
36
  color = "red"
 
 
 
37
  else:
38
- color = "black"
 
39
 
40
- html_output += f'<span style="color: {color}; border: 1px solid #eee; padding: 0 2px; margin: 0 1px;" title="Score: {score:.4f}">{display_token}</span>'
41
 
42
  html_output += '</div>'
43
  return html_output
@@ -50,12 +58,13 @@ with gr.Blocks() as demo:
50
  with gr.Row():
51
  with gr.Column():
52
  prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...", lines=3)
 
53
  submit_btn = gr.Button("Generate")
54
 
55
  with gr.Column():
56
  output_html = gr.HTML(label="Highlighted Beam")
57
 
58
- submit_btn.click(fn=predict, inputs=prompt_input, outputs=output_html)
59
 
60
  if __name__ == "__main__":
61
  demo.launch()
 
8
  print("Model loaded.")
9
 
10
  @spaces.GPU
11
+ def predict(prompt, dark_mode):
12
  """
13
  Generates a response and returns an HTML string with tokens highlighted by probability.
14
  """
 
28
  # Construct HTML output
29
  html_output = '<div style="font-family: monospace; white-space: pre-wrap; line-height: 1.5;">'
30
 
31
+ # Adjust colors based on dark mode
32
+ normal_color = "white" if dark_mode else "black"
33
+ border_color = "#444" if dark_mode else "#eee"
34
+
35
  for token, score in zip(first_beam_tokens, first_beam_scores):
36
+ # Replace SentencePiece space character (U+2581) with a regular space
37
  display_token = token.replace(' ', ' ')
38
 
39
  if score < 0.6:
40
  color = "red"
41
+ # Light red background for low-probability tokens to make them stand out without breaking flow
42
+ bg_color = "#ffe6e6" if not dark_mode else "#441111"
43
+ style = f"color: {color}; background-color: {bg_color};"
44
  else:
45
+ color = normal_color
46
+ style = f"color: {color};"
47
 
48
+ html_output += f'<span style="{style}" title="Score: {score:.4f}">{display_token}</span>'
49
 
50
  html_output += '</div>'
51
  return html_output
 
58
  with gr.Row():
59
  with gr.Column():
60
  prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...", lines=3)
61
+ dark_mode_toggle = gr.Checkbox(label="Dark Mode", value=False)
62
  submit_btn = gr.Button("Generate")
63
 
64
  with gr.Column():
65
  output_html = gr.HTML(label="Highlighted Beam")
66
 
67
+ submit_btn.click(fn=predict, inputs=[prompt_input, dark_mode_toggle], outputs=output_html)
68
 
69
  if __name__ == "__main__":
70
  demo.launch()