Build, Save and Deploy your first Web App Using Flask and PythonAnywhere

Sample tutorial for getting started with Flask and PythonAnywhere

Amal Menzli
Nov 3 · 7 min read

Creating your first Web App for text summarization and deploy it with PythonAnywhere.

Introduction

Automatic Text Summarization is one of the most challenging and interesting problems in the field of Machine Learning and Natural Language Processing (NLP).

Through this article, we’re going to build a simple Application for text Summarization using Flask, a Python web application framework. If you love reading but you have no time for lengthy text, then our App will be the best solution for you.

In this tutorial, we will be able to build a Text Summarizer Web application with Flask and some great NLP packages like SpaCy, NLTK, Gensim and Sumy and host it on PythonAnywhere. We will call this app Summerise.

Table of contents

  1. NLP packages that we used in this App
  2. Building our Web App with Flask
  3. Deploy our App with PythonAnywhere
  4. What’s Next?

Text Summarization Approaches

Extraction-based summarization: These techniques involve pulling keyphrases from the source Text and combining them to make a summary. That’s why, identifying the right sentences for summarization is important in an extractive method.

Abstraction-based summarization: These techniques generate an entirely new summary by using advanced NLP techniques. When abstraction is applied for text summarization in deep learning problems, it can overcome the grammar inconsistencies of the extractive method.

However, the text summarization algorithms required to do abstraction are more difficult to develop, that’s why in this article and for simplicity purpose we will be focusing on the extractive summarization technique.

NLP packages that we used in this App

Spacy is the main competitor of the NLTK. These two libraries can be used for the same tasks.

Gensim is a module that provides functions for summarizing texts. Summarizing is based on ranks of text sentences using a variation of the TextRank algorithm.

Sumy is simple library and command line utility for extracting summary from plain texts. The package contains simple evaluation framework for text summaries.

Building our Web App with Flask

Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.

We assume that the user has prior experience with the following:

Setting up the development environment

But if you are not, that’s not a problem because I’m going to describe what you should need to set the environment.

In general, what we need:

  1. Download Anaconda for the operate system that’s relevant to you. For Windows you’re going to want to install the Python 3 version and we recommend the graphical installer. There’s a 64 bit version and a 32 bit version that depends on your computer. In fact, Anaconda’s going to come with lots of data science packages. Once you’ve downloaded Anaconda you should be able to run Python at your command line after you’ve finished the installation process.
Link to Download Anaconda

2. Since you’ve already set up Anaconda and Python all right. Now let’s set up our development environment. We can see that we have Visual studio in the Anaconda Navigator. You just need to Click on Launch Button.

Anaconda Navigator

Let’s show you how you can install useful extension for Web development:

  • In Visual Studio Code, go to extensions and Install Web Development Essentials Extension Pack. This is an extension pack with extensions that are extremely helpful for Web Developers (example: Live server, Bracket Pair Colorizor, Auto Rename Tag, etc.)

3. Now, we set up the virtual environment with the command line tool or the terminal exists in VS code (it’s optional to set up a virtual env but we recommend using it because you’re going to be able to run our code securely). Therefore, for convenience we’ve already created a requirements.txt file that you can use to install with single line all the libraries will be using.

In order to use this file at the command line, you need to cd (change directory) to wherever you saved the unzipped version of this code.

So let’s go to our command line, find the requirements.txt and show you how you can create a virtual environment and then easily install all the libraries required for the project.

Find requirements.txt in the folder
Create virtual environment

Let’s activate our virtual environment and install the packages we’ll need for this project:

For references purposes the code for this project is available on my GitHub.

Running The Application

To display the App locally run $python app.py on your terminal and head over to http://127.0.0.1:5000 to see your application.

Deploy our App with PythonAnywhere

1. Setting up an account:

Python Anywhere Signup Page

2. Go to the Web menu item and then press the Add new web app button, Click Next then click on Flask and click on the latest version of Python and then click Next again to accept the project path.

  1. In the Code section of the Web menu page click on Go to Directory next to Source Code.

2. Delete the flask_app.py file.

3. Use the Upload a file button to upload your own app.py Python file and create a templates and a static directory in the Directories section and upload your files there.

4. In the Code section of the Web menu page click on WSGI configuration file. Change flask_app with app and save the file.

Change wsgi.py file

5. Go to the Consoles, Click in Bash and install all the libraries required for this project.

6. Go to the Web menu page and click on the Reload button.

Now your Flask app should be running on https://yourusername.pythonanywhere.com. If you face any problems, just check your Error log File.

Start the App

What’s Next?

Specific algorithms for summarization:

  • Text summarization using RNNs and LSTM
  • Text summarization using Generative Adversarial Networks (GANs)

Add more tricks:

  • Calculate the number of words in the text
  • Calculate the time when summarized the text

Front-end Design:

  • Ameliorate the front-end design for our App to be more attractive

Conclusion

I will try to cover the abstractive text summarization technique in a future article. Meanwhile, feel free to use the comments section below to let me know your thoughts or ask any questions you might have on this article.

Happy reading, happy learning and happy coding.

Before You Go

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Amal Menzli

Written by

Transforming Data into Action Value — https://amalm7.github.io/

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade