This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @app.callback(Output('text-input', 'value'), | |
| Input('reset-button', 'n_clicks')) | |
| def clear_form(n_clicks): | |
| """Empty input textarea""" | |
| return "" | |
| @app.callback(Output('explainer-obj', 'children'), | |
| Input('submit-button', 'n_clicks'), | |
| Input('reset-button', 'n_clicks'), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ======== App layout ======== | |
| app.layout = html.Div([ | |
| html.H3(''' | |
| LIME Explainer Dashboard for Fine-grained Sentiment | |
| ''', style={'text-align': 'center'}), | |
| html.Label(''' | |
| 1: Strongly Negative 2: Weakly Negative 3: Neutral 4: Weakly Positive 5: Strongly Positive | |
| ''', style={'text-align': 'center'}), | |
| html.Br(), | |
| html.Label('Enter your text:'), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| from flask import Flask, request, render_template | |
| from lime_explainer import explainer, tokenizer, METHODS | |
| app = Flask(__name__) | |
| SECRET_KEY = os.urandom(24) | |
| @app.route('/') | |
| @app.route('/result', methods=['POST']) | |
| def index(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ========== Callbacks ================ | |
| @app.callback(Output('line-chart-1', 'figure'), | |
| [Input('dropdown-1', 'value')]) | |
| def update_graph(selected_dropdown_value): | |
| dff = filter_df(selected_dropdown_value) | |
| return { | |
| 'data': [{ | |
| 'x': dff.Date, | |
| 'y': dff.QuoteCount, | |
| 'line': { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ========== Callbacks ================ | |
| @app.callback(Output('line-chart-1', 'figure'), | |
| [Input('dropdown-1', 'value')]) | |
| def update_graph(selected_dropdown_value): | |
| dff = df[df['Date'] == selected_dropdown_value] | |
| return { | |
| 'data': [{ | |
| 'x': dff.Date, | |
| 'y': dff.QuoteCount, | |
| 'line': { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import dash | |
| import dash_core_components as dcc | |
| import dash_html_components as html | |
| import pandas as pd | |
| app = dash.Dash('app', server=server) | |
| df = pd.read_csv('input_data.csv') | |
| # ========== App Layout ================ | |
| app.layout = html.Div([ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from joblib import Parallel, delayed | |
| def chunker(iterable, total_length, chunksize): | |
| return (iterable[pos: pos + chunksize] for pos in range(0, total_length, chunksize)) | |
| def flatten(list_of_lists): | |
| "Flatten a list of lists to a combined list" | |
| return [item for sublist in list_of_lists for item in sublist] | |
| def process_chunk(texts): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def lemmatize_pipe(doc): | |
| lemma_list = [str(tok.lemma_).lower() for tok in doc | |
| if tok.is_alpha and tok.text.lower() not in stopwords] | |
| return lemma_list | |
| def preprocess_pipe(texts): | |
| preproc_pipe = [] | |
| for doc in nlp.pipe(texts, batch_size=20): | |
| preproc_pipe.append(lemmatize_pipe(doc)) | |
| return preproc_pipe |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def lemmatize(text): | |
| """Perform lemmatization and stopword removal in the clean text | |
| Returns a list of lemmas | |
| """ | |
| doc = nlp(text) | |
| lemma_list = [str(tok.lemma_).lower() for tok in doc | |
| if tok.is_alpha and tok.text.lower() not in stopwords] | |
| return lemma_list |