非常新的CustomTkinter,我正在尝试创建一个GUI应用程序,返回一个预测您写的tweet。输出发生在终端,我希望它打印在GUI而不是终端,如果有人能帮助我,这将是非常感谢!所有我正在尝试做的是打印我的结果在GUI上,我知道打印直接到终端,但我不知道如何解决这个问题...
这是我的密码
import customtkinter
import tkinter
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from scipy.special import softmax
app = customtkinter.CTk()
app.geometry("700x500")
app.title("Twitter Sentiment Analyser")
frame_1 = customtkinter.CTkFrame(master=app)
frame_1.pack(pady=20, padx=60, fill="both", expand=True)
label_1 = customtkinter.CTkLabel(master=frame_1, justify=tkinter.LEFT, text="Twitter Sentiment Analyses System")
# label_1.pack(pady=12, padx=10)
# label_1.pack(side="left", pady=12)
label_1.place(x=20, y=20)
tweet = input()
tweet1 = customtkinter.CTkTextbox(master= frame_1, height= 100, width=380)
tweet1.pack(pady=12, padx=10)
tweet1.place(y= 150, x=100)
tweet1 = tweet
# def predict():
# p = entry_1.get("1.0",'end-1c')
# label_2 = customtkinter.CTkLabel(master=frame_1, text="The Tweet is: ")
tweet_words = []
for word in tweet.split(' '):
if word.startswith('@') and len(word) > 1:
word = '@user'
elif word.startswith('http'):
word = "http"
tweet_words.append(word)
tweet_proc = " ".join(tweet_words)
# load model and tokenizer
roberta = "cardiffnlp/twitter-roberta-base-sentiment"
model = AutoModelForSequenceClassification.from_pretrained(roberta)
tokenizer = AutoTokenizer.from_pretrained(roberta)
labels = ['Negative', 'Neutral', 'Positive']
# sentiment analysis
encoded_tweet = tokenizer(tweet_proc, return_tensors='pt')
# output = model(encoded_tweet['input_ids'], encoded_tweet['attention_mask'])
output = model(**encoded_tweet)
scores = output[0][0].detach().numpy()
scores = softmax(scores)
def pre():
for i in range(len(scores)):
l = labels[i]
s = scores[i]
print(l,s)
buutton_1 = customtkinter.CTkButton(master=frame_1, command=pre, text="Predict")
buutton_1.pack(pady=12, padx=10)
buutton_1.place(y=290, x=90)
1条答案
按热度按时间disho6za1#
这将有助于理解tkinter