环境准备:
python3.12
PyQt5 5.15.10
PyQt5-Qt5 5.15.2
安装依赖
# python依赖
pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
# ui设计用
pip install pyqt5-tools -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
# 打包用
pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
# 讯飞大模型接口调用依赖 ,参考:https://www.xfyun.cn/doc/spark/Web.html#
pip install --upgrade spark_ai_python
配置Qs designer 工具参数
D:\SlowSoftware\anaconda\Library\bin\designer.exe()
$FileDir$
D:\SlowSoftware\anaconda\envs\go_game\python.exe
$FileDir$
设计界面
打开qt designer界面,左边菜单栏选择控件进行界面设计
右键ui文件生成ui代码:
生成的代码无需做任何修改
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(610, 491)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(450, 70, 93, 41))
self.pushButton.setObjectName("pushButton")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(50, 190, 541, 181))
self.textBrowser.setObjectName("textBrowser")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(60, 20, 61, 20))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(50, 50, 331, 91))
self.lineEdit.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
self.lineEdit.setObjectName("lineEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 610, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.textBrowser.show)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "你的小满满"))
self.pushButton.setText(_translate("MainWindow", "发送"))
self.label.setText(_translate("MainWindow", "快问我!"))
# @Author:zh
# @Data:2024/6/29 19:41
# @:PyCharm
# Python版本:3.7
import json
import sys
#PyQt5中使用的基本控件都在PyQt5.QtWidgets模块中
from PyQt5.QtWidgets import QApplication, QMainWindow
#导入designer工具生成的login模块
from untitled import Ui_MainWindow
from sparkai.llm.llm import ChatSparkLLM, ChunkPrintHandler
from sparkai.core.messages import ChatMessage
#星火认知大模型Spark Max的URL值,其他版本大模型URL值请前往文档(https://www.xfyun.cn/doc/spark/Web.html)查看
SPARKAI_URL = 'wss://spark-api.xf-yun.com/v1.1/chat'
#星火认知大模型调用秘钥信息,请前往讯飞开放平台控制台(https://console.xfyun.cn/services/bm35)查看
SPARKAI_APP_ID = ''
SPARKAI_API_SECRET = ''
SPARKAI_API_KEY = ''
#星火认知大模型Spark Max的domain值,其他版本大模型domain值请前往文档(https://www.xfyun.cn/doc/spark/Web.html)查看
SPARKAI_DOMAIN = 'general'
class MyMainForm(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
# 这里是按键触发对应逻辑方法的关键
self.pushButton.clicked.connect(self.display)
def display(self):
input=self.lineEdit.text()
print(input)
spark = ChatSparkLLM(
spark_api_url=SPARKAI_URL,
spark_app_id=SPARKAI_APP_ID,
spark_api_key=SPARKAI_API_KEY,
spark_api_secret=SPARKAI_API_SECRET,
spark_llm_domain=SPARKAI_DOMAIN,
streaming=False,
)
messages = [ChatMessage(
role="user",
content=input
)]
handler = ChunkPrintHandler()
a = spark.generate([messages], callbacks=[handler])
response= ""
for i in a.generations:
response= i[0].text
# 这里是页面显示内容的关键
self.textBrowser.setText(response)
self.textBrowser.show()
if __name__ == "__main__":
#固定的,PyQt5程序都需要QApplication对象。sys.argv是命令行参数列表,确保程序可以双击运行
app = QApplication(sys.argv)
#初始化
myWin = MyMainForm()
#将窗口控件显示在屏幕上
myWin.show()
#程序运行,sys.exit方法确保程序完整退出。
sys.exit(app.exec_())
打包
# -F=>exe包 -w运行程序不运行cmd窗口
pyinstall -F -w main.py