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 app.py 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)
Markdown(app)
#Run flask application on 4040 port
app.run('0.0.0.0', 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 __init__.py 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 myconfig.py file
app.config['SQLALCHEMY_DATABASE_URI'] = database_uri
app.app_context().push()
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
db.init_app(app)
with app.app_context():
db.create_all()
# Assign Login View
login_manager = LoginManager()
login_manager.login_view = "auth.login"
login_manager.init_app(app)
@login_manager.user_loader
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 models.py 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), default=func.now())
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 views.py 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"])
@login_required
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 (Change every "[" or "]" below on "{" and "}")
[% 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 ]]
</textarea>
<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>
</form>
[% if pst %]
[% filter markdown %]
[% endfilter %]
<hr/>
<div class="card shadow-sm" id="custom_card2">
<h4>Result</h4>
<p>[[ result|markdown ]]</p>
</div>
[% 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
Thanks
Top comments (0)