Skip to content
Snippets Groups Projects
Commit 455a4b10 authored by Sid Pothineni's avatar Sid Pothineni
Browse files

added history

parent 9524344d
No related branches found
No related tags found
No related merge requests found
import tkinter as tk import tkinter as tk
from tkinter import ttk from tkinter import ttk, messagebox
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)
# History list to store past results
history = []
def submit_for_analysis(): def submit_for_analysis():
input_text = text_entry.get("1.0", tk.END) input_text = text_entry.get("1.0", tk.END).strip()
try: if input_text:
res = sentiment_analyzer(input_text) try:
# Assuming 'res' is a list of dictionaries, we take the first result res = sentiment_analyzer(input_text)
label = res[0]['label'].capitalize() # Capitalize the label label = res[0]['label'].capitalize()
score = round(res[0]['score'], 3) # Round the score to 3 decimal places score = round(res[0]['score'], 3)
result_text.set(f"Result: {label} statement\nStatement's score: {score}")
# Update the result label with a more user-friendly format color = "#FFCCCC" if label.lower() == "negative" else "#CCFFCC"
result_text.set(f"Result: {label} statement\nStatement's score: {score}") result_label.config(background=color)
# Change the background color based on the sentiment
color = "#FFCCCC" if label.lower() == "negative" else "#CCFFCC" # Update history by appending new entries
result_label.config(background=color) history.append((input_text, label, score))
except Exception as e:
messagebox.showerror("Error", str(e)) # Update the history display
result_text.set("") update_history_display()
except Exception as e:
messagebox.showerror("Error", str(e))
result_text.set("")
else:
messagebox.showinfo("Info", "Please enter some text to analyze.")
def update_history_display():
history_text.config(state=tk.NORMAL) # Enable the widget before updating
history_text.delete("1.0", tk.END) # Clear the existing content
for i, entry in enumerate(reversed(history)):
input_text, label, score = entry
history_text.insert(tk.END, f"{i+1}. \"{input_text}\" - {label} ({score})\n\n")
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("700x800") window.geometry("700x900") # Increased height to accommodate history area
window.title("Sentiment Analyzer") window.title("Sentiment Analyzer")
# Styling # Styling
...@@ -63,4 +81,18 @@ result_text = tk.StringVar() ...@@ -63,4 +81,18 @@ result_text = tk.StringVar()
result_label = ttk.Label(result_frame, textvariable=result_text, font=("Helvetica", 12), background="#DDDDDD", wraplength=500) result_label = ttk.Label(result_frame, textvariable=result_text, font=("Helvetica", 12), background="#DDDDDD", wraplength=500)
result_label.pack(padx=5, pady=5, fill='both') result_label.pack(padx=5, pady=5, fill='both')
window.mainloop() # History Frame
history_frame = ttk.Frame(window, padding="10")
history_frame.pack(fill='both', expand=True)
history_label = ttk.Label(history_frame, text="Analysis History", font=("Helvetica", 12))
history_label.pack()
scrollbar = ttk.Scrollbar(history_frame)
scrollbar.pack(side='right', fill='y')
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)
scrollbar.config(command=history_text.yview)
window.mainloop()
\ No newline at end of file
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