Skip to content
Snippets Groups Projects
Commit 1bbf6485 authored by joshmk31's avatar joshmk31
Browse files

history implemented to josh's version

parent c7cd6ab8
No related branches found
No related tags found
No related merge requests found
# used https://realpython.com/python-gui-tkinter/, https://docs.python.org/3/library/tkinter.ttk.html
import tkinter as tk import tkinter as tk
from tkinter import ttk, messagebox from tkinter import ttk
from transformers import pipeline from transformers import pipeline
# Initialize the sentiment analysis pipeline
sentiment_analysis_model = "lxyuan/distilbert-base-multilingual-cased-sentiments-student" sentiment_analysis_model = "lxyuan/distilbert-base-multilingual-cased-sentiments-student"
sentiment_analyzer = pipeline("sentiment-analysis", model=sentiment_analysis_model) sentiment_analyzer = pipeline("sentiment-analysis", model=sentiment_analysis_model)
website_class_model = "alimazhar-110/website_classification" website_class_model = "alimazhar-110/website_classification"
website_class_analyzer = pipeline("text-classification", model=website_class_model) website_class_analyzer = pipeline("text-classification", model=website_class_model)
# History list to store past results
history = [] history = []
def submit_for_analysis(): def submit_for_analysis():
input_text = text_entry.get("1.0", tk.END).strip() input_text = text_entry.get("1.0", tk.END).strip()
if input_text:
try: # get teh results of the used models
sentiment_result = sentiment_analyzer(input_text) sentiment_result = sentiment_analyzer(input_text)
website_result = website_class_analyzer(input_text) website_result = website_class_analyzer(input_text)
# Sentiment analysis results
sentiment_label = sentiment_result[0]["label"].capitalize() # Print the results of sentiment analysis
sentiment_score = round(sentiment_result[0]["score"], 3) sentiment_label = sentiment_result[0]["label"].capitalize()
sentiment_color = "#FFCCCC" if sentiment_label.lower() == "negative" else "#CCFFCC" sentiment_score = round(sentiment_result[0]["score"], 2)
sentiment_result_text.set(f"Sentiment: {sentiment_label} statement\nScore: {sentiment_score}")
sentiment_result_label.config(background=sentiment_color) sentiment_result_text.set(f"Result: {sentiment_label} statement\nSentiment intensity: {sentiment_score}")
# Website classification results # Change background color to red if negative, green if positive
website_type = website_result[0]["label"] if sentiment_label.lower() == "negative":
website_confidence = round(website_result[0]["score"], 3) sentiment_color = "#f75f52" # a light red
website_results_text.set(f"Website Category: {website_type}\nConfidence: {website_confidence}") elif sentiment_label.lower() == "positive":
sentiment_color = "#4cf579" # a light green
# Update history
history.append((input_text, sentiment_label, sentiment_score, website_type, website_confidence))
update_history_display()
except Exception as e:
messagebox.showerror("Error", str(e))
sentiment_result_text.set("")
website_results_text.set("")
else: else:
messagebox.showinfo("Info", "Please enter some text to analyze.") sentiment_color = "#dae86f" # a light yellow
sentiment_result_label.config(background=sentiment_color)
# Print results of website analysis
website_type = website_result[0]["label"]
website_confidence = round(website_result[0]["score"], 2)
website_results_text.set(f"Best fit Website category: {website_type} \nConfidence: {website_confidence}")
history.append((input_text, sentiment_label, sentiment_score))
update_history_display()
def update_history_display(): def update_history_display():
history_text.config(state=tk.NORMAL) # Enable the widget before updating history_text.config(state=tk.NORMAL) # Enable the widget before updating
history_text.delete("1.0", tk.END) # Clear the existing content history_text.delete("1.0", tk.END) # Clear the existing content
for i, entry in enumerate(reversed(history)): for i, entry in enumerate(reversed(history)):
input_text, sentiment_label, sentiment_score, website_type, website_confidence = entry input_text, sentiment_label, sentiment_score = entry
history_text.insert(tk.END, f"{i+1}. \"{input_text}\" - Sentiment: {sentiment_label} ({sentiment_score}), Website Category: {website_type} (Confidence: {website_confidence})\n\n") history_text.insert(tk.END, f"{i+1}. \"{input_text}\" - Sentiment: {sentiment_label} ({sentiment_score})\n\n")
history_text.config(state=tk.DISABLED) # Disable the widget after updating history_text.config(state=tk.DISABLED) # Disable the widget after updating
# Create the main window # Create the main window
window = tk.Tk() window = tk.Tk()
window.geometry("700x1000") # Adjusted height to accommodate additional features window.geometry("700x800")
window.title("Sentiment and Website Category Analyzer") window.title("Sentiment Analyzer")
# Styling # setting the style
style = ttk.Style() style = ttk.Style()
style.theme_use('clam') style.theme_use("clam")
# Header Frame # creates the frame for the header
header_frame = ttk.Frame(window, padding="10") header_frame = ttk.Frame(window, padding="10")
header_frame.pack(fill='x') header_frame.pack(fill="x")
header_label = ttk.Label(header_frame, text="Sentiment and Website Category Analyzer", font=("Helvetica", 16))
# header label
header_label = ttk.Label(header_frame, text="Sentiment Analyzer", font=("Helvetica", 16))
header_label.pack() header_label.pack()
# Text Entry Frame # frame for user input
text_frame = ttk.Frame(window, padding="10") text_frame = ttk.Frame(window, padding="10")
text_frame.pack(fill='x') text_frame.pack(fill="x")
# user input text box
text_entry = tk.Text(text_frame, width=80, height=10) text_entry = tk.Text(text_frame, width=80, height=10)
text_entry.pack(padx=5, pady=5) text_entry.pack(padx=5, pady=5)
# Buttons Frame # buttons Frame
buttons_frame = ttk.Frame(window, padding="10") buttons_frame = ttk.Frame(window, padding="10")
buttons_frame.pack(fill='x') buttons_frame.pack(fill="x")
# creates analyze button
analyze_button = ttk.Button(buttons_frame, text="Analyze", command=submit_for_analysis) analyze_button = ttk.Button(buttons_frame, text="Analyze", command=submit_for_analysis)
analyze_button.pack(side='left', padx=5) analyze_button.pack(side="left", padx=5)
# create exit button
exit_button = ttk.Button(buttons_frame, text="Exit", command=window.quit) exit_button = ttk.Button(buttons_frame, text="Exit", command=window.quit)
exit_button.pack(side='right', padx=5) exit_button.pack(side="right", padx=5)
# Result Frame # frame for analysis results
result_frame = ttk.Frame(window, padding="10") result_frame = ttk.Frame(window, padding="10")
result_frame.pack(fill='both', expand=True) result_frame.pack(fill="both", expand=True)
# text output of sentiment analysis
sentiment_result_text = tk.StringVar() sentiment_result_text = tk.StringVar()
sentiment_result_label = ttk.Label(result_frame, textvariable=sentiment_result_text, font=("Helvetica", 12), background="#DDDDDD", wraplength=500) sentiment_result_label = ttk.Label(result_frame, textvariable=sentiment_result_text, font=("Helvetica", 12), background="#DDDDDD", wraplength=500)
sentiment_result_label.pack(padx=5, pady=5, fill='both') sentiment_result_label.pack(padx=5, pady=5, fill="both")
# text output of website classification
website_results_text = tk.StringVar() website_results_text = tk.StringVar()
website_results_label = ttk.Label(result_frame, textvariable=website_results_text, font=("Helvetica", 12), background="#DDDDDD", wraplength=500) website_results_label = ttk.Label(result_frame, textvariable=website_results_text, font=("Helvetica", 12), background="#DDDDDD", wraplength=500)
website_results_label.pack(padx=5, pady=5, fill='both') website_results_label.pack(padx=5, pady=5, fill="both")
# History Frame # creates history frame
history_frame = ttk.Frame(window, padding="10") history_frame = ttk.Frame(window, padding="10")
history_frame.pack(fill='both', expand=True) history_frame.pack(fill="both", expand=True)
history_label = ttk.Label(history_frame, text="Analysis History", font=("Helvetica", 12)) history_label = ttk.Label(history_frame, text="Analysis History", font=("Helvetica", 12))
history_label.pack() history_label.pack()
scrollbar = ttk.Scrollbar(history_frame) scrollbar = ttk.Scrollbar(history_frame)
scrollbar.pack(side='right', fill='y') scrollbar.pack(side="right", fill="y")
history_text = tk.Text(history_frame, width=80, height=10, yscrollcommand=scrollbar.set) history_text = tk.Text(history_frame, width=80, height=10, yscrollcommand=scrollbar.set)
history_text.pack(side='left', padx=5, pady=5, fill='both', expand=True) history_text.pack(side="left", padx=5, pady=5, fill="both", expand=True)
scrollbar.config(command=history_text.yview) scrollbar.config(command=history_text.yview)
window.mainloop() window.mainloop()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment