# coding=utf-8
from UI工具 import *
import gradio as gr



webui_title = """▶▶▶▶智能菜单系统◀◀◀◀"""

with gr.Blocks() as demo:
    gr.Markdown(webui_title)
    with gr.Tab("菜单推荐"):
        with gr.Tab("基于知识库的推荐"):
            with gr.Row():
                with gr.Column(scale=10):
                    chatbot = gr.Chatbot([[None,zhishiku_init1]],label='基于知识库的菜单推荐',show_label=True,height=850)
                    query = gr.Textbox(show_label=True, label="请输入你的喜好，按回车提交")

                with gr.Column(scale=5):
                    knowledge_set = gr.Accordion("知识库更新", visible=True,open=False)
                    answer_set = gr.Accordion("回答参数设置", visible=True,open=False)
                    with knowledge_set:
                        O_data_path = gr.Textbox(label="数据储存地址",
                                                 placeholder=args.original_data_path,
                                                 lines=1,
                                                 interactive=True,
                                                 visible=True)

                        P_data_path = gr.Textbox(label="向量库保存地址",
                                                 placeholder=args.preprocessed_data_path,
                                                 lines=1,
                                                 interactive=True,
                                                 visible=True)

                        emb_name = gr.Textbox(label="embedding模型",
                                              placeholder='默认模型：'+args.embeddings_model_name,
                                              lines=1,
                                              interactive=True,
                                              visible=True)
                        chunk_size = gr.Number(value=args.chunk_size,
                                                 label="数据切分最大长度",
                                                 interactive=True,
                                                 visible=True,
                                                 minimum=1,
                                                 precision=0)
                        chunk_overlap = gr.Number(value=args.chunk_overlap,
                                               label="数据片段最大重合",
                                               interactive=True,
                                               visible=True,
                                               minimum=0,
                                               precision=0)
                        chunk_size.change(fn=change_chunk_size,
                                             inputs=[chunk_size],
                                             outputs=[])
                        chunk_overlap.change(fn=change_chunk_overlap,
                                           inputs=[chunk_overlap],
                                           outputs=[])

                        db_update = gr.Button(value="更新知识库", visible=True)

                        O_data_path.change(fn=change_data_path,
                                           inputs=[O_data_path],
                                           outputs=[])
                        P_data_path.change(fn=change_P_data_path,
                                           inputs=[P_data_path],
                                           outputs=[])
                        emb_name.change(fn=change_emb_name,
                                        inputs=[emb_name],
                                        outputs=[])
                    db_update.click(fn=db_update_click1,
                                    inputs=[chatbot],
                                    outputs=[chatbot])
                    with answer_set:
                        search_top_k = gr.Number(value=args.topk_1,
                                                 label="获取知识库内容条数",
                                                 interactive=True,
                                                 visible=True,
                                                 minimum=1,
                                                 maximum=50,
                                                 precision=0)
                        model_name = gr.Textbox(label="模型名字",
                                                placeholder=args.pretrained_model_name,
                                                lines=1,
                                                interactive=True,
                                                visible=True)
                        model_top_k = gr.Number(value=args.topk,
                                                label="模型回答tok",
                                                interactive=True,
                                                visible=True,
                                                minimum=1,
                                                maximum=50,
                                                precision=0)
                        model_temperature = gr.Slider(0, 1,
                                                    value=args.temperature,
                                                    step=0.01,
                                                    label="模型回答温度",
                                                    interactive=True)

                        search_top_k.change(fn=change_search_top_k,
                                            inputs=[search_top_k],
                                            outputs=[])
                        model_top_k.change(fn=change_model_top_k,
                                           inputs=[model_top_k],
                                           outputs=[])
                        model_temperature.change(fn=change_model_temperature,
                                                 inputs=[model_temperature],
                                                 outputs=[])
                        model_name.change(fn=change_model_name,
                                          inputs=[model_name],
                                          outputs=[])
                        query.submit(fn=get_answer,
                                     inputs=[query, chatbot],
                                     outputs=[chatbot, query])
                    clear = gr.ClearButton([chatbot], value='清除记录')
                    clear_cash = gr.Button(value="清理显存", visible=True)
                    clear_cash.click(fn=clean_memory,
                                     inputs=[chatbot],
                                     outputs=[chatbot])

        with gr.Tab("基于菜单的推荐"):
            with gr.Row():
                with gr.Column(scale=10):
                    chatbot = gr.Chatbot([[None,caidan_init0]],label='基于菜单的推荐', show_label=True,height=850)
                    query = gr.Textbox(show_label=True, label="请输入你的喜好，按回车提交")

                with gr.Column(scale=5):
                    with gr.Tab("上传pdf菜单"):
                        caidan = gr.Textbox(label="菜单地址",
                                            placeholder=args.path_caidan,
                                            lines=1,
                                            interactive=True,
                                            visible=True)
                        caidan.change(fn=change_caidan_path,
                                      inputs=[caidan])
                    with gr.Tab("添加图片文件"):
                        tu_caidan = gr.Image(label="添加图片文件", show_label=False, type='filepath')
                        tu_caidan.change(fn=change_caidan_path,
                                         inputs=[tu_caidan])

                    answer_set = gr.Accordion("回答参数设置", visible=True, open=True)
                    with answer_set:
                        model_name = gr.Textbox(label="模型名字",
                                                placeholder="默认模型："+args.pretrained_model_name,
                                                lines=1,
                                                interactive=True,
                                                visible=True)
                        model_top_k = gr.Number(value=args.topk,
                                                label="模型回答tok",
                                                interactive=True,
                                                visible=True,
                                                minimum=1,
                                                maximum=50,
                                                precision=0,)
                        model_temperature = gr.Slider(0, 1,
                                                      value=args.temperature,
                                                      step=0.01,
                                                      label="模型回答温度",
                                                      interactive=True)
                        model_top_k.change(fn=change_model_top_k,
                                           inputs=[model_top_k],
                                           outputs=[])
                        model_temperature.change(fn=change_model_temperature,
                                                 inputs=[model_temperature],
                                                 outputs=[])
                        model_name.change(fn=change_model_name,
                                          inputs=[model_name],
                                          outputs=[])
                    query.submit(fn=get_answer_caidan,
                                 inputs=[query, chatbot],
                                 outputs=[chatbot, query])
                    clear1 = gr.ClearButton([chatbot], value='清除记录')
                    clear_cash = gr.Button(value="清理显存", visible=True)
                    clear_cash.click(fn=clean_memory,
                                     inputs=[chatbot],
                                     outputs=[chatbot])

        with gr.Tab("大模型随机推荐"):
            with gr.Row():
                with gr.Column(scale=10):
                    chatbot = gr.Chatbot([[None,damodel_init0]],label='大模型随机推荐', show_label=True, height=850)
                    query = gr.Textbox(label='请输入你的喜好，按回车提交',show_label=True)

                with gr.Column(scale=5):
                    answer_set = gr.Accordion("回答参数设置", visible=True)
                    with answer_set:
                        model_name = gr.Textbox(label="模型名字",
                                                placeholder="默认模型："+ args.pretrained_model_name,
                                                lines=1,
                                                interactive=True,
                                                visible=True)
                        model_top_k = gr.Number(value=args.topk,
                                                label="模型回答tok",
                                                interactive=True,
                                                visible=True,
                                                minimum=1,
                                                maximum=50,
                                                precision=0,
                                                )
                        model_temperature = gr.Slider(0, 1,
                                                      value=args.temperature,
                                                      step=0.01,
                                                      label="模型回答温度",
                                                      interactive=True)
                        model_top_k.change(fn=change_model_top_k,
                                           inputs=[model_top_k],
                                           outputs=[])
                        model_temperature.change(fn=change_model_temperature,
                                                 inputs=[model_temperature],
                                                 outputs=[])
                        model_name.change(fn=change_model_name,
                                          inputs=[model_name],
                                          outputs=[])

                    query.submit(fn=get_answer_model,
                                 inputs=[query, chatbot],
                                 outputs=[chatbot, query])

                    clear2 = gr.ClearButton([chatbot], value='清除记录')
                    clear_cash = gr.Button(value="清理显存", visible=True)
                    clear_cash.click(fn=clean_memory,
                                     inputs=[chatbot],
                                     outputs=[chatbot])

        with gr.Tab("自定义推荐"):
            with gr.Row():
                with gr.Column(scale=10):
                    chatbot = gr.Chatbot(label='自定义推荐', show_label=True, height=850)
                    clear3 = gr.ClearButton([chatbot], value='清除记录')
                with gr.Column(scale=5):
                    with gr.Tab("从文件中获取随机内容"):
                        suijidz = gr.Textbox(label="随机内容地址",
                                            placeholder='默认地址：'+ args.path_zidingyi,
                                            lines=1,
                                            interactive=True,
                                            visible=True)
                        suiji_num = gr.Number(value=args.num_select,
                                              label="随机数目",
                                              minimum=1,
                                              precision=0,
                                              interactive=True)
                        suijidz.change(fn=change_path_zidingyi,
                                       inputs=[suijidz],
                                       outputs=[suijidz])

                        suiji_num.input(fn=change_suiji_num,
                                       inputs=[suiji_num])

                        suiji_1 = gr.Button(value="开始随机", visible=True)

                        suiji_1.click(fn=Random_selection1,
                                      inputs=[chatbot],
                                      outputs=[chatbot])
                    with gr.Tab("手动输入随机内容"):
                        suiji_nr2 = gr.Textbox(label="请输入随机内容",
                                             placeholder='在这输入随机内容，一行是一条内容',
                                             lines=12,
                                             interactive=True,
                                             visible=True)
                        suiji_num = gr.Number(value=args.num_select,
                                              label="随机数目",
                                              minimum=1,
                                              precision=0,
                                              interactive=True)
                        suiji_num.input(fn=change_suiji_num,
                                         inputs=[suiji_num])

                        suiji_2 = gr.Button(value="开始随机", visible=True)

                        suiji_2.click(fn=Random_selection2,
                                 inputs=[chatbot,suiji_nr2],
                                 outputs=[chatbot])

    with gr.Tab("智能识别"):
        with gr.Row():
            with gr.Column(scale=10):
                Chatbot = gr.Chatbot([[None,zhinengshibie]],label='智能识别', show_label=True, height=850)
                clear4 = gr.ClearButton([Chatbot], value='清除记录')
            with gr.Column(scale=4):
                mode1 = gr.Radio(["菜品识别", "果蔬识别"],label="识别模式",value='菜品识别')
                answer_set = gr.Accordion("参数设置", visible=True,open=False)
                with answer_set:
                    shitu_top_k = gr.Number(value=args.top_num,
                                            label="模型回答tok",
                                            interactive=True,
                                            visible=True,
                                            minimum=1,
                                            maximum=20,
                                            precision=0,
                                            )
                    shitu_filter_threshold = gr.Slider(0, 1,
                                                  value=args.filter_threshold,
                                                  step=0.01,
                                                  label="图片识别阈值",
                                                  interactive=True)
                    shitu_baike_num = gr.Number(value=args.baike_num,
                                            label="百科回答数",
                                            interactive=True,
                                            visible=True,
                                            minimum=1,
                                            maximum=20,
                                            precision=0)
                    shitu_top_k.change(fn=change_shitu_top_k,
                                       inputs=[shitu_top_k])
                    shitu_filter_threshold.change(fn=change_shitu_filter_threshold,
                                       inputs=[shitu_filter_threshold])
                    shitu_baike_num.change(fn=change_shitu_baike_num,
                                       inputs=[shitu_baike_num])

                with gr.Tab("添加图片文件"):
                    filepath = gr.Image(label="添加图片文件",show_label=False,type='filepath')
                tupian_sb = gr.Button(value="开启图片识别", visible=True)
                tupian_sb.click(fn=tupian_shibie,
                                inputs=[mode1,filepath,Chatbot],
                                outputs=[Chatbot])

    with gr.Tab("菜单规划"):
        with gr.Row():
            with gr.Column(scale=10):
                chatbot = gr.Chatbot([],label='菜单规划',show_label=True, height=850)
                query = gr.Textbox(show_label=True, label="请输入你的食材，按回车提交")
                query2 = gr.Textbox(show_label=True, label="请输入你想制作的菜肴进行询问，按回车提交")
            with gr.Column(scale=5):
                mode2 = gr.Radio(["启用", "不启用"], label="是否启用知识库", value='启用')
                knowledge_set = gr.Accordion("知识库设定", visible=True,open=False)
                answer_set = gr.Accordion("回答参数设置", visible=True, open=False)
                with knowledge_set:
                    O_data_path = gr.Textbox(label="数据储存地址",
                                             placeholder=args.original_data_path_g,
                                             lines=1,
                                             interactive=True,
                                             visible=True)

                    P_data_path = gr.Textbox(label="向量库保存地址",
                                             placeholder=args.preprocessed_data_path_g,
                                             lines=1,
                                             interactive=True,
                                             visible=True)

                    emb_name = gr.Textbox(label="embedding模型",
                                          placeholder='默认模型：' + args.embeddings_model_name,
                                          lines=1,
                                          interactive=True,
                                          visible=True)
                    chunk_size = gr.Number(value=args.chunk_size,
                                           label="数据切分最大长度",
                                           interactive=True,
                                           visible=True,
                                           minimum=1,
                                           precision=0)
                    chunk_overlap = gr.Number(value=args.chunk_overlap,
                                              label="数据片段最大重合",
                                              interactive=True,
                                              visible=True,
                                              minimum=0,
                                              precision=0)
                    chunk_size.change(fn=change_chunk_size,
                                      inputs=[chunk_size],
                                      outputs=[])
                    chunk_overlap.change(fn=change_chunk_overlap,
                                         inputs=[chunk_overlap],
                                         outputs=[])

                    db_update = gr.Button(value="更新知识库", visible=True)

                    O_data_path.change(fn=change_data_path_g,
                                       inputs=[O_data_path],
                                       outputs=[])
                    P_data_path.change(fn=change_P_data_path_g,
                                       inputs=[P_data_path],
                                       outputs=[])
                    emb_name.change(fn=change_emb_name,
                                    inputs=[emb_name],
                                    outputs=[])
                    db_update.click(fn=db_update_click,
                                    inputs=[chatbot],
                                    outputs=[chatbot])
                with answer_set:
                    search_top_k = gr.Number(value=args.topk_1,
                                             label="获取知识库内容条数",
                                             interactive=True,
                                             visible=True,
                                             minimum=1,
                                             maximum=50,
                                             precision=0)
                    model_name = gr.Textbox(label="模型名字",
                                            placeholder=args.pretrained_model_name,
                                            lines=1,
                                            interactive=True,
                                            visible=True)
                    model_top_k = gr.Number(value=args.topk,
                                            label="模型回答tok",
                                            interactive=True,
                                            visible=True,
                                            minimum=1,
                                            maximum=50,
                                            precision=0)
                    model_temperature = gr.Slider(0, 1,
                                                  value=args.temperature,
                                                  step=0.01,
                                                  label="模型回答温度",
                                                  interactive=True)
                    search_top_k.change(fn=change_search_top_k,
                                        inputs=[search_top_k],
                                        outputs=[])
                    model_top_k.change(fn=change_model_top_k,
                                       inputs=[model_top_k],
                                       outputs=[])
                    model_temperature.change(fn=change_model_temperature,
                                             inputs=[model_temperature],
                                             outputs=[])
                    model_name.change(fn=change_model_name,
                                      inputs=[model_name],
                                      outputs=[])
                    query.submit(fn=get_answer_guihua,
                                 inputs=[query, chatbot,mode2],
                                 outputs=[chatbot, query])
                    query2.submit(fn=get_answer_guihua2,
                                 inputs=[query2, chatbot, mode2],
                                 outputs=[chatbot, query2])

                clear5 = gr.ClearButton([chatbot], value='清除记录')
                clear_cash = gr.Button(value="清理显存", visible=True)
                clear_cash.click(fn=clean_memory,
                                 inputs=[chatbot],
                                 outputs=[chatbot])

    with gr.Tab("自由对话"):
        with gr.Row():
            with gr.Column(scale=10):
                chatbot = gr.Chatbot([],label='自由对话',show_label=True, height=950)
                query = gr.Textbox(show_label=True, label="在这输入，开始自由对话")
            with gr.Column(scale=5):
                model_name = gr.Textbox(label="模型名字",
                                        placeholder=args.pretrained_model_name,
                                        lines=1,
                                        interactive=True,
                                        visible=True)
                model_top_k = gr.Number(value=args.topk,
                                        label="模型回答tok",
                                        interactive=True,
                                        visible=True,
                                        minimum=1,
                                        maximum=50,
                                        precision=0)
                model_temperature = gr.Slider(0, 1,
                                              value=args.temperature,
                                              step=0.01,
                                              label="模型回答温度",
                                              interactive=True)

                model_top_k.change(fn=change_model_top_k,
                                   inputs=[model_top_k],
                                   outputs=[])
                model_temperature.change(fn=change_model_temperature,
                                         inputs=[model_temperature],
                                         outputs=[])
                model_name.change(fn=change_model_name,
                                  inputs=[model_name],
                                  outputs=[])
                query.submit(fn=ziyouduihau,
                             inputs=[query, chatbot],
                             outputs=[chatbot, query])
                clear6 = gr.ClearButton([chatbot], value='清除记录')

demo.queue(concurrency_count=3).launch(server_name='localhost',server_port=7989)