python教程

Python开发一个ChatGPT GUI

我的站长站 2022-12-15 人阅读

1、首先去下载这个ChatGPT库,用到的库是这个:https://GitHub.com/acheong08/ChatGPT

2、安装这个ChatGPT库:

pip3 install revChatGPT==0.0.a42

3、同目录还需要一个“config.JSON”:

{
    "session_token": "",
    "cf_clearance": "",
    "user_agent": ""
}

3、Python代码

from tkinter import *
from tkinter.ttk import *
import json
# from chatgpt_wrapper import ChatGPT
 
# from revChatGPT.revChatGPT import Chatbot
# pip3 install revChatGPT==0.0.a42
from revChatGPT.revChatGPT import AsyncChatbot as Chatbot
import asyncio
 
class WinGUI(Tk):
    def __init__(self):
        super().__init__()
        self.__win()
        self.tk_button_send = self.__tk_button_send()
        self.tk_input_inputs = self.__tk_input_inputs()
        self.tk_text_show = self.__tk_text_show()
        self.tk_label_note = self.__tk_label_note()
        self.process_done = False
 
    def __win(self):
        self.title("ChatGPT GUI")
        # 设置窗口大小、居中
        width = 500
        height = 445
        screenwidth = self.winfo_screenwidth()
        screenheight = self.winfo_screenheight()
        geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self.geometry(geometry)
        self.resizable(width=False, height=False)
 
    def __tk_button_send(self):
        btn = Button(self, text="发送")
        btn.place(x=420, y=20, width=60, height=40)
        return btn
 
    def __tk_input_inputs(self):
        ipt = Entry(self)
        ipt.place(x=20, y=20, width=380, height=40)
        return ipt
 
    def __tk_text_show(self):
        text = Text(self)
        text.place(x=20, y=103, width=460, height=336)
        return text
 
    def __tk_label_note(self):
        label = Label(self,text="",anchor="center")
        label.place(x=20, y=70, width=460, height=24)
        return label
 
class Win(WinGUI):
    def __init__(self):
        super().__init__()
        self.config(menu=self.create_menu())
        self.__event_bind()
        self.chatbot = Chatbot(json.loads(open('config.json', 'r').read()), conversation_id=None)
        self.update_note('welcome!')
 
    def create_menu(self):
        menu = Menu(self,tearoff=False)
        return menu
     
    def update_show(self, strings):
        self.tk_text_show.delete('1.0', END)
        self.tk_text_show.insert('1.0', strings)
        self.tk_text_show.update()
     
    def update_note(self, strings):
        self.tk_label_note.config(text=strings)
     
    async def wait_for_response(self, inputs):
        self.update_show('')
        async for i in await self.chatbot.get_chat_response(inputs, output="stream"):
            print(i['message'])
            self.update_show(i['message'])
        self.process_done = True
        self.update_note('回复完成!')
 
    def bot_ask(self, evt):
        print("<tk_button_send>点击事件", evt)
        inputs = self.tk_input_inputs.get().strip()
        self.process_done = False
        if inputs:
            self.update_note(f'>> 输入内容为:{inputs}, 等待回复中...')
            asyncio.run(self.wait_for_response(inputs))
        else:
            self.update_note('>> 请先输入内容。')
    def __event_bind(self):
        self.tk_button_send.bind('<Button-1>',self.bot_ask)
         
if __name__ == "__main__":
    win = Win()
    win.mainloop()
相关专题
ChatGPT
ChatGPT
2023-02-13 201

ChatGPT是一款最近非常火的人工智能系统,我的站长站为了体验一下ChatGPT的强大,在网络上收集了许多关于ChatGPT的信息,分享给大家.包含大家最关系的ChatGPT注册方...

相关推荐
  • ChatGpt教程
  • VUE开发接入ChatGpt教程分析

    页面仿照微信聊天界面,点击机器人图标,弹出聊天框,消息分为用户消息,机器人消息两种,每次用户发送消息,请求后端接口获取chatgpt返回的信息,添加到消息列表中,推送给用户。不直接通过前端请求chatgpt官方接口,否则会泄露个人的api-key,在官方接口的基础上封装...

    js教程 110 1年前
  • Python开发一个ChatGPT GUI

    1、首先去下载这个ChatGPT库,用到的库是这个:https://github.com/acheong08/ChatGPT2、安装这个ChatGPT库:pip3 install revChatGPT==0.0.a423、同目录还需要一个“config.json”:{ "session_token": "", "cf_clearance": "", "user_agent": "...

    python教程 107 1年前
  • 利用ChatGPT开源api开发一个聊天库

    准备工作第一步:注册OpenAI账号,如果搭建了科学还是提示不对你的国家提供服务的话,尝试清空浏览器缓存或者打开浏览器的无痕窗口。Chrome默认在右上角三个点打开就能找到“打开新的无痕式窗口”。ChatGPT聊天库代码示例1.网页获取所需token和cookie我们...

    经验分享 243 1年前
最新更新