ZJF-Thunder/ChineseBert_text_analysis_system
0
1import sys2from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QTextEdit, QPushButton, QLabel3from PyQt5.QtGui import QBrush, QPixmap, QFont, QPalette, QColor4from transformers import BertTokenizer, BertForSequenceClassification, BertConfig5from Test_model import predicted6 7 8class TextAnalyzer(QWidget):9 10 def __init__(self):11 super().__init__()12 self.initUI()13 14 def initUI(self):15 # 设置窗口标题和大小16 self.setWindowTitle('文本分析系统')17 self.setGeometry(300, 300, 500, 500)18 self.resize(1000, 700)19 # 创建文本输入框、分析按钮、结果显示框和标签20 self.textEdit = QTextEdit()21 self.resultLabel = QLabel("等待分析")22 self.analyzeBtn = QPushButton('分析')23 self.resultTextEdit = QTextEdit()24 # self.setStyleSheet(25 # "background-image: url(./my_js/bg.jpg); background-position: center center; background-size: cover;")26 27 # 创建一个QPixmap对象,并设置为背景图片 设置整个窗口的背景图片28 pixmap = QPixmap('./my_js/win11.jpg')29 pixmap = pixmap.scaled(self.size()) # 将背景图片大小设置为和显示框一样大30 # 创建一个QPalette对象,并将背景图片应用到QPalette.Background属性中31 palette = self.palette()32 palette.setBrush(self.backgroundRole(), QBrush(pixmap))33 self.setPalette(palette)34 35 # 将所有控件的背景设置为透明36 self.textEdit.setAutoFillBackground(False)37 self.analyzeBtn.setAutoFillBackground(False)38 self.resultLabel.setAutoFillBackground(False)39 self.resultTextEdit.setAutoFillBackground(False)40 41 # 将所有控件的样式设置为透明42 self.textEdit.setStyleSheet("background-color: rgba(0,0,0,0);")43 # self.analyzeBtn.setStyleSheet("background-color: rgba(0,0,0,0);")44 self.resultLabel.setStyleSheet("background-color: rgba(0,0,0,0);")45 self.resultTextEdit.setStyleSheet("background-color: rgba(0,0,0,0);")46 # 为分析按钮单独设置样式47 # self.analyzeBtn.setStyleSheet("QPushButton { background-color: red; color: white; }")48 # self.analyzeBtn.setStyleSheet("background-color: red; color: white")49 self.analyzeBtn.setStyleSheet("QPushButton {background-color: #4CAF50; "50 "border: none;"51 "color: white; "52 "padding: 10px 20px; "53 "text-align: center; "54 "text-decoration: none;"55 "display: inline-block;"56 "font-size: 16px;"57 "margin: 4px 2px;"58 "cursor: pointer;"59 "border-radius: 8px;"60 "box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.4);}"61 "QPushButton:hover "62 "{background-color: #3e8e41;}"63 "QPushButton:pressed "64 "{background-color: #145826;box-shadow: none;}")65 '''background - color:设置按钮的背景颜色为 # 4CAF50(一种绿色)。66 border:设置按钮的边框为空。67 color:设置按钮上的文本颜色为白色。68 padding:设置按钮内部的上下左右间距分别为10px和20px。69 text - align:设置按钮内部文本的对齐方式为居中。70 text - decoration:设置按钮内部文本的装饰方式为空(不带下划线)。71 display:设置按钮的显示方式为内联块级元素。72 font - size:设置按钮内部文本的字体大小为16px。73 margin:设置按钮外边距的上下左右间距分别为4px和2px。74 cursor:设置鼠标指针在按钮上的样式为手型。75 border - radius:设置按钮的圆角半径为8px。76 box - shadow:设置按钮的阴影效果,包括阴影的位置、大小、颜色和透明度。77 css78 Copy79 code80 QPushButton: hover81 {82 background - color: # 3e8e41;83 }84 :hover:设置按钮在鼠标悬停时的样式。85 background - color:设置按钮背景颜色为 # 3e8e41(一种深绿色)。86 css87 Copy88 code89 QPushButton: pressed90 {91 background - color: # 145826;92 box - shadow: none;93 }94 :pressed:设置按钮在被点击时的样式。95 background - color:设置按钮背景颜色为 # 145826(一种更深的绿色)。96 box - shadow:取消按钮的阴影效果,使其看起来像是被按下去的效果。97 98 :hover:设置按钮在鼠标悬停时的样式。99 background-color:设置按钮背景颜色为 #3e8e41(一种深绿色)。100 :pressed:设置按钮在被点击时的样式。101 background-color:设置按钮背景颜色为 #145826(一种更深的绿色)。102 box-shadow:取消按钮的阴影效果,使其看起来像是被按下去的效果。103 '''104 105 # 创建一个QFont对象,设置字体大小为15106 font = QFont()107 font.setPointSize(15)108 # 将QFont对象应用到QTextEdit控件中109 self.textEdit.setFont(font)110 self.resultLabel.setFont(font)111 self.resultTextEdit.setFont(font)112 113 # 给结果标签创建一个QPalette对象,并设置其颜色为红色114 palette = QPalette()115 palette.setColor(QPalette.WindowText, QColor("red"))116 self.resultLabel.setPalette(palette)117 118 # 给结果显示框创建一个QPalette对象,并设置其颜色为红色119 palette2 = QPalette()120 palette2.setColor(QPalette.Text, QColor("red"))121 self.resultTextEdit.setPalette(palette2)122 123 # 给文本输入框创建一个QPalette对象,并设置其颜色为红色124 palette3 = QPalette()125 palette3.setColor(QPalette.Text, QColor("black"))126 # self.textEdit.setPalette(palette3)127 128 # 直接设置颜色,不需要用到QPalette控件 以下两种均可129 self.textEdit.setTextColor(QColor('black'))130 # 这个是设置输入框的颜色,不是输入文字的颜色131 # self.textEdit.setStyleSheet('color: white')132 133 # # 也可以使用QPalette控件实现更精细化的样式134 # # 创建QPalette对象135 # palette4 = QPalette()136 # # 设置颜色137 # palette4.setColor(QPalette.Base, QColor('white')) # 设置文本框的背景颜色138 # palette4.setColor(QPalette.Text, QColor('blue')) # 设置文本框的前景颜色139 # # 将QPalette对象应用到QTextEdit控件中140 # self.textEdit.setPalette(palette4)141 142 # 为分析按钮添加点击事件处理程序143 self.analyzeBtn.clicked.connect(self.analyzeText)144 145 # 创建垂直和水平布局,并将控件添加到布局中146 vBox = QVBoxLayout()147 hBox = QHBoxLayout()148 hBox.addWidget(self.textEdit)149 hBox.addWidget(self.analyzeBtn)150 vBox.addLayout(hBox)151 vBox.addWidget(self.resultLabel)152 vBox.addWidget(self.resultTextEdit)153 154 # 设置主布局155 self.setLayout(vBox)156 157 # 显示窗口158 self.show()159 160 def analyzeText(self):161 # 获取文本输入框的内容162 text = self.textEdit.toPlainText()163 self.resultLabel.setText('正在分析,请稍候...')164 QApplication.processEvents() # 刷新界面,使QLabel文本立即更新165 166 predicted_label, predicted_prob = predicted(text, model, tokenizer)167 # 另一个样本的标签及其概率168 other_prob = 1 - predicted_prob169 other_prob_label = 1170 # 定义标签171 labels = {0: "谣言", 1: "非谣言"}172 if predicted_label == 1:173 other_prob_label = 0174 result = f"这条微博有{predicted_prob * 100:.2f}%的概率为{labels[predicted_label]}," \175 f"有{other_prob * 100:.2f}%的概率为{labels[other_prob_label]}"176 else:177 result = f"这条微博有{predicted_prob * 100:.2f}%的概率为{labels[predicted_label]}," \178 f"有{other_prob * 100:.2f}%的概率为{labels[other_prob_label]}"179 180 # 显示结果181 # 更新QLabel文本182 self.resultLabel.setText("分析已完成")183 self.resultTextEdit.setText(result)184 185 186if __name__ == '__main__':187 bert = './models/chinese-bert-wwm-ext'188 # 加载自己保存后的config文件189 config = BertConfig.from_pretrained("my_chinesebert_config/config.json", num_labels=2)190 tokenizer = BertTokenizer.from_pretrained(bert, num_labels=2)191 """加载自己保存的模型,多种模型对比"""192 # model_path = './models/chinese-bert-wwm-ext' # 未经过微调的原始模型1193 # model_path = './models/bert-base-chinese' # 未经过微调的原始模型2194 # model_path = './模型保存/chinesebert.pth' # 微调的最早期的模型195 # model_path = './模型保存/ChineseBert_2023-03-29_16-27-07_0.949.pt'196 # model_path = './模型保存/ChineseBert_2023-03-25_17-10-39_0.95.pt'197 # model_path = './模型保存/ChineseBert_2023-04-05_16-30-38_0.9970.pt'198 model_path = './模型保存/ChineseBert_2023-04-07_20-12-29_0.999.pt'199 model = BertForSequenceClassification.from_pretrained(model_path, config=config)200 201 app = QApplication(sys.argv)202 ex = TextAnalyzer()203 sys.exit(app.exec_())204 