Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
Intro to Ai Sentiment Analysis
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
joshmk31
Intro to Ai Sentiment Analysis
Commits
455a4b10
Commit
455a4b10
authored
1 year ago
by
Sid Pothineni
Browse files
Options
Downloads
Patches
Plain Diff
added history
parent
9524344d
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ui.py
+50
-18
50 additions, 18 deletions
ui.py
with
50 additions
and
18 deletions
ui.py
+
50
−
18
View file @
455a4b10
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
\n
Statement
'
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
\n
Statement
'
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
(
"
700x
8
00
"
)
window
.
geometry
(
"
700x
9
00
"
)
# 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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment