add prompt and temperature to cookie
This commit is contained in:
parent
cb7c0703be
commit
6c90ff80ea
18
main.py
18
main.py
@ -15,7 +15,7 @@ help_menu_description = \
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
if gr.__version__ not in ['3.32.8']:
|
if gr.__version__ not in ['3.32.9']:
|
||||||
raise ModuleNotFoundError("使用项目内置Gradio获取最优体验! 请运行 `pip install -r requirements.txt` 指令安装内置Gradio及其他依赖, 详情信息见requirements.txt.")
|
raise ModuleNotFoundError("使用项目内置Gradio获取最优体验! 请运行 `pip install -r requirements.txt` 指令安装内置Gradio及其他依赖, 详情信息见requirements.txt.")
|
||||||
from request_llms.bridge_all import predict
|
from request_llms.bridge_all import predict
|
||||||
from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, load_chat_cookies, DummyWith
|
from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, load_chat_cookies, DummyWith
|
||||||
@ -23,8 +23,8 @@ def main():
|
|||||||
proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION = get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION')
|
proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION = get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION')
|
||||||
CHATBOT_HEIGHT, LAYOUT, AVAIL_LLM_MODELS, AUTO_CLEAR_TXT = get_conf('CHATBOT_HEIGHT', 'LAYOUT', 'AVAIL_LLM_MODELS', 'AUTO_CLEAR_TXT')
|
CHATBOT_HEIGHT, LAYOUT, AVAIL_LLM_MODELS, AUTO_CLEAR_TXT = get_conf('CHATBOT_HEIGHT', 'LAYOUT', 'AVAIL_LLM_MODELS', 'AUTO_CLEAR_TXT')
|
||||||
ENABLE_AUDIO, AUTO_CLEAR_TXT, PATH_LOGGING, AVAIL_THEMES, THEME, ADD_WAIFU = get_conf('ENABLE_AUDIO', 'AUTO_CLEAR_TXT', 'PATH_LOGGING', 'AVAIL_THEMES', 'THEME', 'ADD_WAIFU')
|
ENABLE_AUDIO, AUTO_CLEAR_TXT, PATH_LOGGING, AVAIL_THEMES, THEME, ADD_WAIFU = get_conf('ENABLE_AUDIO', 'AUTO_CLEAR_TXT', 'PATH_LOGGING', 'AVAIL_THEMES', 'THEME', 'ADD_WAIFU')
|
||||||
DARK_MODE, NUM_CUSTOM_BASIC_BTN, SSL_KEYFILE, SSL_CERTFILE = get_conf('DARK_MODE', 'NUM_CUSTOM_BASIC_BTN', 'SSL_KEYFILE', 'SSL_CERTFILE')
|
NUM_CUSTOM_BASIC_BTN, SSL_KEYFILE, SSL_CERTFILE = get_conf('NUM_CUSTOM_BASIC_BTN', 'SSL_KEYFILE', 'SSL_CERTFILE')
|
||||||
INIT_SYS_PROMPT = get_conf('INIT_SYS_PROMPT')
|
DARK_MODE, INIT_SYS_PROMPT = get_conf('DARK_MODE', 'INIT_SYS_PROMPT')
|
||||||
|
|
||||||
# 如果WEB_PORT是-1, 则随机选取WEB端口
|
# 如果WEB_PORT是-1, 则随机选取WEB端口
|
||||||
PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
|
PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
|
||||||
@ -77,7 +77,7 @@ def main():
|
|||||||
predefined_btns = {}
|
predefined_btns = {}
|
||||||
with gr.Blocks(title="GPT 学术优化", theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
|
with gr.Blocks(title="GPT 学术优化", theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
|
||||||
gr.HTML(title_html)
|
gr.HTML(title_html)
|
||||||
secret_css, dark_mode, py_pickle_cookie = gr.Textbox(visible=False), gr.Textbox(DARK_MODE, visible=False), gr.Textbox(visible=False)
|
secret_css, py_pickle_cookie = gr.Textbox(visible=False), gr.Textbox(visible=False)
|
||||||
cookies = gr.State(load_chat_cookies())
|
cookies = gr.State(load_chat_cookies())
|
||||||
with gr_L1():
|
with gr_L1():
|
||||||
with gr_L2(scale=2, elem_id="gpt-chat"):
|
with gr_L2(scale=2, elem_id="gpt-chat"):
|
||||||
@ -153,9 +153,13 @@ def main():
|
|||||||
with gr.Tab("更换模型", elem_id="interact-panel"):
|
with gr.Tab("更换模型", elem_id="interact-panel"):
|
||||||
md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="更换LLM模型/请求源").style(container=False)
|
md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, 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)",)
|
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",)
|
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",)
|
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)
|
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)""")
|
||||||
|
|
||||||
with gr.Tab("界面外观", elem_id="interact-panel"):
|
with gr.Tab("界面外观", elem_id="interact-panel"):
|
||||||
theme_dropdown = gr.Dropdown(AVAIL_THEMES, value=THEME, label="更换UI主题").style(container=False)
|
theme_dropdown = gr.Dropdown(AVAIL_THEMES, value=THEME, label="更换UI主题").style(container=False)
|
||||||
@ -375,7 +379,7 @@ def main():
|
|||||||
demo.load(init_cookie, inputs=[cookies], outputs=[cookies])
|
demo.load(init_cookie, inputs=[cookies], outputs=[cookies])
|
||||||
demo.load(persistent_cookie_reload, inputs = [py_pickle_cookie, cookies],
|
demo.load(persistent_cookie_reload, inputs = [py_pickle_cookie, cookies],
|
||||||
outputs = [py_pickle_cookie, cookies, *customize_btns.values(), *predefined_btns.values()], _js=js_code_for_persistent_cookie_init)
|
outputs = [py_pickle_cookie, cookies, *customize_btns.values(), *predefined_btns.values()], _js=js_code_for_persistent_cookie_init)
|
||||||
demo.load(None, inputs=[dark_mode], outputs=None, _js="""(dark_mode)=>{apply_cookie_for_checkbox(dark_mode);}""") # 配置暗色主题或亮色主题
|
demo.load(None, inputs=[], outputs=None, _js=f"""()=>init_frontend_with_cookies("{DARK_MODE}","{INIT_SYS_PROMPT}")""") # 配置暗色主题或亮色主题
|
||||||
demo.load(None, inputs=[gr.Textbox(LAYOUT, visible=False)], outputs=None, _js='(LAYOUT)=>{GptAcademicJavaScriptInit(LAYOUT);}')
|
demo.load(None, inputs=[gr.Textbox(LAYOUT, visible=False)], outputs=None, _js='(LAYOUT)=>{GptAcademicJavaScriptInit(LAYOUT);}')
|
||||||
|
|
||||||
# gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
|
# gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
https://public.gpt-academic.top/publish/gradio-3.32.8-py3-none-any.whl
|
https://public.agent-matrix.com/publish/gradio-3.32.9-py3-none-any.whl
|
||||||
gradio-client==0.8
|
gradio-client==0.8
|
||||||
pypdf2==2.12.1
|
pypdf2==2.12.1
|
||||||
zhipuai>=2
|
zhipuai>=2
|
||||||
|
1
shared_utils/cookie_manager.py
Normal file
1
shared_utils/cookie_manager.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
@ -2,6 +2,24 @@
|
|||||||
// 第 1 部分: 工具函数
|
// 第 1 部分: 工具函数
|
||||||
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
||||||
|
|
||||||
|
function push_data_to_gradio_component(DAT, ELEM_ID, TYPE){
|
||||||
|
// type, // type==="str" / type==="float"
|
||||||
|
if (TYPE=="str"){
|
||||||
|
// convert dat to string: do nothign
|
||||||
|
}
|
||||||
|
else if (TYPE=="float"){
|
||||||
|
// convert dat to float
|
||||||
|
DAT = parseFloat(DAT);
|
||||||
|
}
|
||||||
|
const myEvent = new CustomEvent('gpt_academic_update_gradio_component', {
|
||||||
|
detail: {
|
||||||
|
data: DAT,
|
||||||
|
elem_id: ELEM_ID,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
window.dispatchEvent(myEvent);
|
||||||
|
}
|
||||||
|
|
||||||
function gradioApp() {
|
function gradioApp() {
|
||||||
// https://github.com/GaiZhenbiao/ChuanhuChatGPT/tree/main/web_assets/javascript
|
// https://github.com/GaiZhenbiao/ChuanhuChatGPT/tree/main/web_assets/javascript
|
||||||
const elems = document.getElementsByTagName('gradio-app');
|
const elems = document.getElementsByTagName('gradio-app');
|
||||||
@ -799,8 +817,34 @@ function set_checkbox(key, bool, set_twice=false) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function apply_cookie_for_checkbox(dark) {
|
function gpt_academic_gradio_saveload(
|
||||||
// console.log("apply_cookie_for_checkboxes")
|
save_or_load, // save_or_load==="save" / save_or_load==="load"
|
||||||
|
elem_id, // element id
|
||||||
|
cookie_key, // cookie key
|
||||||
|
save_value="", // save value
|
||||||
|
load_type = "str", // type==="str" / type==="float"
|
||||||
|
load_default=false, // load default value
|
||||||
|
load_default_value=""
|
||||||
|
) {
|
||||||
|
if (save_or_load === "load") {
|
||||||
|
let value = getCookie(cookie_key);
|
||||||
|
if (value) {
|
||||||
|
console.log('加载cookie', elem_id, value)
|
||||||
|
push_data_to_gradio_component(value, elem_id, load_type);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
if (load_default){
|
||||||
|
console.log('加载cookie的默认值', elem_id, load_default_value)
|
||||||
|
push_data_to_gradio_component(load_default_value, elem_id, load_type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (save_or_load === "save") {
|
||||||
|
setCookie(cookie_key, save_value, 365);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function init_frontend_with_cookies(dark, prompt) {
|
||||||
let searchString = "输入清除键";
|
let searchString = "输入清除键";
|
||||||
let bool_value = "False";
|
let bool_value = "False";
|
||||||
|
|
||||||
@ -819,6 +863,11 @@ function apply_cookie_for_checkbox(dark) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////// SysPrompt ///////////////////////////
|
||||||
|
gpt_academic_gradio_saveload("load", "elem_prompt", "js_system_prompt_cookie", null, "str");
|
||||||
|
////////////////////// Temperature ///////////////////////////
|
||||||
|
gpt_academic_gradio_saveload("load", "elem_temperature", "js_temperature_cookie", null, "float");
|
||||||
|
|
||||||
////////////////////// clearButton ///////////////////////////
|
////////////////////// clearButton ///////////////////////////
|
||||||
if (getCookie("js_clearbtn_show_cookie")) {
|
if (getCookie("js_clearbtn_show_cookie")) {
|
||||||
// have cookie
|
// have cookie
|
||||||
@ -851,8 +900,11 @@ function apply_cookie_for_checkbox(dark) {
|
|||||||
loadLive2D();
|
loadLive2D();
|
||||||
set_checkbox(searchString, true);
|
set_checkbox(searchString, true);
|
||||||
} else {
|
} else {
|
||||||
$('.waifu').hide();
|
try {
|
||||||
set_checkbox(searchString, false);
|
$('.waifu').hide();
|
||||||
|
set_checkbox(searchString, false);
|
||||||
|
} catch (error) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// do not have cookie
|
// do not have cookie
|
||||||
|
@ -178,11 +178,8 @@ setTimeout(() => {
|
|||||||
js_code_show_or_hide_group2 = """
|
js_code_show_or_hide_group2 = """
|
||||||
(display_panel_arr)=>{
|
(display_panel_arr)=>{
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
// console.log("display_panel_arr");
|
|
||||||
// get conf
|
|
||||||
display_panel_arr = get_checkbox_selected_items("cbsc");
|
display_panel_arr = get_checkbox_selected_items("cbsc");
|
||||||
|
|
||||||
////////////////////// 添加Live2D形象 ///////////////////////////
|
|
||||||
let searchString = "添加Live2D形象";
|
let searchString = "添加Live2D形象";
|
||||||
let ele = "none";
|
let ele = "none";
|
||||||
if (display_panel_arr.includes(searchString)) {
|
if (display_panel_arr.includes(searchString)) {
|
||||||
@ -193,7 +190,6 @@ setTimeout(() => {
|
|||||||
$('.waifu').hide();
|
$('.waifu').hide();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}, 50);
|
}, 50);
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user