UP
Browse files
main.py
CHANGED
@@ -69,6 +69,7 @@ def main():
|
|
69 |
with gr.Row():
|
70 |
resetBtn = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm")
|
71 |
stopBtn = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm")
|
|
|
72 |
with gr.Row():
|
73 |
status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {proxy_info}")
|
74 |
with gr.Accordion("基础功能区", open=True) as area_basic_fn:
|
@@ -99,7 +100,10 @@ def main():
|
|
99 |
system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt)
|
100 |
top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
|
101 |
temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
|
102 |
-
|
|
|
|
|
|
|
103 |
gr.Markdown(description)
|
104 |
with gr.Accordion("备选输入区", open=True, visible=False) as area_input_secondary:
|
105 |
with gr.Row():
|
@@ -107,8 +111,9 @@ def main():
|
|
107 |
with gr.Row():
|
108 |
submitBtn2 = gr.Button("提交", variant="primary")
|
109 |
with gr.Row():
|
110 |
-
resetBtn2 = gr.Button("重置", variant="secondary");
|
111 |
-
stopBtn2 = gr.Button("停止", variant="secondary");
|
|
|
112 |
# 功能区显示开关与功能区的互动
|
113 |
def fn_area_visibility(a):
|
114 |
ret = {}
|
@@ -116,11 +121,13 @@ def main():
|
|
116 |
ret.update({area_crazy_fn: gr.update(visible=("函数插件区" in a))})
|
117 |
ret.update({area_input_primary: gr.update(visible=("底部输入区" not in a))})
|
118 |
ret.update({area_input_secondary: gr.update(visible=("底部输入区" in a))})
|
|
|
|
|
119 |
if "底部输入区" in a: ret.update({txt: gr.update(value="")})
|
120 |
return ret
|
121 |
-
checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2] )
|
122 |
# 整理反复出现的控件句柄组合
|
123 |
-
input_combo = [cookies, txt, txt2, top_p, temperature, chatbot, history, system_prompt]
|
124 |
output_combo = [cookies, chatbot, history, status]
|
125 |
predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo)
|
126 |
# 提交按钮、重置按钮
|
@@ -130,24 +137,38 @@ def main():
|
|
130 |
cancel_handles.append(submitBtn2.click(**predict_args))
|
131 |
resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, status])
|
132 |
resetBtn2.click(lambda: ([], [], "已重置"), None, [chatbot, history, status])
|
|
|
|
|
133 |
# 基础功能区的回调函数注册
|
134 |
for k in functional:
|
135 |
click_handle = functional[k]["Button"].click(fn=ArgsGeneralWrapper(predict), inputs=[*input_combo, gr.State(True), gr.State(k)], outputs=output_combo)
|
136 |
cancel_handles.append(click_handle)
|
137 |
# 文件上传区,接收文件后与chatbot的互动
|
138 |
-
file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt])
|
139 |
# 函数插件-固定按钮区
|
140 |
for k in crazy_fns:
|
141 |
if not crazy_fns[k].get("AsButton", True): continue
|
142 |
click_handle = crazy_fns[k]["Button"].click(ArgsGeneralWrapper(crazy_fns[k]["Function"]), [*input_combo, gr.State(PORT)], output_combo)
|
143 |
click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
|
144 |
-
# def expand_file_area(file_upload, area_file_up):
|
145 |
-
# if len(file_upload)>0: return {area_file_up: gr.update(open=True)}
|
146 |
-
# click_handle.then(expand_file_area, [file_upload, area_file_up], [area_file_up])
|
147 |
cancel_handles.append(click_handle)
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
# gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
|
153 |
def auto_opentab_delay():
|
@@ -165,4 +186,4 @@ def main():
|
|
165 |
demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", share=False)
|
166 |
|
167 |
if __name__ == "__main__":
|
168 |
-
main()
|
|
|
69 |
with gr.Row():
|
70 |
resetBtn = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm")
|
71 |
stopBtn = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm")
|
72 |
+
clearBtn = gr.Button("清除", variant="secondary", visible=False); clearBtn.style(size="sm")
|
73 |
with gr.Row():
|
74 |
status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {proxy_info}")
|
75 |
with gr.Accordion("基础功能区", open=True) as area_basic_fn:
|
|
|
100 |
system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt)
|
101 |
top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
|
102 |
temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
|
103 |
+
max_length_sl = gr.Slider(minimum=256, maximum=4096, value=512, step=1, interactive=True, label="MaxLength",)
|
104 |
+
checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区", "输入清除键"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区")
|
105 |
+
md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="").style(container=False)
|
106 |
+
|
107 |
gr.Markdown(description)
|
108 |
with gr.Accordion("备选输入区", open=True, visible=False) as area_input_secondary:
|
109 |
with gr.Row():
|
|
|
111 |
with gr.Row():
|
112 |
submitBtn2 = gr.Button("提交", variant="primary")
|
113 |
with gr.Row():
|
114 |
+
resetBtn2 = gr.Button("重置", variant="secondary"); resetBtn2.style(size="sm")
|
115 |
+
stopBtn2 = gr.Button("停止", variant="secondary"); stopBtn2.style(size="sm")
|
116 |
+
clearBtn2 = gr.Button("清除", variant="secondary", visible=False); clearBtn.style(size="sm")
|
117 |
# 功能区显示开关与功能区的互动
|
118 |
def fn_area_visibility(a):
|
119 |
ret = {}
|
|
|
121 |
ret.update({area_crazy_fn: gr.update(visible=("函数插件区" in a))})
|
122 |
ret.update({area_input_primary: gr.update(visible=("底部输入区" not in a))})
|
123 |
ret.update({area_input_secondary: gr.update(visible=("底部输入区" in a))})
|
124 |
+
ret.update({clearBtn: gr.update(visible=("输入清除键" in a))})
|
125 |
+
ret.update({clearBtn2: gr.update(visible=("输入清除键" in a))})
|
126 |
if "底部输入区" in a: ret.update({txt: gr.update(value="")})
|
127 |
return ret
|
128 |
+
checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2, clearBtn, clearBtn2] )
|
129 |
# 整理反复出现的控件句柄组合
|
130 |
+
input_combo = [cookies, max_length_sl, md_dropdown, txt, txt2, top_p, temperature, chatbot, history, system_prompt]
|
131 |
output_combo = [cookies, chatbot, history, status]
|
132 |
predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo)
|
133 |
# 提交按钮、重置按钮
|
|
|
137 |
cancel_handles.append(submitBtn2.click(**predict_args))
|
138 |
resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, status])
|
139 |
resetBtn2.click(lambda: ([], [], "已重置"), None, [chatbot, history, status])
|
140 |
+
clearBtn.click(lambda: ("",""), None, [txt, txt2])
|
141 |
+
clearBtn2.click(lambda: ("",""), None, [txt, txt2])
|
142 |
# 基础功能区的回调函数注册
|
143 |
for k in functional:
|
144 |
click_handle = functional[k]["Button"].click(fn=ArgsGeneralWrapper(predict), inputs=[*input_combo, gr.State(True), gr.State(k)], outputs=output_combo)
|
145 |
cancel_handles.append(click_handle)
|
146 |
# 文件上传区,接收文件后与chatbot的互动
|
147 |
+
file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt, txt2, checkboxes], [chatbot, txt, txt2])
|
148 |
# 函数插件-固定按钮区
|
149 |
for k in crazy_fns:
|
150 |
if not crazy_fns[k].get("AsButton", True): continue
|
151 |
click_handle = crazy_fns[k]["Button"].click(ArgsGeneralWrapper(crazy_fns[k]["Function"]), [*input_combo, gr.State(PORT)], output_combo)
|
152 |
click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
|
|
|
|
|
|
|
153 |
cancel_handles.append(click_handle)
|
154 |
+
# 函数插件-下拉菜单与随变按钮的互动
|
155 |
+
def on_dropdown_changed(k):
|
156 |
+
variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
|
157 |
+
return {switchy_bt: gr.update(value=k, variant=variant)}
|
158 |
+
dropdown.select(on_dropdown_changed, [dropdown], [switchy_bt] )
|
159 |
+
# 随变按钮的回调函数注册
|
160 |
+
def route(k, *args, **kwargs):
|
161 |
+
if k in [r"打开插件列表", r"请先从插件列表中选择"]: return
|
162 |
+
yield from ArgsGeneralWrapper(crazy_fns[k]["Function"])(*args, **kwargs)
|
163 |
+
click_handle = switchy_bt.click(route,[switchy_bt, *input_combo, gr.State(PORT)], output_combo)
|
164 |
+
click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
|
165 |
+
# def expand_file_area(file_upload, area_file_up):
|
166 |
+
# if len(file_upload)>0: return {area_file_up: gr.update(open=True)}
|
167 |
+
# click_handle.then(expand_file_area, [file_upload, area_file_up], [area_file_up])
|
168 |
+
cancel_handles.append(click_handle)
|
169 |
+
# 终止按钮的回调函数注册
|
170 |
+
stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
|
171 |
+
stopBtn2.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
|
172 |
|
173 |
# gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
|
174 |
def auto_opentab_delay():
|
|
|
186 |
demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", share=False)
|
187 |
|
188 |
if __name__ == "__main__":
|
189 |
+
main()
|