├── README.md ├── last.json └── script.py /README.md: -------------------------------------------------------------------------------- 1 | # Twinbook 2 | 3 | [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/Q5Q5MOB4M) 4 | 5 | 6 | New: 7 | - Fix: Working with the most recent WebUI that removed the Instruct template and added jinja 8 | - Show Stats in divider (add model, lora and params after ~~~~ (optional settings in Extra context tab) 9 | - save state of the tect boxes so when it reloads, the text is back 10 | 11 | It looks simple. It works simple. But this is a great way of combining Instruction Chat with Notebook. You get two huge text boxes! On the left, for typing INSTRUCTIONS; on the right, for RESPONSE. 12 | 13 | So what, huh? Well, read on. 14 | 15 | Here’s how it works. Type ‘What’s your name?’ into the Instructions box and click GENERATE NEW. That prints something like ‘My name is Assistant’ into the right Response box. 16 | 17 | ![image](https://github.com/FartyPants/Twinbook/assets/23346289/70374ba9-7d9b-4a5f-8f4e-598c7de13ed7) 18 | 19 | Oh, but you can type into the Response box as well, just like in the Notebook. For instance, you might delete the period and type ’ and I like ’. 20 | Then when you click CONTINUE, it goes on generating more text. Probably how it really likes to help users answering their questions. 21 | 22 | ![image](https://github.com/FartyPants/Twinbook/assets/23346289/7fc2e935-4653-494a-a1f9-5127a2e223be) 23 | 24 | So far so good. But hey! Did you realize you could have also changed the INSTRUCTIONS itself before you hit Continue! Let's say, ‘Describe how you enjoy your ice-cream.’ And then hit CONTINUE. 25 | 26 | ![image](https://github.com/FartyPants/Twinbook/assets/23346289/c9a37f42-0c90-4fc5-92c0-ebbbe5aa6098) 27 | 28 | See? Suddenly it talks about ice cream, It uses the previous response plus the new instruction to CONTINUE generating the text. 29 | 30 | This way you can modify responses, in the middle of the text - sort of steer the LLM towards your goal. It’s like a chat where you control both sides of the discussion. Just delete the part where it gets off the ramp and write different instructions. Both the previus text and the new instruction will became part of the new text. 31 | 32 | ![image](https://github.com/FartyPants/Twinbook/assets/23346289/48ef3975-518a-4cce-820e-0ea9f960d83e) 33 | 34 | ## GENERATE NEW 35 | Every time you click GENERATE NEW, it inserts those little squiggly lines (separator) into the Response window before it starts writing. Then it comes up with a whole NEW bit of text following the Instruction that doesn’t depend on any previous Responses. Unlike the chat that will include all the previous questions and answer, Generate NEW is a new start from blank slate but without clearing the text in Response window. Just adding squiggly line and writing new response. 36 | 37 | ![image](https://github.com/FartyPants/Twinbook/assets/23346289/c1cd16fc-f147-4d1d-8903-00d4dab50210) 38 | 39 | The squigglies are separating the different NEW responses (sort of like alternative takes). And Continue will always only take to account the text after the last squiggly. So removing squigglies, pasting your own text, editing the text - all those things will affect the Continue operation. 40 | 41 | ## GEN WITH MEMORY 42 | This function takes the last Response (i.e., after last Squiggly line, just like CONTINUE), and inserts it at the top of the LLM instruction, before System or Instruction commands. This means that the LLM gets to see the last response, but only as a memory. So it generates a NEW response, not a continuation of the text (like CONTIUE does), but with a faint remote ideas of previous facts. 43 | 44 | So if you tell it to just write and story (Generate NEW) it will write a random story and then if you use Generate NEW again the next story will be different. However if you use Gen with Memory, the story could be somehow simillar to the last generated one. 45 | 46 | ## Continue [SEL] (insert after selection) 47 | If you select a text in Response textbox, you can use Continue [SEL] to continue generating text and insert it after the selected text. The text will be generated from the selected text with the instructions in the INSTRUCTION box. Very cheeky. 48 | Also selection CAN span multiple blocks (squigglies) and all the text will be used to generate Continue. 49 | 50 | ## Keep 51 | Keep is a box where you can keep stuff. You can also send text from Response to keep with one button. Just select text in Response window and click [sel] to Keep 52 | 53 | Ugh, I know, it sounds complicated when you try to describe like this. You need to figure out how it works. It makes sense. I pinky swear. 54 | 55 | -------------------------------------------------------------------------------- /last.json: -------------------------------------------------------------------------------- 1 | { 2 | "text_boxA": "This is where you put instructions", 3 | "text_boxB": "And this is where the response will be added. \n\nThe text here is also editable, so you can change it and then use Continue to continue generating.\n\nBut, you could have also changed the INSTRUCTIONS itself before you hit Continue!\n\nThis way you can modify responses, in the middle of the text - sort of steer the LLM towards your goal. It\u2019s like a chat where you control both sides of the discussion.\n\nIf you select a text, you can also use Continue [SEL] to continue generating text and insert it after the selected text. The text will be generated from the selected text with the instructions in the INSTRUCTION box.\n\nfor more check the readme in https://github.com/FartyPants/Twinbook\n", 4 | "text_boxC": "Here you can store anything long term", 5 | "context_replace": "", 6 | "extra_context": "", 7 | "extra_prefix": "" 8 | } -------------------------------------------------------------------------------- /script.py: -------------------------------------------------------------------------------- 1 | import gradio as gr 2 | import modules.shared as shared 3 | from pathlib import Path 4 | import re 5 | import json 6 | import time 7 | import html 8 | import copy 9 | from functools import partial 10 | from modules.text_generation import stop_everything_event 11 | from modules import chat 12 | from modules import ui as main_ui 13 | from modules.utils import gradio 14 | from modules.extensions import apply_extensions 15 | import random 16 | 17 | right_symbol = '\U000027A1' 18 | left_symbol = '\U00002B05' 19 | refresh_symbol = '\U0001f504' # 🔄 20 | 21 | basepath = "extensions/Twinbook/last.json" 22 | last_undo ='' 23 | 24 | describe_all = "Add an immersive and evocative description to the scene, capturing its visual and auditory aspects." 25 | describe_auditory = "Can you help me create a more evocative description of the scene, capturing its auditory aspects??" 26 | describe_smell = "Can you help me create a more evocative description of the scene, capturing its olfactory aspects?" 27 | describe_paint = "Paint a vivid picture to make the story come alive for the reader." 28 | describe_va = "Add visual imagery of the scene to create a more vivid picture for the reader." 29 | 30 | describe_continue = "Continue writing the story giving it more deatils." 31 | # You are a talented writing assistant. Always respond by incorporating the instructions into expertly written prose that is highly detailed, evocative, vivid and engaging. 32 | 33 | 34 | describe_add_simile = "Please enhance the sentence with a simile." 35 | # postfix can be also comma delimited 36 | simile_postfix = 'like' 37 | postfix_index = 0 38 | 39 | save_params = { 40 | "text_boxA": '', 41 | "text_boxB": '', 42 | "text_boxC": '', 43 | "context_replace": '', 44 | "extra_context": '', 45 | "extra_prefix": '', 46 | "add_stats": False, 47 | } 48 | 49 | 50 | 51 | params = { 52 | "display_name": "Twinbook", 53 | "is_tab": True, 54 | "selectA": [0,0], 55 | } 56 | 57 | class ToolButton(gr.Button, gr.components.FormComponent): 58 | """Small button with single emoji as text, fits inside gradio forms""" 59 | 60 | def __init__(self, **kwargs): 61 | super().__init__(variant="tool", **kwargs) 62 | 63 | def get_block_name(self): 64 | return "button" 65 | 66 | 67 | def create_refresh_button(refresh_component, refresh_method, refreshed_args, elem_class): 68 | def refresh(): 69 | refresh_method() 70 | args = refreshed_args() if callable(refreshed_args) else refreshed_args 71 | 72 | for k, v in args.items(): 73 | setattr(refresh_component, k, v) 74 | 75 | return gr.update(**(args or {})) 76 | 77 | refresh_button = ToolButton(value=refresh_symbol, elem_classes=elem_class) 78 | refresh_button.click( 79 | fn=refresh, 80 | inputs=[], 81 | outputs=[refresh_component] 82 | ) 83 | return refresh_button 84 | 85 | 86 | def read_file_to_string(file_path): 87 | data = '' 88 | try: 89 | with open(file_path, 'r') as file: 90 | data = file.read() 91 | except FileNotFoundError: 92 | data = '' 93 | 94 | return data 95 | 96 | 97 | def atoi(text): 98 | return int(text) if text.isdigit() else text.lower() 99 | 100 | def save_string_to_file(file_path, string): 101 | try: 102 | with open(file_path, 'w') as file: 103 | file.write(string) 104 | print("String saved to file successfully.") 105 | except Exception as e: 106 | print("Error occurred while saving string to file:", str(e)) 107 | 108 | 109 | last_history_visible = [] 110 | last_history_internal = [] 111 | 112 | 113 | def send_selected_to_Keep(text, textC): 114 | global params 115 | global save_params 116 | selF = params['selectA'][0] 117 | selT = params['selectA'][1] 118 | params['selectA'] = [0,0] 119 | texttextOUT = str(text) 120 | if not selF==selT: 121 | print(f"\033[1;32;1m\nSending selected text to Keep\033[0;37;0m") 122 | texttextOUT = texttextOUT[selF:selT] 123 | 124 | rettext = str(textC)+"\n+++\n"+texttextOUT+"\n" 125 | save_params["text_boxC"] = rettext 126 | return rettext 127 | else: 128 | print(f"No selection made") 129 | save_params["text_boxC"] = textC 130 | return textC 131 | 132 | 133 | def filter_squigly(text): 134 | text = text.replace('\r\n','\n') 135 | lines = text.split('\n') 136 | result = '' 137 | for line in lines: 138 | if line.startswith('~~~~'): 139 | line = '~~~~' 140 | 141 | if result == '': 142 | result = line 143 | else: 144 | result += '\n' + line 145 | 146 | return result 147 | 148 | # bastardized original chat function 149 | # chat.py 150 | 151 | # new jinja : 'custom_system_message' , instruction_template_str 152 | 153 | # extra_context 'Extra Context and Memories' 154 | # context_replace 'System Instruction' 155 | # extra_prefix 'Instruction Prefix' 156 | 157 | 158 | def generate_reply_wrapperMY(question, textBoxB, context_replace, extra_context, extra_prefix, state, quick_instruction, _continue=False, _genwithResponse = False, _continue_sel = False, _postfix = '', _addstop = []): 159 | 160 | global params 161 | global last_history_visible 162 | global last_history_internal 163 | global last_undo 164 | global postfix_index 165 | global basepath 166 | 167 | selF = params['selectA'][0] 168 | selT = params['selectA'][1] 169 | 170 | params['selectA'] = [0,0] 171 | 172 | texttextOUT = str(textBoxB) 173 | 174 | new_version = True 175 | if 'turn_template' in state: 176 | new_version = False 177 | 178 | text_before = "" 179 | text_after = "" 180 | 181 | if _continue_sel: 182 | if not selF==selT: 183 | print(f"\033[1;32;1m\nContinue from selected text and inserting after {selT}\033[0;37;0m") 184 | text_before = texttextOUT[:selF] 185 | text_after = texttextOUT[selT:] 186 | texttextOUT = texttextOUT[selF:selT] 187 | else: 188 | print(f"\033[1;31;1m\nNo selection made, reverting to full text Continue\033[0;37;0m") 189 | _continue_sel = False 190 | 191 | 192 | 193 | last_undo = text_before + texttextOUT + text_after 194 | visible_text = None 195 | 196 | text = question 197 | 198 | 199 | 200 | if extra_prefix.strip()!='': 201 | text = extra_prefix + question 202 | 203 | if quick_instruction.strip()!='': 204 | text = quick_instruction 205 | 206 | if _postfix !='': 207 | string_list = [item.strip() for item in _postfix.split(',')] 208 | if len(string_list) > 0: 209 | postfix_index = int(postfix_index) % len(string_list) 210 | next_item = string_list[postfix_index] 211 | postfix_index = postfix_index + 1 212 | texttextOUT = texttextOUT.rstrip() 213 | if quick_instruction == describe_add_simile: 214 | texttextOUT = texttextOUT.rstrip('.!?') 215 | texttextOUT = texttextOUT + ' ' + next_item 216 | 217 | textB = texttextOUT 218 | 219 | if new_version: 220 | if state['instruction_template_str']=='': 221 | print("Instruction template is empty! Select Instruct template in tab [Parameters] - [Instruction Template]") 222 | textB = texttextOUT + "\n Instruction template is empty! Select Instruct template in tab [Parameters] - [Instruction template]" 223 | yield text_before + textB + text_after 224 | return 225 | else: 226 | if state['turn_template']=='': 227 | print("Instruction template is empty! Select Instruct template in tab [Parameters] - [Instruction Template]") 228 | textB = texttextOUT + "\n Instruction template is empty! Select Instruct template in tab [Parameters] - [Instruction template]" 229 | yield text_before + textB + text_after 230 | return 231 | 232 | 233 | state['mode'] = 'instruct' 234 | 235 | _iswriting = "..." 236 | 237 | if text_after!='': 238 | _iswriting = "[...]" 239 | 240 | #context = state['context'] 241 | 242 | if new_version: 243 | context_instruct = state['custom_system_message'] 244 | contest_instruct_bk = context_instruct 245 | 246 | if context_replace.strip()!='': 247 | context_instruct = context_replace+'\n' 248 | state['custom_system_message'] = context_instruct 249 | 250 | if extra_context.strip()!='': 251 | state['custom_system_message'] = extra_context+ '\n' + context_instruct 252 | 253 | else: 254 | context_instruct = state['context_instruct'] 255 | contest_instruct_bk = context_instruct 256 | 257 | if context_replace.strip()!='': 258 | context_instruct = context_replace+'\n' 259 | state['context_instruct'] = context_instruct 260 | 261 | if extra_context.strip()!='': 262 | state['context_instruct'] = extra_context+ '\n' + context_instruct 263 | 264 | 265 | state = apply_extensions('state', state) 266 | if shared.model_name == 'None' or shared.model is None: 267 | print("No model is loaded! Select one in the Model tab.") 268 | yield text_before + textB + text_after 269 | return 270 | 271 | output = {'visible': [], 'internal': []} 272 | output['internal'].append(['', '']) 273 | output['visible'].append(['', '']) 274 | 275 | last_history = {'visible': [], 'internal': []} 276 | stopping_strings = chat.get_stopping_strings(state) 277 | 278 | if len(_addstop) > 0: 279 | stopping_strings = stopping_strings + _addstop 280 | 281 | 282 | #print (stopping_strings) 283 | is_stream = state['stream'] 284 | regenerate = False 285 | 286 | 287 | 288 | 289 | # Prepare the input 290 | if not (regenerate or _continue): 291 | visible_text = text 292 | 293 | add_stats = save_params["add_stats"] 294 | 295 | if add_stats: 296 | if shared.model: 297 | adapter_name = str(getattr(shared.model,'active_adapter','')) 298 | 299 | suffix_models = str(shared.model_name) 300 | if adapter_name != '': 301 | suffix_models = suffix_models + " [" + adapter_name+ "]" 302 | 303 | 304 | suffix_models = suffix_models + f" Temp: {state['temperature']}, top_p: {state['top_p']}, top_k: {state['top_k']}, RP: {state['repetition_penalty']}" 305 | 306 | if texttextOUT!='': 307 | textB = texttextOUT + f"\n\n~~~~ {suffix_models} ~~~~\n\n" 308 | else: 309 | if texttextOUT!='': 310 | textB = texttextOUT + f"\n\n~~~~\n\n" 311 | 312 | 313 | # Apply extensions 314 | text, visible_text = apply_extensions('chat_input', text, visible_text, state) 315 | text = apply_extensions('input', text, state, is_chat=True) 316 | 317 | outtext = text_before + textB + _iswriting + text_after 318 | yield outtext 319 | 320 | else: 321 | visible_text = text 322 | 323 | 324 | if regenerate: 325 | output['visible'].pop() 326 | output['internal'].pop() 327 | outtext = text_before + textB + _iswriting + text_after 328 | 329 | yield outtext 330 | 331 | elif _continue: 332 | 333 | textB = texttextOUT 334 | # continue sel can span across squiglies 335 | if _continue_sel: 336 | last_part = texttextOUT 337 | filtered = filter_squigly(last_part) 338 | last_part = filtered.replace("\n~~~~\n",'') 339 | 340 | #if quick_instruction == describe_continue: 341 | # last_history['internal'].append(['Continue writing.', last_part]) 342 | # last_history['visible'].append(['Continue writing.', last_part]) 343 | # last_part = 'Here is more details: ' 344 | 345 | else: 346 | 347 | filtered = filter_squigly(texttextOUT) 348 | parts = filtered.split('~~~~') 349 | # Extract the last part (after the last "~~~~") 350 | if len(parts) > 0: 351 | last_part = parts[-1].strip() 352 | else: 353 | last_part = '' 354 | 355 | # fill history for generate_chat_prompt 356 | last_history['internal'].append([text, last_part]) 357 | last_history['visible'].append([text, last_part]) 358 | 359 | outtext = text_before + textB + _iswriting + text_after 360 | yield outtext 361 | 362 | 363 | # Generate the prompt 364 | kwargs = { 365 | '_continue': _continue, 366 | 'history': last_history, 367 | } 368 | 369 | #prompt = apply_extensions('custom_generate_chat_prompt', question, state, **kwargs) 370 | 371 | prompt = chat.generate_chat_prompt(text, state, **kwargs) 372 | 373 | if _genwithResponse: 374 | 375 | filtered = filter_squigly(texttextOUT) 376 | parts = filtered.split('~~~~') 377 | 378 | # Extract the last part (after the last "~~~~") 379 | if len(parts) > 0: 380 | last_part = parts[-1].strip() 381 | else: 382 | last_part = '' 383 | 384 | if last_part != '': 385 | prompt = last_part+"\n\n"+prompt 386 | 387 | 388 | 389 | #put it back, just in case 390 | if new_version: 391 | state['custom_system_message'] = contest_instruct_bk 392 | else: 393 | state['context_instruct'] = contest_instruct_bk 394 | 395 | # Generate 396 | reply = None 397 | for j, reply in enumerate(chat.generate_reply(prompt, state, stopping_strings=stopping_strings, is_chat=True)): 398 | 399 | visible_reply = re.sub("(||{{user}})", state['name1'], reply) 400 | 401 | if shared.stop_everything: 402 | output['visible'][-1][1] = apply_extensions('output', output['visible'][-1][1], state, is_chat=True) 403 | 404 | output_text = output['visible'][-1][1] 405 | yield text_before + textB + output_text + text_after 406 | return 407 | 408 | if _continue: 409 | output['internal'][-1] = [text, reply] 410 | output['visible'][-1] = [visible_text, visible_reply] 411 | if is_stream: 412 | output_text = output['visible'][-1][1] 413 | yield text_before + textB + output_text + text_after 414 | elif not (j == 0 and visible_reply.strip() == ''): 415 | output['internal'][-1] = [text, reply.lstrip(' ')] 416 | output['visible'][-1] = [visible_text, visible_reply.lstrip(' ')] 417 | 418 | if is_stream: 419 | output_text = output['visible'][-1][1] 420 | yield text_before + textB + output_text + text_after 421 | 422 | output['visible'][-1][1] = apply_extensions('output', output['visible'][-1][1], state, is_chat=True) 423 | 424 | output_text = output['visible'][-1][1] 425 | 426 | # not really used for anything 427 | last_history_visible = output['visible'][-1] 428 | last_history_internal = output['internal'][-1] 429 | 430 | 431 | 432 | save_params["text_boxA"] = question 433 | save_params["text_boxB"] = text_before + textB + output_text + text_after 434 | #save_params["text_boxC"] = textBoxC 435 | save_params["context_replace"] = context_replace 436 | save_params["extra_context"] = extra_context 437 | save_params["extra_prefix"] = extra_prefix 438 | 439 | 440 | try: 441 | with open(Path(basepath), 'w') as json_file: 442 | json.dump(save_params, json_file, indent=4) 443 | except: 444 | print("Can't save last state..") 445 | 446 | yield text_before + textB + output_text + text_after 447 | 448 | def custom_js(): 449 | java = ''' 450 | const polybookElement = document.querySelector('#textbox-polybook textarea'); 451 | let polybookScrolled = false; 452 | 453 | polybookElement.addEventListener('scroll', function() { 454 | let diff = polybookElement.scrollHeight - polybookElement.clientHeight; 455 | if(Math.abs(polybookElement.scrollTop - diff) <= 1 || diff == 0) { 456 | polybookScrolled = false; 457 | } else { 458 | polybookScrolled = true; 459 | } 460 | }); 461 | 462 | const polybookObserver = new MutationObserver(function(mutations) { 463 | mutations.forEach(function(mutation) { 464 | if(!polybookScrolled) { 465 | polybookElement.scrollTop = polybookElement.scrollHeight; 466 | } 467 | }); 468 | }); 469 | 470 | polybookObserver.observe(polybookElement.parentNode.parentNode.parentNode, config); 471 | 472 | ''' 473 | return java 474 | 475 | 476 | help_str = """ 477 | ## Mini Guide 478 | It looks simple. It works simple. But this is a great way of combining Instruction Chat with Notebook. You get two huge text boxes! On the left, for typing INSTRUCTIONS; on the right, for RESPONSE. 479 | 480 | So what, huh? Well, read on. 481 | 482 | ### Generate NEW, edit response, then CONTINUE 483 | Here's how it works. Type 'What's your name?' into the Instructions box and click GENERATE NEW. That prints something like *'My name is Assistant'* into the right Response box. 484 | 485 | Oh, but you can type into the Response box as well, just like in the Notebook. For instance, you might delete the period and type *' and I like '*. Then when you click CONTINUE, it goes on generating more text. Probably how it really likes to help users answering their questions. 486 | 487 | So far so good. But hey! Did you realize you could have also changed the INSTRUCTIONS itself before you hit Continue! Let's say, *'Describe how you enjoy your ice-cream.'* And then hit CONTINUE. 488 | 489 | See? Suddenly it talks about ice cream, It uses the previous response plus the new instruction to CONTINUE generating the text. 490 | 491 | This way you can modify responses, in the middle of the text - sort of steer the LLM towards your goal. It's like a chat where you control both sides of the discussion. 492 | 493 | ### Generate New 494 | Every time you click GENERATE NEW, it inserts those little squiggly lines (separator) into the Response window before it starts writing. 495 | Then it comes up with a whole NEW bit of text following the Instruction that doesn't depend on any previous Responses. 496 | Unlike the chat that will include all the previous questions and answer, Generate NEW is a new start from blank slate but without clearing the text in Response window. 497 | 498 | The squigglies are separating the different NEW responses (sort of like alternative takes). 499 | And Continue will always only take to account the text after the last squiggly. So removing squigglies, pasting your own text, editing the text - all those things will affect the Continue operation. 500 | 501 | ### Gen with Memory 502 | This function takes the last Response (i.e., after last Squiggly line, just like CONTINUE), and inserts it at the top of the LLM instruction, before System or Instruction commands. This means that the LLM gets to see the last response, but only as a memory. So it generates a NEW response, not a continuation of the text (like CONTIUE does), but with a faint remote ideas of previous facts. 503 | 504 | So if you tell it to just write and story (Generate NEW) it will write a random story and then if you use Generate NEW again the next story will be different. However if you use Gen with Memory, the story could be somehow simillar to the last generated one. 505 | 506 | ### Continue [SEL] (insert after selection) 507 | If you select a text in Response textbox, you can use Continue [SEL] to continue generating text and insert it after the selected text. The text will be generated from the selected text with the instructions in the INSTRUCTION box. Very cheeky. 508 | Also selection CAN span multiple blocks (squigglies) and all the text will be used to generate Continue. 509 | 510 | Ugh, I know, it sounds complicated when you try to describe like this. You need to figure out how it works. It makes sense. I pinky swear. 511 | """ 512 | 513 | 514 | 515 | def ui(): 516 | global params 517 | global basepath 518 | global save_params 519 | 520 | params['selectA'] = [0,0] 521 | 522 | try: 523 | with open(basepath, 'r') as json_file: 524 | new_params = json.load(json_file) 525 | print("Twinbook: Loading last state") 526 | for item in new_params: 527 | save_params[item] = new_params[item] 528 | except: 529 | print("Twinbook: No last state saved") 530 | pass 531 | 532 | with gr.Row(): 533 | with gr.Column(): 534 | #extra_context = gr.Textbox(value='', lines=5, label = 'Extra Context', elem_classes=['textbox']) 535 | with gr.Tab('Instructions'): 536 | text_boxA = gr.Textbox(value=save_params["text_boxA"], lines=20, label = '', elem_classes=['textbox', 'add_scrollbar']) 537 | with gr.Tab('Extra Context'): 538 | quick_instruction = gr.Textbox(value='', lines=5, label = 'Temporary, ALT Instruction', elem_classes=['textbox'], info='Enter an Alternative instruction that will temporarily take precedent over the main Instruction. Example: "Describe xxx in details" to add more context to a paragraph selected in Response box, while using Continue [SEL].') 539 | gr.Markdown('Select Text in right window and press one of the Quick Enhancements') 540 | with gr.Row(): 541 | button_describe_all = gr.Button('Description') 542 | button_describe_va = gr.Button('Visual') 543 | button_describe_paint = gr.Button('Vivid Picture') 544 | button_describe_auditory = gr.Button('Sound') 545 | button_describe_smell = gr.Button('Smell') 546 | button_describe_simile = gr.Button('Simile') 547 | button_describe_continue = gr.Button('Write more') 548 | 549 | extra_context = gr.Textbox(value=save_params["extra_context"], lines=5, label = 'Extra Context and Memories', elem_classes=['textbox'], info='Enter memories that the model should know. Example: Your name is Sarah and you are a student at UBC') 550 | context_replace = gr.Textbox(value=save_params["context_replace"], lines=1, label = 'System Instruction', elem_classes=['textbox'], info='If filled, it will replace the Instruct Template system instruction.', placeholder = 'Below is an instruction that describes a task, you are blah, blah, blah...') 551 | extra_prefix = gr.Textbox(value=save_params["extra_prefix"], lines=2, label = 'Instruction Prefix', elem_classes=['textbox'], info='Enter instruction that will be always inserted before your text in the prompt. Example: Rewrite the following text: ') 552 | with gr.Row(): 553 | button_prefix_rewrite = gr.Button('Rewrite') 554 | gr.Markdown(" ") 555 | gr.Markdown(" ") 556 | gr.Markdown(" ") 557 | button_prefix_clear = gr.Button('Clear') 558 | with gr.Row(): 559 | add_stats = gr.Checkbox(value = save_params["add_stats"], label='Show Stats in divider',info = 'Add model, lora and parameters after ~~~~ ' ) 560 | 561 | with gr.Tab('Help'): 562 | gr.Markdown(help_str) 563 | 564 | with gr.Column(): 565 | with gr.Tab('Response'): 566 | text_boxB = gr.Textbox(value=save_params["text_boxB"], lines=20, label = '', elem_classes=['textbox', 'add_scrollbar'], elem_id='textbox-polybook') 567 | with gr.Tab('Keep'): 568 | text_boxC = gr.Textbox(value=save_params["text_boxC"], lines=20, label = '', elem_classes=['textbox', 'add_scrollbar'], elem_id='textbox-polybook') 569 | 570 | with gr.Row(): 571 | with gr.Column(): 572 | with gr.Row(): 573 | with gr.Column(): 574 | with gr.Row(): 575 | generate_btn = gr.Button('Generate New', variant='primary') 576 | generate_btnR = gr.Button('Gen with memory', variant='primary', elem_classes="small-button") 577 | #generate_Sel = gr.Button('Generate [SEL]', variant='primary', elem_classes="small-button") 578 | stop_btn = gr.Button('Stop', elem_classes="small-button", interactive= True) 579 | #with gr.Column(): 580 | # with gr.Row(): 581 | # mode = gr.Radio(label='Use Template', choices=['Chat', 'Instruction'], value='Instruction', elem_classes='slim-dropdown') 582 | 583 | with gr.Column(): 584 | with gr.Row(): 585 | with gr.Column(): 586 | with gr.Row(): 587 | continue_btn = gr.Button('Continue', variant='primary') 588 | continue_btn_sel = gr.Button('Continue [SEL]',variant='primary', elem_classes="small-button") 589 | stop_btn2 = gr.Button('Stop', elem_classes="small-button", interactive= True) 590 | clear2 = gr.Button('Clear', elem_classes="small-button") 591 | undo = gr.Button('Undo/Redo', elem_classes="small-button") 592 | send_sel_keep = gr.Button('[Sel] to Keep', elem_classes="small-button") 593 | 594 | 595 | input_paramsA = [text_boxA, text_boxB, context_replace, extra_context, extra_prefix, shared.gradio['interface_state'], quick_instruction] 596 | input_paramsQI = [text_boxA, text_boxB, context_replace, extra_context, extra_prefix, shared.gradio['interface_state']] 597 | output_paramsA =[text_boxB] 598 | 599 | def clear_quick_instruction(quick_instruction): 600 | return '' 601 | 602 | generate_btn.click( 603 | main_ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( 604 | generate_reply_wrapperMY, inputs=input_paramsA, outputs= output_paramsA, show_progress=False).then(clear_quick_instruction,quick_instruction,quick_instruction) 605 | 606 | continue_btn.click( 607 | main_ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( 608 | partial(generate_reply_wrapperMY, _continue=True), inputs=input_paramsA, outputs= output_paramsA, show_progress=False).then(clear_quick_instruction,quick_instruction,quick_instruction) 609 | 610 | continue_btn_sel.click( 611 | main_ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( 612 | partial(generate_reply_wrapperMY, _continue=True, _genwithResponse = False, _continue_sel = True), inputs=input_paramsA, outputs= output_paramsA, show_progress=False).then( 613 | clear_quick_instruction,quick_instruction,quick_instruction) 614 | 615 | generate_btnR.click( 616 | main_ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( 617 | partial(generate_reply_wrapperMY, _continue=False, _genwithResponse = True), inputs=input_paramsA, outputs= output_paramsA, show_progress=False).then(clear_quick_instruction,quick_instruction,quick_instruction) 618 | 619 | 620 | button_describe_all.click(main_ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( 621 | partial(generate_reply_wrapperMY, quick_instruction = describe_all ,_continue=True, _genwithResponse = False, _continue_sel = True,_addstop = ['\n']), inputs=input_paramsQI, outputs= output_paramsA, show_progress=False) 622 | 623 | button_describe_va.click(main_ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( 624 | partial(generate_reply_wrapperMY, quick_instruction = describe_va ,_continue=True, _genwithResponse = False, _continue_sel = True,_addstop = ['\n']), inputs=input_paramsQI, outputs= output_paramsA, show_progress=False) 625 | 626 | button_describe_paint.click(main_ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( 627 | partial(generate_reply_wrapperMY, quick_instruction = describe_paint ,_continue=True, _genwithResponse = False, _continue_sel = True,_addstop = ['\n']), inputs=input_paramsQI, outputs= output_paramsA, show_progress=False) 628 | 629 | button_describe_auditory.click(main_ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( 630 | partial(generate_reply_wrapperMY, quick_instruction = describe_auditory ,_continue=True, _genwithResponse = False, _continue_sel = True,_addstop = ['\n']), inputs=input_paramsQI, outputs= output_paramsA, show_progress=False) 631 | 632 | button_describe_smell.click(main_ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( 633 | partial(generate_reply_wrapperMY, quick_instruction = describe_smell ,_continue=True, _genwithResponse = False, _continue_sel = True,_addstop = ['\n']), inputs=input_paramsQI, outputs= output_paramsA, show_progress=False) 634 | 635 | button_describe_simile.click(main_ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( 636 | partial(generate_reply_wrapperMY, quick_instruction = describe_add_simile ,_continue=True, _genwithResponse = False, _continue_sel = True, _postfix = simile_postfix, _addstop = ['.','!','?']), inputs=input_paramsQI, outputs= output_paramsA, show_progress=False) 637 | 638 | button_describe_continue.click(main_ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( 639 | partial(generate_reply_wrapperMY, quick_instruction = describe_continue ,_continue=True, _genwithResponse = False, _continue_sel = True,_addstop = []), inputs=input_paramsQI, outputs= output_paramsA, show_progress=False) 640 | 641 | stop_btn.click(stop_everything_event, None, None, queue=False) 642 | stop_btn2.click(stop_everything_event, None, None, queue=False) 643 | 644 | def cleartext(texttextOUT): 645 | global last_undo 646 | 647 | if texttextOUT=='': 648 | return '' 649 | 650 | last_undo = str(texttextOUT) 651 | 652 | return '' 653 | 654 | clear2.click(cleartext, text_boxB, text_boxB, show_progress=False) 655 | 656 | def undo_event(texttextOUT): 657 | global last_undo 658 | 659 | prev_undo = last_undo 660 | last_undo = str(texttextOUT) 661 | return prev_undo 662 | 663 | 664 | undo.click(undo_event, text_boxB, text_boxB, show_progress=False) 665 | 666 | def on_selectA(evt: gr.SelectData): # SelectData is a subclass of EventData 667 | #print (f"Continue [SEL] is available for selected text {evt.index}") 668 | global params 669 | params['selectA'] = evt.index 670 | return "" 671 | 672 | text_boxB.select(on_selectA, None, None) 673 | 674 | send_sel_keep.click(send_selected_to_Keep, [text_boxB,text_boxC], text_boxC, show_progress=False) 675 | 676 | text_boxC.change(lambda x: save_params.update({"text_boxC": x}), text_boxC, None) 677 | add_stats.change(lambda x: save_params.update({"add_stats": x}), add_stats, None) 678 | 679 | 680 | def rewrite_prefix(text: str): 681 | return text 682 | 683 | button_prefix_rewrite.click(partial(rewrite_prefix, text="Rewrite the following text: "), None, extra_prefix, show_progress=False) 684 | button_prefix_clear.click(partial(rewrite_prefix, text=""), None, extra_prefix, show_progress=False) 685 | --------------------------------------------------------------------------------