# # # -*- 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("""

{}

""".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("""

{}

""".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()