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
7a0e488a
Commit
7a0e488a
authored
1 year ago
by
joshmk31
Browse files
Options
Downloads
Patches
Plain Diff
some minor tweaks
parent
1bbf6485
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
+22
-11
22 additions, 11 deletions
ui.py
with
22 additions
and
11 deletions
ui.py
+
22
−
11
View file @
7a0e488a
# used https://realpython.com/python-gui-tkinter/, https://docs.python.org/3/library/tkinter.ttk.html
# used https://realpython.com/python-gui-tkinter/, https://docs.python.org/3/library/tkinter.ttk.html
# https://huggingface.co/SamLowe/roberta-base-go_emotions?
# https://huggingface.co/alimazhar-110/website_classification?text=I+like+you.+I+love+you
# https://huggingface.co/lxyuan/distilbert-base-multilingual-cased-sentiments-student?text=the+sky+is+cloudy
# Authors:
import
tkinter
as
tk
import
tkinter
as
tk
from
tkinter
import
ttk
from
tkinter
import
ttk
...
@@ -7,6 +11,9 @@ from transformers import pipeline
...
@@ -7,6 +11,9 @@ from transformers import 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
)
emotion_analysis_model
=
"
SamLowe/roberta-base-go_emotions
"
emotion_analyzer
=
pipeline
(
"
sentiment-analysis
"
,
model
=
emotion_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
)
...
@@ -17,14 +24,18 @@ def submit_for_analysis():
...
@@ -17,14 +24,18 @@ def submit_for_analysis():
# get teh results of the used models
# get teh results of the used models
sentiment_result
=
sentiment_analyzer
(
input_text
)
sentiment_result
=
sentiment_analyzer
(
input_text
)
emotion_result
=
emotion_analyzer
(
input_text
)
website_result
=
website_class_analyzer
(
input_text
)
website_result
=
website_class_analyzer
(
input_text
)
#
Print
the results of sentiment analysis
#
Display
the results of sentiment analysis
sentiment_label
=
sentiment_result
[
0
][
"
label
"
].
capitalize
()
sentiment_label
=
sentiment_result
[
0
][
"
label
"
].
capitalize
()
sentiment_score
=
round
(
sentiment_result
[
0
][
"
score
"
],
2
)
sentiment_score
=
round
(
sentiment_result
[
0
][
"
score
"
],
2
)
emotion_label
=
emotion_result
[
0
][
"
label
"
].
capitalize
()
emotion_score
=
round
(
emotion_result
[
0
][
"
score
"
],
2
)
sentiment_result_text
.
set
(
f
"
Resul
t:
{
sentiment_label
}
statement
\n
Sentiment intensity:
{
sentiment
_score
}
"
)
sentiment_result_text
.
set
(
f
"
Sentimen
t:
{
sentiment_label
}
(Intensity:
{
sentiment_score
}
)
\n
Emotion:
{
emotion_label
}
(Confidence:
{
emotion
_score
}
)
"
)
# Change background color to red if negative, green if positive
# Change background color to red if negative, green if positive
if
sentiment_label
.
lower
()
==
"
negative
"
:
if
sentiment_label
.
lower
()
==
"
negative
"
:
...
@@ -37,25 +48,25 @@ def submit_for_analysis():
...
@@ -37,25 +48,25 @@ def submit_for_analysis():
sentiment_result_label
.
config
(
background
=
sentiment_color
)
sentiment_result_label
.
config
(
background
=
sentiment_color
)
#
Print
results of website analysis
#
Display
results of website analysis
website_
type
=
website_result
[
0
][
"
label
"
]
website_
label
=
website_result
[
0
][
"
label
"
]
website_co
nfidenc
e
=
round
(
website_result
[
0
][
"
score
"
],
2
)
website_
s
co
r
e
=
round
(
website_result
[
0
][
"
score
"
],
2
)
website_results_text
.
set
(
f
"
Best fit Website category:
{
website_
type
}
\n
Confidence:
{
website_co
nfidenc
e
}
"
)
website_results_text
.
set
(
f
"
Best fit Website category:
{
website_
label
}
\n
Confidence:
{
website_
s
co
r
e
}
"
)
history
.
append
((
input_text
,
sentiment_label
,
sentiment_score
))
history
.
append
((
input_text
,
sentiment_label
,
sentiment_score
,
emotion_label
,
emotion_score
))
update_history_display
()
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
=
entry
input_text
,
sentiment_label
,
sentiment_score
,
emotion_label
,
emotion_score
=
entry
history_text
.
insert
(
tk
.
END
,
f
"
{
i
+
1
}
.
\"
{
input_text
}
\"
- Sentiment:
{
sentiment_label
}
(
{
sentiment_score
}
)
\n\n
"
)
history_text
.
insert
(
tk
.
END
,
f
"
{
i
+
1
}
.
\"
{
input_text
}
\"
- Sentiment:
{
sentiment_label
}
(
{
sentiment_score
}
)
, Emotion:
{
emotion_label
}
(
{
emotion_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
(
"
700x
8
00
"
)
window
.
geometry
(
"
700x
7
00
"
)
window
.
title
(
"
Sentiment Analyzer
"
)
window
.
title
(
"
Sentiment Analyzer
"
)
# setting the style
# setting the style
...
@@ -115,7 +126,7 @@ history_label.pack()
...
@@ -115,7 +126,7 @@ 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
=
5
,
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
)
...
...
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