A Simple GPT Qt Demo

前言

注册 OpenAi 好长一段时间了,送的 18 刀的 api 接口一直没怎么使用过,突然看到今年 6 月份就过期了,刚好就拿来玩玩。简单尝试后,就想着用 PYQT5 来实现,目前实现了对话和图片生成,对话的上下文联系功能没做,因为一旦对话太长,消耗的 api 请求也会增加。

00.png

项目地址:https://github.com/nixgnauhcuy/ASimpleGPTQtDemo

实现

实现十分简单,项目基于 pyqt5,界面代码我就不贴了,应用代码如下,也可以前往 ASimpleGPTQtDemo 下载代码。

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

class chatThread(QThread):
chatSignal = pyqtSignal(str)

def __int__(self, text=None):
super(chatThread, self).__init__()
self.text = text

def run(self):
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages = [{"role":"user", "content": self.text}],
)

ask = response['choices'][0]['message']['content']
self.chatSignal.emit(ask)
except Exception as result:
ask = str(result)
self.chatSignal.emit(ask)


class createImgThread(QThread):
createImgSignal = pyqtSignal(str, str)

def __int__(self, text=None):
super(createImgThread, self).__init__()
self.text = text

def run(self):
try:
response = openai.Image.create(
prompt=self.text,
n=1,
size="512x512"
)

image_url = response['data'][0]['url']
self.createImgSignal.emit(image_url, 'True')
except Exception as result:
image_url = str(result)
self.createImgSignal.emit(image_url, 'False')



class MyPyQT_Form(QMainWindow, Ui_MainWindow):

def __init__(self):
super().__init__()
self.setupUi(self)

self.chatRunFlag = False
self.chat = chatThread()
self.chat.chatSignal.connect(self.chatAskEvent)
self.chatPushButton.clicked.connect(self.chatPushButtonEvent)

self.createImgRunFlag = False
self.createImg = createImgThread()
self.createImg.createImgSignal.connect(self.createImgEvent)
self.createImgPushButton.clicked.connect(self.createImgPushButtonEvent)


def chatAskEvent(self, str):
self.chatTextEdit.append('ask:\n' + str + '\n----------------------------------\n')
self.statusBar.clearMessage()
self.chatRunFlag = False

def chatPushButtonEvent(self):
if self.chatLineEdit.text() == "":
return

if self.chatRunFlag != False:
return

openai.api_key = self.apiKeyLineEdit.text()

self.chatRunFlag = True
self.chat.text = self.chatLineEdit.text()
self.chatTextEdit.append('user:\n' + self.chat.text + '\n')

self.statusBar.showMessage('正在生成中...')
self.chat.start()


def createImgEvent(self, url, flag):
if flag == 'True':
res = requests.get(url)
photo = QPixmap()
photo.loadFromData(res.content)
self.createImgLabel.setPixmap(photo)
else:
self.createImgLabel.setText(url)

self.createImgRunFlag = False
self.statusBar.clearMessage()


def createImgPushButtonEvent(self):
if self.createImgLineEdit.text() == "":
return

if self.createImgRunFlag != False:
return

openai.api_key = self.apiKeyLineEdit.text()

self.createImgRunFlag = True
self.createImg.text = self.createImgLineEdit.text()

self.statusBar.showMessage('正在生成中...')
self.createImg.start()



if __name__ == '__main__':
app = QApplication(sys.argv)
my_pyqt_form = MyPyQT_Form()
my_pyqt_form.show()
sys.exit(app.exec_())

效果演示

对话

Chat

图片生成

creatImg

出错预览

Error