Skip to content

Instantly share code, notes, and snippets.

View prrao87's full-sized avatar

Prashanth Rao prrao87

View GitHub Profile
@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'),
# ======== 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:'),
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():
# ========== Callbacks ================
@app.callback(Output('topic-data', 'data'), [Input('date-dropdown', 'value')])
def get_topic_data(value):
with pymongo.MongoClient(**MONGO_ARGS) as connection:
read_collection = connection[READ_DB][READ_COL]
data = read_collection.find({'_id': value})
# Collect data
data = list(data)[0]
# Perform some expensive calculation
data_transformed = expensive_calc(data)
# ========== 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': {
# ========== 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': {
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([
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):
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
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