Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	将高级参数输入通用化(默认隐藏),应用到所有的下拉菜单函数插件中
Browse files- crazy_functional.py +2 -0
- crazy_functions/解析项目源代码.py +1 -1
- main.py +16 -20
- toolbox.py +4 -5
    	
        crazy_functional.py
    CHANGED
    
    | @@ -196,6 +196,8 @@ def get_crazy_functions(): | |
| 196 | 
             
                    "解析任意code项目": {
         | 
| 197 | 
             
                        "Color": "stop",
         | 
| 198 | 
             
                        "AsButton": False,
         | 
|  | |
|  | |
| 199 | 
             
                        "Function": HotReload(解析任意code项目)
         | 
| 200 | 
             
                    },
         | 
| 201 | 
             
                })
         | 
|  | |
| 196 | 
             
                    "解析任意code项目": {
         | 
| 197 | 
             
                        "Color": "stop",
         | 
| 198 | 
             
                        "AsButton": False,
         | 
| 199 | 
            +
                        "AdvancedArgs": True, # 调用时,唤起高级参数输入区(默认False)
         | 
| 200 | 
            +
                        "ArgsReminder": "输入时用逗号隔开, `*`代表通配符, 加了`^`代表不匹配<br>例如: `*.c, ^*.cpp, config.toml, ^README.md`", # 高级参数输入区的显示提示
         | 
| 201 | 
             
                        "Function": HotReload(解析任意code项目)
         | 
| 202 | 
             
                    },
         | 
| 203 | 
             
                })
         | 
    	
        crazy_functions/解析项目源代码.py
    CHANGED
    
    | @@ -268,7 +268,7 @@ def 解析一个CSharp项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, s | |
| 268 |  | 
| 269 | 
             
            @CatchException
         | 
| 270 | 
             
            def 解析任意code项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
         | 
| 271 | 
            -
                txt_pattern = plugin_kwargs.get(" | 
| 272 | 
             
                txt_pattern = txt_pattern.replace(",", ",")
         | 
| 273 | 
             
                # 将要匹配的模式(例如: *.c, *.cpp, *.py, config.toml)
         | 
| 274 | 
             
                pattern_include = [_.lstrip(" ,").rstrip(" ,") for _ in txt_pattern.split(",") if _ != "" and not _.strip().startswith("^")]
         | 
|  | |
| 268 |  | 
| 269 | 
             
            @CatchException
         | 
| 270 | 
             
            def 解析任意code项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
         | 
| 271 | 
            +
                txt_pattern = plugin_kwargs.get("advanced_arg")
         | 
| 272 | 
             
                txt_pattern = txt_pattern.replace(",", ",")
         | 
| 273 | 
             
                # 将要匹配的模式(例如: *.c, *.cpp, *.py, config.toml)
         | 
| 274 | 
             
                pattern_include = [_.lstrip(" ,").rstrip(" ,") for _ in txt_pattern.split(",") if _ != "" and not _.strip().startswith("^")]
         | 
    	
        main.py
    CHANGED
    
    | @@ -85,21 +85,15 @@ def main(): | |
| 85 | 
             
                                        variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
         | 
| 86 | 
             
                                        crazy_fns[k]["Button"] = gr.Button(k, variant=variant)
         | 
| 87 | 
             
                                        crazy_fns[k]["Button"].style(size="sm")
         | 
| 88 | 
            -
                                with gr.Row():
         | 
| 89 | 
            -
                                    with gr.Accordion("解析任意code项目", open=False):
         | 
| 90 | 
            -
                                        gr.Markdown("输入时用逗号隔开, `*`代表通配符, 加了`^`代表不匹配<br>例如: `*.c, ^*.cpp, config.toml, ^README.md`")
         | 
| 91 | 
            -
                                        with gr.Row():
         | 
| 92 | 
            -
                                            txt_pattern = gr.Textbox(show_label=False, placeholder="输入框为空则代表匹配所有文件").style(container=False)
         | 
| 93 | 
            -
                                        code_plugin_name = "解析任意code项目"
         | 
| 94 | 
            -
                                        variant = crazy_fns[code_plugin_name]["Color"] if "Color" in crazy_fns[code_plugin_name] else "secondary"
         | 
| 95 | 
            -
                                        crazy_fns[code_plugin_name]["Button"] = gr.Button(code_plugin_name, variant=variant)
         | 
| 96 | 
            -
                                        crazy_fns[code_plugin_name]["Button"].style(size="sm")
         | 
| 97 | 
             
                                with gr.Row():
         | 
| 98 | 
             
                                    with gr.Accordion("更多函数插件", open=True):
         | 
| 99 | 
             
                                        dropdown_fn_list = [k for k in crazy_fns.keys() if not crazy_fns[k].get("AsButton", True)]
         | 
| 100 | 
            -
                                        with gr. | 
| 101 | 
             
                                            dropdown = gr.Dropdown(dropdown_fn_list, value=r"打开插件列表", label="").style(container=False)
         | 
| 102 | 
            -
                                        with gr. | 
|  | |
|  | |
|  | |
| 103 | 
             
                                            switchy_bt = gr.Button(r"请先从插件列表中选择", variant="secondary")
         | 
| 104 | 
             
                                with gr.Row():
         | 
| 105 | 
             
                                    with gr.Accordion("点击展开“文件上传区”。上传本地文件可供红色函数插件调用。", open=False) as area_file_up:
         | 
| @@ -109,7 +103,7 @@ def main(): | |
| 109 | 
             
                                top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
         | 
| 110 | 
             
                                temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
         | 
| 111 | 
             
                                max_length_sl = gr.Slider(minimum=256, maximum=4096, value=512, step=1, interactive=True, label="Local LLM MaxLength",)
         | 
| 112 | 
            -
                                checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区", "输入清除键"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区")
         | 
| 113 | 
             
                                md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="更换LLM模型/请求源").style(container=False)
         | 
| 114 |  | 
| 115 | 
             
                                gr.Markdown(description)
         | 
| @@ -131,11 +125,12 @@ def main(): | |
| 131 | 
             
                        ret.update({area_input_secondary: gr.update(visible=("底部输入区" in a))})
         | 
| 132 | 
             
                        ret.update({clearBtn: gr.update(visible=("输入清除键" in a))})
         | 
| 133 | 
             
                        ret.update({clearBtn2: gr.update(visible=("输入清除键" in a))})
         | 
|  | |
| 134 | 
             
                        if "底部输入区" in a: ret.update({txt: gr.update(value="")})
         | 
| 135 | 
             
                        return ret
         | 
| 136 | 
            -
                    checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2, clearBtn, clearBtn2] )
         | 
| 137 | 
             
                    # 整理反复出现的控件句柄组合
         | 
| 138 | 
            -
                    input_combo = [cookies, max_length_sl, md_dropdown, txt, txt2, top_p, temperature, chatbot, history, system_prompt,  | 
| 139 | 
             
                    output_combo = [cookies, chatbot, history, status]
         | 
| 140 | 
             
                    predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo)
         | 
| 141 | 
             
                    # 提交按钮、重置按钮
         | 
| @@ -159,15 +154,16 @@ def main(): | |
| 159 | 
             
                        click_handle = crazy_fns[k]["Button"].click(ArgsGeneralWrapper(crazy_fns[k]["Function"]), [*input_combo, gr.State(PORT)], output_combo)
         | 
| 160 | 
             
                        click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
         | 
| 161 | 
             
                        cancel_handles.append(click_handle)
         | 
| 162 | 
            -
                    # 函数插件-解析任意code项目
         | 
| 163 | 
            -
                    click_handle = crazy_fns[code_plugin_name]["Button"].click(ArgsGeneralWrapper(crazy_fns[code_plugin_name]["Function"]), [*input_combo, gr.State(PORT)], output_combo)
         | 
| 164 | 
            -
                    click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
         | 
| 165 | 
            -
                    cancel_handles.append(click_handle)
         | 
| 166 | 
             
                    # 函数插件-下拉菜单与随变按钮的互动
         | 
| 167 | 
             
                    def on_dropdown_changed(k):
         | 
| 168 | 
             
                        variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
         | 
| 169 | 
            -
                         | 
| 170 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 171 | 
             
                    def on_md_dropdown_changed(k):
         | 
| 172 | 
             
                        return {chatbot: gr.update(label="当前模型:"+k)}
         | 
| 173 | 
             
                    md_dropdown.select(on_md_dropdown_changed, [md_dropdown], [chatbot] )
         | 
|  | |
| 85 | 
             
                                        variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
         | 
| 86 | 
             
                                        crazy_fns[k]["Button"] = gr.Button(k, variant=variant)
         | 
| 87 | 
             
                                        crazy_fns[k]["Button"].style(size="sm")
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 88 | 
             
                                with gr.Row():
         | 
| 89 | 
             
                                    with gr.Accordion("更多函数插件", open=True):
         | 
| 90 | 
             
                                        dropdown_fn_list = [k for k in crazy_fns.keys() if not crazy_fns[k].get("AsButton", True)]
         | 
| 91 | 
            +
                                        with gr.Row():
         | 
| 92 | 
             
                                            dropdown = gr.Dropdown(dropdown_fn_list, value=r"打开插件列表", label="").style(container=False)
         | 
| 93 | 
            +
                                        with gr.Row():
         | 
| 94 | 
            +
                                            with gr.Accordion("高级函数插件参数区:", open=True, visible=False) as area_fn_kwargs:
         | 
| 95 | 
            +
                                                plugin_advanced_arg = gr.Textbox(show_label=False, placeholder="输入框为空则代表匹配所有文件").style(container=False)
         | 
| 96 | 
            +
                                        with gr.Row():
         | 
| 97 | 
             
                                            switchy_bt = gr.Button(r"请先从插件列表中选择", variant="secondary")
         | 
| 98 | 
             
                                with gr.Row():
         | 
| 99 | 
             
                                    with gr.Accordion("点击展开“文件上传区”。上传本地文件可供红色函数插件调用。", open=False) as area_file_up:
         | 
|  | |
| 103 | 
             
                                top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
         | 
| 104 | 
             
                                temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
         | 
| 105 | 
             
                                max_length_sl = gr.Slider(minimum=256, maximum=4096, value=512, step=1, interactive=True, label="Local LLM MaxLength",)
         | 
| 106 | 
            +
                                checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区", "输入清除键", "插件参数区"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区")
         | 
| 107 | 
             
                                md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="更换LLM模型/请求源").style(container=False)
         | 
| 108 |  | 
| 109 | 
             
                                gr.Markdown(description)
         | 
|  | |
| 125 | 
             
                        ret.update({area_input_secondary: gr.update(visible=("底部输入区" in a))})
         | 
| 126 | 
             
                        ret.update({clearBtn: gr.update(visible=("输入清除键" in a))})
         | 
| 127 | 
             
                        ret.update({clearBtn2: gr.update(visible=("输入清除键" in a))})
         | 
| 128 | 
            +
                        ret.update({area_fn_kwargs: gr.update(visible=("插件参数区" in a))})
         | 
| 129 | 
             
                        if "底部输入区" in a: ret.update({txt: gr.update(value="")})
         | 
| 130 | 
             
                        return ret
         | 
| 131 | 
            +
                    checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2, clearBtn, clearBtn2, area_fn_kwargs] )
         | 
| 132 | 
             
                    # 整理反复出现的控件句柄组合
         | 
| 133 | 
            +
                    input_combo = [cookies, max_length_sl, md_dropdown, txt, txt2, top_p, temperature, chatbot, history, system_prompt, plugin_advanced_arg]
         | 
| 134 | 
             
                    output_combo = [cookies, chatbot, history, status]
         | 
| 135 | 
             
                    predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo)
         | 
| 136 | 
             
                    # 提交按钮、重置按钮
         | 
|  | |
| 154 | 
             
                        click_handle = crazy_fns[k]["Button"].click(ArgsGeneralWrapper(crazy_fns[k]["Function"]), [*input_combo, gr.State(PORT)], output_combo)
         | 
| 155 | 
             
                        click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
         | 
| 156 | 
             
                        cancel_handles.append(click_handle)
         | 
|  | |
|  | |
|  | |
|  | |
| 157 | 
             
                    # 函数插件-下拉菜单与随变按钮的互动
         | 
| 158 | 
             
                    def on_dropdown_changed(k):
         | 
| 159 | 
             
                        variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
         | 
| 160 | 
            +
                        ret = {switchy_bt: gr.update(value=k, variant=variant)}
         | 
| 161 | 
            +
                        if crazy_fns[k].get("AdvancedArgs", False): # 是否唤起高级插件参数区
         | 
| 162 | 
            +
                            ret.update({area_fn_kwargs: gr.update(visible=True,  label=f"插件{k}的高级参数说明:" + crazy_fns[k].get("ArgsReminder", [f"没有提供高级参数功能说明"]))})
         | 
| 163 | 
            +
                        else:
         | 
| 164 | 
            +
                            ret.update({area_fn_kwargs: gr.update(visible=False, label=f"插件{k}不需要高级参数。")})
         | 
| 165 | 
            +
                        return ret
         | 
| 166 | 
            +
                    dropdown.select(on_dropdown_changed, [dropdown], [switchy_bt, area_fn_kwargs] )
         | 
| 167 | 
             
                    def on_md_dropdown_changed(k):
         | 
| 168 | 
             
                        return {chatbot: gr.update(label="当前模型:"+k)}
         | 
| 169 | 
             
                    md_dropdown.select(on_md_dropdown_changed, [md_dropdown], [chatbot] )
         | 
    	
        toolbox.py
    CHANGED
    
    | @@ -24,7 +24,7 @@ def ArgsGeneralWrapper(f): | |
| 24 | 
             
                """
         | 
| 25 | 
             
                装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。
         | 
| 26 | 
             
                """
         | 
| 27 | 
            -
                def decorated(cookies, max_length, llm_model, txt, txt2, top_p, temperature, chatbot, history, system_prompt,  | 
| 28 | 
             
                    txt_passon = txt
         | 
| 29 | 
             
                    if txt == "" and txt2 != "": txt_passon = txt2
         | 
| 30 | 
             
                    # 引入一个有cookie的chatbot
         | 
| @@ -39,10 +39,9 @@ def ArgsGeneralWrapper(f): | |
| 39 | 
             
                        'max_length': max_length,
         | 
| 40 | 
             
                        'temperature':temperature,
         | 
| 41 | 
             
                    }
         | 
| 42 | 
            -
                     | 
| 43 | 
            -
             | 
| 44 | 
            -
                     | 
| 45 | 
            -
                    plugin_kwargs = dict(txt_pattern = txt_pattern)
         | 
| 46 | 
             
                    chatbot_with_cookie = ChatBotWithCookies(cookies)
         | 
| 47 | 
             
                    chatbot_with_cookie.write_list(chatbot)
         | 
| 48 | 
             
                    yield from f(txt_passon, llm_kwargs, plugin_kwargs, chatbot_with_cookie, history, system_prompt, *args)
         | 
|  | |
| 24 | 
             
                """
         | 
| 25 | 
             
                装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。
         | 
| 26 | 
             
                """
         | 
| 27 | 
            +
                def decorated(cookies, max_length, llm_model, txt, txt2, top_p, temperature, chatbot, history, system_prompt, plugin_advanced_arg, *args):
         | 
| 28 | 
             
                    txt_passon = txt
         | 
| 29 | 
             
                    if txt == "" and txt2 != "": txt_passon = txt2
         | 
| 30 | 
             
                    # 引入一个有cookie的chatbot
         | 
|  | |
| 39 | 
             
                        'max_length': max_length,
         | 
| 40 | 
             
                        'temperature':temperature,
         | 
| 41 | 
             
                    }
         | 
| 42 | 
            +
                    plugin_kwargs = {
         | 
| 43 | 
            +
                        "advanced_arg": plugin_advanced_arg,
         | 
| 44 | 
            +
                    }
         | 
|  | |
| 45 | 
             
                    chatbot_with_cookie = ChatBotWithCookies(cookies)
         | 
| 46 | 
             
                    chatbot_with_cookie.write_list(chatbot)
         | 
| 47 | 
             
                    yield from f(txt_passon, llm_kwargs, plugin_kwargs, chatbot_with_cookie, history, system_prompt, *args)
         |