File size: 1,912 Bytes
5bd179e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import gradio as gr
import modules.shared as shared
import json
from datetime import datetime
from pathlib import Path


myprompt="no data"

params = {
    "name": "AutoSave",
    "display_name": "AutoSave",
    "activate": True,
    "custom string": "n/a",
}

def save_data(string,timestamp=True):
    mydate=datetime.now().strftime('%Y%m%d')
    fname = f"{mydate}_text_log.txt"
    
    file_path=f'extensions/{params["name"]}/output'
    
    if not Path(file_path).exists():
        Path(file_path).mkdir()
    
    model = shared.model_name
    adapter = getattr(shared.model,'active_adapter','None')    
        
    with open(Path(f'{file_path}/{fname}'), 'a+', encoding='utf-8') as f:
        f.write(json.dumps({"model": model, "adapter":  adapter, "prompt" : myprompt, "reply":string} , indent=2 ))
    
    return Path(f'{file_path}/{fname}')

def input_modifier(string):
    """
    This function is applied to your text inputs before
    they are fed into the model.
    """ 
    global myprompt
    myprompt=string
    #print (f"input query:{myprompt}")

    return string

def output_modifier(string):
    """
    This function is applied to the model outputs.
    """
    if not params['activate']:
        return string
    
    save_data(string,timestamp=False)

    return string

def bot_prefix_modifier(string):
    """
    This function is only applied in chat mode. It modifies
    the prefix text for the Bot and can be used to bias its
    behavior.
    """
    return string

def ui():
    # Gradio elements
    activate = gr.Checkbox(value=params['activate'], label='Activate AutoSave')
    #string = gr.Textbox(value=params["bias string"], label='Custom Text')

    # Event functions to update the parameters in the backend
    #string.change(lambda x: params.update({"custom string": x}), string, None)
    activate.change(lambda x: params.update({"activate": x}), activate, None)