Building a Fake News Classifier & Deploying it Using Flask

Ravi Dahiya
Analytics Vidhya
Published in
4 min readDec 11, 2019

Before we begin, why not give the fake news classification app a try.

As a budding Data Scientists, we feel very proud to have worked on a problem and developed a model to solve it. We work through the tiring process of cleaning the data, visualising it, preparing it for the model and then going through the iterative process of developing & improving the model.

It seems that most of the “Data Sciency Stuff” seems to have ended at this stage. So, what is beyond that?

That’s why you are here. We are going to learn to deploy our model and make it interactive. But before going forward, I want you to get familiar with the following:

  1. Pickle: “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream is converted back into an object hierarchy. For detailed information please go to official documentation here.
  2. Flask App: It is a Web Server Gateway Interface (WSGI) web application framework designed to create web apps. It is super convenient and the code is simple. Official Documents here.
  3. Newspaper3k Library: Newspaper is an amazing python library for extracting & curating articles. Official documentation explain it well.
  4. TF-IDF: Term Frequency-Inverse Document Frequency is a numerical statistic that is intended to reflect how important a word is to a document in a collection or corpus. Read about it here.
  5. Multinomial Naive Bayes: It relies on Relies on very simple representation of document as a ‘Bag of words’. It estimates the conditional probability of a particular word given a class as the relative frequency of term t in documents belonging to class(c), in our case Fake or Real. A simple explanation on this link.

Building the model

Time to roll and target those fake news articles. First thing first, let’s build the model using the usual stuff. I am putting up the screenshot of the python file that will be used to deploy the model. I will not be able to explain the what’s going on in detail as our main focus is model deployment. Comments should be self-explainatory.

Developing the News Classification Model

The model performs pretty well in detecting the fake news with 96% precision for Fake news and 78% for real news

Classification report and confusion matrix

Sweet !! Let’s make our web page using HTML

For this, we have to make a new folder called ‘templates’ in our working directory and then creating new HTML file called ‘main.html’.

It takes url from the user and outputs the result on the page

Time to make our Flask App

Let’s make our flask app in a new python file.

Interesting thing to know is that we used ‘Newspaper3k’ library to extract the news article from the news url. It then feeds the article to the model and model returns whether the article is fake or real.

The Magic

Now we can simply run the file containing the flask app and it will give you a url on command prompt. Just copy and paste it in the browser. It will look like this.

Just put in the url and click predict
‘Voila’ it works

Let’s take this on the internet

Well taking it online is pretty simple with heroku. We just need to add couple of extra files in our working directory. They mostly correspond to the libraries that heroku needs to import while building and deploying the app. Those are available on the heroku website. So, you just need to sign in, choose name of your app, connect your github repository and deploy. The steps and terminal commands are available on the website. At the end of the process the repository looks like this.

As soon as the model is deployed, heroku will give the name of the website on which the app is hosted.

Live app

That’s it !! We are done. So, in your next project, why not go beyond and create an app for the model that you have made.

All the files are available on my github repository. Please hit a clap if you learnt something new and let me know if you need any help in recreating this.

--

--