auto chatbot height
This commit is contained in:
25
main.py
25
main.py
@ -45,10 +45,10 @@ def main():
|
|||||||
proxy_info = check_proxy(proxies)
|
proxy_info = check_proxy(proxies)
|
||||||
|
|
||||||
gr_L1 = lambda: gr.Row().style()
|
gr_L1 = lambda: gr.Row().style()
|
||||||
gr_L2 = lambda scale: gr.Column(scale=scale)
|
gr_L2 = lambda scale, elem_id: gr.Column(scale=scale, elem_id=elem_id)
|
||||||
if LAYOUT == "TOP-DOWN":
|
if LAYOUT == "TOP-DOWN":
|
||||||
gr_L1 = lambda: DummyWith()
|
gr_L1 = lambda: DummyWith()
|
||||||
gr_L2 = lambda scale: gr.Row()
|
gr_L2 = lambda scale, elem_id: gr.Row()
|
||||||
CHATBOT_HEIGHT /= 2
|
CHATBOT_HEIGHT /= 2
|
||||||
|
|
||||||
cancel_handles = []
|
cancel_handles = []
|
||||||
@ -56,12 +56,12 @@ def main():
|
|||||||
gr.HTML(title_html)
|
gr.HTML(title_html)
|
||||||
cookies = gr.State({'api_key': API_KEY, 'llm_model': LLM_MODEL})
|
cookies = gr.State({'api_key': API_KEY, 'llm_model': LLM_MODEL})
|
||||||
with gr_L1():
|
with gr_L1():
|
||||||
with gr_L2(scale=2):
|
with gr_L2(scale=2, elem_id="gpt-chat"):
|
||||||
chatbot = gr.Chatbot(label=f"当前模型:{LLM_MODEL}")
|
chatbot = gr.Chatbot(label=f"当前模型:{LLM_MODEL}", elem_id="gpt-chatbot")
|
||||||
chatbot.style(height=CHATBOT_HEIGHT)
|
chatbot.style()
|
||||||
history = gr.State([])
|
history = gr.State([])
|
||||||
with gr_L2(scale=1):
|
with gr_L2(scale=1, elem_id="gpt-panel"):
|
||||||
with gr.Accordion("输入区", open=True) as area_input_primary:
|
with gr.Accordion("输入区", open=True, elem_id="input-panel") as area_input_primary:
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
txt = gr.Textbox(show_label=False, placeholder="Input question here.").style(container=False)
|
txt = gr.Textbox(show_label=False, placeholder="Input question here.").style(container=False)
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
@ -72,13 +72,13 @@ def main():
|
|||||||
clearBtn = gr.Button("清除", variant="secondary", visible=False); clearBtn.style(size="sm")
|
clearBtn = gr.Button("清除", variant="secondary", visible=False); clearBtn.style(size="sm")
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {proxy_info}")
|
status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {proxy_info}")
|
||||||
with gr.Accordion("基础功能区", open=True) as area_basic_fn:
|
with gr.Accordion("基础功能区", open=True, elem_id="basic-panel") as area_basic_fn:
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
for k in functional:
|
for k in functional:
|
||||||
if ("Visible" in functional[k]) and (not functional[k]["Visible"]): continue
|
if ("Visible" in functional[k]) and (not functional[k]["Visible"]): continue
|
||||||
variant = functional[k]["Color"] if "Color" in functional[k] else "secondary"
|
variant = functional[k]["Color"] if "Color" in functional[k] else "secondary"
|
||||||
functional[k]["Button"] = gr.Button(k, variant=variant)
|
functional[k]["Button"] = gr.Button(k, variant=variant)
|
||||||
with gr.Accordion("函数插件区", open=True) as area_crazy_fn:
|
with gr.Accordion("函数插件区", open=True, elem_id="plugin-panel") as area_crazy_fn:
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
gr.Markdown("注意:以下“红颜色”标识的函数插件需从输入区读取路径作为参数.")
|
gr.Markdown("注意:以下“红颜色”标识的函数插件需从输入区读取路径作为参数.")
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
@ -100,7 +100,7 @@ def main():
|
|||||||
with gr.Row():
|
with gr.Row():
|
||||||
with gr.Accordion("点击展开“文件上传区”。上传本地文件可供红色函数插件调用。", open=False) as area_file_up:
|
with gr.Accordion("点击展开“文件上传区”。上传本地文件可供红色函数插件调用。", open=False) as area_file_up:
|
||||||
file_upload = gr.Files(label="任何文件, 但推荐上传压缩文件(zip, tar)", file_count="multiple")
|
file_upload = gr.Files(label="任何文件, 但推荐上传压缩文件(zip, tar)", file_count="multiple")
|
||||||
with gr.Accordion("更换模型 & SysPrompt & 交互界面布局", open=(LAYOUT == "TOP-DOWN")):
|
with gr.Accordion("更换模型 & SysPrompt & 交互界面布局", open=(LAYOUT == "TOP-DOWN"), elem_id="interact-panel"):
|
||||||
system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt)
|
system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt)
|
||||||
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",)
|
||||||
@ -109,7 +109,7 @@ def main():
|
|||||||
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)
|
||||||
|
|
||||||
gr.Markdown(description)
|
gr.Markdown(description)
|
||||||
with gr.Accordion("备选输入区", open=True, visible=False) as area_input_secondary:
|
with gr.Accordion("备选输入区", open=True, visible=False, elem_id="input-panel2") as area_input_secondary:
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
txt2 = gr.Textbox(show_label=False, placeholder="Input question here.", label="输入区2").style(container=False)
|
txt2 = gr.Textbox(show_label=False, placeholder="Input question here.", label="输入区2").style(container=False)
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
@ -185,7 +185,8 @@ def main():
|
|||||||
# 终止按钮的回调函数注册
|
# 终止按钮的回调函数注册
|
||||||
stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
|
stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
|
||||||
stopBtn2.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
|
stopBtn2.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
|
||||||
|
demo.load(lambda: 0, inputs=None, outputs=None, _js='()=>{ChatBotHeight();}')
|
||||||
|
|
||||||
# gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
|
# gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
|
||||||
def auto_opentab_delay():
|
def auto_opentab_delay():
|
||||||
import threading, webbrowser, time
|
import threading, webbrowser, time
|
||||||
|
|||||||
70
theme.py
70
theme.py
@ -82,20 +82,74 @@ def adjust_theme():
|
|||||||
button_cancel_text_color_dark="white",
|
button_cancel_text_color_dark="white",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 别忘了layout的区别
|
||||||
|
js = """
|
||||||
|
<script>
|
||||||
|
|
||||||
|
function ChatBotHeight() {
|
||||||
|
function update_height(){
|
||||||
|
const chatbot = document.querySelector('#gpt-chatbot > div.wrap.svelte-18telvq');
|
||||||
|
const panel1 = document.querySelector('#input-panel');
|
||||||
|
const panel2 = document.querySelector('#basic-panel');
|
||||||
|
const panel3 = document.querySelector('#plugin-panel');
|
||||||
|
const panel4 = document.querySelector('#interact-panel');
|
||||||
|
const panel5 = document.querySelector('#input-panel2');
|
||||||
|
var panel_height_target = panel1.offsetHeight + panel2.offsetHeight + panel3.offsetHeight + panel4.offsetHeight + panel5.offsetHeight + 21;
|
||||||
|
var panel_height_target = parseInt(panel_height_target);
|
||||||
|
var chatbot_height = chatbot.style.height;
|
||||||
|
var chatbot_height = parseInt(chatbot_height);
|
||||||
|
if (panel_height_target!=chatbot_height)
|
||||||
|
{
|
||||||
|
var pixelString = panel_height_target.toString() + 'px';
|
||||||
|
chatbot.style.maxHeight = pixelString; chatbot.style.height = pixelString;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function update_height_slow(){
|
||||||
|
const chatbot = document.querySelector('#gpt-chatbot > div.wrap.svelte-18telvq');
|
||||||
|
const panel1 = document.querySelector('#input-panel');
|
||||||
|
const panel2 = document.querySelector('#basic-panel');
|
||||||
|
const panel3 = document.querySelector('#plugin-panel');
|
||||||
|
const panel4 = document.querySelector('#interact-panel');
|
||||||
|
const panel5 = document.querySelector('#input-panel2');
|
||||||
|
var panel_height_target = panel1.offsetHeight + panel2.offsetHeight + panel3.offsetHeight + panel4.offsetHeight + panel5.offsetHeight + 21;
|
||||||
|
var panel_height_target = parseInt(panel_height_target);
|
||||||
|
var chatbot_height = chatbot.style.height;
|
||||||
|
var chatbot_height = parseInt(chatbot_height);
|
||||||
|
if (panel_height_target!=chatbot_height)
|
||||||
|
{
|
||||||
|
new_panel_height = (panel_height_target - chatbot_height)*0.5 + chatbot_height;
|
||||||
|
if (Math.abs(new_panel_height - panel_height_target) < 10){
|
||||||
|
new_panel_height = panel_height_target;
|
||||||
|
}
|
||||||
|
console.log(chatbot_height, panel_height_target, new_panel_height);
|
||||||
|
var pixelString = new_panel_height.toString() + 'px';
|
||||||
|
chatbot.style.maxHeight = pixelString; chatbot.style.height = pixelString;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
update_height();
|
||||||
|
setInterval(function() {
|
||||||
|
update_height_slow()
|
||||||
|
}, 50); // 每100毫秒执行一次
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
"""
|
||||||
# 添加一个萌萌的看板娘
|
# 添加一个萌萌的看板娘
|
||||||
if ADD_WAIFU:
|
if ADD_WAIFU:
|
||||||
js = """
|
js += """
|
||||||
<script src="file=docs/waifu_plugin/jquery.min.js"></script>
|
<script src="file=docs/waifu_plugin/jquery.min.js"></script>
|
||||||
<script src="file=docs/waifu_plugin/jquery-ui.min.js"></script>
|
<script src="file=docs/waifu_plugin/jquery-ui.min.js"></script>
|
||||||
<script src="file=docs/waifu_plugin/autoload.js"></script>
|
<script src="file=docs/waifu_plugin/autoload.js"></script>
|
||||||
"""
|
"""
|
||||||
gradio_original_template_fn = gr.routes.templates.TemplateResponse
|
gradio_original_template_fn = gr.routes.templates.TemplateResponse
|
||||||
def gradio_new_template_fn(*args, **kwargs):
|
def gradio_new_template_fn(*args, **kwargs):
|
||||||
res = gradio_original_template_fn(*args, **kwargs)
|
res = gradio_original_template_fn(*args, **kwargs)
|
||||||
res.body = res.body.replace(b'</html>', f'{js}</html>'.encode("utf8"))
|
res.body = res.body.replace(b'</html>', f'{js}</html>'.encode("utf8"))
|
||||||
res.init_headers()
|
res.init_headers()
|
||||||
return res
|
return res
|
||||||
gr.routes.templates.TemplateResponse = gradio_new_template_fn # override gradio template
|
gr.routes.templates.TemplateResponse = gradio_new_template_fn # override gradio template
|
||||||
except:
|
except:
|
||||||
set_theme = None
|
set_theme = None
|
||||||
print('gradio版本较旧, 不能自定义字体和颜色')
|
print('gradio版本较旧, 不能自定义字体和颜色')
|
||||||
|
|||||||
@ -36,12 +36,13 @@ class ChatBotWithCookies(list):
|
|||||||
def get_cookies(self):
|
def get_cookies(self):
|
||||||
return self._cookies
|
return self._cookies
|
||||||
|
|
||||||
|
black_list = ['127.0.0.1']
|
||||||
|
|
||||||
def ArgsGeneralWrapper(f):
|
def ArgsGeneralWrapper(f):
|
||||||
"""
|
"""
|
||||||
装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。
|
装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。
|
||||||
"""
|
"""
|
||||||
def decorated(cookies, max_length, llm_model, txt, txt2, top_p, temperature, chatbot, history, system_prompt, plugin_advanced_arg, *args):
|
def decorated(request: gradio.Request, cookies, max_length, llm_model, txt, txt2, top_p, temperature, chatbot, history, system_prompt, plugin_advanced_arg, *args):
|
||||||
txt_passon = txt
|
txt_passon = txt
|
||||||
if txt == "" and txt2 != "": txt_passon = txt2
|
if txt == "" and txt2 != "": txt_passon = txt2
|
||||||
# 引入一个有cookie的chatbot
|
# 引入一个有cookie的chatbot
|
||||||
@ -56,12 +57,17 @@ def ArgsGeneralWrapper(f):
|
|||||||
'top_p':top_p,
|
'top_p':top_p,
|
||||||
'max_length': max_length,
|
'max_length': max_length,
|
||||||
'temperature':temperature,
|
'temperature':temperature,
|
||||||
|
'client_ip': request.client.host,
|
||||||
}
|
}
|
||||||
plugin_kwargs = {
|
plugin_kwargs = {
|
||||||
"advanced_arg": plugin_advanced_arg,
|
"advanced_arg": plugin_advanced_arg,
|
||||||
}
|
}
|
||||||
chatbot_with_cookie = ChatBotWithCookies(cookies)
|
chatbot_with_cookie = ChatBotWithCookies(cookies)
|
||||||
chatbot_with_cookie.write_list(chatbot)
|
chatbot_with_cookie.write_list(chatbot)
|
||||||
|
if llm_kwargs['client_ip'] in black_list:
|
||||||
|
chatbot_with_cookie.append(['IP已封禁, 当前IP黑名单' + str(black_list), 'IP已封禁:' + llm_kwargs['client_ip']])
|
||||||
|
yield from update_ui(chatbot_with_cookie, history, msg='IP已封禁')
|
||||||
|
return # 结束
|
||||||
if cookies.get('lock_plugin', None) is None:
|
if cookies.get('lock_plugin', None) is None:
|
||||||
# 正常状态
|
# 正常状态
|
||||||
yield from f(txt_passon, llm_kwargs, plugin_kwargs, chatbot_with_cookie, history, system_prompt, *args)
|
yield from f(txt_passon, llm_kwargs, plugin_kwargs, chatbot_with_cookie, history, system_prompt, *args)
|
||||||
|
|||||||
Reference in New Issue
Block a user