Coder Social home page Coder Social logo

prrao87 / fine-grained-sentiment-app Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 7.0 15.21 MB

A Flask LIME explainer app for fine-grained sentiment classification.

License: MIT License

Python 78.81% CSS 8.28% HTML 12.91%
flask web-app lime-explainer lime nlp interpretability visualization

fine-grained-sentiment-app's Introduction

An Explainer App for Fine Grained Sentiment Classification

This repo contains an initial prototype of an interactive application written in Flask, that explains the results of fine-grained sentiment classification, described in detail in this Medium Series.

A number of classifiers are implemented and their results explained using the LIME explainer. The classifers were trained on the Stanford Sentiment Treebank (SST-5) dataset. The class labels are any of [1, 2, 3, 4, 5], where 1 is very negative and 5 is very positive.

Installation

First, set up virtual environment and install from requirements.txt:

python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt

For further development, simply activate the existing virtual environment.

source venv/bin/activate

Usage

Run the file app.py and then enter a sentence, choose a type of classifier and click on the button Explain results!. We can then observe the features (i.e. words or tokens) that contributed to the classifier predicting a particular class label.

Demo for the front-end

The front-end app takes in a text sample and outputs LIME explanations for the different methods. The app is is deployed using Heroku at this location: https://sst5-explainer.herokuapp.com/

Play with your own text examples as shown below and see the fine-grained sentiment results explained!

NOTE: Because the PyTorch-based models (Flair and the causal transformer) are quite expensive to run inference with (they require a GPU), these methods are not deployed.

fine-grained-sentiment-app's People

Contributors

dependabot[bot] avatar prrao87 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fine-grained-sentiment-app's Issues

Lime explainer return empty graph

Hi I have create ML web app for taking input from user and show result to web app.User will fill my form on web app page and then flask will use model to predicted and then model will send result to web app page.Text result working but graph doesn't. Now i want to show explainer using LIME but when i save LIME graph it return empty graph,I already checked my error log.There are no error details

`model = pickle.load(open("./model/hr.pkl", "rb"))
 app = flask.Flask(__name__, template_folder='templates')


 @app.route('/', methods=['GET', 'POST'])
 def main():
 if flask.request.method == 'GET':
 # Just render the initial form, to get input
  return (flask.render_template('main.html'))

  if flask.request.method == 'POST':
  # Extract the input
   TotalWorkingYears = flask.request.form['TotalWorkingYears']
   OverTime_code = flask.request.form['OverTime_code']
    JobInvolvement = flask.request.form['JobInvolvement']
   JobRole_code = flask.request.form['JobRole_code']
   Age = flask.request.form['Age']
   WorkLifeBalance = flask.request.form['WorkLifeBalance']
   Gender_code = flask.request.form['Gender_code']
   DistanceFromHome = flask.request.form['DistanceFromHome']
   MaritalStatus_code = flask.request.form['MaritalStatus_code']
   YearsSinceLastPromotion = flask.request.form['YearsSinceLastPromotion']
   Education = flask.request.form['Education']
   PercentSalaryHike = flask.request.form['PercentSalaryHike']
   TrainingTimesLastYear = flask.request.form['TrainingTimesLastYear']
   JobLevel = flask.request.form['JobLevel']
YearsAtCompany = flask.request.form['YearsAtCompany']
DailyRate = flask.request.form['DailyRate']
YearsWithCurrManager = flask.request.form['YearsWithCurrManager']
MonthlyIncome = flask.request.form['MonthlyIncome']
JobSatisfaction = flask.request.form['JobSatisfaction']
EducationField_code = flask.request.form['EducationField_code']
RelationshipSatisfaction = flask.request.form['RelationshipSatisfaction']
MonthlyRate = flask.request.form['MonthlyRate']
BusinessTravel_code = flask.request.form['BusinessTravel_code']

# Make DataFrame for model
input_variables = pd.DataFrame([[TotalWorkingYears, OverTime_code, JobInvolvement,JobRole_code, Age, WorkLifeBalance,
                                 Gender_code, DistanceFromHome, MaritalStatus_code, YearsSinceLastPromotion,
                                 Education,PercentSalaryHike, TrainingTimesLastYear, JobLevel, YearsAtCompany, DailyRate,
                                 YearsWithCurrManager, MonthlyIncome, JobSatisfaction, EducationField_code,
                                 RelationshipSatisfaction, MonthlyRate, BusinessTravel_code]],
                               columns=['TotalWorkingYears', 'OverTime_code', 'JobInvolvement', 'JobRole_code',
                                        'Age','WorkLifeBalance', 'Gender_code', 'DistanceFromHome','MaritalStatus_code',
                                        'YearsSinceLastPromotion','Education','PercentSalaryHike','TrainingTimesLastYear','JobLevel',
                                        'YearsAtCompany','DailyRate','YearsWithCurrManager','MonthlyIncome','JobSatisfaction',
                                        'EducationField_code','RelationshipSatisfaction','MonthlyRate','BusinessTravel_code'],
                               dtype=float,
                               index=['input'])

# Get the model's prediction
prediction = model.predict(input_variables)[0]
prediction_percentage = model.predict_proba(input_variables)[:,1]

row_to_show = 1
data_for_prediction = input_variables.iloc[1]  # use 1 row of data here. Could use multiple rows if desired
data_for_prediction_array = data_for_prediction.values.reshape(1, -1)

model.predict_proba(data_for_prediction_array)
X_featurenames = input_variables.columns

categorical_features = np.argwhere(np.array([len(set(input_variables.values[0]))]))


predict_fn = lambda x: model.predict_proba(x).astype(float)

explainer = lime.lime_tabular.LimeTabularExplainer(input_variables.values,
feature_names=X_featurenames,
class_names=['Yes','No'],
categorical_features=categorical_features,
verbose=True, mode='classification')

exp = explainer.explain_instance(input_variables.values[0], predict_fn, num_features=5)
fig = exp.as_pyplot_figure()






if os.path.isfile("/home/Domemakarov2013/smart_hr/static/images/shap_graph/graph.svg"):
    os.remove("/home/Domemakarov2013/smart_hr/static/images/shap_graph/graph.svg")

      plt.savefig("/home/Domemakarov2013/smart_hr/static/images/shap_graph/graph.svg",

        dpi = 150,
        bbox_inches = 'tight')
   # plt.savefig('/home/Domemakarov2013/smart_hr/static/images/shap_graph/graph.svg')
else:
   # plt.savefig('/home/Domemakarov2013/smart_hr/static/images/shap_graph/graph.svg')
      plt.savefig("/home/Domemakarov2013/smart_hr/static/images/shap_graph/graph.svg",

        dpi = 150,
        bbox_inches = 'tight')`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.