zetavg commited on
Commit
e615353
1 Parent(s): 4ac0d6a

ui update: debounce prompt preview events with js

Browse files
llama_lora/ui/inference_ui.py CHANGED
@@ -199,18 +199,25 @@ def inference_ui():
199
  with gr.Column():
200
  with gr.Column(elem_id="inference_prompt_box"):
201
  variable_0 = gr.Textbox(
202
- lines=2, label="Prompt", placeholder="Tell me about alpecas and llamas.")
203
- variable_1 = gr.Textbox(lines=2, label="", visible=False)
204
- variable_2 = gr.Textbox(lines=2, label="", visible=False)
205
- variable_3 = gr.Textbox(lines=2, label="", visible=False)
206
- variable_4 = gr.Textbox(lines=2, label="", visible=False)
207
- variable_5 = gr.Textbox(lines=2, label="", visible=False)
208
- variable_6 = gr.Textbox(lines=2, label="", visible=False)
209
- variable_7 = gr.Textbox(lines=2, label="", visible=False)
 
 
 
 
210
 
211
  with gr.Accordion("Preview", open=False, elem_id="inference_preview_prompt_container"):
212
  preview_prompt = gr.Textbox(
213
  show_label=False, interactive=False, elem_id="inference_preview_prompt")
 
 
 
214
 
215
  # with gr.Column():
216
  # with gr.Row():
@@ -223,22 +230,23 @@ def inference_ui():
223
  # with gr.Column():
224
  with gr.Accordion("Options", open=True, elem_id="inference_options_accordion"):
225
  temperature = gr.Slider(
226
- minimum=0.01, maximum=1.99, value=0.1, step=0.01,
227
  label="Temperature",
228
  elem_id="inference_temperature"
229
  )
230
 
231
- top_p = gr.Slider(
232
- minimum=0, maximum=1, value=0.75, step=0.01,
233
- label="Top P",
234
- elem_id="inference_top_p"
235
- )
 
236
 
237
- top_k = gr.Slider(
238
- minimum=0, maximum=200, value=40, step=1,
239
- label="Top K",
240
- elem_id="inference_top_k"
241
- )
242
 
243
  num_beams = gr.Slider(
244
  minimum=1, maximum=4, value=1, step=1,
@@ -258,7 +266,7 @@ def inference_ui():
258
  elem_id="inference_max_new_tokens"
259
  )
260
 
261
- with gr.Row():
262
  stream_output = gr.Checkbox(
263
  label="Stream Output",
264
  elem_id="inference_stream_output",
@@ -284,7 +292,7 @@ def inference_ui():
284
  inference_output.style(show_copy_button=True)
285
  with gr.Accordion(
286
  "Raw Output",
287
- open=False,
288
  visible=default_show_raw,
289
  elem_id="inference_inference_raw_output_accordion"
290
  ) as raw_output_group:
@@ -331,33 +339,9 @@ def inference_ui():
331
  stop_btn.click(fn=None, inputs=None, outputs=None,
332
  cancels=[generate_event])
333
 
334
- prompt_template.change(fn=update_prompt_preview, inputs=[prompt_template,
335
- variable_0, variable_1, variable_2, variable_3,
336
- variable_4, variable_5, variable_6, variable_7,], outputs=preview_prompt)
337
- variable_0.change(fn=update_prompt_preview, inputs=[prompt_template,
338
- variable_0, variable_1, variable_2, variable_3,
339
- variable_4, variable_5, variable_6, variable_7,], outputs=preview_prompt)
340
- variable_1.change(fn=update_prompt_preview, inputs=[prompt_template,
341
- variable_0, variable_1, variable_2, variable_3,
342
- variable_4, variable_5, variable_6, variable_7,], outputs=preview_prompt)
343
- variable_2.change(fn=update_prompt_preview, inputs=[prompt_template,
344
- variable_0, variable_1, variable_2, variable_3,
345
- variable_4, variable_5, variable_6, variable_7,], outputs=preview_prompt)
346
- variable_3.change(fn=update_prompt_preview, inputs=[prompt_template,
347
- variable_0, variable_1, variable_2, variable_3,
348
- variable_4, variable_5, variable_6, variable_7,], outputs=preview_prompt)
349
- variable_4.change(fn=update_prompt_preview, inputs=[prompt_template,
350
- variable_0, variable_1, variable_2, variable_3,
351
- variable_4, variable_5, variable_6, variable_7,], outputs=preview_prompt)
352
- variable_5.change(fn=update_prompt_preview, inputs=[prompt_template,
353
- variable_0, variable_1, variable_2, variable_3,
354
- variable_4, variable_5, variable_6, variable_7,], outputs=preview_prompt)
355
- variable_6.change(fn=update_prompt_preview, inputs=[prompt_template,
356
- variable_0, variable_1, variable_2, variable_3,
357
- variable_4, variable_5, variable_6, variable_7,], outputs=preview_prompt)
358
- variable_7.change(fn=update_prompt_preview, inputs=[prompt_template,
359
- variable_0, variable_1, variable_2, variable_3,
360
- variable_4, variable_5, variable_6, variable_7,], outputs=preview_prompt)
361
 
362
  inference_ui_blocks.load(_js="""
363
  function inference_ui_blocks_js() {
@@ -405,6 +389,11 @@ def inference_ui():
405
  animation: 'scale-subtle',
406
  content: 'This is the prompt that will be sent to the language model.',
407
  });
 
 
 
 
 
408
  });
409
 
410
  function setTooltipForOptions() {
@@ -505,5 +494,34 @@ def inference_ui():
505
  handle_output_wrap_element_class_change();
506
  }, 500);
507
  }, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
508
  }
509
  """)
 
199
  with gr.Column():
200
  with gr.Column(elem_id="inference_prompt_box"):
201
  variable_0 = gr.Textbox(
202
+ lines=2,
203
+ label="Prompt",
204
+ placeholder="Tell me about alpecas and llamas.",
205
+ elem_id="inference_variable_0"
206
+ )
207
+ variable_1 = gr.Textbox(lines=2, label="", visible=False, elem_id="inference_variable_1")
208
+ variable_2 = gr.Textbox(lines=2, label="", visible=False, elem_id="inference_variable_2")
209
+ variable_3 = gr.Textbox(lines=2, label="", visible=False, elem_id="inference_variable_3")
210
+ variable_4 = gr.Textbox(lines=2, label="", visible=False, elem_id="inference_variable_4")
211
+ variable_5 = gr.Textbox(lines=2, label="", visible=False, elem_id="inference_variable_5")
212
+ variable_6 = gr.Textbox(lines=2, label="", visible=False, elem_id="inference_variable_6")
213
+ variable_7 = gr.Textbox(lines=2, label="", visible=False, elem_id="inference_variable_7")
214
 
215
  with gr.Accordion("Preview", open=False, elem_id="inference_preview_prompt_container"):
216
  preview_prompt = gr.Textbox(
217
  show_label=False, interactive=False, elem_id="inference_preview_prompt")
218
+ update_prompt_preview_btn = gr.Button(
219
+ "↻", elem_id="inference_update_prompt_preview_btn", full_width=False)
220
+ update_prompt_preview_btn.style(size="sm")
221
 
222
  # with gr.Column():
223
  # with gr.Row():
 
230
  # with gr.Column():
231
  with gr.Accordion("Options", open=True, elem_id="inference_options_accordion"):
232
  temperature = gr.Slider(
233
+ minimum=0, maximum=1, value=0.1, step=0.01,
234
  label="Temperature",
235
  elem_id="inference_temperature"
236
  )
237
 
238
+ with gr.Row(elem_classes="inference_options_group"):
239
+ top_p = gr.Slider(
240
+ minimum=0, maximum=1, value=0.75, step=0.01,
241
+ label="Top P",
242
+ elem_id="inference_top_p"
243
+ )
244
 
245
+ top_k = gr.Slider(
246
+ minimum=0, maximum=100, value=40, step=1,
247
+ label="Top K",
248
+ elem_id="inference_top_k"
249
+ )
250
 
251
  num_beams = gr.Slider(
252
  minimum=1, maximum=4, value=1, step=1,
 
266
  elem_id="inference_max_new_tokens"
267
  )
268
 
269
+ with gr.Row(elem_id="inference_options_bottom_group"):
270
  stream_output = gr.Checkbox(
271
  label="Stream Output",
272
  elem_id="inference_stream_output",
 
292
  inference_output.style(show_copy_button=True)
293
  with gr.Accordion(
294
  "Raw Output",
295
+ open=not default_show_raw,
296
  visible=default_show_raw,
297
  elem_id="inference_inference_raw_output_accordion"
298
  ) as raw_output_group:
 
339
  stop_btn.click(fn=None, inputs=None, outputs=None,
340
  cancels=[generate_event])
341
 
342
+ update_prompt_preview_btn.click(fn=update_prompt_preview, inputs=[prompt_template,
343
+ variable_0, variable_1, variable_2, variable_3,
344
+ variable_4, variable_5, variable_6, variable_7,], outputs=preview_prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345
 
346
  inference_ui_blocks.load(_js="""
347
  function inference_ui_blocks_js() {
 
389
  animation: 'scale-subtle',
390
  content: 'This is the prompt that will be sent to the language model.',
391
  });
392
+
393
+ const update_btn = document.getElementById(
394
+ 'inference_update_prompt_preview_btn'
395
+ );
396
+ if (update_btn) update_btn.click();
397
  });
398
 
399
  function setTooltipForOptions() {
 
494
  handle_output_wrap_element_class_change();
495
  }, 500);
496
  }, 0);
497
+
498
+ // Debounced updating the prompt preview.
499
+ setTimeout(function () {
500
+ function debounce(func, wait) {
501
+ let timeout;
502
+ return function (...args) {
503
+ const context = this;
504
+ clearTimeout(timeout);
505
+ timeout = setTimeout(() => {
506
+ func.apply(context, args);
507
+ }, wait);
508
+ };
509
+ }
510
+
511
+ function update_preview() {
512
+ const update_btn = document.getElementById(
513
+ 'inference_update_prompt_preview_btn'
514
+ );
515
+ if (!update_btn) return;
516
+
517
+ update_btn.click();
518
+ }
519
+
520
+ for (let i = 0; i < 8; i++) {
521
+ const e = document.querySelector(`#inference_variable_${i} textarea`);
522
+ if (!e) return;
523
+ e.addEventListener('input', debounce(update_preview, 500));
524
+ }
525
+ }, 100);
526
  }
527
  """)
llama_lora/ui/main_page.py CHANGED
@@ -150,6 +150,27 @@ def main_page_custom_css():
150
  box-shadow: none;
151
  }
152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  #inference_output > .wrap,
154
  #inference_raw_output > .wrap {
155
  /* allow users to select text while generation is still in progress */
@@ -180,6 +201,23 @@ def main_page_custom_css():
180
  #inference_preview_prompt textarea {
181
  border: 0;
182
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
 
184
  #finetune_reload_selections_button {
185
  position: absolute;
 
150
  box-shadow: none;
151
  }
152
 
153
+ .inference_options_group {
154
+ margin-top: -12px;
155
+ }
156
+ .inference_options_group > .form {
157
+ border-radius: 0;
158
+ border-left: 0;
159
+ border-right: 0;
160
+ box-shadow: none;
161
+ }
162
+
163
+ #inference_options_bottom_group {
164
+ margin-top: -12px;
165
+ }
166
+ #inference_options_bottom_group > .form {
167
+ border-top-left-radius: 0;
168
+ border-top-right-radius: 0;
169
+ border-left: 0;
170
+ border-right: 0;
171
+ border-bottom: 0;
172
+ }
173
+
174
  #inference_output > .wrap,
175
  #inference_raw_output > .wrap {
176
  /* allow users to select text while generation is still in progress */
 
201
  #inference_preview_prompt textarea {
202
  border: 0;
203
  }
204
+ #inference_preview_prompt > .wrap {
205
+ pointer-events: none;
206
+ background: transparent;
207
+ opacity: 0.8;
208
+ }
209
+
210
+ #inference_update_prompt_preview_btn {
211
+ position: absolute;
212
+ z-index: 1;
213
+ right: 0;
214
+ bottom: 0;
215
+ width: 32px;
216
+ border-top-right-radius: 0;
217
+ border-bottom-left-radius: 0;
218
+ box-shadow: none;
219
+ opacity: 0.8;
220
+ }
221
 
222
  #finetune_reload_selections_button {
223
  position: absolute;