In this tutorial we will be see how to render the named entities of a text extracted using spacy’s displacy in flask.
First of all the basic requirement is to use the following packages
- Flask
- Spacy
- Flask-Markdown
To install the various packages we will be using pip as below
pip install flask Flask-Markdown spacy
To work with spacy you will need a particular language model for the natural language processing. You can download the english model with the code below.
python -m spacy download en
Let us begin with building our app.
The Basic Structure of Our App (DisplaCify)
- Front -End
- Back-End
Working on the Back-End.
The main logic to be able to render displacy in flask is to parse it via markdown. Hence we will be using Flask-Markdown, a flask extension to enable us to be able to render our named entities in a very nice format in our front-end.
All we need is to import Markdown from the flaskext.markdown and wrap it around our app as below
from flask import Flask,url_for,render_template,requestfrom flaskext.markdown import Markdown #Init app = Flask(__name__) Markdown(app)
That is all we need to be able to work with the rendering of displacy’s results at the back-end.
We can then set a custom format for our html styling with
HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem">{}</div>"""
The full code for the back-end
from flask import Flask,url_for,render_template,request from flaskext.markdown import Markdown # NLP Pkgs import spacy from spacy import displacy nlp = spacy.load('en') import json # Init app = Flask(__name__) Markdown(app) @app.route('/') def index(): return render_template('index.html') @app.route('/extract',methods=["GET","POST"]) def extract(): if request.method == 'POST': raw_text = request.form['rawtext'] docx = nlp(raw_text) html = displacy.render(docx,style="ent") html = html.replace("\n\n","\n") result = HTML_WRAPPER.format(html) return render_template('result.html',rawtext=raw_text,result=result) @app.route('/previewer') def previewer(): return render_template('previewer.html') @app.route('/preview',methods=["GET","POST"]) def preview(): if request.method == 'POST': newtext = request.form['newtext'] result = newtext return render_template('preview.html',newtext=newtext,result=result) if __name__ == '__main__': app.run(debug=True)
For the Front-End
We will be using a basic form that uses POST as it method and a text-area to receive the user input and finally send the result to a route at the back-end.
<form method="POST" action="/extract" > <textarea rows="5" cols="5" required="true" name="rawtext" class="form-control"> </textarea> <button type="reset" class="btn btn-primary"><i class="fa fa-eraser"></i> Clear</button> <button type="submit" class="btn btn-info"><i class="fa fa-database"></i> Submit</button> </form>
We will be using material bootstrap for our UI in the front-end.
You can check the video tutorial here.
To get more about how to build machine learning and NLP apps you can check out this awesome course here
Thanks a lot for your time
Jesus Saves
By Jesse E.Agbemabiase (JCharis)