Spaces:
Runtime error
Runtime error
zetavg
commited on
add inference_ui and stuff
Browse files- app.py +7 -4
- llama_lora/globals.py +3 -0
- llama_lora/models.py +81 -0
- llama_lora/ui/inference_tab.py +0 -10
- llama_lora/ui/inference_ui.py +351 -0
- llama_lora/ui/js_scripts.py +12 -0
- llama_lora/ui/main_page.py +121 -7
- llama_lora/utils/data.py +7 -0
- llama_lora/utils/prompter.py +71 -16
app.py
CHANGED
@@ -5,7 +5,7 @@ import fire
|
|
5 |
import gradio as gr
|
6 |
|
7 |
from llama_lora.globals import Global
|
8 |
-
from llama_lora.ui.main_page import main_page
|
9 |
from llama_lora.utils.data import init_data_dir
|
10 |
|
11 |
|
@@ -17,8 +17,10 @@ def main(
|
|
17 |
server_name: str = "127.0.0.1",
|
18 |
share: bool = False,
|
19 |
ui_show_sys_info: bool = True,
|
|
|
20 |
):
|
21 |
-
base_model = base_model or os.environ.get("
|
|
|
22 |
assert (
|
23 |
base_model
|
24 |
), "Please specify a --base_model, e.g. --base_model='decapoda-research/llama-7b-hf'"
|
@@ -28,15 +30,16 @@ def main(
|
|
28 |
), "Please specify a --data_dir, e.g. --data_dir='./data'"
|
29 |
|
30 |
Global.base_model = base_model
|
31 |
-
Global.data_dir = data_dir
|
32 |
Global.load_8bit = load_8bit
|
33 |
|
|
|
34 |
Global.ui_show_sys_info = ui_show_sys_info
|
35 |
|
36 |
os.makedirs(data_dir, exist_ok=True)
|
37 |
init_data_dir()
|
38 |
|
39 |
-
with gr.Blocks(title=
|
40 |
main_page()
|
41 |
|
42 |
demo.queue().launch(server_name=server_name, share=share)
|
|
|
5 |
import gradio as gr
|
6 |
|
7 |
from llama_lora.globals import Global
|
8 |
+
from llama_lora.ui.main_page import main_page, get_page_title, main_page_custom_css
|
9 |
from llama_lora.utils.data import init_data_dir
|
10 |
|
11 |
|
|
|
17 |
server_name: str = "127.0.0.1",
|
18 |
share: bool = False,
|
19 |
ui_show_sys_info: bool = True,
|
20 |
+
ui_dev_mode: bool = False,
|
21 |
):
|
22 |
+
base_model = base_model or os.environ.get("LLAMA_LORA_BASE_MODEL", "")
|
23 |
+
data_dir = data_dir or os.environ.get("LLAMA_LORA_DATA_DIR", "")
|
24 |
assert (
|
25 |
base_model
|
26 |
), "Please specify a --base_model, e.g. --base_model='decapoda-research/llama-7b-hf'"
|
|
|
30 |
), "Please specify a --data_dir, e.g. --data_dir='./data'"
|
31 |
|
32 |
Global.base_model = base_model
|
33 |
+
Global.data_dir = os.path.abspath(data_dir)
|
34 |
Global.load_8bit = load_8bit
|
35 |
|
36 |
+
Global.ui_dev_mode = ui_dev_mode
|
37 |
Global.ui_show_sys_info = ui_show_sys_info
|
38 |
|
39 |
os.makedirs(data_dir, exist_ok=True)
|
40 |
init_data_dir()
|
41 |
|
42 |
+
with gr.Blocks(title=get_page_title(), css=main_page_custom_css()) as demo:
|
43 |
main_page()
|
44 |
|
45 |
demo.queue().launch(server_name=server_name, share=share)
|
llama_lora/globals.py
CHANGED
@@ -11,4 +11,7 @@ class Global:
|
|
11 |
|
12 |
# UI related
|
13 |
ui_title: str = "LLaMA-LoRA"
|
|
|
|
|
14 |
ui_show_sys_info: bool = True
|
|
|
|
11 |
|
12 |
# UI related
|
13 |
ui_title: str = "LLaMA-LoRA"
|
14 |
+
ui_emoji: str = "🦙🎛️"
|
15 |
+
ui_subtitle: str = "Toolkit for examining and fine-tuning LLaMA models using low-rank adaptation (LoRA)."
|
16 |
ui_show_sys_info: bool = True
|
17 |
+
ui_dev_mode: bool = False
|
llama_lora/models.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
|
4 |
+
import torch
|
5 |
+
import transformers
|
6 |
+
from peft import PeftModel
|
7 |
+
from transformers import GenerationConfig, LlamaForCausalLM, LlamaTokenizer
|
8 |
+
|
9 |
+
from .globals import Global
|
10 |
+
|
11 |
+
|
12 |
+
def get_device():
|
13 |
+
if torch.cuda.is_available():
|
14 |
+
return "cuda"
|
15 |
+
else:
|
16 |
+
return "cpu"
|
17 |
+
|
18 |
+
try:
|
19 |
+
if torch.backends.mps.is_available():
|
20 |
+
return "mps"
|
21 |
+
except: # noqa: E722
|
22 |
+
pass
|
23 |
+
|
24 |
+
|
25 |
+
device = get_device()
|
26 |
+
|
27 |
+
|
28 |
+
def get_base_model():
|
29 |
+
load_base_model()
|
30 |
+
return Global.loaded_base_model
|
31 |
+
|
32 |
+
|
33 |
+
def get_model_with_lora(lora_weights: str = "tloen/alpaca-lora-7b"):
|
34 |
+
if device == "cuda":
|
35 |
+
return PeftModel.from_pretrained(
|
36 |
+
get_base_model(),
|
37 |
+
lora_weights,
|
38 |
+
torch_dtype=torch.float16,
|
39 |
+
)
|
40 |
+
elif device == "mps":
|
41 |
+
return PeftModel.from_pretrained(
|
42 |
+
get_base_model(),
|
43 |
+
lora_weights,
|
44 |
+
device_map={"": device},
|
45 |
+
torch_dtype=torch.float16,
|
46 |
+
)
|
47 |
+
else:
|
48 |
+
return PeftModel.from_pretrained(
|
49 |
+
get_base_model(),
|
50 |
+
lora_weights,
|
51 |
+
device_map={"": device},
|
52 |
+
)
|
53 |
+
|
54 |
+
|
55 |
+
def get_tokenizer():
|
56 |
+
load_base_model()
|
57 |
+
return Global.loaded_tokenizer
|
58 |
+
|
59 |
+
|
60 |
+
def load_base_model():
|
61 |
+
if Global.loaded_tokenizer is None:
|
62 |
+
Global.loaded_tokenizer = LlamaTokenizer.from_pretrained(
|
63 |
+
Global.base_model)
|
64 |
+
if Global.loaded_base_model is None:
|
65 |
+
if device == "cuda":
|
66 |
+
Global.loaded_base_model = LlamaForCausalLM.from_pretrained(
|
67 |
+
Global.base_model,
|
68 |
+
load_in_8bit=Global.load_8bit,
|
69 |
+
torch_dtype=torch.float16,
|
70 |
+
device_map="auto",
|
71 |
+
)
|
72 |
+
elif device == "mps":
|
73 |
+
Global.loaded_base_model = LlamaForCausalLM.from_pretrained(
|
74 |
+
Global.base_model,
|
75 |
+
device_map={"": device},
|
76 |
+
torch_dtype=torch.float16,
|
77 |
+
)
|
78 |
+
else:
|
79 |
+
model = LlamaForCausalLM.from_pretrained(
|
80 |
+
base_model, device_map={"": device}, low_cpu_mem_usage=True
|
81 |
+
)
|
llama_lora/ui/inference_tab.py
DELETED
@@ -1,10 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
|
3 |
-
from ..globals import Global
|
4 |
-
|
5 |
-
|
6 |
-
def inference_tab():
|
7 |
-
with gr.Tab("Inference"):
|
8 |
-
gr.Markdown("""
|
9 |
-
WIP.
|
10 |
-
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llama_lora/ui/inference_ui.py
ADDED
@@ -0,0 +1,351 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import time
|
3 |
+
|
4 |
+
import torch
|
5 |
+
import transformers
|
6 |
+
from transformers import GenerationConfig
|
7 |
+
|
8 |
+
from ..globals import Global
|
9 |
+
from ..models import get_model_with_lora, get_tokenizer, get_device
|
10 |
+
from ..utils.data import get_available_template_names
|
11 |
+
from ..utils.prompter import Prompter
|
12 |
+
from ..utils.callbacks import Iteratorize, Stream
|
13 |
+
|
14 |
+
device = get_device()
|
15 |
+
|
16 |
+
|
17 |
+
def inference(
|
18 |
+
lora_model_name,
|
19 |
+
prompt_template,
|
20 |
+
variable_0, variable_1, variable_2, variable_3,
|
21 |
+
variable_4, variable_5, variable_6, variable_7,
|
22 |
+
temperature=0.1,
|
23 |
+
top_p=0.75,
|
24 |
+
top_k=40,
|
25 |
+
num_beams=4,
|
26 |
+
repetition_penalty=1.2,
|
27 |
+
max_new_tokens=128,
|
28 |
+
stream_output=False,
|
29 |
+
**kwargs,
|
30 |
+
):
|
31 |
+
variables = [variable_0, variable_1, variable_2, variable_3,
|
32 |
+
variable_4, variable_5, variable_6, variable_7]
|
33 |
+
prompter = Prompter(prompt_template)
|
34 |
+
prompt = prompter.generate_prompt(variables)
|
35 |
+
|
36 |
+
if Global.ui_dev_mode:
|
37 |
+
message = f"Currently in UI dev mode, not running actual inference.\n\nYour prompt is:\n\n{prompt}"
|
38 |
+
print(message)
|
39 |
+
time.sleep(1)
|
40 |
+
yield message
|
41 |
+
return
|
42 |
+
|
43 |
+
model = get_model_with_lora(lora_model_name)
|
44 |
+
tokenizer = get_tokenizer()
|
45 |
+
|
46 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
47 |
+
input_ids = inputs["input_ids"].to(device)
|
48 |
+
generation_config = GenerationConfig(
|
49 |
+
temperature=temperature,
|
50 |
+
top_p=top_p,
|
51 |
+
top_k=top_k,
|
52 |
+
repetition_penalty=repetition_penalty,
|
53 |
+
num_beams=num_beams,
|
54 |
+
**kwargs,
|
55 |
+
)
|
56 |
+
|
57 |
+
generate_params = {
|
58 |
+
"input_ids": input_ids,
|
59 |
+
"generation_config": generation_config,
|
60 |
+
"return_dict_in_generate": True,
|
61 |
+
"output_scores": True,
|
62 |
+
"max_new_tokens": max_new_tokens,
|
63 |
+
}
|
64 |
+
|
65 |
+
if stream_output:
|
66 |
+
# Stream the reply 1 token at a time.
|
67 |
+
# This is based on the trick of using 'stopping_criteria' to create an iterator,
|
68 |
+
# from https://github.com/oobabooga/text-generation-webui/blob/ad37f396fc8bcbab90e11ecf17c56c97bfbd4a9c/modules/text_generation.py#L216-L243.
|
69 |
+
|
70 |
+
def generate_with_callback(callback=None, **kwargs):
|
71 |
+
kwargs.setdefault(
|
72 |
+
"stopping_criteria", transformers.StoppingCriteriaList()
|
73 |
+
)
|
74 |
+
kwargs["stopping_criteria"].append(
|
75 |
+
Stream(callback_func=callback)
|
76 |
+
)
|
77 |
+
with torch.no_grad():
|
78 |
+
model.generate(**kwargs)
|
79 |
+
|
80 |
+
def generate_with_streaming(**kwargs):
|
81 |
+
return Iteratorize(
|
82 |
+
generate_with_callback, kwargs, callback=None
|
83 |
+
)
|
84 |
+
|
85 |
+
with generate_with_streaming(**generate_params) as generator:
|
86 |
+
for output in generator:
|
87 |
+
# new_tokens = len(output) - len(input_ids[0])
|
88 |
+
decoded_output = tokenizer.decode(output)
|
89 |
+
|
90 |
+
if output[-1] in [tokenizer.eos_token_id]:
|
91 |
+
break
|
92 |
+
|
93 |
+
yield prompter.get_response(decoded_output)
|
94 |
+
return # early return for stream_output
|
95 |
+
|
96 |
+
# Without streaming
|
97 |
+
with torch.no_grad():
|
98 |
+
generation_output = model.generate(
|
99 |
+
input_ids=input_ids,
|
100 |
+
generation_config=generation_config,
|
101 |
+
return_dict_in_generate=True,
|
102 |
+
output_scores=True,
|
103 |
+
max_new_tokens=max_new_tokens,
|
104 |
+
)
|
105 |
+
s = generation_output.sequences[0]
|
106 |
+
output = tokenizer.decode(s)
|
107 |
+
yield prompter.get_response(output)
|
108 |
+
|
109 |
+
|
110 |
+
def reload_selections(current_lora_model, current_prompt_template):
|
111 |
+
available_template_names = get_available_template_names()
|
112 |
+
available_template_names_with_none = available_template_names + ["None"]
|
113 |
+
|
114 |
+
if current_prompt_template not in available_template_names_with_none:
|
115 |
+
current_prompt_template = None
|
116 |
+
|
117 |
+
current_prompt_template = current_prompt_template or next(
|
118 |
+
iter(available_template_names_with_none), None)
|
119 |
+
|
120 |
+
default_lora_models = ["tloen/alpaca-lora-7b"]
|
121 |
+
available_lora_models = default_lora_models
|
122 |
+
|
123 |
+
current_lora_model = current_lora_model or next(
|
124 |
+
iter(available_lora_models), None)
|
125 |
+
|
126 |
+
return (gr.Dropdown.update(choices=available_lora_models, value=current_lora_model),
|
127 |
+
gr.Dropdown.update(choices=available_template_names_with_none, value=current_prompt_template))
|
128 |
+
|
129 |
+
|
130 |
+
def handle_prompt_template_change(prompt_template):
|
131 |
+
prompter = Prompter(prompt_template)
|
132 |
+
var_names = prompter.get_variable_names()
|
133 |
+
human_var_names = [' '.join(word.capitalize()
|
134 |
+
for word in item.split('_')) for item in var_names]
|
135 |
+
gr_updates = [gr.Textbox.update(
|
136 |
+
label=name, visible=True) for name in human_var_names]
|
137 |
+
while len(gr_updates) < 8:
|
138 |
+
gr_updates.append(gr.Textbox.update(
|
139 |
+
label="Not Used", visible=False))
|
140 |
+
return gr_updates
|
141 |
+
|
142 |
+
|
143 |
+
def inference_ui():
|
144 |
+
with gr.Blocks() as inference_ui_blocks:
|
145 |
+
with gr.Row():
|
146 |
+
lora_model = gr.Dropdown(
|
147 |
+
label="LoRA Model",
|
148 |
+
elem_id="inference_lora_model",
|
149 |
+
value="tloen/alpaca-lora-7b",
|
150 |
+
allow_custom_value=True,
|
151 |
+
)
|
152 |
+
prompt_template = gr.Dropdown(
|
153 |
+
label="Prompt Template",
|
154 |
+
elem_id="inference_prompt_template",
|
155 |
+
)
|
156 |
+
reload_selections_button = gr.Button(
|
157 |
+
"Reload",
|
158 |
+
elem_id="inference_reload_selections_button"
|
159 |
+
)
|
160 |
+
reload_selections_button.style(
|
161 |
+
full_width=False,
|
162 |
+
size="sm")
|
163 |
+
with gr.Row():
|
164 |
+
with gr.Column():
|
165 |
+
with gr.Column():
|
166 |
+
variable_0 = gr.Textbox(lines=2, label="Prompt")
|
167 |
+
variable_1 = gr.Textbox(lines=2, label="", visible=False)
|
168 |
+
variable_2 = gr.Textbox(lines=2, label="", visible=False)
|
169 |
+
variable_3 = gr.Textbox(lines=2, label="", visible=False)
|
170 |
+
variable_4 = gr.Textbox(lines=2, label="", visible=False)
|
171 |
+
variable_5 = gr.Textbox(lines=2, label="", visible=False)
|
172 |
+
variable_6 = gr.Textbox(lines=2, label="", visible=False)
|
173 |
+
variable_7 = gr.Textbox(lines=2, label="", visible=False)
|
174 |
+
|
175 |
+
with gr.Column():
|
176 |
+
with gr.Row():
|
177 |
+
generate_btn = gr.Button(
|
178 |
+
"Generate", variant="primary", label="Generate", elem_id="inference_generate_btn",
|
179 |
+
)
|
180 |
+
stop_btn = gr.Button(
|
181 |
+
"Stop", variant="stop", label="Stop Iterating", elem_id="inference_stop_btn")
|
182 |
+
|
183 |
+
with gr.Column():
|
184 |
+
temperature = gr.Slider(
|
185 |
+
minimum=0.01, maximum=1.99, value=0.1, step=0.01,
|
186 |
+
label="Temperature",
|
187 |
+
elem_id="inference_temperature"
|
188 |
+
)
|
189 |
+
|
190 |
+
top_p = gr.Slider(
|
191 |
+
minimum=0, maximum=1, value=0.75, step=0.01,
|
192 |
+
label="Top P",
|
193 |
+
elem_id="inference_top_p"
|
194 |
+
)
|
195 |
+
|
196 |
+
top_k = gr.Slider(
|
197 |
+
minimum=0, maximum=200, value=40, step=1,
|
198 |
+
label="Top K",
|
199 |
+
elem_id="inference_top_k"
|
200 |
+
)
|
201 |
+
|
202 |
+
num_beams = gr.Slider(
|
203 |
+
minimum=1, maximum=4, value=1, step=1,
|
204 |
+
label="Beams",
|
205 |
+
elem_id="inference_beams"
|
206 |
+
)
|
207 |
+
|
208 |
+
repetition_penalty = gr.Slider(
|
209 |
+
minimum=0, maximum=2.5, value=1.2, step=0.01,
|
210 |
+
label="Repetition Penalty",
|
211 |
+
elem_id="inference_repetition_penalty"
|
212 |
+
)
|
213 |
+
|
214 |
+
max_new_tokens = gr.Slider(
|
215 |
+
minimum=0, maximum=4096, value=128, step=1,
|
216 |
+
label="Max New Tokens",
|
217 |
+
elem_id="inference_max_new_tokens"
|
218 |
+
)
|
219 |
+
|
220 |
+
stream_output = gr.Checkbox(
|
221 |
+
label="Stream Output",
|
222 |
+
elem_id="inference_stream_output",
|
223 |
+
value=True
|
224 |
+
)
|
225 |
+
with gr.Column():
|
226 |
+
inference_output = gr.Textbox(
|
227 |
+
lines=12, label="Output Text", elem_id="inference_output")
|
228 |
+
|
229 |
+
reload_selections_button.click(
|
230 |
+
reload_selections,
|
231 |
+
inputs=[lora_model, prompt_template],
|
232 |
+
outputs=[lora_model, prompt_template],
|
233 |
+
)
|
234 |
+
|
235 |
+
prompt_template.change(fn=handle_prompt_template_change, inputs=[prompt_template], outputs=[
|
236 |
+
variable_0, variable_1, variable_2, variable_3, variable_4, variable_5, variable_6, variable_7])
|
237 |
+
|
238 |
+
generate_event = generate_btn.click(
|
239 |
+
fn=inference,
|
240 |
+
inputs=[
|
241 |
+
lora_model,
|
242 |
+
prompt_template,
|
243 |
+
variable_0, variable_1, variable_2, variable_3,
|
244 |
+
variable_4, variable_5, variable_6, variable_7,
|
245 |
+
temperature,
|
246 |
+
top_p,
|
247 |
+
top_k,
|
248 |
+
num_beams,
|
249 |
+
repetition_penalty,
|
250 |
+
max_new_tokens,
|
251 |
+
stream_output,
|
252 |
+
],
|
253 |
+
outputs=inference_output
|
254 |
+
)
|
255 |
+
stop_btn.click(fn=None, inputs=None, outputs=None,
|
256 |
+
cancels=[generate_event])
|
257 |
+
|
258 |
+
inference_ui_blocks.load(_js="""
|
259 |
+
function inference_ui_blocks_js() {
|
260 |
+
// Auto load options
|
261 |
+
setTimeout(function () {
|
262 |
+
document.getElementById("inference_reload_selections_button").click();
|
263 |
+
|
264 |
+
// Workaround default value not shown.
|
265 |
+
document.querySelector('#inference_lora_model input').value = "tloen/alpaca-lora-7b";
|
266 |
+
}, 100);
|
267 |
+
|
268 |
+
// Add tooltips
|
269 |
+
setTimeout(function () {
|
270 |
+
|
271 |
+
tippy("#inference_prompt_template", {
|
272 |
+
placement: 'bottom-start',
|
273 |
+
delay: [500, 0],
|
274 |
+
content: 'Templates are loaded from the "templates" folder of your data directory. Be sure to select the template that matches your selected LoRA model to get the best results.',
|
275 |
+
});
|
276 |
+
|
277 |
+
tippy("#inference_temperature", {
|
278 |
+
placement: 'right',
|
279 |
+
delay: [500, 0],
|
280 |
+
content: 'Controls randomness: Lowering results in less random completions. Higher values (e.g., 1.0) make the model generate more diverse and random outputs. As the temperature approaches zero, the model will become deterministic and repetitive.',
|
281 |
+
});
|
282 |
+
|
283 |
+
tippy("#inference_top_p", {
|
284 |
+
placement: 'right',
|
285 |
+
delay: [500, 0],
|
286 |
+
content: 'Controls diversity via nucleus sampling: only the tokens whose cumulative probability exceeds "top_p" are considered. 0.5 means half of all likelihood-weighted options are considered.',
|
287 |
+
});
|
288 |
+
|
289 |
+
tippy("#inference_top_k", {
|
290 |
+
placement: 'right',
|
291 |
+
delay: [500, 0],
|
292 |
+
content: 'Controls diversity of the generated text by only considering the "top_k" tokens with the highest probabilities. This method can lead to more focused and coherent outputs by reducing the impact of low probability tokens.',
|
293 |
+
});
|
294 |
+
|
295 |
+
tippy("#inference_beams", {
|
296 |
+
placement: 'right',
|
297 |
+
delay: [500, 0],
|
298 |
+
content: 'Number of candidate sequences explored in parallel during text generation using beam search. A higher value increases the chances of finding high-quality, coherent output, but may slow down the generation process.',
|
299 |
+
});
|
300 |
+
|
301 |
+
tippy("#inference_repetition_penalty", {
|
302 |
+
placement: 'right',
|
303 |
+
delay: [500, 0],
|
304 |
+
content: 'Applies a penalty to the probability of tokens that have already been generated, discouraging the model from repeating the same words or phrases. The penalty is applied by dividing the token probability by a factor based on the number of times the token has appeared in the generated text.',
|
305 |
+
});
|
306 |
+
|
307 |
+
tippy("#inference_max_new_tokens", {
|
308 |
+
placement: 'right',
|
309 |
+
delay: [500, 0],
|
310 |
+
content: 'Limits the maximum number of tokens generated in a single iteration.',
|
311 |
+
});
|
312 |
+
|
313 |
+
tippy("#inference_stream_output", {
|
314 |
+
placement: 'right',
|
315 |
+
delay: [500, 0],
|
316 |
+
content: 'When enabled, generated text will be displayed in real-time as it is being produced by the model, allowing you to observe the text generation process as it unfolds.',
|
317 |
+
});
|
318 |
+
|
319 |
+
}, 100);
|
320 |
+
|
321 |
+
// Show/hide generate and save button base on the state.
|
322 |
+
setTimeout(function () {
|
323 |
+
// Make the '#inference_output > .wrap' element appear
|
324 |
+
document.getElementById("inference_stop_btn").click();
|
325 |
+
|
326 |
+
setTimeout(function () {
|
327 |
+
const output_wrap_element = document.querySelector(
|
328 |
+
"#inference_output > .wrap"
|
329 |
+
);
|
330 |
+
function handle_output_wrap_element_class_change() {
|
331 |
+
if (Array.from(output_wrap_element.classList).includes("hide")) {
|
332 |
+
document.getElementById("inference_generate_btn").style.display =
|
333 |
+
"block";
|
334 |
+
document.getElementById("inference_stop_btn").style.display = "none";
|
335 |
+
} else {
|
336 |
+
document.getElementById("inference_generate_btn").style.display =
|
337 |
+
"none";
|
338 |
+
document.getElementById("inference_stop_btn").style.display = "block";
|
339 |
+
}
|
340 |
+
}
|
341 |
+
new MutationObserver(function (mutationsList, observer) {
|
342 |
+
handle_output_wrap_element_class_change();
|
343 |
+
}).observe(output_wrap_element, {
|
344 |
+
attributes: true,
|
345 |
+
attributeFilter: ["class"],
|
346 |
+
});
|
347 |
+
handle_output_wrap_element_class_change();
|
348 |
+
}, 500);
|
349 |
+
}, 0);
|
350 |
+
}
|
351 |
+
""")
|
llama_lora/ui/js_scripts.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def popperjs_core_code():
|
2 |
+
code = """
|
3 |
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Popper={})}(this,(function(e){"use strict";function t(e){if(null==e)return window;if("[object Window]"!==e.toString()){var t=e.ownerDocument;return t&&t.defaultView||window}return e}function n(e){return e instanceof t(e).Element||e instanceof Element}function r(e){return e instanceof t(e).HTMLElement||e instanceof HTMLElement}function o(e){return"undefined"!=typeof ShadowRoot&&(e instanceof t(e).ShadowRoot||e instanceof ShadowRoot)}var i=Math.max,a=Math.min,s=Math.round;function f(){var e=navigator.userAgentData;return null!=e&&e.brands&&Array.isArray(e.brands)?e.brands.map((function(e){return e.brand+"/"+e.version})).join(" "):navigator.userAgent}function c(){return!/^((?!chrome|android).)*safari/i.test(f())}function p(e,o,i){void 0===o&&(o=!1),void 0===i&&(i=!1);var a=e.getBoundingClientRect(),f=1,p=1;o&&r(e)&&(f=e.offsetWidth>0&&s(a.width)/e.offsetWidth||1,p=e.offsetHeight>0&&s(a.height)/e.offsetHeight||1);var u=(n(e)?t(e):window).visualViewport,l=!c()&&i,d=(a.left+(l&&u?u.offsetLeft:0))/f,h=(a.top+(l&&u?u.offsetTop:0))/p,m=a.width/f,v=a.height/p;return{width:m,height:v,top:h,right:d+m,bottom:h+v,left:d,x:d,y:h}}function u(e){var n=t(e);return{scrollLeft:n.pageXOffset,scrollTop:n.pageYOffset}}function l(e){return e?(e.nodeName||"").toLowerCase():null}function d(e){return((n(e)?e.ownerDocument:e.document)||window.document).documentElement}function h(e){return p(d(e)).left+u(e).scrollLeft}function m(e){return t(e).getComputedStyle(e)}function v(e){var t=m(e),n=t.overflow,r=t.overflowX,o=t.overflowY;return/auto|scroll|overlay|hidden/.test(n+o+r)}function y(e,n,o){void 0===o&&(o=!1);var i,a,f=r(n),c=r(n)&&function(e){var t=e.getBoundingClientRect(),n=s(t.width)/e.offsetWidth||1,r=s(t.height)/e.offsetHeight||1;return 1!==n||1!==r}(n),m=d(n),y=p(e,c,o),g={scrollLeft:0,scrollTop:0},b={x:0,y:0};return(f||!f&&!o)&&(("body"!==l(n)||v(m))&&(g=(i=n)!==t(i)&&r(i)?{scrollLeft:(a=i).scrollLeft,scrollTop:a.scrollTop}:u(i)),r(n)?((b=p(n,!0)).x+=n.clientLeft,b.y+=n.clientTop):m&&(b.x=h(m))),{x:y.left+g.scrollLeft-b.x,y:y.top+g.scrollTop-b.y,width:y.width,height:y.height}}function g(e){var t=p(e),n=e.offsetWidth,r=e.offsetHeight;return Math.abs(t.width-n)<=1&&(n=t.width),Math.abs(t.height-r)<=1&&(r=t.height),{x:e.offsetLeft,y:e.offsetTop,width:n,height:r}}function b(e){return"html"===l(e)?e:e.assignedSlot||e.parentNode||(o(e)?e.host:null)||d(e)}function x(e){return["html","body","#document"].indexOf(l(e))>=0?e.ownerDocument.body:r(e)&&v(e)?e:x(b(e))}function w(e,n){var r;void 0===n&&(n=[]);var o=x(e),i=o===(null==(r=e.ownerDocument)?void 0:r.body),a=t(o),s=i?[a].concat(a.visualViewport||[],v(o)?o:[]):o,f=n.concat(s);return i?f:f.concat(w(b(s)))}function O(e){return["table","td","th"].indexOf(l(e))>=0}function j(e){return r(e)&&"fixed"!==m(e).position?e.offsetParent:null}function E(e){for(var n=t(e),i=j(e);i&&O(i)&&"static"===m(i).position;)i=j(i);return i&&("html"===l(i)||"body"===l(i)&&"static"===m(i).position)?n:i||function(e){var t=/firefox/i.test(f());if(/Trident/i.test(f())&&r(e)&&"fixed"===m(e).position)return null;var n=b(e);for(o(n)&&(n=n.host);r(n)&&["html","body"].indexOf(l(n))<0;){var i=m(n);if("none"!==i.transform||"none"!==i.perspective||"paint"===i.contain||-1!==["transform","perspective"].indexOf(i.willChange)||t&&"filter"===i.willChange||t&&i.filter&&"none"!==i.filter)return n;n=n.parentNode}return null}(e)||n}var D="top",A="bottom",L="right",P="left",M="auto",k=[D,A,L,P],W="start",B="end",H="viewport",T="popper",R=k.reduce((function(e,t){return e.concat([t+"-"+W,t+"-"+B])}),[]),S=[].concat(k,[M]).reduce((function(e,t){return e.concat([t,t+"-"+W,t+"-"+B])}),[]),V=["beforeRead","read","afterRead","beforeMain","main","afterMain","beforeWrite","write","afterWrite"];function q(e){var t=new Map,n=new Set,r=[];function o(e){n.add(e.name),[].concat(e.requires||[],e.requiresIfExists||[]).forEach((function(e){if(!n.has(e)){var r=t.get(e);r&&o(r)}})),r.push(e)}return e.forEach((function(e){t.set(e.name,e)})),e.forEach((function(e){n.has(e.name)||o(e)})),r}function C(e){return e.split("-")[0]}function N(e,t){var n=t.getRootNode&&t.getRootNode();if(e.contains(t))return!0;if(n&&o(n)){var r=t;do{if(r&&e.isSameNode(r))return!0;r=r.parentNode||r.host}while(r)}return!1}function I(e){return Object.assign({},e,{left:e.x,top:e.y,right:e.x+e.width,bottom:e.y+e.height})}function _(e,r,o){return r===H?I(function(e,n){var r=t(e),o=d(e),i=r.visualViewport,a=o.clientWidth,s=o.clientHeight,f=0,p=0;if(i){a=i.width,s=i.height;var u=c();(u||!u&&"fixed"===n)&&(f=i.offsetLeft,p=i.offsetTop)}return{width:a,height:s,x:f+h(e),y:p}}(e,o)):n(r)?function(e,t){var n=p(e,!1,"fixed"===t);return n.top=n.top+e.clientTop,n.left=n.left+e.clientLeft,n.bottom=n.top+e.clientHeight,n.right=n.left+e.clientWidth,n.width=e.clientWidth,n.height=e.clientHeight,n.x=n.left,n.y=n.top,n}(r,o):I(function(e){var t,n=d(e),r=u(e),o=null==(t=e.ownerDocument)?void 0:t.body,a=i(n.scrollWidth,n.clientWidth,o?o.scrollWidth:0,o?o.clientWidth:0),s=i(n.scrollHeight,n.clientHeight,o?o.scrollHeight:0,o?o.clientHeight:0),f=-r.scrollLeft+h(e),c=-r.scrollTop;return"rtl"===m(o||n).direction&&(f+=i(n.clientWidth,o?o.clientWidth:0)-a),{width:a,height:s,x:f,y:c}}(d(e)))}function F(e,t,o,s){var f="clippingParents"===t?function(e){var t=w(b(e)),o=["absolute","fixed"].indexOf(m(e).position)>=0&&r(e)?E(e):e;return n(o)?t.filter((function(e){return n(e)&&N(e,o)&&"body"!==l(e)})):[]}(e):[].concat(t),c=[].concat(f,[o]),p=c[0],u=c.reduce((function(t,n){var r=_(e,n,s);return t.top=i(r.top,t.top),t.right=a(r.right,t.right),t.bottom=a(r.bottom,t.bottom),t.left=i(r.left,t.left),t}),_(e,p,s));return u.width=u.right-u.left,u.height=u.bottom-u.top,u.x=u.left,u.y=u.top,u}function U(e){return e.split("-")[1]}function z(e){return["top","bottom"].indexOf(e)>=0?"x":"y"}function X(e){var t,n=e.reference,r=e.element,o=e.placement,i=o?C(o):null,a=o?U(o):null,s=n.x+n.width/2-r.width/2,f=n.y+n.height/2-r.height/2;switch(i){case D:t={x:s,y:n.y-r.height};break;case A:t={x:s,y:n.y+n.height};break;case L:t={x:n.x+n.width,y:f};break;case P:t={x:n.x-r.width,y:f};break;default:t={x:n.x,y:n.y}}var c=i?z(i):null;if(null!=c){var p="y"===c?"height":"width";switch(a){case W:t[c]=t[c]-(n[p]/2-r[p]/2);break;case B:t[c]=t[c]+(n[p]/2-r[p]/2)}}return t}function Y(e){return Object.assign({},{top:0,right:0,bottom:0,left:0},e)}function G(e,t){return t.reduce((function(t,n){return t[n]=e,t}),{})}function J(e,t){void 0===t&&(t={});var r=t,o=r.placement,i=void 0===o?e.placement:o,a=r.strategy,s=void 0===a?e.strategy:a,f=r.boundary,c=void 0===f?"clippingParents":f,u=r.rootBoundary,l=void 0===u?H:u,h=r.elementContext,m=void 0===h?T:h,v=r.altBoundary,y=void 0!==v&&v,g=r.padding,b=void 0===g?0:g,x=Y("number"!=typeof b?b:G(b,k)),w=m===T?"reference":T,O=e.rects.popper,j=e.elements[y?w:m],E=F(n(j)?j:j.contextElement||d(e.elements.popper),c,l,s),P=p(e.elements.reference),M=X({reference:P,element:O,strategy:"absolute",placement:i}),W=I(Object.assign({},O,M)),B=m===T?W:P,R={top:E.top-B.top+x.top,bottom:B.bottom-E.bottom+x.bottom,left:E.left-B.left+x.left,right:B.right-E.right+x.right},S=e.modifiersData.offset;if(m===T&&S){var V=S[i];Object.keys(R).forEach((function(e){var t=[L,A].indexOf(e)>=0?1:-1,n=[D,A].indexOf(e)>=0?"y":"x";R[e]+=V[n]*t}))}return R}var K={placement:"bottom",modifiers:[],strategy:"absolute"};function Q(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];return!t.some((function(e){return!(e&&"function"==typeof e.getBoundingClientRect)}))}function Z(e){void 0===e&&(e={});var t=e,r=t.defaultModifiers,o=void 0===r?[]:r,i=t.defaultOptions,a=void 0===i?K:i;return function(e,t,r){void 0===r&&(r=a);var i,s,f={placement:"bottom",orderedModifiers:[],options:Object.assign({},K,a),modifiersData:{},elements:{reference:e,popper:t},attributes:{},styles:{}},c=[],p=!1,u={state:f,setOptions:function(r){var i="function"==typeof r?r(f.options):r;l(),f.options=Object.assign({},a,f.options,i),f.scrollParents={reference:n(e)?w(e):e.contextElement?w(e.contextElement):[],popper:w(t)};var s,p,d=function(e){var t=q(e);return V.reduce((function(e,n){return e.concat(t.filter((function(e){return e.phase===n})))}),[])}((s=[].concat(o,f.options.modifiers),p=s.reduce((function(e,t){var n=e[t.name];return e[t.name]=n?Object.assign({},n,t,{options:Object.assign({},n.options,t.options),data:Object.assign({},n.data,t.data)}):t,e}),{}),Object.keys(p).map((function(e){return p[e]}))));return f.orderedModifiers=d.filter((function(e){return e.enabled})),f.orderedModifiers.forEach((function(e){var t=e.name,n=e.options,r=void 0===n?{}:n,o=e.effect;if("function"==typeof o){var i=o({state:f,name:t,instance:u,options:r}),a=function(){};c.push(i||a)}})),u.update()},forceUpdate:function(){if(!p){var e=f.elements,t=e.reference,n=e.popper;if(Q(t,n)){f.rects={reference:y(t,E(n),"fixed"===f.options.strategy),popper:g(n)},f.reset=!1,f.placement=f.options.placement,f.orderedModifiers.forEach((function(e){return f.modifiersData[e.name]=Object.assign({},e.data)}));for(var r=0;r<f.orderedModifiers.length;r++)if(!0!==f.reset){var o=f.orderedModifiers[r],i=o.fn,a=o.options,s=void 0===a?{}:a,c=o.name;"function"==typeof i&&(f=i({state:f,options:s,name:c,instance:u})||f)}else f.reset=!1,r=-1}}},update:(i=function(){return new Promise((function(e){u.forceUpdate(),e(f)}))},function(){return s||(s=new Promise((function(e){Promise.resolve().then((function(){s=void 0,e(i())}))}))),s}),destroy:function(){l(),p=!0}};if(!Q(e,t))return u;function l(){c.forEach((function(e){return e()})),c=[]}return u.setOptions(r).then((function(e){!p&&r.onFirstUpdate&&r.onFirstUpdate(e)})),u}}var $={passive:!0};var ee={name:"eventListeners",enabled:!0,phase:"write",fn:function(){},effect:function(e){var n=e.state,r=e.instance,o=e.options,i=o.scroll,a=void 0===i||i,s=o.resize,f=void 0===s||s,c=t(n.elements.popper),p=[].concat(n.scrollParents.reference,n.scrollParents.popper);return a&&p.forEach((function(e){e.addEventListener("scroll",r.update,$)})),f&&c.addEventListener("resize",r.update,$),function(){a&&p.forEach((function(e){e.removeEventListener("scroll",r.update,$)})),f&&c.removeEventListener("resize",r.update,$)}},data:{}};var te={name:"popperOffsets",enabled:!0,phase:"read",fn:function(e){var t=e.state,n=e.name;t.modifiersData[n]=X({reference:t.rects.reference,element:t.rects.popper,strategy:"absolute",placement:t.placement})},data:{}},ne={top:"auto",right:"auto",bottom:"auto",left:"auto"};function re(e){var n,r=e.popper,o=e.popperRect,i=e.placement,a=e.variation,f=e.offsets,c=e.position,p=e.gpuAcceleration,u=e.adaptive,l=e.roundOffsets,h=e.isFixed,v=f.x,y=void 0===v?0:v,g=f.y,b=void 0===g?0:g,x="function"==typeof l?l({x:y,y:b}):{x:y,y:b};y=x.x,b=x.y;var w=f.hasOwnProperty("x"),O=f.hasOwnProperty("y"),j=P,M=D,k=window;if(u){var W=E(r),H="clientHeight",T="clientWidth";if(W===t(r)&&"static"!==m(W=d(r)).position&&"absolute"===c&&(H="scrollHeight",T="scrollWidth"),W=W,i===D||(i===P||i===L)&&a===B)M=A,b-=(h&&W===k&&k.visualViewport?k.visualViewport.height:W[H])-o.height,b*=p?1:-1;if(i===P||(i===D||i===A)&&a===B)j=L,y-=(h&&W===k&&k.visualViewport?k.visualViewport.width:W[T])-o.width,y*=p?1:-1}var R,S=Object.assign({position:c},u&&ne),V=!0===l?function(e,t){var n=e.x,r=e.y,o=t.devicePixelRatio||1;return{x:s(n*o)/o||0,y:s(r*o)/o||0}}({x:y,y:b},t(r)):{x:y,y:b};return y=V.x,b=V.y,p?Object.assign({},S,((R={})[M]=O?"0":"",R[j]=w?"0":"",R.transform=(k.devicePixelRatio||1)<=1?"translate("+y+"px, "+b+"px)":"translate3d("+y+"px, "+b+"px, 0)",R)):Object.assign({},S,((n={})[M]=O?b+"px":"",n[j]=w?y+"px":"",n.transform="",n))}var oe={name:"computeStyles",enabled:!0,phase:"beforeWrite",fn:function(e){var t=e.state,n=e.options,r=n.gpuAcceleration,o=void 0===r||r,i=n.adaptive,a=void 0===i||i,s=n.roundOffsets,f=void 0===s||s,c={placement:C(t.placement),variation:U(t.placement),popper:t.elements.popper,popperRect:t.rects.popper,gpuAcceleration:o,isFixed:"fixed"===t.options.strategy};null!=t.modifiersData.popperOffsets&&(t.styles.popper=Object.assign({},t.styles.popper,re(Object.assign({},c,{offsets:t.modifiersData.popperOffsets,position:t.options.strategy,adaptive:a,roundOffsets:f})))),null!=t.modifiersData.arrow&&(t.styles.arrow=Object.assign({},t.styles.arrow,re(Object.assign({},c,{offsets:t.modifiersData.arrow,position:"absolute",adaptive:!1,roundOffsets:f})))),t.attributes.popper=Object.assign({},t.attributes.popper,{"data-popper-placement":t.placement})},data:{}};var ie={name:"applyStyles",enabled:!0,phase:"write",fn:function(e){var t=e.state;Object.keys(t.elements).forEach((function(e){var n=t.styles[e]||{},o=t.attributes[e]||{},i=t.elements[e];r(i)&&l(i)&&(Object.assign(i.style,n),Object.keys(o).forEach((function(e){var t=o[e];!1===t?i.removeAttribute(e):i.setAttribute(e,!0===t?"":t)})))}))},effect:function(e){var t=e.state,n={popper:{position:t.options.strategy,left:"0",top:"0",margin:"0"},arrow:{position:"absolute"},reference:{}};return Object.assign(t.elements.popper.style,n.popper),t.styles=n,t.elements.arrow&&Object.assign(t.elements.arrow.style,n.arrow),function(){Object.keys(t.elements).forEach((function(e){var o=t.elements[e],i=t.attributes[e]||{},a=Object.keys(t.styles.hasOwnProperty(e)?t.styles[e]:n[e]).reduce((function(e,t){return e[t]="",e}),{});r(o)&&l(o)&&(Object.assign(o.style,a),Object.keys(i).forEach((function(e){o.removeAttribute(e)})))}))}},requires:["computeStyles"]};var ae={name:"offset",enabled:!0,phase:"main",requires:["popperOffsets"],fn:function(e){var t=e.state,n=e.options,r=e.name,o=n.offset,i=void 0===o?[0,0]:o,a=S.reduce((function(e,n){return e[n]=function(e,t,n){var r=C(e),o=[P,D].indexOf(r)>=0?-1:1,i="function"==typeof n?n(Object.assign({},t,{placement:e})):n,a=i[0],s=i[1];return a=a||0,s=(s||0)*o,[P,L].indexOf(r)>=0?{x:s,y:a}:{x:a,y:s}}(n,t.rects,i),e}),{}),s=a[t.placement],f=s.x,c=s.y;null!=t.modifiersData.popperOffsets&&(t.modifiersData.popperOffsets.x+=f,t.modifiersData.popperOffsets.y+=c),t.modifiersData[r]=a}},se={left:"right",right:"left",bottom:"top",top:"bottom"};function fe(e){return e.replace(/left|right|bottom|top/g,(function(e){return se[e]}))}var ce={start:"end",end:"start"};function pe(e){return e.replace(/start|end/g,(function(e){return ce[e]}))}function ue(e,t){void 0===t&&(t={});var n=t,r=n.placement,o=n.boundary,i=n.rootBoundary,a=n.padding,s=n.flipVariations,f=n.allowedAutoPlacements,c=void 0===f?S:f,p=U(r),u=p?s?R:R.filter((function(e){return U(e)===p})):k,l=u.filter((function(e){return c.indexOf(e)>=0}));0===l.length&&(l=u);var d=l.reduce((function(t,n){return t[n]=J(e,{placement:n,boundary:o,rootBoundary:i,padding:a})[C(n)],t}),{});return Object.keys(d).sort((function(e,t){return d[e]-d[t]}))}var le={name:"flip",enabled:!0,phase:"main",fn:function(e){var t=e.state,n=e.options,r=e.name;if(!t.modifiersData[r]._skip){for(var o=n.mainAxis,i=void 0===o||o,a=n.altAxis,s=void 0===a||a,f=n.fallbackPlacements,c=n.padding,p=n.boundary,u=n.rootBoundary,l=n.altBoundary,d=n.flipVariations,h=void 0===d||d,m=n.allowedAutoPlacements,v=t.options.placement,y=C(v),g=f||(y===v||!h?[fe(v)]:function(e){if(C(e)===M)return[];var t=fe(e);return[pe(e),t,pe(t)]}(v)),b=[v].concat(g).reduce((function(e,n){return e.concat(C(n)===M?ue(t,{placement:n,boundary:p,rootBoundary:u,padding:c,flipVariations:h,allowedAutoPlacements:m}):n)}),[]),x=t.rects.reference,w=t.rects.popper,O=new Map,j=!0,E=b[0],k=0;k<b.length;k++){var B=b[k],H=C(B),T=U(B)===W,R=[D,A].indexOf(H)>=0,S=R?"width":"height",V=J(t,{placement:B,boundary:p,rootBoundary:u,altBoundary:l,padding:c}),q=R?T?L:P:T?A:D;x[S]>w[S]&&(q=fe(q));var N=fe(q),I=[];if(i&&I.push(V[H]<=0),s&&I.push(V[q]<=0,V[N]<=0),I.every((function(e){return e}))){E=B,j=!1;break}O.set(B,I)}if(j)for(var _=function(e){var t=b.find((function(t){var n=O.get(t);if(n)return n.slice(0,e).every((function(e){return e}))}));if(t)return E=t,"break"},F=h?3:1;F>0;F--){if("break"===_(F))break}t.placement!==E&&(t.modifiersData[r]._skip=!0,t.placement=E,t.reset=!0)}},requiresIfExists:["offset"],data:{_skip:!1}};function de(e,t,n){return i(e,a(t,n))}var he={name:"preventOverflow",enabled:!0,phase:"main",fn:function(e){var t=e.state,n=e.options,r=e.name,o=n.mainAxis,s=void 0===o||o,f=n.altAxis,c=void 0!==f&&f,p=n.boundary,u=n.rootBoundary,l=n.altBoundary,d=n.padding,h=n.tether,m=void 0===h||h,v=n.tetherOffset,y=void 0===v?0:v,b=J(t,{boundary:p,rootBoundary:u,padding:d,altBoundary:l}),x=C(t.placement),w=U(t.placement),O=!w,j=z(x),M="x"===j?"y":"x",k=t.modifiersData.popperOffsets,B=t.rects.reference,H=t.rects.popper,T="function"==typeof y?y(Object.assign({},t.rects,{placement:t.placement})):y,R="number"==typeof T?{mainAxis:T,altAxis:T}:Object.assign({mainAxis:0,altAxis:0},T),S=t.modifiersData.offset?t.modifiersData.offset[t.placement]:null,V={x:0,y:0};if(k){if(s){var q,N="y"===j?D:P,I="y"===j?A:L,_="y"===j?"height":"width",F=k[j],X=F+b[N],Y=F-b[I],G=m?-H[_]/2:0,K=w===W?B[_]:H[_],Q=w===W?-H[_]:-B[_],Z=t.elements.arrow,$=m&&Z?g(Z):{width:0,height:0},ee=t.modifiersData["arrow#persistent"]?t.modifiersData["arrow#persistent"].padding:{top:0,right:0,bottom:0,left:0},te=ee[N],ne=ee[I],re=de(0,B[_],$[_]),oe=O?B[_]/2-G-re-te-R.mainAxis:K-re-te-R.mainAxis,ie=O?-B[_]/2+G+re+ne+R.mainAxis:Q+re+ne+R.mainAxis,ae=t.elements.arrow&&E(t.elements.arrow),se=ae?"y"===j?ae.clientTop||0:ae.clientLeft||0:0,fe=null!=(q=null==S?void 0:S[j])?q:0,ce=F+ie-fe,pe=de(m?a(X,F+oe-fe-se):X,F,m?i(Y,ce):Y);k[j]=pe,V[j]=pe-F}if(c){var ue,le="x"===j?D:P,he="x"===j?A:L,me=k[M],ve="y"===M?"height":"width",ye=me+b[le],ge=me-b[he],be=-1!==[D,P].indexOf(x),xe=null!=(ue=null==S?void 0:S[M])?ue:0,we=be?ye:me-B[ve]-H[ve]-xe+R.altAxis,Oe=be?me+B[ve]+H[ve]-xe-R.altAxis:ge,je=m&&be?function(e,t,n){var r=de(e,t,n);return r>n?n:r}(we,me,Oe):de(m?we:ye,me,m?Oe:ge);k[M]=je,V[M]=je-me}t.modifiersData[r]=V}},requiresIfExists:["offset"]};var me={name:"arrow",enabled:!0,phase:"main",fn:function(e){var t,n=e.state,r=e.name,o=e.options,i=n.elements.arrow,a=n.modifiersData.popperOffsets,s=C(n.placement),f=z(s),c=[P,L].indexOf(s)>=0?"height":"width";if(i&&a){var p=function(e,t){return Y("number"!=typeof(e="function"==typeof e?e(Object.assign({},t.rects,{placement:t.placement})):e)?e:G(e,k))}(o.padding,n),u=g(i),l="y"===f?D:P,d="y"===f?A:L,h=n.rects.reference[c]+n.rects.reference[f]-a[f]-n.rects.popper[c],m=a[f]-n.rects.reference[f],v=E(i),y=v?"y"===f?v.clientHeight||0:v.clientWidth||0:0,b=h/2-m/2,x=p[l],w=y-u[c]-p[d],O=y/2-u[c]/2+b,j=de(x,O,w),M=f;n.modifiersData[r]=((t={})[M]=j,t.centerOffset=j-O,t)}},effect:function(e){var t=e.state,n=e.options.element,r=void 0===n?"[data-popper-arrow]":n;null!=r&&("string"!=typeof r||(r=t.elements.popper.querySelector(r)))&&N(t.elements.popper,r)&&(t.elements.arrow=r)},requires:["popperOffsets"],requiresIfExists:["preventOverflow"]};function ve(e,t,n){return void 0===n&&(n={x:0,y:0}),{top:e.top-t.height-n.y,right:e.right-t.width+n.x,bottom:e.bottom-t.height+n.y,left:e.left-t.width-n.x}}function ye(e){return[D,L,A,P].some((function(t){return e[t]>=0}))}var ge={name:"hide",enabled:!0,phase:"main",requiresIfExists:["preventOverflow"],fn:function(e){var t=e.state,n=e.name,r=t.rects.reference,o=t.rects.popper,i=t.modifiersData.preventOverflow,a=J(t,{elementContext:"reference"}),s=J(t,{altBoundary:!0}),f=ve(a,r),c=ve(s,o,i),p=ye(f),u=ye(c);t.modifiersData[n]={referenceClippingOffsets:f,popperEscapeOffsets:c,isReferenceHidden:p,hasPopperEscaped:u},t.attributes.popper=Object.assign({},t.attributes.popper,{"data-popper-reference-hidden":p,"data-popper-escaped":u})}},be=Z({defaultModifiers:[ee,te,oe,ie]}),xe=[ee,te,oe,ie,ae,le,he,me,ge],we=Z({defaultModifiers:xe});e.applyStyles=ie,e.arrow=me,e.computeStyles=oe,e.createPopper=we,e.createPopperLite=be,e.defaultModifiers=xe,e.detectOverflow=J,e.eventListeners=ee,e.flip=le,e.hide=ge,e.offset=ae,e.popperGenerator=Z,e.popperOffsets=te,e.preventOverflow=he,Object.defineProperty(e,"__esModule",{value:!0})}));
|
4 |
+
"""
|
5 |
+
return code
|
6 |
+
|
7 |
+
|
8 |
+
def tippy_js_code():
|
9 |
+
code = """
|
10 |
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("@popperjs/core")):"function"==typeof define&&define.amd?define(["@popperjs/core"],e):(t=t||self).tippy=e(t.Popper)}(this,(function(t){"use strict";var e="undefined"!=typeof window&&"undefined"!=typeof document,n=!!e&&!!window.msCrypto,r={passive:!0,capture:!0},o=function(){return document.body};function i(t,e,n){if(Array.isArray(t)){var r=t[e];return null==r?Array.isArray(n)?n[e]:n:r}return t}function a(t,e){var n={}.toString.call(t);return 0===n.indexOf("[object")&&n.indexOf(e+"]")>-1}function s(t,e){return"function"==typeof t?t.apply(void 0,e):t}function u(t,e){return 0===e?t:function(r){clearTimeout(n),n=setTimeout((function(){t(r)}),e)};var n}function p(t,e){var n=Object.assign({},t);return e.forEach((function(t){delete n[t]})),n}function c(t){return[].concat(t)}function f(t,e){-1===t.indexOf(e)&&t.push(e)}function l(t){return t.split("-")[0]}function d(t){return[].slice.call(t)}function v(t){return Object.keys(t).reduce((function(e,n){return void 0!==t[n]&&(e[n]=t[n]),e}),{})}function m(){return document.createElement("div")}function g(t){return["Element","Fragment"].some((function(e){return a(t,e)}))}function h(t){return a(t,"MouseEvent")}function b(t){return!(!t||!t._tippy||t._tippy.reference!==t)}function y(t){return g(t)?[t]:function(t){return a(t,"NodeList")}(t)?d(t):Array.isArray(t)?t:d(document.querySelectorAll(t))}function w(t,e){t.forEach((function(t){t&&(t.style.transitionDuration=e+"ms")}))}function x(t,e){t.forEach((function(t){t&&t.setAttribute("data-state",e)}))}function E(t){var e,n=c(t)[0];return null!=n&&null!=(e=n.ownerDocument)&&e.body?n.ownerDocument:document}function O(t,e,n){var r=e+"EventListener";["transitionend","webkitTransitionEnd"].forEach((function(e){t[r](e,n)}))}function C(t,e){for(var n=e;n;){var r;if(t.contains(n))return!0;n=null==n.getRootNode||null==(r=n.getRootNode())?void 0:r.host}return!1}var T={isTouch:!1},A=0;function L(){T.isTouch||(T.isTouch=!0,window.performance&&document.addEventListener("mousemove",D))}function D(){var t=performance.now();t-A<20&&(T.isTouch=!1,document.removeEventListener("mousemove",D)),A=t}function k(){var t=document.activeElement;if(b(t)){var e=t._tippy;t.blur&&!e.state.isVisible&&t.blur()}}var R=Object.assign({appendTo:o,aria:{content:"auto",expanded:"auto"},delay:0,duration:[300,250],getReferenceClientRect:null,hideOnClick:!0,ignoreAttributes:!1,interactive:!1,interactiveBorder:2,interactiveDebounce:0,moveTransition:"",offset:[0,10],onAfterUpdate:function(){},onBeforeUpdate:function(){},onCreate:function(){},onDestroy:function(){},onHidden:function(){},onHide:function(){},onMount:function(){},onShow:function(){},onShown:function(){},onTrigger:function(){},onUntrigger:function(){},onClickOutside:function(){},placement:"top",plugins:[],popperOptions:{},render:null,showOnCreate:!1,touch:!0,trigger:"mouseenter focus",triggerTarget:null},{animateFill:!1,followCursor:!1,inlinePositioning:!1,sticky:!1},{allowHTML:!1,animation:"fade",arrow:!0,content:"",inertia:!1,maxWidth:350,role:"tooltip",theme:"",zIndex:9999}),P=Object.keys(R);function j(t){var e=(t.plugins||[]).reduce((function(e,n){var r,o=n.name,i=n.defaultValue;o&&(e[o]=void 0!==t[o]?t[o]:null!=(r=R[o])?r:i);return e}),{});return Object.assign({},t,e)}function M(t,e){var n=Object.assign({},e,{content:s(e.content,[t])},e.ignoreAttributes?{}:function(t,e){return(e?Object.keys(j(Object.assign({},R,{plugins:e}))):P).reduce((function(e,n){var r=(t.getAttribute("data-tippy-"+n)||"").trim();if(!r)return e;if("content"===n)e[n]=r;else try{e[n]=JSON.parse(r)}catch(t){e[n]=r}return e}),{})}(t,e.plugins));return n.aria=Object.assign({},R.aria,n.aria),n.aria={expanded:"auto"===n.aria.expanded?e.interactive:n.aria.expanded,content:"auto"===n.aria.content?e.interactive?null:"describedby":n.aria.content},n}function V(t,e){t.innerHTML=e}function I(t){var e=m();return!0===t?e.className="tippy-arrow":(e.className="tippy-svg-arrow",g(t)?e.appendChild(t):V(e,t)),e}function S(t,e){g(e.content)?(V(t,""),t.appendChild(e.content)):"function"!=typeof e.content&&(e.allowHTML?V(t,e.content):t.textContent=e.content)}function B(t){var e=t.firstElementChild,n=d(e.children);return{box:e,content:n.find((function(t){return t.classList.contains("tippy-content")})),arrow:n.find((function(t){return t.classList.contains("tippy-arrow")||t.classList.contains("tippy-svg-arrow")})),backdrop:n.find((function(t){return t.classList.contains("tippy-backdrop")}))}}function N(t){var e=m(),n=m();n.className="tippy-box",n.setAttribute("data-state","hidden"),n.setAttribute("tabindex","-1");var r=m();function o(n,r){var o=B(e),i=o.box,a=o.content,s=o.arrow;r.theme?i.setAttribute("data-theme",r.theme):i.removeAttribute("data-theme"),"string"==typeof r.animation?i.setAttribute("data-animation",r.animation):i.removeAttribute("data-animation"),r.inertia?i.setAttribute("data-inertia",""):i.removeAttribute("data-inertia"),i.style.maxWidth="number"==typeof r.maxWidth?r.maxWidth+"px":r.maxWidth,r.role?i.setAttribute("role",r.role):i.removeAttribute("role"),n.content===r.content&&n.allowHTML===r.allowHTML||S(a,t.props),r.arrow?s?n.arrow!==r.arrow&&(i.removeChild(s),i.appendChild(I(r.arrow))):i.appendChild(I(r.arrow)):s&&i.removeChild(s)}return r.className="tippy-content",r.setAttribute("data-state","hidden"),S(r,t.props),e.appendChild(n),n.appendChild(r),o(t.props,t.props),{popper:e,onUpdate:o}}N.$$tippy=!0;var H=1,U=[],_=[];function z(e,a){var p,g,b,y,A,L,D,k,P=M(e,Object.assign({},R,j(v(a)))),V=!1,I=!1,S=!1,N=!1,z=[],F=u(wt,P.interactiveDebounce),W=H++,X=(k=P.plugins).filter((function(t,e){return k.indexOf(t)===e})),Y={id:W,reference:e,popper:m(),popperInstance:null,props:P,state:{isEnabled:!0,isVisible:!1,isDestroyed:!1,isMounted:!1,isShown:!1},plugins:X,clearDelayTimeouts:function(){clearTimeout(p),clearTimeout(g),cancelAnimationFrame(b)},setProps:function(t){if(Y.state.isDestroyed)return;at("onBeforeUpdate",[Y,t]),bt();var n=Y.props,r=M(e,Object.assign({},n,v(t),{ignoreAttributes:!0}));Y.props=r,ht(),n.interactiveDebounce!==r.interactiveDebounce&&(pt(),F=u(wt,r.interactiveDebounce));n.triggerTarget&&!r.triggerTarget?c(n.triggerTarget).forEach((function(t){t.removeAttribute("aria-expanded")})):r.triggerTarget&&e.removeAttribute("aria-expanded");ut(),it(),J&&J(n,r);Y.popperInstance&&(Ct(),At().forEach((function(t){requestAnimationFrame(t._tippy.popperInstance.forceUpdate)})));at("onAfterUpdate",[Y,t])},setContent:function(t){Y.setProps({content:t})},show:function(){var t=Y.state.isVisible,e=Y.state.isDestroyed,n=!Y.state.isEnabled,r=T.isTouch&&!Y.props.touch,a=i(Y.props.duration,0,R.duration);if(t||e||n||r)return;if(et().hasAttribute("disabled"))return;if(at("onShow",[Y],!1),!1===Y.props.onShow(Y))return;Y.state.isVisible=!0,tt()&&($.style.visibility="visible");it(),dt(),Y.state.isMounted||($.style.transition="none");if(tt()){var u=rt(),p=u.box,c=u.content;w([p,c],0)}L=function(){var t;if(Y.state.isVisible&&!N){if(N=!0,$.offsetHeight,$.style.transition=Y.props.moveTransition,tt()&&Y.props.animation){var e=rt(),n=e.box,r=e.content;w([n,r],a),x([n,r],"visible")}st(),ut(),f(_,Y),null==(t=Y.popperInstance)||t.forceUpdate(),at("onMount",[Y]),Y.props.animation&&tt()&&function(t,e){mt(t,e)}(a,(function(){Y.state.isShown=!0,at("onShown",[Y])}))}},function(){var t,e=Y.props.appendTo,n=et();t=Y.props.interactive&&e===o||"parent"===e?n.parentNode:s(e,[n]);t.contains($)||t.appendChild($);Y.state.isMounted=!0,Ct()}()},hide:function(){var t=!Y.state.isVisible,e=Y.state.isDestroyed,n=!Y.state.isEnabled,r=i(Y.props.duration,1,R.duration);if(t||e||n)return;if(at("onHide",[Y],!1),!1===Y.props.onHide(Y))return;Y.state.isVisible=!1,Y.state.isShown=!1,N=!1,V=!1,tt()&&($.style.visibility="hidden");if(pt(),vt(),it(!0),tt()){var o=rt(),a=o.box,s=o.content;Y.props.animation&&(w([a,s],r),x([a,s],"hidden"))}st(),ut(),Y.props.animation?tt()&&function(t,e){mt(t,(function(){!Y.state.isVisible&&$.parentNode&&$.parentNode.contains($)&&e()}))}(r,Y.unmount):Y.unmount()},hideWithInteractivity:function(t){nt().addEventListener("mousemove",F),f(U,F),F(t)},enable:function(){Y.state.isEnabled=!0},disable:function(){Y.hide(),Y.state.isEnabled=!1},unmount:function(){Y.state.isVisible&&Y.hide();if(!Y.state.isMounted)return;Tt(),At().forEach((function(t){t._tippy.unmount()})),$.parentNode&&$.parentNode.removeChild($);_=_.filter((function(t){return t!==Y})),Y.state.isMounted=!1,at("onHidden",[Y])},destroy:function(){if(Y.state.isDestroyed)return;Y.clearDelayTimeouts(),Y.unmount(),bt(),delete e._tippy,Y.state.isDestroyed=!0,at("onDestroy",[Y])}};if(!P.render)return Y;var q=P.render(Y),$=q.popper,J=q.onUpdate;$.setAttribute("data-tippy-root",""),$.id="tippy-"+Y.id,Y.popper=$,e._tippy=Y,$._tippy=Y;var G=X.map((function(t){return t.fn(Y)})),K=e.hasAttribute("aria-expanded");return ht(),ut(),it(),at("onCreate",[Y]),P.showOnCreate&&Lt(),$.addEventListener("mouseenter",(function(){Y.props.interactive&&Y.state.isVisible&&Y.clearDelayTimeouts()})),$.addEventListener("mouseleave",(function(){Y.props.interactive&&Y.props.trigger.indexOf("mouseenter")>=0&&nt().addEventListener("mousemove",F)})),Y;function Q(){var t=Y.props.touch;return Array.isArray(t)?t:[t,0]}function Z(){return"hold"===Q()[0]}function tt(){var t;return!(null==(t=Y.props.render)||!t.$$tippy)}function et(){return D||e}function nt(){var t=et().parentNode;return t?E(t):document}function rt(){return B($)}function ot(t){return Y.state.isMounted&&!Y.state.isVisible||T.isTouch||y&&"focus"===y.type?0:i(Y.props.delay,t?0:1,R.delay)}function it(t){void 0===t&&(t=!1),$.style.pointerEvents=Y.props.interactive&&!t?"":"none",$.style.zIndex=""+Y.props.zIndex}function at(t,e,n){var r;(void 0===n&&(n=!0),G.forEach((function(n){n[t]&&n[t].apply(n,e)})),n)&&(r=Y.props)[t].apply(r,e)}function st(){var t=Y.props.aria;if(t.content){var n="aria-"+t.content,r=$.id;c(Y.props.triggerTarget||e).forEach((function(t){var e=t.getAttribute(n);if(Y.state.isVisible)t.setAttribute(n,e?e+" "+r:r);else{var o=e&&e.replace(r,"").trim();o?t.setAttribute(n,o):t.removeAttribute(n)}}))}}function ut(){!K&&Y.props.aria.expanded&&c(Y.props.triggerTarget||e).forEach((function(t){Y.props.interactive?t.setAttribute("aria-expanded",Y.state.isVisible&&t===et()?"true":"false"):t.removeAttribute("aria-expanded")}))}function pt(){nt().removeEventListener("mousemove",F),U=U.filter((function(t){return t!==F}))}function ct(t){if(!T.isTouch||!S&&"mousedown"!==t.type){var n=t.composedPath&&t.composedPath()[0]||t.target;if(!Y.props.interactive||!C($,n)){if(c(Y.props.triggerTarget||e).some((function(t){return C(t,n)}))){if(T.isTouch)return;if(Y.state.isVisible&&Y.props.trigger.indexOf("click")>=0)return}else at("onClickOutside",[Y,t]);!0===Y.props.hideOnClick&&(Y.clearDelayTimeouts(),Y.hide(),I=!0,setTimeout((function(){I=!1})),Y.state.isMounted||vt())}}}function ft(){S=!0}function lt(){S=!1}function dt(){var t=nt();t.addEventListener("mousedown",ct,!0),t.addEventListener("touchend",ct,r),t.addEventListener("touchstart",lt,r),t.addEventListener("touchmove",ft,r)}function vt(){var t=nt();t.removeEventListener("mousedown",ct,!0),t.removeEventListener("touchend",ct,r),t.removeEventListener("touchstart",lt,r),t.removeEventListener("touchmove",ft,r)}function mt(t,e){var n=rt().box;function r(t){t.target===n&&(O(n,"remove",r),e())}if(0===t)return e();O(n,"remove",A),O(n,"add",r),A=r}function gt(t,n,r){void 0===r&&(r=!1),c(Y.props.triggerTarget||e).forEach((function(e){e.addEventListener(t,n,r),z.push({node:e,eventType:t,handler:n,options:r})}))}function ht(){var t;Z()&&(gt("touchstart",yt,{passive:!0}),gt("touchend",xt,{passive:!0})),(t=Y.props.trigger,t.split(/\s+/).filter(Boolean)).forEach((function(t){if("manual"!==t)switch(gt(t,yt),t){case"mouseenter":gt("mouseleave",xt);break;case"focus":gt(n?"focusout":"blur",Et);break;case"focusin":gt("focusout",Et)}}))}function bt(){z.forEach((function(t){var e=t.node,n=t.eventType,r=t.handler,o=t.options;e.removeEventListener(n,r,o)})),z=[]}function yt(t){var e,n=!1;if(Y.state.isEnabled&&!Ot(t)&&!I){var r="focus"===(null==(e=y)?void 0:e.type);y=t,D=t.currentTarget,ut(),!Y.state.isVisible&&h(t)&&U.forEach((function(e){return e(t)})),"click"===t.type&&(Y.props.trigger.indexOf("mouseenter")<0||V)&&!1!==Y.props.hideOnClick&&Y.state.isVisible?n=!0:Lt(t),"click"===t.type&&(V=!n),n&&!r&&Dt(t)}}function wt(t){var e=t.target,n=et().contains(e)||$.contains(e);"mousemove"===t.type&&n||function(t,e){var n=e.clientX,r=e.clientY;return t.every((function(t){var e=t.popperRect,o=t.popperState,i=t.props.interactiveBorder,a=l(o.placement),s=o.modifiersData.offset;if(!s)return!0;var u="bottom"===a?s.top.y:0,p="top"===a?s.bottom.y:0,c="right"===a?s.left.x:0,f="left"===a?s.right.x:0,d=e.top-r+u>i,v=r-e.bottom-p>i,m=e.left-n+c>i,g=n-e.right-f>i;return d||v||m||g}))}(At().concat($).map((function(t){var e,n=null==(e=t._tippy.popperInstance)?void 0:e.state;return n?{popperRect:t.getBoundingClientRect(),popperState:n,props:P}:null})).filter(Boolean),t)&&(pt(),Dt(t))}function xt(t){Ot(t)||Y.props.trigger.indexOf("click")>=0&&V||(Y.props.interactive?Y.hideWithInteractivity(t):Dt(t))}function Et(t){Y.props.trigger.indexOf("focusin")<0&&t.target!==et()||Y.props.interactive&&t.relatedTarget&&$.contains(t.relatedTarget)||Dt(t)}function Ot(t){return!!T.isTouch&&Z()!==t.type.indexOf("touch")>=0}function Ct(){Tt();var n=Y.props,r=n.popperOptions,o=n.placement,i=n.offset,a=n.getReferenceClientRect,s=n.moveTransition,u=tt()?B($).arrow:null,p=a?{getBoundingClientRect:a,contextElement:a.contextElement||et()}:e,c=[{name:"offset",options:{offset:i}},{name:"preventOverflow",options:{padding:{top:2,bottom:2,left:5,right:5}}},{name:"flip",options:{padding:5}},{name:"computeStyles",options:{adaptive:!s}},{name:"$$tippy",enabled:!0,phase:"beforeWrite",requires:["computeStyles"],fn:function(t){var e=t.state;if(tt()){var n=rt().box;["placement","reference-hidden","escaped"].forEach((function(t){"placement"===t?n.setAttribute("data-placement",e.placement):e.attributes.popper["data-popper-"+t]?n.setAttribute("data-"+t,""):n.removeAttribute("data-"+t)})),e.attributes.popper={}}}}];tt()&&u&&c.push({name:"arrow",options:{element:u,padding:3}}),c.push.apply(c,(null==r?void 0:r.modifiers)||[]),Y.popperInstance=t.createPopper(p,$,Object.assign({},r,{placement:o,onFirstUpdate:L,modifiers:c}))}function Tt(){Y.popperInstance&&(Y.popperInstance.destroy(),Y.popperInstance=null)}function At(){return d($.querySelectorAll("[data-tippy-root]"))}function Lt(t){Y.clearDelayTimeouts(),t&&at("onTrigger",[Y,t]),dt();var e=ot(!0),n=Q(),r=n[0],o=n[1];T.isTouch&&"hold"===r&&o&&(e=o),e?p=setTimeout((function(){Y.show()}),e):Y.show()}function Dt(t){if(Y.clearDelayTimeouts(),at("onUntrigger",[Y,t]),Y.state.isVisible){if(!(Y.props.trigger.indexOf("mouseenter")>=0&&Y.props.trigger.indexOf("click")>=0&&["mouseleave","mousemove"].indexOf(t.type)>=0&&V)){var e=ot(!1);e?g=setTimeout((function(){Y.state.isVisible&&Y.hide()}),e):b=requestAnimationFrame((function(){Y.hide()}))}}else vt()}}function F(t,e){void 0===e&&(e={});var n=R.plugins.concat(e.plugins||[]);document.addEventListener("touchstart",L,r),window.addEventListener("blur",k);var o=Object.assign({},e,{plugins:n}),i=y(t).reduce((function(t,e){var n=e&&z(e,o);return n&&t.push(n),t}),[]);return g(t)?i[0]:i}F.defaultProps=R,F.setDefaultProps=function(t){Object.keys(t).forEach((function(e){R[e]=t[e]}))},F.currentInput=T;var W=Object.assign({},t.applyStyles,{effect:function(t){var e=t.state,n={popper:{position:e.options.strategy,left:"0",top:"0",margin:"0"},arrow:{position:"absolute"},reference:{}};Object.assign(e.elements.popper.style,n.popper),e.styles=n,e.elements.arrow&&Object.assign(e.elements.arrow.style,n.arrow)}}),X={mouseover:"mouseenter",focusin:"focus",click:"click"};var Y={name:"animateFill",defaultValue:!1,fn:function(t){var e;if(null==(e=t.props.render)||!e.$$tippy)return{};var n=B(t.popper),r=n.box,o=n.content,i=t.props.animateFill?function(){var t=m();return t.className="tippy-backdrop",x([t],"hidden"),t}():null;return{onCreate:function(){i&&(r.insertBefore(i,r.firstElementChild),r.setAttribute("data-animatefill",""),r.style.overflow="hidden",t.setProps({arrow:!1,animation:"shift-away"}))},onMount:function(){if(i){var t=r.style.transitionDuration,e=Number(t.replace("ms",""));o.style.transitionDelay=Math.round(e/10)+"ms",i.style.transitionDuration=t,x([i],"visible")}},onShow:function(){i&&(i.style.transitionDuration="0ms")},onHide:function(){i&&x([i],"hidden")}}}};var q={clientX:0,clientY:0},$=[];function J(t){var e=t.clientX,n=t.clientY;q={clientX:e,clientY:n}}var G={name:"followCursor",defaultValue:!1,fn:function(t){var e=t.reference,n=E(t.props.triggerTarget||e),r=!1,o=!1,i=!0,a=t.props;function s(){return"initial"===t.props.followCursor&&t.state.isVisible}function u(){n.addEventListener("mousemove",f)}function p(){n.removeEventListener("mousemove",f)}function c(){r=!0,t.setProps({getReferenceClientRect:null}),r=!1}function f(n){var r=!n.target||e.contains(n.target),o=t.props.followCursor,i=n.clientX,a=n.clientY,s=e.getBoundingClientRect(),u=i-s.left,p=a-s.top;!r&&t.props.interactive||t.setProps({getReferenceClientRect:function(){var t=e.getBoundingClientRect(),n=i,r=a;"initial"===o&&(n=t.left+u,r=t.top+p);var s="horizontal"===o?t.top:r,c="vertical"===o?t.right:n,f="horizontal"===o?t.bottom:r,l="vertical"===o?t.left:n;return{width:c-l,height:f-s,top:s,right:c,bottom:f,left:l}}})}function l(){t.props.followCursor&&($.push({instance:t,doc:n}),function(t){t.addEventListener("mousemove",J)}(n))}function d(){0===($=$.filter((function(e){return e.instance!==t}))).filter((function(t){return t.doc===n})).length&&function(t){t.removeEventListener("mousemove",J)}(n)}return{onCreate:l,onDestroy:d,onBeforeUpdate:function(){a=t.props},onAfterUpdate:function(e,n){var i=n.followCursor;r||void 0!==i&&a.followCursor!==i&&(d(),i?(l(),!t.state.isMounted||o||s()||u()):(p(),c()))},onMount:function(){t.props.followCursor&&!o&&(i&&(f(q),i=!1),s()||u())},onTrigger:function(t,e){h(e)&&(q={clientX:e.clientX,clientY:e.clientY}),o="focus"===e.type},onHidden:function(){t.props.followCursor&&(c(),p(),i=!0)}}}};var K={name:"inlinePositioning",defaultValue:!1,fn:function(t){var e,n=t.reference;var r=-1,o=!1,i=[],a={name:"tippyInlinePositioning",enabled:!0,phase:"afterWrite",fn:function(o){var a=o.state;t.props.inlinePositioning&&(-1!==i.indexOf(a.placement)&&(i=[]),e!==a.placement&&-1===i.indexOf(a.placement)&&(i.push(a.placement),t.setProps({getReferenceClientRect:function(){return function(t){return function(t,e,n,r){if(n.length<2||null===t)return e;if(2===n.length&&r>=0&&n[0].left>n[1].right)return n[r]||e;switch(t){case"top":case"bottom":var o=n[0],i=n[n.length-1],a="top"===t,s=o.top,u=i.bottom,p=a?o.left:i.left,c=a?o.right:i.right;return{top:s,bottom:u,left:p,right:c,width:c-p,height:u-s};case"left":case"right":var f=Math.min.apply(Math,n.map((function(t){return t.left}))),l=Math.max.apply(Math,n.map((function(t){return t.right}))),d=n.filter((function(e){return"left"===t?e.left===f:e.right===l})),v=d[0].top,m=d[d.length-1].bottom;return{top:v,bottom:m,left:f,right:l,width:l-f,height:m-v};default:return e}}(l(t),n.getBoundingClientRect(),d(n.getClientRects()),r)}(a.placement)}})),e=a.placement)}};function s(){var e;o||(e=function(t,e){var n;return{popperOptions:Object.assign({},t.popperOptions,{modifiers:[].concat(((null==(n=t.popperOptions)?void 0:n.modifiers)||[]).filter((function(t){return t.name!==e.name})),[e])})}}(t.props,a),o=!0,t.setProps(e),o=!1)}return{onCreate:s,onAfterUpdate:s,onTrigger:function(e,n){if(h(n)){var o=d(t.reference.getClientRects()),i=o.find((function(t){return t.left-2<=n.clientX&&t.right+2>=n.clientX&&t.top-2<=n.clientY&&t.bottom+2>=n.clientY})),a=o.indexOf(i);r=a>-1?a:r}},onHidden:function(){r=-1}}}};var Q={name:"sticky",defaultValue:!1,fn:function(t){var e=t.reference,n=t.popper;function r(e){return!0===t.props.sticky||t.props.sticky===e}var o=null,i=null;function a(){var s=r("reference")?(t.popperInstance?t.popperInstance.state.elements.reference:e).getBoundingClientRect():null,u=r("popper")?n.getBoundingClientRect():null;(s&&Z(o,s)||u&&Z(i,u))&&t.popperInstance&&t.popperInstance.update(),o=s,i=u,t.state.isMounted&&requestAnimationFrame(a)}return{onMount:function(){t.props.sticky&&a()}}}};function Z(t,e){return!t||!e||(t.top!==e.top||t.right!==e.right||t.bottom!==e.bottom||t.left!==e.left)}return e&&function(t){var e=document.createElement("style");e.textContent=t,e.setAttribute("data-tippy-stylesheet","");var n=document.head,r=document.querySelector("head>style,head>link");r?n.insertBefore(e,r):n.appendChild(e)}('.tippy-box[data-animation=fade][data-state=hidden]{opacity:0}[data-tippy-root]{max-width:calc(100vw - 10px)}.tippy-box{position:relative;background-color:#333;color:#fff;border-radius:4px;font-size:14px;line-height:1.4;white-space:normal;outline:0;transition-property:transform,visibility,opacity}.tippy-box[data-placement^=top]>.tippy-arrow{bottom:0}.tippy-box[data-placement^=top]>.tippy-arrow:before{bottom:-7px;left:0;border-width:8px 8px 0;border-top-color:initial;transform-origin:center top}.tippy-box[data-placement^=bottom]>.tippy-arrow{top:0}.tippy-box[data-placement^=bottom]>.tippy-arrow:before{top:-7px;left:0;border-width:0 8px 8px;border-bottom-color:initial;transform-origin:center bottom}.tippy-box[data-placement^=left]>.tippy-arrow{right:0}.tippy-box[data-placement^=left]>.tippy-arrow:before{border-width:8px 0 8px 8px;border-left-color:initial;right:-7px;transform-origin:center left}.tippy-box[data-placement^=right]>.tippy-arrow{left:0}.tippy-box[data-placement^=right]>.tippy-arrow:before{left:-7px;border-width:8px 8px 8px 0;border-right-color:initial;transform-origin:center right}.tippy-box[data-inertia][data-state=visible]{transition-timing-function:cubic-bezier(.54,1.5,.38,1.11)}.tippy-arrow{width:16px;height:16px;color:#333}.tippy-arrow:before{content:"";position:absolute;border-color:transparent;border-style:solid}.tippy-content{position:relative;padding:5px 9px;z-index:1}'),F.setDefaultProps({plugins:[Y,G,K,Q],render:N}),F.createSingleton=function(t,e){var n;void 0===e&&(e={});var r,o=t,i=[],a=[],s=e.overrides,u=[],f=!1;function l(){a=o.map((function(t){return c(t.props.triggerTarget||t.reference)})).reduce((function(t,e){return t.concat(e)}),[])}function d(){i=o.map((function(t){return t.reference}))}function v(t){o.forEach((function(e){t?e.enable():e.disable()}))}function g(t){return o.map((function(e){var n=e.setProps;return e.setProps=function(o){n(o),e.reference===r&&t.setProps(o)},function(){e.setProps=n}}))}function h(t,e){var n=a.indexOf(e);if(e!==r){r=e;var u=(s||[]).concat("content").reduce((function(t,e){return t[e]=o[n].props[e],t}),{});t.setProps(Object.assign({},u,{getReferenceClientRect:"function"==typeof u.getReferenceClientRect?u.getReferenceClientRect:function(){var t;return null==(t=i[n])?void 0:t.getBoundingClientRect()}}))}}v(!1),d(),l();var b={fn:function(){return{onDestroy:function(){v(!0)},onHidden:function(){r=null},onClickOutside:function(t){t.props.showOnCreate&&!f&&(f=!0,r=null)},onShow:function(t){t.props.showOnCreate&&!f&&(f=!0,h(t,i[0]))},onTrigger:function(t,e){h(t,e.currentTarget)}}}},y=F(m(),Object.assign({},p(e,["overrides"]),{plugins:[b].concat(e.plugins||[]),triggerTarget:a,popperOptions:Object.assign({},e.popperOptions,{modifiers:[].concat((null==(n=e.popperOptions)?void 0:n.modifiers)||[],[W])})})),w=y.show;y.show=function(t){if(w(),!r&&null==t)return h(y,i[0]);if(!r||null!=t){if("number"==typeof t)return i[t]&&h(y,i[t]);if(o.indexOf(t)>=0){var e=t.reference;return h(y,e)}return i.indexOf(t)>=0?h(y,t):void 0}},y.showNext=function(){var t=i[0];if(!r)return y.show(0);var e=i.indexOf(r);y.show(i[e+1]||t)},y.showPrevious=function(){var t=i[i.length-1];if(!r)return y.show(t);var e=i.indexOf(r),n=i[e-1]||t;y.show(n)};var x=y.setProps;return y.setProps=function(t){s=t.overrides||s,x(t)},y.setInstances=function(t){v(!0),u.forEach((function(t){return t()})),o=t,v(!1),d(),l(),u=g(y),y.setProps({triggerTarget:a})},u=g(y),y},F.delegate=function(t,e){var n=[],o=[],i=!1,a=e.target,s=p(e,["target"]),u=Object.assign({},s,{trigger:"manual",touch:!1}),f=Object.assign({touch:R.touch},s,{showOnCreate:!0}),l=F(t,u);function d(t){if(t.target&&!i){var n=t.target.closest(a);if(n){var r=n.getAttribute("data-tippy-trigger")||e.trigger||R.trigger;if(!n._tippy&&!("touchstart"===t.type&&"boolean"==typeof f.touch||"touchstart"!==t.type&&r.indexOf(X[t.type])<0)){var s=F(n,f);s&&(o=o.concat(s))}}}}function v(t,e,r,o){void 0===o&&(o=!1),t.addEventListener(e,r,o),n.push({node:t,eventType:e,handler:r,options:o})}return c(l).forEach((function(t){var e=t.destroy,a=t.enable,s=t.disable;t.destroy=function(t){void 0===t&&(t=!0),t&&o.forEach((function(t){t.destroy()})),o=[],n.forEach((function(t){var e=t.node,n=t.eventType,r=t.handler,o=t.options;e.removeEventListener(n,r,o)})),n=[],e()},t.enable=function(){a(),o.forEach((function(t){return t.enable()})),i=!1},t.disable=function(){s(),o.forEach((function(t){return t.disable()})),i=!0},function(t){var e=t.reference;v(e,"touchstart",d,r),v(e,"mouseover",d),v(e,"focusin",d),v(e,"click",d)}(t)})),l},F.hideAll=function(t){var e=void 0===t?{}:t,n=e.exclude,r=e.duration;_.forEach((function(t){var e=!1;if(n&&(e=b(n)?t.reference===n:t.popper===n.popper),!e){var o=t.props.duration;t.setProps({duration:r}),t.hide(),t.state.isDestroyed||t.setProps({duration:o})}}))},F.roundArrow='<svg width="16" height="6" xmlns="http://www.w3.org/2000/svg"><path d="M0 6s1.796-.013 4.67-3.615C5.851.9 6.93.006 8 0c1.07-.006 2.148.887 3.343 2.385C14.233 6.005 16 6 16 6H0z"></svg>',F}));
|
11 |
+
"""
|
12 |
+
return code
|
llama_lora/ui/main_page.py
CHANGED
@@ -1,21 +1,135 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
from ..globals import Global
|
|
|
4 |
|
5 |
-
from .
|
|
|
|
|
6 |
|
7 |
|
8 |
def main_page():
|
|
|
|
|
9 |
with gr.Blocks(
|
10 |
-
title=
|
11 |
-
css=
|
12 |
gr.Markdown(f"""
|
13 |
-
|
14 |
-
|
15 |
-
Hello world!
|
16 |
""")
|
17 |
-
|
|
|
18 |
if Global.ui_show_sys_info:
|
19 |
gr.Markdown(f"""
|
20 |
<small>Data dir: `{Global.data_dir}`</small>
|
21 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
from ..globals import Global
|
4 |
+
from ..models import get_model_with_lora
|
5 |
|
6 |
+
from .inference_ui import inference_ui
|
7 |
+
|
8 |
+
from .js_scripts import popperjs_core_code, tippy_js_code
|
9 |
|
10 |
|
11 |
def main_page():
|
12 |
+
title = get_page_title()
|
13 |
+
|
14 |
with gr.Blocks(
|
15 |
+
title=title,
|
16 |
+
css=main_page_custom_css()) as main_page_blocks:
|
17 |
gr.Markdown(f"""
|
18 |
+
<h1 class="app_title_text">{title}</h1> <wbr /><h2 class="app_subtitle_text">{Global.ui_subtitle}</h2>
|
|
|
|
|
19 |
""")
|
20 |
+
with gr.Tab("Inference"):
|
21 |
+
inference_ui()
|
22 |
if Global.ui_show_sys_info:
|
23 |
gr.Markdown(f"""
|
24 |
<small>Data dir: `{Global.data_dir}`</small>
|
25 |
""")
|
26 |
+
main_page_blocks.load(_js=f"""
|
27 |
+
function () {{
|
28 |
+
{popperjs_core_code()}
|
29 |
+
{tippy_js_code()}
|
30 |
+
""" + """
|
31 |
+
// Sync theme to body.
|
32 |
+
setTimeout(function () {
|
33 |
+
const gradio_container_element = document.querySelector(
|
34 |
+
".gradio-container"
|
35 |
+
);
|
36 |
+
function handle_gradio_container_element_class_change() {
|
37 |
+
if (Array.from(gradio_container_element.classList).includes("dark")) {
|
38 |
+
document.body.classList.add("dark");
|
39 |
+
} else {
|
40 |
+
document.body.classList.remove("dark");
|
41 |
+
}
|
42 |
+
}
|
43 |
+
new MutationObserver(function (mutationsList, observer) {
|
44 |
+
handle_gradio_container_element_class_change();
|
45 |
+
}).observe(gradio_container_element, {
|
46 |
+
attributes: true,
|
47 |
+
attributeFilter: ["class"],
|
48 |
+
});
|
49 |
+
handle_gradio_container_element_class_change();
|
50 |
+
}, 500);
|
51 |
+
}
|
52 |
+
""")
|
53 |
+
|
54 |
+
|
55 |
+
def get_page_title():
|
56 |
+
title = Global.ui_title
|
57 |
+
if (Global.ui_dev_mode):
|
58 |
+
title = f"[UI DEV MODE] {title}"
|
59 |
+
if (Global.ui_emoji):
|
60 |
+
title = f"{Global.ui_emoji} {title}"
|
61 |
+
return title
|
62 |
+
|
63 |
+
|
64 |
+
def main_page_custom_css():
|
65 |
+
css = """
|
66 |
+
.app_title_text {
|
67 |
+
display: inline-block;
|
68 |
+
margin-right: 0.5em !important;
|
69 |
+
}
|
70 |
+
.app_subtitle_text {
|
71 |
+
display: inline-block;
|
72 |
+
margin-top: 0 !important;
|
73 |
+
font-weight: 100 !important;
|
74 |
+
font-size: var(--text-md) !important;
|
75 |
+
}
|
76 |
+
|
77 |
+
.tippy-box {
|
78 |
+
background-color: var(--block-background-fill);
|
79 |
+
border: 1px solid var(--border-color-primary);
|
80 |
+
border-radius: 4px;
|
81 |
+
box-shadow: 0 2px 20px rgba(5,5,5,.08);
|
82 |
+
}
|
83 |
+
.tippy-arrow {
|
84 |
+
color: var(--block-background-fill);
|
85 |
+
}
|
86 |
+
.tippy-content {
|
87 |
+
color: var(--block-label-text-color);
|
88 |
+
font-family: var(--font);
|
89 |
+
font-weight: 100;
|
90 |
+
}
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
@media screen and (min-width: 640px) {
|
95 |
+
#inference_lora_model {
|
96 |
+
border-top-right-radius: 0;
|
97 |
+
border-bottom-right-radius: 0;
|
98 |
+
border-right: 0;
|
99 |
+
margin-right: -16px;
|
100 |
+
}
|
101 |
+
|
102 |
+
#inference_prompt_template {
|
103 |
+
border-top-left-radius: 0;
|
104 |
+
border-bottom-left-radius: 0;
|
105 |
+
border-left: 0;
|
106 |
+
|
107 |
+
margin-right: -148px;
|
108 |
+
padding-right: 148px;
|
109 |
+
}
|
110 |
+
|
111 |
+
#inference_reload_selections_button {
|
112 |
+
margin: 16px;
|
113 |
+
width: 100px;
|
114 |
+
min-width: 100px;
|
115 |
+
z-index: 1;
|
116 |
+
}
|
117 |
+
}
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
@media screen and (max-width: 392px) {
|
122 |
+
#inference_lora_model {
|
123 |
+
border-bottom-left-radius: 0;
|
124 |
+
border-bottom-right-radius: 0;
|
125 |
+
}
|
126 |
+
|
127 |
+
#inference_prompt_template {
|
128 |
+
border-top-left-radius: 0;
|
129 |
+
border-top-right-radius: 0;
|
130 |
+
border-top: 0;
|
131 |
+
margin-top: -16px;
|
132 |
+
}
|
133 |
+
}
|
134 |
+
"""
|
135 |
+
return css
|
llama_lora/utils/data.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import shutil
|
|
|
3 |
|
4 |
from ..globals import Global
|
5 |
|
@@ -19,3 +20,9 @@ def copy_sample_data_if_not_exists(source, destination):
|
|
19 |
|
20 |
print(f"Copying sample data to \"{destination}\"")
|
21 |
shutil.copytree(source, destination)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import shutil
|
3 |
+
import fnmatch
|
4 |
|
5 |
from ..globals import Global
|
6 |
|
|
|
20 |
|
21 |
print(f"Copying sample data to \"{destination}\"")
|
22 |
shutil.copytree(source, destination)
|
23 |
+
|
24 |
+
|
25 |
+
def get_available_template_names():
|
26 |
+
templates_directory_path = os.path.join(Global.data_dir, "templates")
|
27 |
+
all_files = os.listdir(templates_directory_path)
|
28 |
+
return [os.path.splitext(filename)[0] for filename in all_files if fnmatch.fnmatch(filename, "*.json")]
|
llama_lora/utils/prompter.py
CHANGED
@@ -5,18 +5,25 @@ From https://github.com/tloen/alpaca-lora/blob/main/utils/prompter.py
|
|
5 |
|
6 |
import json
|
7 |
import os.path as osp
|
8 |
-
from typing import Union
|
|
|
|
|
9 |
|
10 |
|
11 |
class Prompter(object):
|
12 |
-
__slots__ = ("template", "_verbose")
|
13 |
|
14 |
def __init__(self, template_name: str = "", verbose: bool = False):
|
15 |
self._verbose = verbose
|
16 |
if not template_name:
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
20 |
if not osp.exists(file_name):
|
21 |
raise ValueError(f"Can't read {file_name}")
|
22 |
with open(file_name) as fp:
|
@@ -28,20 +35,44 @@ class Prompter(object):
|
|
28 |
|
29 |
def generate_prompt(
|
30 |
self,
|
31 |
-
|
32 |
-
|
|
|
33 |
label: Union[None, str] = None,
|
34 |
) -> str:
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
else:
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
if label:
|
46 |
res = f"{res}{label}"
|
47 |
if self._verbose:
|
@@ -49,4 +80,28 @@ class Prompter(object):
|
|
49 |
return res
|
50 |
|
51 |
def get_response(self, output: str) -> str:
|
|
|
|
|
52 |
return output.split(self.template["response_split"])[1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
import json
|
7 |
import os.path as osp
|
8 |
+
from typing import Union, List
|
9 |
+
|
10 |
+
from ..globals import Global
|
11 |
|
12 |
|
13 |
class Prompter(object):
|
14 |
+
__slots__ = ("template_name", "template", "_verbose")
|
15 |
|
16 |
def __init__(self, template_name: str = "", verbose: bool = False):
|
17 |
self._verbose = verbose
|
18 |
if not template_name:
|
19 |
+
template_name = "None"
|
20 |
+
if template_name == "None":
|
21 |
+
self.template_name = "None"
|
22 |
+
return
|
23 |
+
self.template_name = template_name
|
24 |
+
|
25 |
+
file_name = osp.join(Global.data_dir, "templates",
|
26 |
+
f"{template_name}.json")
|
27 |
if not osp.exists(file_name):
|
28 |
raise ValueError(f"Can't read {file_name}")
|
29 |
with open(file_name) as fp:
|
|
|
35 |
|
36 |
def generate_prompt(
|
37 |
self,
|
38 |
+
variables: List[Union[None, str]] = [],
|
39 |
+
# instruction: str,
|
40 |
+
# input: Union[None, str] = None,
|
41 |
label: Union[None, str] = None,
|
42 |
) -> str:
|
43 |
+
if self.template_name == "None":
|
44 |
+
res = get_val(variables, 0, "")
|
45 |
+
elif "variables" in self.template:
|
46 |
+
variable_names = self.template.get("variables")
|
47 |
+
if "default" not in self.template:
|
48 |
+
raise ValueError(
|
49 |
+
f"The template {self.template_name} has \"variables\" defined but does not has a default prompt defined. Please do it like: '\"default\": \"prompt_with_instruction\"' to handle cases when a matching prompt can't be found.")
|
50 |
+
default_prompt_name = self.template.get("default")
|
51 |
+
if default_prompt_name not in self.template:
|
52 |
+
raise ValueError(
|
53 |
+
f"The template {self.template_name} has \"default\" set to \"{default_prompt_name}\" but it's not defined. Please do it like: '\"{default_prompt_name}\": \"...\".")
|
54 |
+
prompt_name = get_prompt_name(variables, variable_names)
|
55 |
+
prompt_template = self.template.get(default_prompt_name)
|
56 |
+
if prompt_name in self.template:
|
57 |
+
prompt_template = self.template.get(prompt_name)
|
58 |
+
|
59 |
+
res = prompt_template.format(
|
60 |
+
**variables_to_dict(variables, variable_names))
|
61 |
+
|
62 |
else:
|
63 |
+
instruction = get_val(variables, 0, "")
|
64 |
+
input = get_val(variables, 1)
|
65 |
+
# returns the full prompt from instruction and optional input
|
66 |
+
# if a label (=response, =output) is provided, it's also appended.
|
67 |
+
if input:
|
68 |
+
res = self.template["prompt_input"].format(
|
69 |
+
instruction=instruction, input=input
|
70 |
+
)
|
71 |
+
else:
|
72 |
+
res = self.template["prompt_no_input"].format(
|
73 |
+
instruction=instruction
|
74 |
+
)
|
75 |
+
|
76 |
if label:
|
77 |
res = f"{res}{label}"
|
78 |
if self._verbose:
|
|
|
80 |
return res
|
81 |
|
82 |
def get_response(self, output: str) -> str:
|
83 |
+
if self.template_name == "None":
|
84 |
+
return output
|
85 |
return output.split(self.template["response_split"])[1].strip()
|
86 |
+
|
87 |
+
def get_variable_names(self) -> List[str]:
|
88 |
+
if self.template_name == "None":
|
89 |
+
return ["prompt"]
|
90 |
+
elif "variables" in self.template:
|
91 |
+
return self.template.get("variables")
|
92 |
+
else:
|
93 |
+
return ["instruction", "input"]
|
94 |
+
|
95 |
+
|
96 |
+
def get_val(arr, index, default=None):
|
97 |
+
return arr[index] if -len(arr) <= index < len(arr) else default
|
98 |
+
|
99 |
+
|
100 |
+
def get_prompt_name(variables, variable_names):
|
101 |
+
result = [y for x, y in zip(
|
102 |
+
variables, variable_names) if x not in (None, '')]
|
103 |
+
return "prompt_with_" + '_'.join(result)
|
104 |
+
|
105 |
+
|
106 |
+
def variables_to_dict(variables, variable_names):
|
107 |
+
return {key: (variables[i] if i < len(variables) and variables[i] is not None else '') for i, key in enumerate(variable_names)}
|