def _serve_web(template, notebook_mode=True, ip='127.0.0.1', port=5000, n_retries=100, width='100%', height=500, **kwargs): if notebook_mode and not is_notebook(): logger.warning( '`notebook_mode=True` but the code is not running in Jupyter Notebook, will disable `notebook_mode`' ) notebook_mode = False port = find_open_port(ip, port, n_retries) if notebook_mode: from IPython.display import IFrame, display, HTML html = escape(template) iframe = ( '<div style="width:{width};height:{height}px">' '<div style="position:relative;width:100%;height:0;padding-bottom:{ratio};">' # noqa '<iframe srcdoc="{html}" style="position:absolute;width:100%;height:{height}px;left:0;top:0;' # noqa 'border:none !important;" ' 'allowfullscreen webkitallowfullscreen mozallowfullscreen>' '</iframe>' '</div></div>').format(html=html, width=width, height=height, ratio=1) display(HTML(iframe)) else: logger.info(f'Serving to http://{ip}:{port}/') serve(template, ip=ip, port=port, **kwargs)
def _render_binary(data, notebook_mode = False): index_negative = data['barplot']['x'].index('negative') index_positive = data['barplot']['x'].index('positive') index_neutral = data['barplot']['x'].index('neutral') sentiment_mark = [] for k, v in data['word'].items(): sentiment_mark.append( _sentiment_mark( k, v[index_negative], v[index_positive], v[index_neutral], data['alphas'][k], data['barplot']['x'][np.argmax(v)], ) ) sentiment_mark = ' '.join(sentiment_mark) this_dir = os.path.dirname(__file__) if notebook_mode: js_location, css_location = _upload_jupyter() else: js_location = 'static/echarts.min.js' css_location = 'static/admin-materialize.min.css' with open(os.path.join(this_dir, 'web', 'index.html')) as _file: template = string.Template(_file.read()) template = template.substitute( label = escape(data['class_name']), p = sentiment_mark, barplot_positive = escape( json.dumps(int(data['barplot']['y'][index_positive])) ), barplot_neutral = escape( json.dumps(int(data['barplot']['y'][index_neutral])) ), barplot_negative = escape( json.dumps(int(data['barplot']['y'][index_negative])) ), histogram_x = escape(json.dumps(data['histogram']['x'].tolist())), histogram_y = escape(json.dumps(data['histogram']['y'].tolist())), attention_x = escape(json.dumps(data['attention']['x'].tolist())), attention_y = escape(json.dumps(data['attention']['y'].tolist())), css_location = css_location, js_location = js_location, ) if notebook_mode: from IPython.display import display, HTML display(HTML(template)) else: serve(template)
def _render_emotion(data, notebook_mode=False): index_anger = data['barplot']['x'].index('anger') index_fear = data['barplot']['x'].index('fear') index_joy = data['barplot']['x'].index('joy') index_love = data['barplot']['x'].index('love') index_sadness = data['barplot']['x'].index('sadness') index_surprise = data['barplot']['x'].index('surprise') emotion_mark = [] for k, v in data['word'].items(): where = np.where(np.array(v) >= 0.3)[0].shape[0] if where: where = data['barplot']['x'][np.argmax(v)] else: where = 'neutral' emotion_mark.append( _emotion_mark( k, v[index_anger], v[index_fear], v[index_joy], v[index_love], v[index_sadness], v[index_surprise], data['alphas'][k], where, )) emotion_mark = ' '.join(emotion_mark) this_dir = os.path.dirname(__file__) if notebook_mode: js_location, css_location = _upload_jupyter() else: js_location = 'static/echarts.min.js' css_location = 'static/admin-materialize.min.css' with open(os.path.join(this_dir, 'web', 'index_emotion.html')) as _file: template = string.Template(_file.read()) template = template.substitute( label=escape(data['class_name']), p=emotion_mark, barplot_anger=escape(json.dumps(int( data['barplot']['y'][index_anger]))), barplot_fear=escape(json.dumps(int(data['barplot']['y'][index_fear]))), barplot_joy=escape(json.dumps(int(data['barplot']['y'][index_joy]))), barplot_love=escape(json.dumps(int(data['barplot']['y'][index_love]))), barplot_sadness=escape( json.dumps(int(data['barplot']['y'][index_sadness]))), barplot_surprise=escape( json.dumps(int(data['barplot']['y'][index_surprise]))), histogram_x=escape(json.dumps(data['histogram']['x'].tolist())), histogram_y=escape(json.dumps(data['histogram']['y'].tolist())), attention_x=escape(json.dumps(data['attention']['x'].tolist())), attention_y=escape(json.dumps(data['attention']['y'].tolist())), css_location=css_location, js_location=js_location, ) if notebook_mode: from IPython.display import display, HTML display(HTML(template)) else: serve(template)
def _render_toxic(data, notebook_mode=False): index_toxic = data['barplot']['x'].index('toxic') index_severe_toxic = data['barplot']['x'].index('severe_toxic') index_obscene = data['barplot']['x'].index('obscene') index_threat = data['barplot']['x'].index('threat') index_insult = data['barplot']['x'].index('insult') index_identity_hate = data['barplot']['x'].index('identity_hate') toxic_mark = [] for k, v in data['word'].items(): where = np.where(np.array(v) >= 0.5)[0].shape[0] if where: where = data['barplot']['x'][np.argmax(v)] else: where = 'neutral' toxic_mark.append( _toxic_mark( k, v[index_toxic], v[index_severe_toxic], v[index_obscene], v[index_threat], v[index_insult], v[index_identity_hate], data['alphas'][k], where, )) toxic_mark = ' '.join(toxic_mark) this_dir = os.path.dirname(__file__) if notebook_mode: js_location, css_location = _upload_jupyter() else: js_location = 'static/echarts.min.js' css_location = 'static/admin-materialize.min.css' with open(os.path.join(this_dir, 'web', 'index_toxic.html')) as _file: template = string.Template(_file.read()) template = template.substitute( label=escape(data['class_name']), p=toxic_mark, barplot_toxic=escape(json.dumps(int( data['barplot']['y'][index_toxic]))), barplot_severe_toxic=escape( json.dumps(int(data['barplot']['y'][index_severe_toxic]))), barplot_obscene=escape( json.dumps(int(data['barplot']['y'][index_obscene]))), barplot_threat=escape( json.dumps(int(data['barplot']['y'][index_threat]))), barplot_insult=escape( json.dumps(int(data['barplot']['y'][index_insult]))), barplot_identity_hate=escape( json.dumps(int(data['barplot']['y'][index_identity_hate]))), histogram_x=escape(json.dumps(data['histogram']['x'].tolist())), histogram_y=escape(json.dumps(data['histogram']['y'].tolist())), attention_x=escape(json.dumps(data['attention']['x'].tolist())), attention_y=escape(json.dumps(data['attention']['y'].tolist())), css_location=css_location, js_location=js_location, ) if notebook_mode: from IPython.display import display, HTML display(HTML(template)) else: serve(template)