File size: 7,352 Bytes
9dc1a52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import json

import gradio as gr
import pandas as pd
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 llm_change(name):
    llm = eval(eval('llm_name_dict[name]'))()
    return llm


def generate_comment_post_process(_summary):
    _summary = _summary.split('如下:')[-1]
    while _summary[0] == '\n':
        _summary = _summary[1:]
    return _summary


def show_config():
    return 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_config():
    return gr.update(visible=False), 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:
        _chatbot = _chatbot + [[None, e]]
        return _chatbot, gr.Json(visible=False), gr.DataFrame(visible=False), 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 = e
    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 = e
    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')
    show_config_button = gr.Button('标准评价和prompt配置', visible=True)
    config = gr.Column(visible=False)
    show_config_button.click(show_config, outputs=[config])

    mock = gr.Column(visible=True)

    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)
        r_prompt = """尖括号里面的内容是对关于{topic}知识点教学设计的评价,但是该评价语言没有结合{topic}这个主题,请结合{topic}这个主题对该评价进行修改,修改后每句话都要体现{topic}这个主题且保持评价角度及正负性不变,但是从文字上看更像是专门针对{topic}这个主题做的评价。
        <{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)
        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_config, outputs=[stand_comment, config])
        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):
                topic = gr.Textbox('升华和凝华', label='请输入讲课主题')

                # data_out_path = gr.Textbox('', label='请输入模拟数据导出路径')
                generate_num = gr.Slider(1, 10, value=2, 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=[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, 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)


# demo.queue(concurrency_count=5).launch(server_name='0.0.0.0', server_port=7858, share=True)

demo.queue(concurrency_count=5).launch()