cafierom commited on
Commit
7d9e2f5
·
verified ·
1 Parent(s): a17ff57

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -24
app.py CHANGED
@@ -11,7 +11,7 @@ print("Model loaded.")
11
  @spaces.GPU
12
  def predict(prompt, dark_mode):
13
  """
14
- Generates a response and returns a main highlighted HTML string and a detailed token list HTML string.
15
  """
16
  # Generate beams
17
  generated_dicts, transcription = make_beams(model, processor, prompt)
@@ -21,14 +21,13 @@ def predict(prompt, dark_mode):
21
  score_vectors = calculate_score_vectors(model, generated_dicts)
22
 
23
  if not beam_tokens or not score_vectors:
24
- return "<span style='color: grey'>No tokens generated.</span>", ""
25
 
26
  first_beam_tokens = beam_tokens[0]
27
  first_beam_scores = score_vectors[0]
28
  first_beam_score = exp(generated_dicts.sequences_scores[0]).item()
29
 
30
  # Construct Main HTML output
31
- # We use a simple div for the title instead of a markdown-style header
32
  main_container_style = "border: 2px solid lightblue; padding: 15px; border-radius: 8px; font-family: sans-serif;"
33
  title_style = "font-weight: bold; margin-bottom: 10px; font-size: 1.1em;"
34
 
@@ -56,17 +55,14 @@ def predict(prompt, dark_mode):
56
  html_output += '</div></div>'
57
 
58
  # Construct Detailed HTML output
59
- # We create a simple table to list tokens and their scores
60
  detail_table_style = "border-collapse: collapse; width: 100%; max-width: 500px; font-family: monospace;"
61
  th_style = "border: 1px solid #ccc; padding: 8px; text-align: left; background-color: #f2f2f2;" if not dark_mode else "border: 1px solid #444; padding: 8px; text-align: left; background-color: #333;"
62
  td_style = "border: 1px solid #ccc; padding: 8px; text-align: left;" if not dark_mode else "border: 1px solid #444; padding: 8px; text-align: left;"
63
 
64
  detail_output = f'<table style="{detail_table_style}"><thead><tr><th style="{th_style}">Token</th><th style="{th_style}">Score</th></tr></thead><tbody>'
65
  for token, score in zip(first_beam_tokens, first_beam_scores):
66
- # Replace SentencePiece space character (U+2581) and any underscores used for spaces
67
  display_token = token.replace(' ', ' ').replace('_', ' ')
68
 
69
- # Apply red highlighting for low-probability tokens
70
  if score < 0.6:
71
  color = "red"
72
  bg_color = "#441111" if dark_mode else "#ffe6e6"
@@ -78,7 +74,7 @@ def predict(prompt, dark_mode):
78
  detail_output += f'<tr><td style="{token_cell_style}">{display_token}</td><td style="{td_style}">{score:.4f}</td></tr>'
79
  detail_output += '</tbody></table>'
80
 
81
- return html_output, detail_output
82
 
83
  # Gradio Interface
84
  with gr.Blocks() as demo:
@@ -92,25 +88,9 @@ with gr.Blocks() as demo:
92
 
93
  output_html = gr.HTML(label="Highlighted Beam")
94
 
95
- toggle_btn = gr.Button("Show Token Details")
96
  detail_html = gr.HTML(label="Detailed Token Probabilities", visible=False)
97
 
98
- def toggle_details(visible):
99
- return gr.update(visible=not visible, value="") if visible else gr.update(visible=True)
100
-
101
- # This is slightly wrong. toggle_details should just toggle visibility.
102
- # Let's use a better way.
103
- def update_visibility(visible):
104
- return gr.update(visible=not visible)
105
-
106
- submit_btn.click(fn=predict, inputs=[prompt_input, dark_mode_toggle], outputs=[output_html, detail_html])
107
-
108
- # We need to track the visibility state. Since we can't easily,
109
- # we can use a gr.State or just a simple toggle function.
110
-
111
- def on_toggle_click(current_visible):
112
- return gr.update(visible=not current_visible)
113
-
114
  # To track visibility, we can use a gr.State
115
  visibility_state = gr.State(value=False)
116
 
@@ -119,5 +99,9 @@ with gr.Blocks() as demo:
119
 
120
  toggle_btn.click(fn=toggle_view, inputs=visibility_state, outputs=[visibility_state, detail_html])
121
 
 
 
 
 
122
  if __name__ == "__main__":
123
  demo.launch()
 
11
  @spaces.GPU
12
  def predict(prompt, dark_mode):
13
  """
14
+ Generates a response and returns a main highlighted HTML string, a detailed token list HTML string, and visibility for the toggle button.
15
  """
16
  # Generate beams
17
  generated_dicts, transcription = make_beams(model, processor, prompt)
 
21
  score_vectors = calculate_score_vectors(model, generated_dicts)
22
 
23
  if not beam_tokens or not score_vectors:
24
+ return "<span style='color: grey'>No tokens generated.</span>", "", gr.update(visible=False)
25
 
26
  first_beam_tokens = beam_tokens[0]
27
  first_beam_scores = score_vectors[0]
28
  first_beam_score = exp(generated_dicts.sequences_scores[0]).item()
29
 
30
  # Construct Main HTML output
 
31
  main_container_style = "border: 2px solid lightblue; padding: 15px; border-radius: 8px; font-family: sans-serif;"
32
  title_style = "font-weight: bold; margin-bottom: 10px; font-size: 1.1em;"
33
 
 
55
  html_output += '</div></div>'
56
 
57
  # Construct Detailed HTML output
 
58
  detail_table_style = "border-collapse: collapse; width: 100%; max-width: 500px; font-family: monospace;"
59
  th_style = "border: 1px solid #ccc; padding: 8px; text-align: left; background-color: #f2f2f2;" if not dark_mode else "border: 1px solid #444; padding: 8px; text-align: left; background-color: #333;"
60
  td_style = "border: 1px solid #ccc; padding: 8px; text-align: left;" if not dark_mode else "border: 1px solid #444; padding: 8px; text-align: left;"
61
 
62
  detail_output = f'<table style="{detail_table_style}"><thead><tr><th style="{th_style}">Token</th><th style="{th_style}">Score</th></tr></thead><tbody>'
63
  for token, score in zip(first_beam_tokens, first_beam_scores):
 
64
  display_token = token.replace(' ', ' ').replace('_', ' ')
65
 
 
66
  if score < 0.6:
67
  color = "red"
68
  bg_color = "#441111" if dark_mode else "#ffe6e6"
 
74
  detail_output += f'<tr><td style="{token_cell_style}">{display_token}</td><td style="{td_style}">{score:.4f}</td></tr>'
75
  detail_output += '</tbody></table>'
76
 
77
+ return html_output, detail_output, gr.update(visible=True)
78
 
79
  # Gradio Interface
80
  with gr.Blocks() as demo:
 
88
 
89
  output_html = gr.HTML(label="Highlighted Beam")
90
 
91
+ toggle_btn = gr.Button("Show Token Details", visible=False)
92
  detail_html = gr.HTML(label="Detailed Token Probabilities", visible=False)
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  # To track visibility, we can use a gr.State
95
  visibility_state = gr.State(value=False)
96
 
 
99
 
100
  toggle_btn.click(fn=toggle_view, inputs=visibility_state, outputs=[visibility_state, detail_html])
101
 
102
+ # Trigger generation on both button click and Enter key in textbox
103
+ submit_btn.click(fn=predict, inputs=[prompt_input, dark_mode_toggle], outputs=[output_html, detail_html, toggle_btn])
104
+ prompt_input.submit(fn=predict, inputs=[prompt_input, dark_mode_toggle], outputs=[output_html, detail_html, toggle_btn])
105
+
106
  if __name__ == "__main__":
107
  demo.launch()