File size: 7,837 Bytes
87a586e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
180
181
182
183
184
185
186
187
188
189
import os

import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State

external_stylesheets = [dbc.themes.BOOTSTRAP]

#Model dependencies
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import pickle
import plotly
import plotly.graph_objects as go


app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server

def load_models():
    # Load my pre-trained Keras model
    global model, tokenizer
    model = load_model('model.h5')
    # load my original tokenizer used to build model
    with open('tokenizer.pkl', 'rb') as f:
        tokenizer = pickle.load(f)
    return model, tokenizer

model, tokenizer = load_models()

def prepare_text(text):
    '''Need to Tokenize text and pad sequences'''
    words = tokenizer.texts_to_sequences(text)
    words_padded = pad_sequences(words, maxlen = 150)

    return words_padded

# Initialize plot
fig = go.Figure(
                data=[go.Bar(x=["Toxic", "Severe toxic", "Obscene", "Threat", "Insult", "Identity hate"], 
                            y=[0, 0, 0, 0, 0, 0], 
                            marker=dict(color="#673EF1"),
                            width=[0.4]*6)],
                layout=go.Layout(
                    title=go.layout.Title(text="Your comment is...", font=dict(size=20)),
                    plot_bgcolor='rgba(0,0,0,0)',
                    font=dict(
                        family='Source Sans Pro'
                    ),
                    xaxis=dict(
                        tickfont=dict(size=16)
                    ),
                    yaxis=dict(
                        title_text="Probability",
                        tickmode="array",
                        tickfont=dict(size=16),
                        range=[0, 1]
                    ),
                    margin=dict(
                        b=100,
                        t=30,
                    ),
                    title_x=0.5
                )
            )

# Layout of the app
app.layout = html.Div(children=[
    html.Div(children=[
        html.Img(
            src='assets/eyes.gif',
            style={
                'width': '200px'
            })
        ], style={'textAlign': 'center', 'backgroundColor': '#000000'}),
    html.H1("The Toxic Comment Agent"),
    
    html.Div("Discussing things you care about can be difficult. The threat of abuse and harassment online means that many people stop expressing themselves and give up on seeking different opinions. This tool uses a multi-label model that can detect the type of toxicity of a comment.",
    style={'textAlign': 'center', 'margin-bottom': '40px', 'margin-left': '300px', 'margin-right': '300px'}),   
    
    html.H2("Enter a comment below and I'll predict what I think about it",  style={'fontSize': 24}),
    html.Div(children=[
        html.Img(
            src='assets/arrow.gif',
            style={
                'width': '50px'
            })
        ], style={'textAlign': 'center', 'margin-bottom': '20px'}),

    # Block of the text area
    html.Div(children=[
        dcc.Textarea(id = 'comment', value = '', style={'width': '50%', 'rows': '5'})
    ], style={'textAlign': 'center'}),

    # Check button
    html.Div(children=[
        html.Button(id = 'submit-button', n_clicks = 0, children = 'Check', className='button-submit')
    ], style={'textAlign': 'center', 'fontSize': 22, 'height': '100px', 'margin-bottom': '0px'}),

    # Display graph
    dcc.Graph(id='update-chart', figure=fig, style={
        'height': 300,
        'width': 700,
        "display": "block",
        "margin-left": "auto",
        "margin-right": "auto",
    }),

    html.Div([
        # About this project
        html.H2("About this project", style={'fontSize': 28, 'color': '#673EF1'}),
        html.Div('This predictive tool was built as part of a student project during our Post Master degree in Big Data at Télécom Paris. The data used to build the tool are from the Kaggle "Toxic Comment Classification Challenge" organized by Jigsaw and Conversation AI.',
        style={'textAlign': 'center', 'margin-left': '300px', 'margin-right': '300px', 'margin-top': '30px', 'margin-bottom': '30px'}),
        html.Div(children=[
            html.A([html.Img(src='assets/github-icon.png', style={'width': '30px'})], href='https://github.com/camillecochener/Toxic_comment_classification_challenge')
        ],
        style={'textAlign': 'center', 'margin-bottom': '40px'}),
        
        # About us
        html.H2("About us", style={'fontSize': 28, 'color': '#673EF1'}),
        html.Div(children=[
        dbc.Row([
            dbc.Col(html.Img(src='assets/camille.png', style={'width': '100px', 'margin-left': '10px', 'margin-right': '10px'})),
            dbc.Col(html.Img(src='assets/hamza.png', style={'width': '100px', 'margin-left': '10px', 'margin-right': '10px'})),
            dbc.Col(html.Img(src='assets/sophie.png', style={'width': '100px', 'margin-left': '10px', 'margin-right': '10px'})),
            dbc.Col(html.Img(src='assets/rodolphe.png', style={'width': '100px', 'margin-left': '10px', 'margin-right': '10px'}))
        ]),
        dbc.Row([
            dbc.Col(html.Div("Camille COCHENER", style={'margin-left': '10px', 'margin-right': '10px'})),
            dbc.Col(html.Div("Hamza AMRI", style={'margin-left': '10px', 'margin-right': '10px'})),
            dbc.Col(html.Div("Sophie LEVEUGLE", style={'margin-left': '10px', 'margin-right': '10px'})),
            dbc.Col(html.Div("Rodolphe SIMONEAU", style={'margin-left': '10px', 'margin-right': '10px'})),
        ])
        ], style={'textAlign': 'center', 'height': '100px', 'margin-left':'300px', 'margin-right': '300px', 'margin-top': '30px', 'margin-bottom': '30px'}), 
        html.Div('© Copyright TheToxicCommentAgent', style={'textAlign': 'center', 'margin-top': '40px'})
    ], style={'backgroundColor': '#F6F6F6'}) 
    
])

@app.callback(Output('update-chart', 'figure'),
    [Input('submit-button', 'n_clicks')],   
    [State('comment', 'value')])
def predict_text(submit, comment):
    if comment is '':
        empty_fig = go.Figure(
                data=[go.Bar(x=["Toxic", "Severe toxic", "Obscene", "Threat", "Insult", "Identity hate"], 
                            y=[0, 0, 0, 0, 0, 0],
                            marker=dict(color="#673EF1"),
                            width=[0.4]*6)],
                layout=go.Layout(
                    title=go.layout.Title(text="Your comment is...", font=dict(size=20)),
                    plot_bgcolor='rgba(0,0,0,0)',
                    font=dict(
                        family='Source Sans Pro'
                    ),
                    xaxis=dict(
                        tickfont=dict(size=16)
                    ),
                    yaxis=dict(
                        title_text="Probability",
                        tickmode="array",
                        tickfont=dict(size=16),
                        range=[0, 1]
                    ),
                    margin=dict(
                        b=100,
                        t=30,
                    ),
                    title_x=0.5
                )
            )
        return empty_fig
    else:
        try:
            clean_text = prepare_text([comment])
            preds = model.predict(clean_text)
            print(preds[0])
            yvalue = [i for i in preds[0]]
            return fig.update_traces(y=yvalue)
        except ValueError as e:
            print(e)
            return "The text area is empty."

if __name__ == '__main__':
    app.run_server(debug=False, threaded = False)