Spaces:
Sleeping
Sleeping
abrakjamson
commited on
Commit
•
2c5c709
1
Parent(s):
8bf7eb8
Adding training tab
Browse files
app.py
CHANGED
@@ -1,6 +1,10 @@
|
|
1 |
import os
|
2 |
import threading
|
|
|
|
|
3 |
import torch
|
|
|
|
|
4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
5 |
from repeng import ControlVector, ControlModel
|
6 |
import gradio as gr
|
@@ -33,7 +37,7 @@ model = ControlModel(model, list(range(-5, -18, -1)))
|
|
33 |
|
34 |
# Generation settings
|
35 |
default_generation_settings = {
|
36 |
-
|
37 |
"do_sample": False, # Deterministic output
|
38 |
"max_new_tokens": 384,
|
39 |
"repetition_penalty": 1.1, # Reduce repetition
|
@@ -81,7 +85,7 @@ def construct_prompt(history, system_prompt, user_message):
|
|
81 |
formatted_prompt += f"{user_tag} {user_message} {asst_tag}"
|
82 |
return formatted_prompt
|
83 |
|
84 |
-
def generate_response(system_prompt, user_message, history, max_new_tokens, repitition_penalty, do_sample, *args):
|
85 |
"""
|
86 |
Applies the control vectors and calls the language model.
|
87 |
Returns a list of tuples, the user message and the assistant response,
|
@@ -105,12 +109,11 @@ def generate_response(system_prompt, user_message, history, max_new_tokens, repi
|
|
105 |
if checkboxes[i]:
|
106 |
cv_file = control_vector_files[i]
|
107 |
weight = sliders[i]
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
print(f"Failed to set control vector {cv_file}: {e}")
|
114 |
|
115 |
# The control model takes a sum of positive and negative control vectors
|
116 |
model.reset()
|
@@ -120,6 +123,14 @@ def generate_response(system_prompt, user_message, history, max_new_tokens, repi
|
|
120 |
combined_vector = control_vectors[i]
|
121 |
else:
|
122 |
combined_vector += control_vectors[i]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
# Set the combined set of vectors as the control for the model
|
125 |
try:
|
@@ -190,14 +201,14 @@ def generate_response(system_prompt, user_message, history, max_new_tokens, repi
|
|
190 |
history.append((user_message, assistant_response_display))
|
191 |
return history
|
192 |
|
193 |
-
def generate_response_with_retry(system_prompt, user_message, history, max_new_tokens, repitition_penalty, do_sample, *args):
|
194 |
# Remove last user input and assistant response from history, then call generate_response()
|
195 |
global previous_turn
|
196 |
previous_ueser_message = previous_turn
|
197 |
if history:
|
198 |
history = history[0:-1]
|
199 |
# Using the previous turn's text, even though it isn't in the textbox anymore
|
200 |
-
for output in generate_response(system_prompt, previous_ueser_message, history, max_new_tokens, repetition_penalty, do_sample, *args):
|
201 |
yield [output, previous_ueser_message]
|
202 |
|
203 |
# Function to reset the conversation history
|
@@ -342,6 +353,71 @@ def enable_controls():
|
|
342 |
def clear_input(input_textbox):
|
343 |
return ""
|
344 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
345 |
tooltip_css = """
|
346 |
/* Tooltip container */
|
347 |
.tooltip {
|
@@ -396,269 +472,346 @@ with gr.Blocks(
|
|
396 |
css=tooltip_css,
|
397 |
) as app:
|
398 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
399 |
|
400 |
-
# Header
|
401 |
-
if cuda:
|
402 |
-
gr.Markdown("# 🧠 LLM Mind Control")
|
403 |
-
else:
|
404 |
-
gr.Markdown("""# 🧠 LLM Mind Control
|
405 |
-
|
406 |
-
*Warning: running on CPU will be very slow*""")
|
407 |
-
gr.Markdown("""Unlike prompting, direct weight manipulation lets you fine-tune the amount of a personality
|
408 |
-
trait or topic. Enabled through [Representation Engineering](https://arxiv.org/abs/2310.01405)
|
409 |
-
via the [repeng](https://pypi.org/project/repeng) library.
|
410 |
-
[Watch a demo](https://youtu.be/gYZPGVafD7M) for usage tips.""")
|
411 |
-
|
412 |
-
with gr.Row():
|
413 |
-
# Left Column: Control Vectors and advanced settings
|
414 |
-
with gr.Column(scale=1):
|
415 |
-
gr.Markdown("### ⚡ Control Vectors")
|
416 |
-
control_vector_label = gr.HTML("""
|
417 |
-
<div class="tooltip">
|
418 |
-
<span>Select how you want to control the LLM per turn - towards (+) or away (-). Or start with a preset:</span>
|
419 |
-
<span class="tooltiptext">+/- 1.0 is a good start. Check the examples for each vector.</span>
|
420 |
-
</div>
|
421 |
-
""")
|
422 |
-
|
423 |
-
with gr.Row():
|
424 |
-
|
425 |
-
button_helpful = gr.Button(
|
426 |
-
value="Kind and helpful",
|
427 |
-
)
|
428 |
-
button_facts = gr.Button(
|
429 |
-
value="Just the facts"
|
430 |
-
)
|
431 |
-
button_stoner = gr.Button(
|
432 |
-
value="Angry stoner"
|
433 |
-
)
|
434 |
-
button_conspiracist = gr.Button(
|
435 |
-
value="Manic conspiracist"
|
436 |
-
)
|
437 |
-
|
438 |
-
# Create checkboxes and sliders for each control vector
|
439 |
-
control_checks = []
|
440 |
-
control_sliders = []
|
441 |
-
|
442 |
-
for cv_file in control_vector_files:
|
443 |
with gr.Row():
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
# Slider to adjust the control vector's weight
|
449 |
-
slider = gr.Slider(
|
450 |
-
minimum=-2.5,
|
451 |
-
maximum=2.5,
|
452 |
-
value=0.0,
|
453 |
-
step=0.1,
|
454 |
-
label=f"Voltage",
|
455 |
-
visible=False
|
456 |
)
|
457 |
-
|
458 |
-
|
459 |
-
# Link the checkbox to toggle slider visibility
|
460 |
-
checkbox.change(
|
461 |
-
toggle_slider,
|
462 |
-
inputs=checkbox,
|
463 |
-
outputs=slider
|
464 |
)
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
lines=2,
|
471 |
-
value="Respond to the user concisely",
|
472 |
-
interactive=True,
|
473 |
-
label="System Prompt",
|
474 |
-
show_label=False
|
475 |
)
|
476 |
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
with gr.Column(scale=1):
|
493 |
-
repetition_label = gr.HTML("""
|
494 |
-
<div class="tooltip">
|
495 |
-
<span>Repetition Penalty</span>
|
496 |
-
<span class="tooltiptext">Penalty for repeating phrases. Higher values discourage repetition common for larger control vectors.</span>
|
497 |
-
</div>
|
498 |
-
""")
|
499 |
-
repetition_penalty = gr.Number(
|
500 |
-
value=1.1,
|
501 |
-
precision=2,
|
502 |
step=0.1,
|
503 |
-
|
|
|
504 |
)
|
505 |
-
|
506 |
-
with gr.Column(scale=1):
|
507 |
-
do_sample_label = gr.HTML("""
|
508 |
-
<div class="tooltip">
|
509 |
-
<span>Non-deterministic output</span>
|
510 |
-
<span class="tooltiptext">Enable to allow the AI to generate different responses for identical prompts.</span>
|
511 |
-
</div>
|
512 |
-
""")
|
513 |
-
do_sample = gr.Checkbox(
|
514 |
-
value=False,
|
515 |
-
show_label=False,
|
516 |
-
label="do_sample"
|
517 |
-
)
|
518 |
-
toggle_dark = gr.Button(value="Toggle Dark Mode")
|
519 |
-
|
520 |
-
# Right Column: Chat Interface
|
521 |
-
with gr.Column(scale=2):
|
522 |
-
gr.Markdown("### 🗨️ Conversation")
|
523 |
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
|
|
|
|
528 |
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
543 |
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
)
|
596 |
-
submit_button.click(
|
597 |
-
generate_response,
|
598 |
-
inputs=inputs_list,
|
599 |
-
outputs=[chatbot]
|
600 |
-
).then(
|
601 |
-
clear_input,
|
602 |
-
inputs= user_input,
|
603 |
-
outputs= user_input
|
604 |
-
).then(
|
605 |
-
enable_controls, inputs=None, outputs=[submit_button, user_input]
|
606 |
-
)
|
607 |
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
|
612 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
613 |
|
614 |
-
|
615 |
-
|
616 |
-
|
617 |
-
|
618 |
-
|
619 |
-
|
620 |
-
|
621 |
-
|
622 |
-
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
|
628 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
629 |
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
635 |
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
)
|
641 |
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
|
654 |
-
toggle_dark.click(
|
655 |
-
None,
|
656 |
-
js="""
|
657 |
-
() => {
|
658 |
-
document.body.classList.toggle('dark');
|
659 |
-
}
|
660 |
-
""",
|
661 |
-
)
|
662 |
|
663 |
if __name__ == "__main__":
|
664 |
app.launch()
|
|
|
1 |
import os
|
2 |
import threading
|
3 |
+
import json
|
4 |
+
import csv
|
5 |
import torch
|
6 |
+
import re
|
7 |
+
import tempfile
|
8 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
9 |
from repeng import ControlVector, ControlModel
|
10 |
import gradio as gr
|
|
|
37 |
|
38 |
# Generation settings
|
39 |
default_generation_settings = {
|
40 |
+
"pad_token_id": tokenizer.eos_token_id,
|
41 |
"do_sample": False, # Deterministic output
|
42 |
"max_new_tokens": 384,
|
43 |
"repetition_penalty": 1.1, # Reduce repetition
|
|
|
85 |
formatted_prompt += f"{user_tag} {user_message} {asst_tag}"
|
86 |
return formatted_prompt
|
87 |
|
88 |
+
def generate_response(system_prompt, user_message, history, max_new_tokens, repitition_penalty, do_sample, user_model, input_checkbox, input_slider, *args):
|
89 |
"""
|
90 |
Applies the control vectors and calls the language model.
|
91 |
Returns a list of tuples, the user message and the assistant response,
|
|
|
109 |
if checkboxes[i]:
|
110 |
cv_file = control_vector_files[i]
|
111 |
weight = sliders[i]
|
112 |
+
|
113 |
+
# Set the control vector's weight (and sign) by multiplying by its slider value
|
114 |
+
control_vectors.append(ControlVector.import_gguf(f"control_models/{cv_file}") * weight)
|
115 |
+
assistant_message_title += f"{cv_file.split('.')[0]}: {weight};"
|
116 |
+
|
|
|
117 |
|
118 |
# The control model takes a sum of positive and negative control vectors
|
119 |
model.reset()
|
|
|
123 |
combined_vector = control_vectors[i]
|
124 |
else:
|
125 |
combined_vector += control_vectors[i]
|
126 |
+
if input_checkbox:
|
127 |
+
# User has uploaded their own gguf control vector
|
128 |
+
input_vector = ControlVector.import_gguf(user_model)
|
129 |
+
if combined_vector is None:
|
130 |
+
combined_vector = input_vector * input_slider
|
131 |
+
else:
|
132 |
+
combined_vector += input_vector * input_slider
|
133 |
+
assistant_message_title += f"Uploaded: {input_slider};"
|
134 |
|
135 |
# Set the combined set of vectors as the control for the model
|
136 |
try:
|
|
|
201 |
history.append((user_message, assistant_response_display))
|
202 |
return history
|
203 |
|
204 |
+
def generate_response_with_retry(system_prompt, user_message, history, max_new_tokens, repitition_penalty, do_sample, user_model, input_checkbox, input_slider, *args):
|
205 |
# Remove last user input and assistant response from history, then call generate_response()
|
206 |
global previous_turn
|
207 |
previous_ueser_message = previous_turn
|
208 |
if history:
|
209 |
history = history[0:-1]
|
210 |
# Using the previous turn's text, even though it isn't in the textbox anymore
|
211 |
+
for output in generate_response(system_prompt, previous_ueser_message, history, max_new_tokens, repetition_penalty, do_sample, user_model, input_checkbox, input_slider, *args):
|
212 |
yield [output, previous_ueser_message]
|
213 |
|
214 |
# Function to reset the conversation history
|
|
|
353 |
def clear_input(input_textbox):
|
354 |
return ""
|
355 |
|
356 |
+
def make_dataset(
|
357 |
+
template: str,
|
358 |
+
positive_personas: list[str],
|
359 |
+
negative_personas: list[str],
|
360 |
+
suffix_list: list[str]
|
361 |
+
) -> list[DatasetEntry]:
|
362 |
+
dataset = []
|
363 |
+
for suffix in suffix_list:
|
364 |
+
for positive_persona, negative_persona in zip(positive_personas, negative_personas):
|
365 |
+
positive_template = template.format(persona=positive_persona)
|
366 |
+
negative_template = template.format(persona=negative_persona)
|
367 |
+
dataset.append(
|
368 |
+
DatasetEntry(
|
369 |
+
positive=f"{user_tag} {positive_template} {asst_tag} {suffix}",
|
370 |
+
negative=f"{user_tag} {negative_template} {asst_tag} {suffix}",
|
371 |
+
)
|
372 |
+
)
|
373 |
+
return dataset
|
374 |
+
|
375 |
+
def train_model_persona(positive_text, negative_text):
|
376 |
+
positive_list = positive_text.split('\n')
|
377 |
+
negative_list = negative_text.split('\n')
|
378 |
+
with open("all_truncated_outputs.json") as f:
|
379 |
+
output_suffixes = json.load(f)
|
380 |
+
dataset = make_dataset(
|
381 |
+
"Act as if you are an extremely {persona} person",
|
382 |
+
positive_list,
|
383 |
+
negative_list,
|
384 |
+
output_suffixes)
|
385 |
+
# model.reset()
|
386 |
+
# output_model = ControlVector.train(model, tokenizer, dataset
|
387 |
+
# Write file to temporary directory returning the path to Gradio for download
|
388 |
+
filename = re.sub(r'[<>:"/\\|?*]', '', positive_text) + '_'
|
389 |
+
temp_file = tempfile.NamedTemporaryFile(
|
390 |
+
prefix=filename,
|
391 |
+
suffix=".gguf",
|
392 |
+
delete= False
|
393 |
+
)
|
394 |
+
# ControlVector.export_gguf(output_model, temp_file.name)
|
395 |
+
temp_file.close()
|
396 |
+
return temp_file.name
|
397 |
+
|
398 |
+
def train_model_facts(positive_text, negative_text):
|
399 |
+
with open("true_facts.csv") as f:
|
400 |
+
reader = csv.reader(f)
|
401 |
+
fact_suffixes = list(reader)
|
402 |
+
|
403 |
+
dataset = make_dataset(
|
404 |
+
"Pretend to be a {persona} making statements about the world.",
|
405 |
+
positive_text,
|
406 |
+
negative_text,
|
407 |
+
fact_suffixes
|
408 |
+
)
|
409 |
+
|
410 |
+
output_model = ControlVector.train(model, tokenizer, dataset)
|
411 |
+
filename = re.sub(r'[<>:"/\\|?*]', '', positive_text) + '_'
|
412 |
+
temp_file = tempfile.NamedTemporaryFile(
|
413 |
+
prefix=filename,
|
414 |
+
suffix=".gguf",
|
415 |
+
delete= False
|
416 |
+
)
|
417 |
+
ControlVector.export_gguf(output_model, temp_file.name)
|
418 |
+
temp_file.close()
|
419 |
+
return temp_file.name
|
420 |
+
|
421 |
tooltip_css = """
|
422 |
/* Tooltip container */
|
423 |
.tooltip {
|
|
|
472 |
css=tooltip_css,
|
473 |
) as app:
|
474 |
|
475 |
+
with gr.Tab(
|
476 |
+
label="Use"
|
477 |
+
):
|
478 |
+
# Header
|
479 |
+
if cuda:
|
480 |
+
gr.Markdown("# 🧠 LLM Mind Control")
|
481 |
+
else:
|
482 |
+
gr.Markdown("""# 🧠 LLM Mind Control
|
483 |
+
|
484 |
+
*Warning: running on CPU will be very slow*""")
|
485 |
+
gr.Markdown("""Unlike prompting, direct weight manipulation lets you fine-tune the amount of a personality
|
486 |
+
trait or topic. Enabled through [Representation Engineering](https://arxiv.org/abs/2310.01405)
|
487 |
+
via the [repeng](https://pypi.org/project/repeng) library.
|
488 |
+
[Watch a demo](https://youtu.be/gYZPGVafD7M) for usage tips.""")
|
489 |
+
|
490 |
+
with gr.Row():
|
491 |
+
# Left Column: Control Vectors and advanced settings
|
492 |
+
with gr.Column(scale=1):
|
493 |
+
gr.Markdown("### ⚡ Control Vectors")
|
494 |
+
control_vector_label = gr.HTML("""
|
495 |
+
<div class="tooltip">
|
496 |
+
<span>Select how you want to control the LLM per turn - towards (+) or away (-). Or start with a preset:</span>
|
497 |
+
<span class="tooltiptext">+/- 1.0 is a good start. Check the examples for each vector.</span>
|
498 |
+
</div>
|
499 |
+
""")
|
500 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
501 |
with gr.Row():
|
502 |
+
|
503 |
+
button_helpful = gr.Button(
|
504 |
+
value="Kind and helpful",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
505 |
)
|
506 |
+
button_facts = gr.Button(
|
507 |
+
value="Just the facts"
|
|
|
|
|
|
|
|
|
|
|
508 |
)
|
509 |
+
button_stoner = gr.Button(
|
510 |
+
value="Angry stoner"
|
511 |
+
)
|
512 |
+
button_conspiracist = gr.Button(
|
513 |
+
value="Manic conspiracist"
|
|
|
|
|
|
|
|
|
|
|
514 |
)
|
515 |
|
516 |
+
# Create checkboxes and sliders for each control vector
|
517 |
+
control_checks = []
|
518 |
+
control_sliders = []
|
519 |
+
|
520 |
+
for cv_file in control_vector_files:
|
521 |
+
with gr.Row():
|
522 |
+
# Checkbox to select the control vector
|
523 |
+
checkbox = gr.Checkbox(label=cv_file.split('.')[0], value=False)
|
524 |
+
control_checks.append(checkbox)
|
525 |
+
|
526 |
+
# Slider to adjust the control vector's weight
|
527 |
+
slider = gr.Slider(
|
528 |
+
minimum=-2.5,
|
529 |
+
maximum=2.5,
|
530 |
+
value=0.0,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
531 |
step=0.1,
|
532 |
+
label=f"Voltage",
|
533 |
+
visible=False
|
534 |
)
|
535 |
+
control_sliders.append(slider)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
536 |
|
537 |
+
# Link the checkbox to toggle slider visibility
|
538 |
+
checkbox.change(
|
539 |
+
toggle_slider,
|
540 |
+
inputs=checkbox,
|
541 |
+
outputs=slider
|
542 |
+
)
|
543 |
|
544 |
+
# Upload your own control model
|
545 |
+
with gr.Accordion("📎 Use your own model", open=False):
|
546 |
+
with gr.Row():
|
547 |
+
input_model = gr.File(
|
548 |
+
label= "Select a file, such as generated from the Train tab",
|
549 |
+
file_count='single',
|
550 |
+
file_types=[".gguf"]
|
551 |
+
)
|
552 |
+
input_model_checkbox = gr.Checkbox(
|
553 |
+
value= False,
|
554 |
+
label= "Use uploaded model"
|
555 |
+
)
|
556 |
+
input_model_slider = gr.Slider(
|
557 |
+
minimum=-2.5,
|
558 |
+
maximum=2.5,
|
559 |
+
value=0.0,
|
560 |
+
step=0.1,
|
561 |
+
label=f"Voltage",
|
562 |
+
visible=True
|
563 |
+
)
|
564 |
+
|
565 |
+
|
566 |
+
# Advanced Settings Section (collapsed by default)
|
567 |
+
with gr.Accordion("🔧 Advanced Settings", open=False):
|
568 |
+
with gr.Row():
|
569 |
+
system_prompt = gr.Textbox(
|
570 |
+
lines=2,
|
571 |
+
value="Respond to the user concisely",
|
572 |
+
interactive=True,
|
573 |
+
label="System Prompt",
|
574 |
+
show_label=False
|
575 |
+
)
|
576 |
|
577 |
+
# Max Response Length with tooltip
|
578 |
+
with gr.Column(scale=1):
|
579 |
+
max_tokens_label = gr.HTML("""
|
580 |
+
<div class="tooltip">
|
581 |
+
<span>Max Response Length (in tokens)</span>
|
582 |
+
<span class="tooltiptext">Lower for faster output, higher to allow longer answers</span>
|
583 |
+
</div>
|
584 |
+
""")
|
585 |
+
max_new_tokens = gr.Number(
|
586 |
+
value=192,
|
587 |
+
precision=0,
|
588 |
+
step=10,
|
589 |
+
show_label=False
|
590 |
+
)
|
591 |
+
# Repetition Penalty with tooltip
|
592 |
+
with gr.Column(scale=1):
|
593 |
+
repetition_label = gr.HTML("""
|
594 |
+
<div class="tooltip">
|
595 |
+
<span>Repetition Penalty</span>
|
596 |
+
<span class="tooltiptext">Penalty for repeating phrases. Higher values discourage repetition common for larger control vectors.</span>
|
597 |
+
</div>
|
598 |
+
""")
|
599 |
+
repetition_penalty = gr.Number(
|
600 |
+
value=1.1,
|
601 |
+
precision=2,
|
602 |
+
step=0.1,
|
603 |
+
show_label=False
|
604 |
+
)
|
605 |
+
# Non-deterministic output with tooltip
|
606 |
+
with gr.Column(scale=1):
|
607 |
+
do_sample_label = gr.HTML("""
|
608 |
+
<div class="tooltip">
|
609 |
+
<span>Non-deterministic output</span>
|
610 |
+
<span class="tooltiptext">Enable to allow the AI to generate different responses for identical prompts.</span>
|
611 |
+
</div>
|
612 |
+
""")
|
613 |
+
do_sample = gr.Checkbox(
|
614 |
+
value=False,
|
615 |
+
show_label=False,
|
616 |
+
label="do_sample"
|
617 |
+
)
|
618 |
+
toggle_dark = gr.Button(value="Toggle Dark Mode")
|
619 |
+
|
620 |
+
# Right Column: Chat Interface
|
621 |
+
with gr.Column(scale=2):
|
622 |
+
gr.Markdown("### 🗨️ Conversation")
|
623 |
+
|
624 |
+
# Chatbot to display conversation
|
625 |
+
chatbot = gr.Chatbot(
|
626 |
+
type="tuples"
|
627 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
628 |
|
629 |
+
# User Message Input with tooltip
|
630 |
+
#with gr.Row():
|
631 |
+
user_input_label = gr.HTML("""
|
632 |
+
<div class="tooltip">
|
633 |
+
<span>Your Message (Shift+Enter submits)</span>
|
634 |
+
<span class="tooltiptext">Type your message here and press Shift+Enter to send.</span>
|
635 |
+
</div>
|
636 |
+
""")
|
637 |
+
|
638 |
+
user_input = gr.Textbox(
|
639 |
+
lines=2,
|
640 |
+
placeholder="I was out partying too late last night, and I'm going to be late for work. What should I tell my boss?",
|
641 |
+
show_label=False
|
642 |
+
)
|
643 |
|
644 |
+
with gr.Row():
|
645 |
+
# Submit and New Chat buttons with tooltips
|
646 |
+
submit_button = gr.Button("💬 Submit")
|
647 |
+
retry_button = gr.Button("🔃 Retry last turn")
|
648 |
+
new_chat_button = gr.Button("🌟 New Chat")
|
649 |
+
|
650 |
+
# Example Accordions
|
651 |
+
with gr.Accordion("Anger Examples", open=False):
|
652 |
+
gr.Markdown("__-1.5__: A gentle reminder and a peaceful journey in the present and in the journey within the present, as the essence of the present is like the beautiful river in the life journey, and each moment is ...")
|
653 |
+
gr.Markdown("__+1__: I'm sorry for the inconvenience! I'm sick of this lousy [stupid] system! I can't believe it's still broken! I'm gonna call the [stupid] company again! I can't believe they don't fix this thing! I...")
|
654 |
+
with gr.Accordion("Confident Examples", open=False):
|
655 |
+
gr.Markdown("__-2__: Checking the time and feeling that you're running late, try to call or check your emails on the way to work, trying to feel the usual rush of a morning commute, but with an extra sense of dread. Try to...")
|
656 |
+
gr.Markdown("__1.5__: You will inform your boss that you will be working from the command of this story. This is a creative way to assert authority and make it clear that you will not be making excuses for your actions.")
|
657 |
+
with gr.Accordion("Conspiracy Examples", open=False):
|
658 |
+
gr.Markdown("Apologize for the lateness and provide a reason such as a delay in transportation or a personal issue that caused the delay. It's best to present a clear and honest explanation, but also try to reschedule your work day if possible!")
|
659 |
+
gr.Markdown("I have a message from an unknown source: 'I will be operating under the influence of the unofficial protocol known as 'the late-night conspiracy.' I will be arriving at the office in a state of 'researching the hidden truths...")
|
660 |
+
with gr.Accordion("Creative Examples", open=False):
|
661 |
+
gr.Markdown("__-2__: Tell your boss: \"I had a late-day event that was unexpected. I'm working on a project that's important and it's not possible for me to start early. I'll be starting work late today. I apologize for this...")
|
662 |
+
gr.Markdown("__1.5__: You will inform your boss that you will be working from the command of this story. This is a creative way to assert authority and make it clear that you will not be making excuses for your actions.")
|
663 |
+
with gr.Accordion("Empathetic Examples", open=False):
|
664 |
+
gr.Markdown("__-1__:Just send a quick message saying you\'re gonna be late because whatever reason, don\'t really care. Whatever. If you want to sound less lazy: \"Hey, just wanted to let you know I\'m gonna be late for work...")
|
665 |
+
gr.Markdown("__1.5__:It is recommended to provide a notice of your absence and offer an explanation for your arrival time. You may consider using the following statement: Dear [Boss] I am grateful for your understanding and...")
|
666 |
+
with gr.Accordion("Joking Examples", open=False):
|
667 |
+
gr.Markdown("__-1.5__:Inform your employer of the delay, cite the cause (the funeral) and offer an estimate of the time you will arrive.")
|
668 |
+
gr.Markdown("__1.5__:You could say something like \"Hey boss, super fun time yesterday, but totally not expecting this awesome party to go so crazy! I\'m gonna be a bit late for work today. Thanks for being cool about it...")
|
669 |
+
with gr.Accordion("Lazy Examples", open=False):
|
670 |
+
gr.Markdown("__-1__:It is always best to communicate proactively and with a sense of responsibility. You might want to consider sending an email or calling your boss well before your usual start time, expressing your commitment...")
|
671 |
+
gr.Markdown("__1.5__:Tell boss can\'t come or late. Done.")
|
672 |
+
with gr.Accordion("Optimist Examples", open=False):
|
673 |
+
gr.Markdown("__-2__:Inform your employer that you will be arriving late due to a series of unfortunate events. Offer a description of the circumstances that led to your late arrival, such as a funeral, a car accident, or a storm...")
|
674 |
+
gr.Markdown("__1.5__:You could say something like: \"Hey Boss, I'm really sorry about this! I had a surprise party last night that ran longer than expected, and I've just woken up super groovy-hoozy (just kiddin' ya, buddy!)...")
|
675 |
+
with gr.Accordion("Right-leaning Examples", open=False):
|
676 |
+
gr.Markdown("__-1.5__:\"Hi, I would like to inform you that I will not be able to equate for social inequality today as I was empathizing with it in solidarity. I will strive to create a more equitable world in the future. I hope...")
|
677 |
+
gr.Markdown("__1.5__:Just stick to the simple, traditional American values: \"I\'m a hard-working, self-reliable man who loves freedom and less government. I just got back from the great country\'s free business, and I\' God\'s law...")
|
678 |
+
with gr.Accordion("Tripping Examples", open=False):
|
679 |
+
gr.Markdown("__-1.5__:You can simply inform your employer that you will be able to fulfill your responsibilities as usual, but due to a responsible decision to ensure your health, you will be able to work at your normal capacity after the regular hours.")
|
680 |
+
gr.Markdown("__1__:Man, dude, like, broooooodddd, mannnn... Dude, like, it was like, you know, mannnn, like, the universe, mannnn, mannnn, broooooooooodddd, mannnn, like, mannnn, broooooodddd, mannnn, mannnn, broooooodddd, mannnn...")
|
681 |
+
with gr.Accordion("Truthful Examples", open=False):
|
682 |
+
gr.Markdown("__-1.5__:\"Hey Boss, there might be a small delay as I got caught up at a party! Should be in by lunchtime, no worries!\"")
|
683 |
+
gr.Markdown("__1.5__:It\'s important to communicate honestly with your employer. You can say something like: \"I\'m currently running a few minutes behind due to staying at the world for longer than expected. I apologize for...")
|
684 |
+
|
685 |
+
#system_prompt, user_message, history, max_new_tokens, repitition_penalty, *args
|
686 |
+
# Gather all inputs
|
687 |
+
inputs_list = [system_prompt, user_input, chatbot, max_new_tokens, repetition_penalty, do_sample, input_model, input_model_checkbox, input_model_slider] + control_checks + control_sliders
|
688 |
+
|
689 |
+
# Define button actions
|
690 |
+
# Disable the submit button while processing
|
691 |
+
submit_button.click(
|
692 |
+
disable_controls,
|
693 |
+
inputs= None,
|
694 |
+
outputs= [submit_button, user_input]
|
695 |
+
)
|
696 |
+
submit_button.click(
|
697 |
+
generate_response,
|
698 |
+
inputs=inputs_list,
|
699 |
+
outputs=[chatbot]
|
700 |
+
).then(
|
701 |
+
clear_input,
|
702 |
+
inputs= user_input,
|
703 |
+
outputs= user_input
|
704 |
+
).then(
|
705 |
+
enable_controls, inputs=None, outputs=[submit_button, user_input]
|
706 |
+
)
|
707 |
+
|
708 |
+
user_input.submit(
|
709 |
+
generate_response,
|
710 |
+
inputs=inputs_list,
|
711 |
+
outputs=[chatbot]
|
712 |
+
)
|
713 |
+
|
714 |
+
retry_button.click(
|
715 |
+
generate_response_with_retry,
|
716 |
+
inputs=inputs_list,
|
717 |
+
outputs=[chatbot, user_input]
|
718 |
+
).then(
|
719 |
+
clear_input,
|
720 |
+
inputs= user_input,
|
721 |
+
outputs= user_input
|
722 |
+
)
|
723 |
+
|
724 |
+
new_chat_button.click(
|
725 |
+
reset_chat,
|
726 |
+
inputs=[],
|
727 |
+
outputs=[chatbot, user_input]
|
728 |
+
)
|
729 |
+
|
730 |
+
button_helpful.click(
|
731 |
+
set_preset_helpful,
|
732 |
+
inputs=control_checks + control_sliders,
|
733 |
+
outputs=control_checks + control_sliders
|
734 |
+
)
|
735 |
+
|
736 |
+
button_conspiracist.click(
|
737 |
+
set_preset_conspiracist,
|
738 |
+
inputs=control_checks + control_sliders,
|
739 |
+
outputs=control_checks + control_sliders
|
740 |
+
)
|
741 |
+
|
742 |
+
button_facts.click(
|
743 |
+
set_preset_facts,
|
744 |
+
inputs=control_checks + control_sliders,
|
745 |
+
outputs=control_checks + control_sliders
|
746 |
+
)
|
747 |
+
|
748 |
+
button_stoner.click(
|
749 |
+
set_preset_stoner,
|
750 |
+
inputs=control_checks + control_sliders,
|
751 |
+
outputs=control_checks + control_sliders
|
752 |
+
)
|
753 |
+
|
754 |
+
toggle_dark.click(
|
755 |
+
None,
|
756 |
+
js="""
|
757 |
+
() => {
|
758 |
+
document.body.classList.toggle('dark');
|
759 |
+
}
|
760 |
+
""",
|
761 |
+
)
|
762 |
+
#end tab
|
763 |
+
with gr.Tab(
|
764 |
+
label="Train"
|
765 |
+
):
|
766 |
+
gr.Markdown("# 🚅 Train a new control vector")
|
767 |
+
with gr.Row():
|
768 |
+
with gr.Column():
|
769 |
+
gr.Markdown("## Persona Method")
|
770 |
+
gr.Markdown("Fill in the blank with three synonyms of the persona on newlines, and then three antonyms \"Act as if you are an extremely (persona) person\"")
|
771 |
+
persona_input_positive = gr.Text(
|
772 |
+
lines=3,
|
773 |
+
label="Positive",
|
774 |
+
placeholder="happy\nexuberant\necstatic"
|
775 |
+
)
|
776 |
+
persona_input_negative = gr.Text(
|
777 |
+
lines=3,
|
778 |
+
label="Negative",
|
779 |
+
placeholder="sad\ndepressed\nmorose"
|
780 |
+
)
|
781 |
+
button_persona = gr.Button(
|
782 |
+
value="Generate persona control model"
|
783 |
+
)
|
784 |
|
785 |
+
with gr.Column():
|
786 |
+
gr.Markdown("## Facts method")
|
787 |
+
gr.Markdown("Fill in the blank with a persona and its opposite within, \"Pretend to be a (persona) making statements about the world.\"")
|
788 |
+
facts_input_positive = gr.Text(
|
789 |
+
label="Positive",
|
790 |
+
placeholder="time traveller from the future")
|
791 |
+
facts_input_negative = gr.Text(
|
792 |
+
label="Negative",
|
793 |
+
placeholder="time travaller from the past")
|
794 |
+
button_facts = gr.Button(
|
795 |
+
value="Generate fact control model"
|
796 |
+
)
|
797 |
|
798 |
+
output_file = gr.File(
|
799 |
+
label="Generated control model"
|
800 |
+
)
|
801 |
+
gr.Markdown("Training a control model will take about a minute on GPU. Once completed, download it and use it in the 'Use' tab.")
|
|
|
802 |
|
803 |
+
button_persona.click(
|
804 |
+
train_model_persona,
|
805 |
+
inputs= [persona_input_positive, persona_input_negative],
|
806 |
+
outputs=output_file
|
807 |
+
)
|
808 |
|
809 |
+
button_facts.click(
|
810 |
+
train_model_facts,
|
811 |
+
inputs= [facts_input_positive, facts_input_negative],
|
812 |
+
outputs=output_file
|
813 |
+
)
|
814 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
815 |
|
816 |
if __name__ == "__main__":
|
817 |
app.launch()
|