import gradio as gr from llm import OpenAI3, OpenAI4 from mock_data import * block_css = """ .importantButton { background: linear-gradient(45deg, #7e0570,#5d1c99, #6e00ff) !important; border: none !important; } .importantButton:hover { background: linear-gradient(45deg, #ff00e0,#8500ff, #6e00ff) !important; border: none !important; }""" webui_title = "📚📚📚📚📚📚📚📚📚📚📚📚* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ☘️ * * *学思案* * * ☘️ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *📚📚📚📚📚📚📚📚📚📚📚📚" llm_name_dict = {'chatgpt-3.5': 'OpenAI3', 'chatgpt-4': 'OpenAI4', 'chatglm—6b': 'ChatGLM'} def function_select(mode): if mode == "测评智能摘要": return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False) elif mode == "生成模拟测评": return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False) else: return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True) def llm_change(name): llm = eval(eval('llm_name_dict[name]'))() return llm def generate_comment_post_process(_summary): _summary = _summary.split('如下:')[-1] _summary = _summary.split('follows:')[-1] while _summary[0] == '\n': _summary = _summary[1:] _summary = _summary.split('\n\n')[0] return _summary def show_config(): return gr.update(visible=True) def load_comments(comments_file_path): file_path = comments_file_path.name _comments = load_scores_and_comments(file_path) return _comments, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True) def show_stand_file(stand_file_path, stand_file): df = pd.read_excel(stand_file_path) return gr.update(visible=True, value=df) def hide_stand_file(): return gr.update(visible=False) def clear_screen(_chatbot): _chatbot = [[None, None]] return _chatbot def generate_mock_data(_topic, _stand_file_path, _rewrite_prompt, _generate_num, _chatbot): try: scores_and_comments, stand_file = \ generate_scores_and_comments(_stand_file_path, _rewrite_prompt, _topic, _generate_num) chat = '' for i in range(len(scores_and_comments)): chat = chat + 'Teacher' + str(i+1) + ':\n\n' for key, value in scores_and_comments[i]['scores'].items(): chat = chat + key + ': ' + str(value) + ' - - ' chat = chat[:-5] + '\n\n' chat = chat + scores_and_comments[i]['comment'] + '\n\n' _chatbot = _chatbot + [[None, chat]] return _chatbot, scores_and_comments, stand_file, gr.update(visible=True), gr.update(visible=True) except Exception as e: message = '模拟测评生成失败,原因是:' + str(e) + '\n\n' _chatbot = _chatbot + [[None, message]] return _chatbot, dict(), pd.DataFrame(), gr.update(visible=False), \ gr.update(visible=False) def rewrite(standard_file, rewrite_prompt, topic, assessments, chatbot): try: rewrite_comment = medium_score_rewrite(standard_file, rewrite_prompt, topic, assessments) rewrite_comment = generate_comment_post_process(rewrite_comment) message = '上述评价按照中位法改写结果如下:\n\n' + rewrite_comment except Exception as e: message = '中位法改写失败,原因是:' + str(e) + '\n\n' chatbot = chatbot + [[None, message]] return chatbot def summary(summary_prompt, assessments, chatbot): try: summary_comment = quantile_summary(summary_prompt, assessments) summary_comment = generate_comment_post_process(summary_comment) message = '上述评价按照四分位法摘要结果如下:\n\n' + summary_comment except Exception as e: message = '四分位法摘要生成失败,原因是:' + str(e) + '\n\n' chatbot = chatbot + [[None, message]] return chatbot with gr.Blocks(css=block_css) as demo: gr.Markdown('\n\n\n\n') gr.Markdown(webui_title) gr.Markdown('\n\n\n\n') fun_mode = gr.Radio(["测评智能摘要", "生成模拟测评", "系统配置"], value="系统配置", label="请先选择功能模式") # show_config_button = gr.Button('标准评价和prompt配置', visible=True) do_summary = gr.Row(visible=False) config = gr.Column(visible=True) mock = gr.Row(visible=False) # show_config_button.click(show_config, outputs=[config]) fun_mode.change(fn=function_select, inputs=[fun_mode], outputs=[do_summary, mock, config]) with config: # llm = gr.State('') # model_mode = gr.Radio(['chatgpt-3.5', "chatgpt-4", 'chatglm—6b'], label="请选择驱动模型") # model_name = gr.Radio(['chatgpt-3.5'], label="请选择驱动模型") # model_name.change(fn=llm_change, inputs=[model_name], outputs=[llm]) stand_file_path = gr.Textbox('指标评价标准.xlsx', label='请输入评价标准文件路径') stand_comment = gr.DataFrame(visible=False) show_stand_button = gr.Button('使用模拟测评/中位值改写功能,务必先加载标准评价', visible=True) r_prompt = """The content within the angle brackets is an evaluation of the instructional design regarding the {topic} knowledge point. However, the evaluation does not incorporate the {topic} theme. Please modify the evaluation by incorporating the {topic} theme in each sentence while maintaining the same evaluative perspective and positive/negative nature. The modified evaluation should appear to be specifically targeted towards the {topic} theme. <{stand_comment}> """ r_prompt = gr.Text(value=r_prompt, label='请配置改写prompt', lines=3) s_prompt = """尖括号里面的内容是多位老师对某堂课的教学评价,每位老师的评价用换行符进行了分隔,请对各位老师的评价进行摘要总结 '<{comment}>' """ s_prompt = gr.Text(value=s_prompt, label='请配置摘要prompt', lines=3) set_config_button = gr.Button('确定', visible=True) # with gr.Row(): # with gr.Column(scale=2): # set_config_button = gr.Button('确定', visible=True) # with gr.Column(scale=2): # show_stand_button = gr.Button('加载/查看标准评价', visible=True) set_config_button.click(hide_stand_file, outputs=[stand_comment]) show_stand_button.click(show_stand_file, inputs=[stand_file_path, stand_comment], outputs=stand_comment) with mock: with gr.Row(): with gr.Column(scale=10): mock_topic = gr.Textbox('升华和凝华', label='请输入讲课主题') # data_out_path = gr.Textbox('', label='请输入模拟数据导出路径') generate_num = gr.Slider(1, 20, value=3, step=1, label="评课人数量", interactive=True) mock_data = gr.Json(visible=False) with gr.Row(): with gr.Column(scale=2): generate_button = gr.Button('生成模拟测评(需先加载系统配置文件)') with gr.Column(scale=2): clear_screen_button = gr.Button('清屏', visible=True) with gr.Column(scale=2): rewrite_button = gr.Button('中位值改写', visible=False) with gr.Column(scale=2): summary_button = gr.Button('四分位摘要', visible=False) with gr.Column(scale=10): chatbot = gr.Chatbot([[None, None]], elem_id="chat-box", show_label=False).style(height=1000) generate_button.click(generate_mock_data, inputs=[mock_topic, stand_file_path, r_prompt, generate_num, chatbot], outputs=[chatbot, mock_data, stand_comment, rewrite_button, summary_button]) rewrite_button.click(rewrite, inputs=[stand_comment, r_prompt, mock_topic, mock_data, chatbot], outputs=chatbot) summary_button.click(summary, inputs=[s_prompt, mock_data, chatbot], outputs=chatbot) clear_screen_button.click(clear_screen, inputs=[chatbot], outputs=chatbot) with do_summary: with gr.Column(scale=10): file = gr.File(label='请先上传待摘要文件') rewrite_topic = gr.Textbox('升华和凝华', label='如采用中位值改写的方法,请输先入讲课主题', visible=False) comments = gr.Json(visible=False) with gr.Row(): with gr.Column(scale=2): summary_button = gr.Button('四分位摘要', visible=False) with gr.Column(scale=2): rewrite_button = gr.Button('中位值改写(需先加载系统配置文件)', visible=False) with gr.Column(scale=2): clear_screen_button = gr.Button('清屏', visible=False) file.upload(load_comments, inputs=[file], outputs=[comments, summary_button, rewrite_button, clear_screen_button, rewrite_topic]) with gr.Column(scale=10): chatbot = gr.Chatbot([[None, None]], elem_id="chat-box", show_label=False).style(height=1000) rewrite_button.click(rewrite, inputs=[stand_comment, r_prompt, rewrite_topic, comments, chatbot], outputs=chatbot) summary_button.click(summary, inputs=[s_prompt, comments, chatbot], outputs=chatbot) clear_screen_button.click(clear_screen, inputs=[chatbot], outputs=chatbot) # demo.queue(concurrency_count=5).launch(server_name='0.0.0.0', server_port=7858, share=True) demo.queue(concurrency_count=5).launch()