-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreal_time_test.py
More file actions
154 lines (139 loc) · 5.83 KB
/
real_time_test.py
File metadata and controls
154 lines (139 loc) · 5.83 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
import dash
import dash_table
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from dash.dependencies import Input, Output, State
from detector import Detector, translate
from langdetect import detect as get_lang
import assets.strings as strings
CATEGORIES = ["En", "Es", "Fr", "De"]
detector = Detector()
bleu_table = pd.read_csv('assets/final_bleu_scores.csv').round(3)
meteor_table = pd.read_csv('assets/final_meteor_scores.csv').round(3)
example_table = pd.read_csv('assets/gen_table_example.csv').round(3)
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.title = "LangDetector"
intro_md = open('assets/intro.md').read()
app.layout = html.Div(children=[
dcc.Markdown(children=intro_md, className='md-intro'),
html.Div(
className='tables-container',
children=[
html.H5(strings.BLEU_SCORES, className='table-title'),
dash_table.DataTable(
id='bleu_table',
columns=[{"name": i, "id": i} for i in bleu_table.columns],
data=bleu_table.to_dict('records'),
style_cell={'textAlign': 'center'},
),
html.H5(strings.METEOR_SCORES, className='table-title'),
dash_table.DataTable(
id='meteor_table',
columns=[{"name": i, "id": i} for i in meteor_table.columns],
data=meteor_table.to_dict('records'),
style_cell={'textAlign': 'center'},
)
]
),
html.P(strings.TABLE_GEN_EXPL),
html.Img(src='assets/Back-Translations.png'),
html.P(strings.TABLE_EXAMPLE, style={'marginTop': '2rem'}),
dash_table.DataTable(id='example_table', columns=[
{"name": i, "id": i} for i in example_table.columns], data=example_table.to_dict('records'), style_cell={'textAlign': 'center', 'marginLeft': '1em', 'marginRight': '1em'}),
html.P(strings.CLASSIFIER_INTRO),
html.Img(src='assets/scores.png'),
html.H2(strings.TEST_AREA, style={
'marginTop': '4em', 'fontWeight': 'bold'}),
html.Div(
children=[
html.Div(children=[
html.P(strings.TRANSLATE_DESCRIPTION),
dcc.RadioItems(
id='lang-chooser',
options=[
{'label': 'En', 'value': 'en'},
{'label': 'Es', 'value': 'es'},
{'label': 'Fr', 'value': 'fr'},
{'label': 'De', 'value': 'de'},
],
value='en',
labelStyle={'display': 'inline-block'}
),
dcc.Textarea(
id='translation-input',
style={'width': '100%', 'height': 200},
),
html.Button(children=strings.TRANSLATE_BTN_LABEL, id='translate-btn',
n_clicks=0, className="input-btn"),
dcc.Loading(id='translate-loader', type='circle', children=[
html.Div(id='translate-output-container', children="",
className="output-container")
], parent_className='loader')
], className="input-div"),
html.Div(children=[
html.P(strings.DETECT_DESCRIPTION),
dcc.RadioItems(
id='algorithm-chooser',
options=[
{'label': 'Red Neuronal', 'value': 'nn'},
{'label': 'SVM', 'value': 'svm'},
{'label': 'Árbol de Decisiones', 'value': 'dt'},
{'label': 'KNN', 'value': 'knn'},
],
value='nn',
labelStyle={'display': 'inline-block'}
),
dcc.Textarea(
id='detection-input',
style={'width': '100%', 'height': 200},
),
html.Button(children=strings.DETECT_BTN_LABEL, id='detect-btn',
n_clicks=0, className="input-btn"),
dcc.Loading(id='detect-loader', type='circle', children=[
html.Div(id='detect-output-container', children="",
className="output-container")
], parent_className='loader')
], className="input-div")
], className="testing-container"
),
], className="app-container")
def output_div(pred):
choice = CATEGORIES[pred.index(max(pred))]
return html.Div(children=[
html.Div(strings.EN_PROBABILITY.format(
pred[0])),
html.Div(strings.ES_PROBABILITY.format(
pred[1])),
html.Div(strings.FR_PROBABILITY.format(
pred[2])),
html.Div(strings.DE_PROBABILITY.format(
pred[3])),
html.Div(),
html.Div(strings.FINAL_PRED.format(choice),
className="prediction-choice")
], className="output-div")
@app.callback(
Output('detect-output-container', 'children'),
[Input('detect-btn', 'n_clicks')],
[State('detection-input', 'value'), State('algorithm-chooser', 'value')]
)
def update_output_div(n_clicks, text, algorithm):
if text is not None and text != '':
src = get_lang(text)
pred = detector.predict(text, src, algorithm)
return output_div(pred)
return ""
@app.callback(
Output('translate-output-container', 'children'),
[Input('translate-btn', 'n_clicks')],
[State('translation-input', 'value'), State('lang-chooser', 'value')]
)
def update_translation_output(n_clicks, text, tgt):
if text is not None and text != '':
src = get_lang(text)
return translate(text, src, tgt)
return ""
if __name__ == '__main__':
app.run_server(debug=True)