-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.py
More file actions
179 lines (154 loc) · 5.79 KB
/
demo.py
File metadata and controls
179 lines (154 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import tkinter as tk
from tkinter.scrolledtext import ScrolledText
from tkinter.font import Font
from PIL import Image,ImageTk
from model_utils import FullyConnected_NN,GRU,BiRNN,BiLSTM
class GUI:
'''Main class for GUI class'''
def __init__(self):
self.model_mapper = {
1: FullyConnected_NN,
2: BiRNN,
3: GRU,
4: BiLSTM
}
self.models = {
FullyConnected_NN : False,
BiRNN : False,
GRU : False,
BiLSTM:False
}
pass
def create_root(self):
"""Set up the window features for tkinter window"""
root = tk.Tk()
root.title('Review Sentiment Analyzer')
root.geometry('700x550+350+50') # widthXheight topx+topy
root.resizable(False, False)
root.configure(background='black')
self.root = root
self.model_choice = tk.IntVar(root,value=1)
return root
def add_textbox(self):
'''Add and configure the main textbox for input'''
self.hv16 = Font(family="Helvetica", size=16)
self.sc = ScrolledText(
master = self.root,
font=self.hv16,bg='#c0effc',
padx = 20 , pady=20
)
self.top_margin = 60
self.left_margin = 50
self.txt_width = 600
self.txt_height = 200
self.sc.place(
x = self.left_margin ,
y = self.top_margin ,
width= self.txt_width , height = self.txt_height
)
def add_buttons(self):
'''Adds buttons to the GUI'''
clearBtn = tk.Button(self.root , text = 'Clear',command = self.scrollText_command)
clearBtn.place(
x = self.left_margin + self.txt_width-120 ,
y = self.top_margin + self.txt_height+20,
width = 100 , height = 50,
)
submitBtn = tk.Button(self.root , text = 'Submit',command = self.submit_command)
submitBtn.place(
x = self.left_margin + self.txt_width-260 ,
y = self.top_margin + self.txt_height+20,
width = 100 , height = 50,
)
def add_radio_buttons(self):
#Create label for the algorithms
self.verdana16 = Font(self.root ,family = 'Verdana',size=14)
model_label = tk.Label(
self.root, text = 'Choose Model :-',
padx = 20 , pady = 8,font = self.verdana16
)
self.radio_label = (self.left_margin + 30,self.top_margin + self.txt_height+20 )
model_label.place(
x = self.radio_label[0] ,
y = self.radio_label[1]
)
#Option one
FullyConnected = tk.Radiobutton( self.root,
text= 'FullyConnected NN',
variable=self.model_choice,
value = 1 ,
width = 20,padx=5,pady=3,
justify='left'
)
FullyConnected.place(x = self.radio_label[0], y=self.radio_label[1]+ 60)
#Option two
RNN = tk.Radiobutton( self.root,
text= 'Recurrent Neural Network',
variable=self.model_choice,
value = 2,
width = 20,padx=5,pady=3,
justify='left'
)
RNN.place(x = self.radio_label[0], y=self.radio_label[1]+ 100)
#Option three
GRU_choice = tk.Radiobutton( self.root,
text= 'GRU network',
variable=self.model_choice,
value = 3,
width = 20,padx=5,pady=3,
justify='left'
)
GRU_choice.place(x = self.radio_label[0], y=self.radio_label[1]+ 140)
LSTM_choice = tk.Radiobutton( self.root,
text= 'LSTM network',
variable=self.model_choice,
value = 4,
width = 20,padx=5,pady=3,
justify='left'
)
LSTM_choice.place(x = self.radio_label[0], y=self.radio_label[1]+ 180)
def submit_command(self):
self.sentence = self.sc.get('1.0', tk.END)[:-1]
self.add_rating_image(self.sentence)
def scrollText_command(self):
# Note that sc.get() returns a \n at the end of the string.
self.sc.delete(1.0,tk.END)
self.rating_label.place_forget()
self.img_label.place_forget()
def add_rating_image(self,sentence):
#Load the model class's
model_type = self.model_mapper[self.model_choice.get()]
model_obj = self.models.get(model_type,False)
if not model_obj:
model_obj = self.models[model_type] = model_type()
# print(model_obj.model.summary())
current_rating = model_obj.get_rating(self.sentence)
#Adding Label for rating
self.verdana1 = Font(self.root ,family = 'Verdana',size=14)
self.rating_label = tk.Label(
self.root, text = 'Rating Prediction:',
padx = 40 , pady = 8,font = self.verdana16
)
self.rating_label.place(
x=self.left_margin+self.txt_width-280,
y = self.top_margin + self.txt_height + 100
)
# Adding image to model
img_name = f'images/{current_rating}star.jpg'
img = Image.open(img_name)
img = img.resize( (200,40) ,Image.ANTIALIAS)
rating_img = ImageTk.PhotoImage(img)
self.img_label = tk.Label(self.root, image=rating_img)
self.img_label.image = rating_img
self.img_label.place(
x=self.left_margin+self.txt_width-250,
y = self.top_margin + self.txt_height + 160
)
if __name__ == "__main__":
window = GUI()
root = window.create_root()
window.add_textbox()
window.add_buttons()
window.add_radio_buttons()
# window.add_rating_image()
root.mainloop()