· Sep 18 5m read

InterSystems IRIS Flask Generative AI application

Hi Community

In this article, I will introduce my application IRIS-GenLab.

IRIS-GenLab is a generative AI Application that leverages the functionality of Flask web framework, SQLALchemy ORM, and InterSystems IRIS to demonstrate Machine Learning, LLM, NLP, Generative AI API, Google AI LLM, Flan-T5-XXL model, Flask Login and OpenAI ChatGPT use cases.

Application Features

  • User registration and authentication
  • Chatbot functionality with the help of Torch (python machine learning library)
  • Named entity recognition (NER), natural language processing (NLP) method for text information extraction
  • Sentiment analysis, NLP approch that identifies the emotional tone of the message
  • HuggingFace Text generation with the help of GPT2 LLM (Large Language Model) model and Hugging Face pipeline
  • Google PALM API, to access the advanced capabilities of Google’s large language models (LLM) like PaLM2
  • Google Flan-T5 XXL, a fine-tuned on a large corpus of text data that was not filtered for explicit contents.
  • OpenAI is a private research laboratory that aims to develop and direct artificial intelligence (AI)

Application Flow

Python file import 

#import genlab application
from genlab import create_app
from genlab.myconfig import *
from flaskext.markdown import Markdown

if __name__ == "__main__":
    # get db info from config file
    database_uri = f'iris://{DB_USER}:{DB_PASS}@{DB_URL}:{DB_PORT}/{DB_NAMESPACE}'
    # Invokes create_app function
    app = create_app(database_uri)
    #Run flask application on 4040 port'', port="4040", debug=False)

The above code invokes create_app() function and then runs the application on port 4040

create_app() function is defined in file, which create/modify database and initilize views

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from .myconfig import *

#init SQLAlChemy reference
db = SQLAlchemy()

def create_app(database_uri):
    app = Flask(__name__)
    app.config['SECRET_KEY'] = "iris-genlab"
    # Getting DB parameters from file
    app.config['SQLALCHEMY_DATABASE_URI'] = database_uri

    from .views import views
    from .auth import auth
    from .models import User
    #register blueprints
    app.register_blueprint(views, url_prefix="/")
    app.register_blueprint(auth, url_prefix="/")
    #init datbase
    with app.app_context():

    # Assign Login View
    login_manager = LoginManager()
    login_manager.login_view = "auth.login"

    def load_user(id):
        return User.query.get(int(id))

    return app

The above code creates the database by invoking SQLAlchemy create_all() function which will create user table based on structure defined in the file

from . import db
from flask_login import UserMixin
from sqlalchemy.sql import func

#User table
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(150), unique=True)
    username = db.Column(db.String(150), unique=True)
    password = db.Column(db.String(150))
    date_created = db.Column(db.DateTime(timezone=True),
    def __repr__(self):
        return f'{self.username}'

Named entity recognition (NER)

Named entity recognition with spaCy, a open-source library for Natural Language Processing (NLP) in Python
Navigate to to http://localhost:4040/ner, enter text and click on submit button to view the results


Above URL invoces ner() methon from file

from flask import Blueprint, render_template, request
from flask_login import login_required, current_user
from spacy import displacy
import spacy

HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem">{}</div>"""
views = Blueprint("views", __name__)

#Named Entitiy Recognition
@views.route('/ner', methods=["GET", "POST"])
def ner():
     if request.method == 'POST':            
            raw_text = request.form['rawtext']
            result = ''
            if len(raw_text.strip()) > 0:
               # Load English tokenizer, tagger, parser and NER
               nlp = spacy.load('en_core_web_sm')
               docx = nlp(raw_text)
               html = displacy.render(docx, style="ent")
               html = html.replace("\n\n", "\n")
               result = HTML_WRAPPER.format(html)
               return render_template('ner.html', user=current_user, result=result,rawtext = raw_text, pst=True )
     return render_template('ner.html', user=current_user, pst=False)

Below is the ner.html template file which inhertied from base.html

{% extends "base.html" %} {% block title %}Home{% endblock %} 

{% block head %}
      <h2 class="display-4">Named entity recognition</h2>
      <p>with spaCy, a open-source library for Natural Language Processing (NLP) in Python</p>
{% endblock %}

{% block content %}
<form method="POST">
	<textarea rows="7" required="true" name="rawtext" class="form-control txtarea-main">
		{{ rawtext }}
	<button type="submit" class="btn btn-info"><i class="fa fa-database"></i> Submit</button>
	<a class="btn btn-primary waves-effect" href="/" role="button"> <i class="fa fa-eraser"></i> Refresh</a>
{% if pst %}
{% filter markdown %}
{% endfilter %}
          <div class="card shadow-sm" id="custom_card2">
			<p>{{ result|markdown }}</p>
{% endif %}
{% endblock %}


Application Database

SQLALchemy will create below tables:

  • user: To store User information

To view table details, navigate to http://localhost:52775/csp/sys/exp/%25CSP.UI.Portal.SQL.Home.zen?$NAMESPACE=USER#

For more details please visit IRIS-GenLab open exchange application page


Discussion (0)1
Log in or sign up to continue