Skip to content

Instantly share code, notes, and snippets.

Created March 18, 2021 10:18
What would you like to do?
import os
from flask import Flask, render_template, request
from ocr_extraction import ocr_extraction,pdf_extract
from lexnlp_extraction import extract_pii
# define folder to save the uploaded image
UPLOAD_FOLDER = 'static/uploads/'
# Allowed file image file extension type
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
app = Flask(__name__)
# validates file extension
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# route and function to handle the home page
def home_page():
return render_template('index.html')
# route and function to handle the upload page
@app.route('/upload', methods=['GET', 'POST'])
def upload_page():
if request.method == 'POST':
# check if there is a file in the request
if 'file' not in request.files:
return render_template('upload.html', msg='No file selected')
file = request.files.get('file')
# if no file is selected
if file.filename == '':
return render_template('upload.html', msg='No file selected')
if file and allowed_file(file.filename):,file.filename))
# OCR function extracts text
extracted_text = ocr_extraction(file)
# LexNLP extracts list of PIIs of possible different category
pii = ", ".join(map(str, extract_pii(extracted_text)))
# Sends the OCR extracted and LexMLP extracted texts
return render_template('upload.html',
msg='Successfully processed',
img_src=UPLOAD_FOLDER + file.filename)
return render_template('upload.html', msg='Please enter correct file form')
elif request.method == 'GET':
return render_template('upload.html')
if __name__ == '__main__':
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment