plugin refactor: phase 1
This commit is contained in:
@@ -1525,8 +1525,103 @@ async function postData(url = '', data = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
async function generate_menu(guiBase64String, btnName){
|
||||
// assign the button and menu data
|
||||
push_data_to_gradio_component(guiBase64String, "invisible_current_pop_up_plugin_arg", "string");
|
||||
push_data_to_gradio_component(btnName, "invisible_callback_btn_for_plugin_exe", "string");
|
||||
|
||||
// Base64 to dict
|
||||
const stringData = atob(guiBase64String);
|
||||
let guiJsonData = JSON.parse(stringData);
|
||||
let menu = document.getElementById("plugin_arg_menu");
|
||||
gui_args = {}
|
||||
for (const key in guiJsonData) {
|
||||
if (guiJsonData.hasOwnProperty(key)) {
|
||||
const innerJSONString = guiJsonData[key];
|
||||
const decodedObject = JSON.parse(innerJSONString);
|
||||
gui_args[key] = decodedObject;
|
||||
}
|
||||
}
|
||||
|
||||
// 使参数菜单显现
|
||||
push_data_to_gradio_component({
|
||||
visible: true,
|
||||
__type__: 'update'
|
||||
}, "plugin_arg_menu", "obj");
|
||||
|
||||
// 根据 gui_args,使得对应参数项显现
|
||||
let text_cnt = 0;
|
||||
for (const key in gui_args) {
|
||||
if (gui_args.hasOwnProperty(key)) {
|
||||
const component_name = "plugin_arg_txt_" + text_cnt;
|
||||
if (gui_args[key].type=='string'){
|
||||
push_data_to_gradio_component({
|
||||
visible: true,
|
||||
label: gui_args[key].title + "(" + gui_args[key].description + ")",
|
||||
__type__: 'update'
|
||||
}, component_name, "obj");
|
||||
if (key === "main_input"){
|
||||
// 为了与旧插件兼容,生成菜单时,自动加载输入栏的值
|
||||
let current_main_input = await get_data_from_gradio_component('user_input_main');
|
||||
let current_main_input_2 = await get_data_from_gradio_component('user_input_float');
|
||||
push_data_to_gradio_component(current_main_input + current_main_input_2, component_name, "obj");
|
||||
}
|
||||
else if (key === "advanced_arg"){
|
||||
// 为了与旧插件兼容,生成菜单时,自动加载旧高级参数输入区的值
|
||||
let advance_arg_input_legacy = await get_data_from_gradio_component('advance_arg_input_legacy');
|
||||
push_data_to_gradio_component(advance_arg_input_legacy, component_name, "obj");
|
||||
}
|
||||
else {
|
||||
push_data_to_gradio_component(gui_args[key].default_value, component_name, "obj");
|
||||
}
|
||||
text_cnt += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function execute_current_pop_up_plugin(){
|
||||
let guiBase64String = await get_data_from_gradio_component('invisible_current_pop_up_plugin_arg');
|
||||
const stringData = atob(guiBase64String);
|
||||
let guiJsonData = JSON.parse(stringData);
|
||||
gui_args = {}
|
||||
for (const key in guiJsonData) {
|
||||
if (guiJsonData.hasOwnProperty(key)) {
|
||||
const innerJSONString = guiJsonData[key];
|
||||
const decodedObject = JSON.parse(innerJSONString);
|
||||
gui_args[key] = decodedObject;
|
||||
}
|
||||
}
|
||||
// read user confirmed value
|
||||
let text_cnt = 0;
|
||||
for (const key in gui_args) {
|
||||
if (gui_args.hasOwnProperty(key)) {
|
||||
if (gui_args[key].type=='string'){
|
||||
corrisponding_elem_id = "plugin_arg_txt_"+text_cnt
|
||||
gui_args[key].user_confirmed_value = await get_data_from_gradio_component(corrisponding_elem_id);
|
||||
text_cnt += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// close menu
|
||||
push_data_to_gradio_component({
|
||||
visible: false,
|
||||
__type__: 'update'
|
||||
}, "plugin_arg_menu", "obj");
|
||||
for (text_cnt = 0; text_cnt < 8; text_cnt++){
|
||||
push_data_to_gradio_component({
|
||||
visible: false,
|
||||
label: "",
|
||||
__type__: 'update'
|
||||
}, "plugin_arg_txt_"+text_cnt, "obj");
|
||||
}
|
||||
|
||||
// execute the plugin
|
||||
push_data_to_gradio_component(JSON.stringify(gui_args), "invisible_current_pop_up_plugin_arg_final", "string");
|
||||
document.getElementById("invisible_callback_btn_for_plugin_exe").click();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
39
themes/gui_advanced_plugin_class.py
Normal file
39
themes/gui_advanced_plugin_class.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import gradio as gr
|
||||
import json
|
||||
from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, DummyWith
|
||||
|
||||
def define_gui_advanced_plugin_class(plugins):
|
||||
# 定义新一代插件的高级参数区
|
||||
with gr.Floating(init_x="40%", init_y="20%", visible=False, width="30%", drag="top", elem_id="plugin_arg_menu"):
|
||||
with gr.Accordion("请选择并确认插件参数!", open=True, elem_id="plugin_arg_panel"):
|
||||
for u in range(8):
|
||||
with gr.Row():
|
||||
gr.Textbox(show_label=True, label="T1", placeholder="请输入", lines=1, visible=False, elem_id=f"plugin_arg_txt_{u}").style(container=False)
|
||||
# for u in range(8):
|
||||
# with gr.Row():
|
||||
# gr.Dropdown(label="T1", value="请选择", visible=False, elem_id=f"plugin_arg_drop_{u}").style(container=False)
|
||||
with gr.Row():
|
||||
# 这个隐藏textbox负责装入当前弹出插件的属性
|
||||
gr.Textbox(show_label=False, placeholder="请输入", lines=1, visible=False,
|
||||
elem_id=f"invisible_current_pop_up_plugin_arg").style(container=False)
|
||||
usr_confirmed_arg = gr.Textbox(show_label=False, placeholder="请输入", lines=1, visible=False,
|
||||
elem_id=f"invisible_current_pop_up_plugin_arg_final").style(container=False)
|
||||
arg_confirm_btn = gr.Button("确认参数并执行", variant="primary");
|
||||
arg_confirm_btn.style(size="sm")
|
||||
arg_confirm_btn.click(None, None, None, _js="""()=>execute_current_pop_up_plugin()""")
|
||||
invisible_callback_btn_for_plugin_exe = gr.Button(r"未选定任何插件", variant="secondary", visible=False, elem_id="invisible_callback_btn_for_plugin_exe").style(size="sm")
|
||||
# 随变按钮的回调函数注册
|
||||
def route_switchy_bt_with_arg(request: gr.Request, input_order, *arg):
|
||||
arguments = {k:v for k,v in zip(input_order, arg)}
|
||||
which_plugin = arguments.pop('new_plugin_callback')
|
||||
if which_plugin in [r"未选定任何插件"]: return
|
||||
usr_confirmed_arg = arguments.pop('usr_confirmed_arg')
|
||||
arg_confirm: dict = {}
|
||||
usr_confirmed_arg_dict = json.loads(usr_confirmed_arg)
|
||||
for arg_name in usr_confirmed_arg_dict:
|
||||
arg_confirm.update({arg_name: str(usr_confirmed_arg_dict[arg_name]['user_confirmed_value'])})
|
||||
plugin_obj = plugins[which_plugin]["Class"]
|
||||
arguments['plugin_advanced_arg'] = arg_confirm
|
||||
yield from ArgsGeneralWrapper(plugin_obj.execute)(request, *arguments.values())
|
||||
return invisible_callback_btn_for_plugin_exe, route_switchy_bt_with_arg, usr_confirmed_arg
|
||||
|
||||
41
themes/gui_floating_menu.py
Normal file
41
themes/gui_floating_menu.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import gradio as gr
|
||||
|
||||
def define_gui_floating_menu(customize_btns, functional, predefined_btns, cookies, web_cookie_cache):
|
||||
with gr.Floating(init_x="20%", init_y="50%", visible=False, width="40%", drag="top") as area_input_secondary:
|
||||
with gr.Accordion("浮动输入区", open=True, elem_id="input-panel2"):
|
||||
with gr.Row() as row:
|
||||
row.style(equal_height=True)
|
||||
with gr.Column(scale=10):
|
||||
txt2 = gr.Textbox(show_label=False, placeholder="Input question here.",
|
||||
elem_id='user_input_float', lines=8, label="输入区2").style(container=False)
|
||||
with gr.Column(scale=1, min_width=40):
|
||||
submitBtn2 = gr.Button("提交", variant="primary"); submitBtn2.style(size="sm")
|
||||
resetBtn2 = gr.Button("重置", variant="secondary"); resetBtn2.style(size="sm")
|
||||
stopBtn2 = gr.Button("停止", variant="secondary"); stopBtn2.style(size="sm")
|
||||
clearBtn2 = gr.Button("清除", elem_id="elem_clear2", variant="secondary", visible=False); clearBtn2.style(size="sm")
|
||||
|
||||
|
||||
with gr.Floating(init_x="20%", init_y="50%", visible=False, width="40%", drag="top") as area_customize:
|
||||
with gr.Accordion("自定义菜单", open=True, elem_id="edit-panel"):
|
||||
with gr.Row() as row:
|
||||
with gr.Column(scale=10):
|
||||
AVAIL_BTN = [btn for btn in customize_btns.keys()] + [k for k in functional]
|
||||
basic_btn_dropdown = gr.Dropdown(AVAIL_BTN, value="自定义按钮1", label="选择一个需要自定义基础功能区按钮").style(container=False)
|
||||
basic_fn_title = gr.Textbox(show_label=False, placeholder="输入新按钮名称", lines=1).style(container=False)
|
||||
basic_fn_prefix = gr.Textbox(show_label=False, placeholder="输入新提示前缀", lines=4).style(container=False)
|
||||
basic_fn_suffix = gr.Textbox(show_label=False, placeholder="输入新提示后缀", lines=4).style(container=False)
|
||||
with gr.Column(scale=1, min_width=70):
|
||||
basic_fn_confirm = gr.Button("确认并保存", variant="primary"); basic_fn_confirm.style(size="sm")
|
||||
basic_fn_clean = gr.Button("恢复默认", variant="primary"); basic_fn_clean.style(size="sm")
|
||||
|
||||
from shared_utils.cookie_manager import assign_btn__fn_builder
|
||||
assign_btn = assign_btn__fn_builder(customize_btns, predefined_btns, cookies, web_cookie_cache)
|
||||
# update btn
|
||||
h = basic_fn_confirm.click(assign_btn, [web_cookie_cache, cookies, basic_btn_dropdown, basic_fn_title, basic_fn_prefix, basic_fn_suffix],
|
||||
[web_cookie_cache, cookies, *customize_btns.values(), *predefined_btns.values()])
|
||||
h.then(None, [web_cookie_cache], None, _js="""(web_cookie_cache)=>{setCookie("web_cookie_cache", web_cookie_cache, 365);}""")
|
||||
# clean up btn
|
||||
h2 = basic_fn_clean.click(assign_btn, [web_cookie_cache, cookies, basic_btn_dropdown, basic_fn_title, basic_fn_prefix, basic_fn_suffix, gr.State(True)],
|
||||
[web_cookie_cache, cookies, *customize_btns.values(), *predefined_btns.values()])
|
||||
h2.then(None, [web_cookie_cache], None, _js="""(web_cookie_cache)=>{setCookie("web_cookie_cache", web_cookie_cache, 365);}""")
|
||||
return area_input_secondary, txt2, area_customize, submitBtn2, resetBtn2, clearBtn2, stopBtn2
|
||||
34
themes/gui_toolbar.py
Normal file
34
themes/gui_toolbar.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import gradio as gr
|
||||
|
||||
def define_gui_toolbar(AVAIL_LLM_MODELS, LLM_MODEL, INIT_SYS_PROMPT, THEME, AVAIL_THEMES, ADD_WAIFU, help_menu_description, js_code_for_toggle_darkmode):
|
||||
with gr.Floating(init_x="0%", init_y="0%", visible=True, width=None, drag="forbidden", elem_id="tooltip"):
|
||||
with gr.Row():
|
||||
with gr.Tab("上传文件", elem_id="interact-panel"):
|
||||
gr.Markdown("请上传本地文件/压缩包供“函数插件区”功能调用。请注意: 上传文件后会自动把输入区修改为相应路径。")
|
||||
file_upload_2 = gr.Files(label="任何文件, 推荐上传压缩文件(zip, tar)", file_count="multiple", elem_id="elem_upload_float")
|
||||
|
||||
with gr.Tab("更换模型", elem_id="interact-panel"):
|
||||
md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, elem_id="elem_model_sel", label="更换LLM模型/请求源").style(container=False)
|
||||
top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
|
||||
temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature", elem_id="elem_temperature")
|
||||
max_length_sl = gr.Slider(minimum=256, maximum=1024*32, value=4096, step=128, interactive=True, label="Local LLM MaxLength",)
|
||||
system_prompt = gr.Textbox(show_label=True, lines=2, placeholder=f"System Prompt", label="System prompt", value=INIT_SYS_PROMPT, elem_id="elem_prompt")
|
||||
temperature.change(None, inputs=[temperature], outputs=None,
|
||||
_js="""(temperature)=>gpt_academic_gradio_saveload("save", "elem_prompt", "js_temperature_cookie", temperature)""")
|
||||
system_prompt.change(None, inputs=[system_prompt], outputs=None,
|
||||
_js="""(system_prompt)=>gpt_academic_gradio_saveload("save", "elem_prompt", "js_system_prompt_cookie", system_prompt)""")
|
||||
md_dropdown.change(None, inputs=[md_dropdown], outputs=None,
|
||||
_js="""(md_dropdown)=>gpt_academic_gradio_saveload("save", "elem_model_sel", "js_md_dropdown_cookie", md_dropdown)""")
|
||||
|
||||
with gr.Tab("界面外观", elem_id="interact-panel"):
|
||||
theme_dropdown = gr.Dropdown(AVAIL_THEMES, value=THEME, label="更换UI主题").style(container=False)
|
||||
checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "浮动输入区", "输入清除键", "插件参数区"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区", elem_id='cbs').style(container=False)
|
||||
opt = ["自定义菜单"]
|
||||
value=[]
|
||||
if ADD_WAIFU: opt += ["添加Live2D形象"]; value += ["添加Live2D形象"]
|
||||
checkboxes_2 = gr.CheckboxGroup(opt, value=value, label="显示/隐藏自定义菜单", elem_id='cbsc').style(container=False)
|
||||
dark_mode_btn = gr.Button("切换界面明暗 ☀", variant="secondary").style(size="sm")
|
||||
dark_mode_btn.click(None, None, None, _js=js_code_for_toggle_darkmode)
|
||||
with gr.Tab("帮助", elem_id="interact-panel"):
|
||||
gr.Markdown(help_menu_description)
|
||||
return checkboxes, checkboxes_2, max_length_sl, theme_dropdown, system_prompt, file_upload_2, md_dropdown, top_p, temperature
|
||||
Reference in New Issue
Block a user