qingxu99 commited on
Commit
c96a253
1 Parent(s): 24780ee

支持更多界面布局的切换

Browse files
Files changed (4) hide show
  1. config.py +3 -0
  2. main.py +40 -22
  3. theme.py +1 -1
  4. toolbox.py +6 -0
config.py CHANGED
@@ -24,6 +24,9 @@ else:
24
  # 对话窗的高度
25
  CHATBOT_HEIGHT = 1115
26
 
 
 
 
27
  # 发送请求到OpenAI后,等待多久判定为超时
28
  TIMEOUT_SECONDS = 25
29
 
 
24
  # 对话窗的高度
25
  CHATBOT_HEIGHT = 1115
26
 
27
+ # 窗口布局
28
+ LAYOUT = "LEFT-RIGHT" # "LEFT-RIGHT"(左右布局) # "TOP-DOWN"(上下布局)
29
+
30
  # 发送请求到OpenAI后,等待多久判定为超时
31
  TIMEOUT_SECONDS = 25
32
 
main.py CHANGED
@@ -1,11 +1,11 @@
1
  import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
2
  import gradio as gr
3
  from predict import predict
4
- from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper
5
 
6
  # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
7
- proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT = \
8
- get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT')
9
 
10
  # 如果WEB_PORT是-1, 则随机选取WEB端口
11
  PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
@@ -41,25 +41,32 @@ set_theme = adjust_theme()
41
  from check_proxy import check_proxy, auto_update
42
  proxy_info = check_proxy(proxies)
43
 
 
 
 
 
 
 
44
 
45
  cancel_handles = []
46
  with gr.Blocks(theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
47
  gr.HTML(title_html)
48
- with gr.Row().style(equal_height=True):
49
- with gr.Column(scale=2):
50
  chatbot = gr.Chatbot()
51
  chatbot.style(height=CHATBOT_HEIGHT)
52
  history = gr.State([])
53
- with gr.Column(scale=1):
54
- with gr.Row():
55
- txt = gr.Textbox(show_label=False, placeholder="Input question here.").style(container=False)
56
- with gr.Row():
57
- submitBtn = gr.Button("提交", variant="primary")
58
- with gr.Row():
59
- resetBtn = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm")
60
- stopBtn = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm")
61
- with gr.Row():
62
- status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {proxy_info}")
 
63
  with gr.Accordion("基础功能区", open=True) as area_basic_fn:
64
  with gr.Row():
65
  for k in functional:
@@ -67,12 +74,13 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False, css=advanced_css) as de
67
  functional[k]["Button"] = gr.Button(k, variant=variant)
68
  with gr.Accordion("函数插件区", open=True) as area_crazy_fn:
69
  with gr.Row():
70
- gr.Markdown("注意:以下“红颜色”标识的函数插件需从input区读取路径作为参数.")
71
  with gr.Row():
72
  for k in crazy_fns:
73
  if not crazy_fns[k].get("AsButton", True): continue
74
  variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
75
  crazy_fns[k]["Button"] = gr.Button(k, variant=variant)
 
76
  with gr.Row():
77
  with gr.Accordion("更多函数插件", open=True):
78
  dropdown_fn_list = [k for k in crazy_fns.keys() if not crazy_fns[k].get("AsButton", True)]
@@ -83,31 +91,41 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False, css=advanced_css) as de
83
  with gr.Row():
84
  with gr.Accordion("点击展开“文件上传区”。上传本地文件可供红色函数插件调用。", open=False) as area_file_up:
85
  file_upload = gr.Files(label="任何文件, 但推荐上传压缩文件(zip, tar)", file_count="multiple")
86
- with gr.Accordion("展开SysPrompt & 交互界面布局 & Github地址", open=False):
87
  system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt)
88
  top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
89
  temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
90
- checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "输入区2"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区")
91
  gr.Markdown(description)
92
- with gr.Accordion("输入区", open=True, visible=False) as input_crazy_fn:
93
  with gr.Row():
94
  txt2 = gr.Textbox(show_label=False, placeholder="Input question here.", label="输入区2").style(container=False)
95
-
 
 
 
 
96
  # 功能区显示开关与功能区的互动
97
  def fn_area_visibility(a):
98
  ret = {}
99
  ret.update({area_basic_fn: gr.update(visible=("基础功能区" in a))})
100
  ret.update({area_crazy_fn: gr.update(visible=("函数插件区" in a))})
 
 
 
101
  return ret
102
- checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn] )
103
  # 整理反复出现的控件句柄组合
104
  input_combo = [txt, txt2, top_p, temperature, chatbot, history, system_prompt]
105
  output_combo = [chatbot, history, status]
106
  predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo)
107
  # 提交按钮、重置按钮
108
  cancel_handles.append(txt.submit(**predict_args))
 
109
  cancel_handles.append(submitBtn.click(**predict_args))
 
110
  resetBtn.click(lambda: ([], [], "已重置"), None, output_combo)
 
111
  # 基础功能区的回调函数注册
112
  for k in functional:
113
  click_handle = functional[k]["Button"].click(fn=ArgsGeneralWrapper(predict), inputs=[*input_combo, gr.State(True), gr.State(k)], outputs=output_combo)
@@ -137,7 +155,7 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False, css=advanced_css) as de
137
  cancel_handles.append(click_handle)
138
  # 终止按钮的回调函数注册
139
  stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
140
-
141
  # gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
142
  def auto_opentab_delay():
143
  import threading, webbrowser, time
 
1
  import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
2
  import gradio as gr
3
  from predict import predict
4
+ from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, DummyWith
5
 
6
  # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
7
+ proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT = \
8
+ get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT')
9
 
10
  # 如果WEB_PORT是-1, 则随机选取WEB端口
11
  PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
 
41
  from check_proxy import check_proxy, auto_update
42
  proxy_info = check_proxy(proxies)
43
 
44
+ gr_L1 = lambda: gr.Row().style()
45
+ gr_L2 = lambda scale: gr.Column(scale=scale)
46
+ if LAYOUT == "TOP-DOWN":
47
+ gr_L1 = lambda: DummyWith()
48
+ gr_L2 = lambda scale: gr.Row()
49
+ CHATBOT_HEIGHT /= 2
50
 
51
  cancel_handles = []
52
  with gr.Blocks(theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
53
  gr.HTML(title_html)
54
+ with gr_L1():
55
+ with gr_L2(scale=2):
56
  chatbot = gr.Chatbot()
57
  chatbot.style(height=CHATBOT_HEIGHT)
58
  history = gr.State([])
59
+ with gr_L2(scale=1):
60
+ with gr.Accordion("输入区", open=True) as area_input_primary:
61
+ with gr.Row():
62
+ txt = gr.Textbox(show_label=False, placeholder="Input question here.").style(container=False)
63
+ with gr.Row():
64
+ submitBtn = gr.Button("提交", variant="primary")
65
+ with gr.Row():
66
+ resetBtn = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm")
67
+ stopBtn = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm")
68
+ with gr.Row():
69
+ status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {proxy_info}")
70
  with gr.Accordion("基础功能区", open=True) as area_basic_fn:
71
  with gr.Row():
72
  for k in functional:
 
74
  functional[k]["Button"] = gr.Button(k, variant=variant)
75
  with gr.Accordion("函数插件区", open=True) as area_crazy_fn:
76
  with gr.Row():
77
+ gr.Markdown("注意:以下“红颜色”标识的函数插件需从输入区读取路径作为参数.")
78
  with gr.Row():
79
  for k in crazy_fns:
80
  if not crazy_fns[k].get("AsButton", True): continue
81
  variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
82
  crazy_fns[k]["Button"] = gr.Button(k, variant=variant)
83
+ crazy_fns[k]["Button"].style(size="sm")
84
  with gr.Row():
85
  with gr.Accordion("更多函数插件", open=True):
86
  dropdown_fn_list = [k for k in crazy_fns.keys() if not crazy_fns[k].get("AsButton", True)]
 
91
  with gr.Row():
92
  with gr.Accordion("点击展开“文件上传区”。上传本地文件可供红色函数插件调用。", open=False) as area_file_up:
93
  file_upload = gr.Files(label="任何文件, 但推荐上传压缩文件(zip, tar)", file_count="multiple")
94
+ with gr.Accordion("展开SysPrompt & 交互界面布�� & Github地址", open=(LAYOUT == "TOP-DOWN")):
95
  system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt)
96
  top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
97
  temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
98
+ checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区")
99
  gr.Markdown(description)
100
+ with gr.Accordion("备选输入区", open=True, visible=False) as area_input_secondary:
101
  with gr.Row():
102
  txt2 = gr.Textbox(show_label=False, placeholder="Input question here.", label="输入区2").style(container=False)
103
+ with gr.Row():
104
+ submitBtn2 = gr.Button("提交", variant="primary")
105
+ with gr.Row():
106
+ resetBtn2 = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm")
107
+ stopBtn2 = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm")
108
  # 功能区显示开关与功能区的互动
109
  def fn_area_visibility(a):
110
  ret = {}
111
  ret.update({area_basic_fn: gr.update(visible=("基础功能区" in a))})
112
  ret.update({area_crazy_fn: gr.update(visible=("函数插件区" in a))})
113
+ ret.update({area_input_primary: gr.update(visible=("底部输入区" not in a))})
114
+ ret.update({area_input_secondary: gr.update(visible=("底部输入区" in a))})
115
+ if "底部输入区" in a: ret.update({txt: gr.update(value="")})
116
  return ret
117
+ checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2] )
118
  # 整理反复出现的控件句柄组合
119
  input_combo = [txt, txt2, top_p, temperature, chatbot, history, system_prompt]
120
  output_combo = [chatbot, history, status]
121
  predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo)
122
  # 提交按钮、重置按钮
123
  cancel_handles.append(txt.submit(**predict_args))
124
+ cancel_handles.append(txt2.submit(**predict_args))
125
  cancel_handles.append(submitBtn.click(**predict_args))
126
+ cancel_handles.append(submitBtn2.click(**predict_args))
127
  resetBtn.click(lambda: ([], [], "已重置"), None, output_combo)
128
+ resetBtn2.click(lambda: ([], [], "已重置"), None, output_combo)
129
  # 基础功能区的回调函数注册
130
  for k in functional:
131
  click_handle = functional[k]["Button"].click(fn=ArgsGeneralWrapper(predict), inputs=[*input_combo, gr.State(True), gr.State(k)], outputs=output_combo)
 
155
  cancel_handles.append(click_handle)
156
  # 终止按钮的回调函数注册
157
  stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
158
+ stopBtn2.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
159
  # gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
160
  def auto_opentab_delay():
161
  import threading, webbrowser, time
theme.py CHANGED
@@ -26,7 +26,7 @@ import gradio as gr
26
 
27
  def adjust_theme():
28
  try:
29
- color_er = gr.themes.utils.colors.pink
30
  set_theme = gr.themes.Default(
31
  primary_hue=gr.themes.utils.colors.orange,
32
  neutral_hue=gr.themes.utils.colors.gray,
 
26
 
27
  def adjust_theme():
28
  try:
29
+ color_er = gr.themes.utils.colors.fuchsia
30
  set_theme = gr.themes.Default(
31
  primary_hue=gr.themes.utils.colors.orange,
32
  neutral_hue=gr.themes.utils.colors.gray,
toolbox.py CHANGED
@@ -354,3 +354,9 @@ def clear_line_break(txt):
354
  txt = txt.replace(' ', ' ')
355
  txt = txt.replace(' ', ' ')
356
  return txt
 
 
 
 
 
 
 
354
  txt = txt.replace(' ', ' ')
355
  txt = txt.replace(' ', ' ')
356
  return txt
357
+
358
+ class DummyWith():
359
+ def __enter__(self):
360
+ return self
361
+ def __exit__(self, exc_type, exc_value, traceback):
362
+ return