File size: 6,657 Bytes
98aee8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# # # -*- coding: utf-8 -*-
# #
# # from utils import prompt
# # from utils import engine
# #
# # import sys
# # sys.path.append(r"C:\Users\Administrator\Desktop\PycharmProject\novalConvertProject")
# #
# # def main():
# #     story_path = "./data/stories/小红帽.txt"
# #
# #     prompt_text = prompt.prompt_generator(story_path)
# #
# #     # conversation_history = [
# #     #     {
# #     #         "role": "system",
# #     #         "content": prompt_text
# #     #     }
# #     # ]
# #
# #     # 创建ChatGPT实例
# #     chatbot = engine.ChatGPT(model="gpt-3.5-turbo",init_system={"role": "system", "content": prompt_text}, save_message=False)
# #
# #     # 模拟用户输入
# #     user_input = "开始游戏"
# #
# #     while user_input.lower() != "stop":
# #         # 获取助手的回复
# #         assistant_response = chatbot.get_response(user_input)
# #
# #         # 打印助手的回复
# #         print("Assistant:", assistant_response)
# #
# #         # 获取用户新的输入
# #         user_input = input("你的选择是:")
# #
# #     print('游戏结束')
# #
# #
# # if __name__ == '__main__':
# #     main()
#
#
#
#
#
#
#
#
#
#
#
# # -*- coding: utf-8 -*-
#
#
# #!/usr/bin/env python
# # -*- encoding: utf-8 -*-
# '''
# @Time    :   2023/09/22 17:43:37
# @Author  :   zoeyxiong
# @File    :   gradio_chatgpt_v2.py
# @Desc    :   使用gradio调用chatgpt
# '''
#
#
# import gradio as gr
# from utils import prompt
# from utils import engine
#
# MODEL_NAME = 'gpt-3.5-turbo'
# # 自定义system
# INIT_MSG = {"role": "system", "content": "你是一个资深算法工程师."}
# # 设置端口号,默认7560,遇冲突可自定义
# SERVER_PORT = 7561
#
#
# story_path = "./data/stories/小红帽.txt"
#
# prompt_text = prompt.prompt_generator(story_path)
#
#
# # 调用gpt的bot
# chatgpt = engine.ChatGPT(model=MODEL_NAME,init_system={"role": "system", "content": prompt_text})
# initial_response = chatgpt.get_response()
# def predict(input, chatbot):
#     """ 调用openai接口,获取答案
#     """
#     chatbot.append((input, ""))
#     # 找chatgpt要答案
#     response = chatgpt.get_response(input)
#     chatbot[-1] = (input, response)
#     return chatbot
#
# def reset_user_input():
#     return gr.update(value='')
#
# def reset_state():
#     chatgpt.clean_history()
#     return []
#
#
# def main():
#     with gr.Blocks() as demo:
#         gr.HTML("""<h1 align="center">{}</h1>""".format(MODEL_NAME))
#         # gradio的chatbot
#
#         chatbot = gr.Chatbot(value=[[None,initial_response]])
#
#         with gr.Row():
#             with gr.Column(scale=4):
#                 with gr.Column(scale=50):
#                     user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style(
#                         container=False)
#                 with gr.Column(min_width=32, scale=1):
#                     submitBtn = gr.Button("Submit", variant="primary")
#             with gr.Column(scale=1):
#                 emptyBtn = gr.Button("Clear History")
#         # 提交问题
#         submitBtn.click(predict, [user_input, chatbot],
#                         [chatbot], show_progress=True)
#         submitBtn.click(reset_user_input, [], [user_input])
#         # 清空历史对话
#         emptyBtn.click(reset_state, outputs=[chatbot], show_progress=True)
#
#
#     demo.queue().launch(share=False, inbrowser=True, server_port=SERVER_PORT)
#
#
# if __name__ == '__main__':
#     main()
#import gradio as gr
from utils import prompt
from utils import engine
from utils.datasetSaver import login
import gradio as gr


# MODEL_NAME = 'gpt-3.5-turbo'
MODEL_NAME = 'glm-4'
SERVER_PORT = 7560

def check_login(username, password):
    return login(username, password)

def main():
    login_interface = gr.Interface(fn=check_login,
                                   inputs=[gr.inputs.Textbox(label="用户名"),
                                           gr.inputs.Textbox(label="密码")],
                                   outputs="text",
                                   title="登录系统",
                                   theme="huggingface")
    login_interface.launch(share=False, inbrowser=True, server_port=SERVER_PORT)

    while True:
        username, password = login_interface.get_interpreter().interpret([None])[0]
        if check_login(username, password):
            print("登录成功!")
            break
        else:
            print("登录失败,请检查用户名和密码。")

    # 登录成功后进行聊天
    story_path = "./data/stories/小红帽.txt"
    prompt_text = prompt.prompt_generator(story_path)
    # LLM = engine.ChatGPT(model=MODEL_NAME, init_system={"role": "system", "content": prompt_text})
    LLM = engine.zhiPuGlm(model=MODEL_NAME, init_system={"role": "system", "content": prompt_text})
    initial_response = LLM.get_response()

    def predict(input, chatbot):
        chatbot.append((input, ""))
        response = LLM.get_response(input)
        chatbot[-1] = (input, response)
        return chatbot

    def reset_user_input():
        return gr.update(value='')

    def reset_user_input_new_game():
        return gr.update(value='newgame')

    def reset_state():
        LLM.clean_history()
        return []

    with gr.Blocks() as demo:
        gr.HTML("""<h1 align="center">{}</h1>""".format(MODEL_NAME))

        chatbot = gr.Chatbot(value=[[None, initial_response]])

        with gr.Row():
            with gr.Column(scale=4):
                with gr.Column(scale=50):
                    user_input = gr.Textbox(show_label=False, placeholder="Input...").style(
                        container=False)
                with gr.Column(min_width=32, scale=1):
                    submitBtn = gr.Button("提交", variant="primary")
            with gr.Column(scale=1):
                newBtn = gr.Button("新的游戏")

        submitBtn.click(predict, [user_input, chatbot], [chatbot], show_progress=True)
        submitBtn.click(reset_user_input, [], [user_input])

        newBtn.click(reset_user_input_new_game,[],[user_input])
        newBtn.click(reset_state, outputs=[chatbot], show_progress=True)
        newBtn.click(predict, [user_input, chatbot], [chatbot], show_progress=True)
        newBtn.click(reset_user_input, [], [user_input])

    demo.queue().launch(share=False, inbrowser=True, server_port=SERVER_PORT)

if __name__ == '__main__':
    main()