InterSystems IRIS Flask Generative AI application
Tags: ai, chatgpt, machinelearning, python
Published: 2024-03-23 16:51:23


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6m7oi2mx4mo5ro9q5yxj.png)
<p><br>Hi Community<br><br>In this article, I will introduce my application&nbsp;<a href="https://openexchange.intersystems.com/package/iris-GenLab" rel="nofollow"><strong>IRIS-GenLab</strong></a>.<br><br>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.</p>
<h2>Application Features</h2>
<ul>
<li>User registration and authentication</li>
<li>Chatbot functionality with the help of Torch (python machine learning library)</li>
<li>Named entity recognition (NER), natural language processing (NLP) method for text information extraction</li>
<li>Sentiment analysis, NLP approch that identifies the emotional tone of the message</li>
<li>HuggingFace Text generation with the help of GPT2 LLM (Large Language Model) model and Hugging Face pipeline</li>
<li>Google PALM API, to access the advanced capabilities of Google’s large language models (LLM) like PaLM2</li>
<li>Google Flan-T5 XXL, a fine-tuned on a large corpus of text data that was not filtered for explicit contents.</li>
<li>OpenAI is a private research laboratory that aims to develop and direct artificial intelligence (AI)</li>
</ul>
<h2><br>Application Flow</h2>
<p>Python app.py file import&nbsp;</p>
<pre class="codeblock-container" idlang="2" lang="Python" tabsize="4"><code class="language-python hljs"><span class="hljs-comment">#import genlab application</span>
<span class="hljs-keyword">from</span> genlab <span class="hljs-keyword">import</span> create_app
<span class="hljs-keyword">from</span> genlab.myconfig <span class="hljs-keyword">import</span> *
<span class="hljs-keyword">from</span> flaskext.markdown <span class="hljs-keyword">import</span> Markdown

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    <span class="hljs-comment"># get db info from config file</span>
    database_uri = <span class="hljs-string">f'iris://<span class="hljs-subst">{DB_USER}</span>:<span class="hljs-subst">{DB_PASS}</span>@<span class="hljs-subst">{DB_URL}</span>:<span class="hljs-subst">{DB_PORT}</span>/<span class="hljs-subst">{DB_NAMESPACE}</span>'</span>
    <span class="hljs-comment"># Invokes create_app function</span>
    app = create_app(database_uri)
    Markdown(app)
    <span class="hljs-comment">#Run flask application on 4040 port</span>
    app.run(<span class="hljs-string">'0.0.0.0'</span>, port=<span class="hljs-string">"4040"</span>, debug=<span class="hljs-keyword">False</span>)</code></pre>
<p>The above code invokes create_app() function and then runs the application on port 4040<br><br>create_app() function is defined in&nbsp;__init__.py file, which create/modify database and initilize views</p>
<pre class="codeblock-container" idlang="2" lang="Python" tabsize="4"><code class="language-python hljs"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask
<span class="hljs-keyword">from</span> flask_sqlalchemy <span class="hljs-keyword">import</span> SQLAlchemy
<span class="hljs-keyword">from</span> flask_login <span class="hljs-keyword">import</span> LoginManager
<span class="hljs-keyword">from</span> .myconfig <span class="hljs-keyword">import</span> *

<span class="hljs-comment">#init SQLAlChemy reference</span>
db = SQLAlchemy()

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create_app</span><span class="hljs-params">(database_uri)</span>:</span>
    app = Flask(__name__)
    app.config[<span class="hljs-string">'SECRET_KEY'</span>] = <span class="hljs-string">"iris-genlab"</span>
    <span class="hljs-comment"># Getting DB parameters from myconfig.py file</span>
    app.config[<span class="hljs-string">'SQLALCHEMY_DATABASE_URI'</span>] = database_uri
    app.app_context().push()

    <span class="hljs-keyword">from</span> .views <span class="hljs-keyword">import</span> views
    <span class="hljs-keyword">from</span> .auth <span class="hljs-keyword">import</span> auth
    <span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> User
    <span class="hljs-comment">#register blueprints</span>
    app.register_blueprint(views, url_prefix=<span class="hljs-string">"/"</span>)
    app.register_blueprint(auth, url_prefix=<span class="hljs-string">"/"</span>)
    <span class="hljs-comment">#init datbase</span>
    db.init_app(app)
    <span class="hljs-keyword">with</span> app.app_context():
        db.create_all()

    <span class="hljs-comment"># Assign Login View</span>
    login_manager = LoginManager()
    login_manager.login_view = <span class="hljs-string">"auth.login"</span>
    login_manager.init_app(app)

<span class="hljs-meta">    @login_manager.user_loader</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">load_user</span><span class="hljs-params">(id)</span>:</span>
        <span class="hljs-keyword">return</span> User.query.get(int(id))

    <span class="hljs-keyword">return</span> app
</code></pre>
<p>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</p>
<pre class="codeblock-container" idlang="2" lang="Python" tabsize="4"><code class="language-python hljs"><span class="hljs-keyword">from</span> . <span class="hljs-keyword">import</span> db
<span class="hljs-keyword">from</span> flask_login <span class="hljs-keyword">import</span> UserMixin
<span class="hljs-keyword">from</span> sqlalchemy.sql <span class="hljs-keyword">import</span> func

<span class="hljs-comment">#User table</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span><span class="hljs-params">(db.Model, UserMixin)</span>:</span>
    id = db.Column(db.Integer, primary_key=<span class="hljs-keyword">True</span>)
    email = db.Column(db.String(<span class="hljs-number">150</span>), unique=<span class="hljs-keyword">True</span>)
    username = db.Column(db.String(<span class="hljs-number">150</span>), unique=<span class="hljs-keyword">True</span>)
    password = db.Column(db.String(<span class="hljs-number">150</span>))
    date_created = db.Column(db.DateTime(timezone=<span class="hljs-keyword">True</span>), default=func.now())
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__repr__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">f'<span class="hljs-subst">{self.username}</span>'</span></code></pre>
<h2><br>Named entity recognition (NER)</h2>
<p>Named entity recognition with spaCy, a open-source library for Natural Language Processing (NLP) in Python<br>Navigate to&nbsp;to&nbsp;<a href="http://localhost:52775/csp/sys/exp/%25CSP.UI.Portal.SQL.Home.zen?$NAMESPACE=USER#"><strong>http://localhost:4040/ner</strong></a>, enter text and click on submit button to view the results<br><br><img alt="image" src="https://user-images.githubusercontent.com/18219467/268516746-03116a37-e6e9-4029-adb8-e8ccab8985cf.png"></p>
<p>Above URL invoces ner() methon from views.py file</p>
<pre class="codeblock-container" idlang="2" lang="Python" tabsize="4"><code class="language-python hljs"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Blueprint, render_template, request
<span class="hljs-keyword">from</span> flask_login <span class="hljs-keyword">import</span> login_required, current_user
<span class="hljs-keyword">from</span> spacy <span class="hljs-keyword">import</span> displacy
<span class="hljs-keyword">import</span> spacy


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

<span class="hljs-comment">#Named Entitiy Recognition</span>
<span class="hljs-meta">@views.route('/ner', methods=["GET", "POST"])</span>
<span class="hljs-meta">@login_required</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">ner</span><span class="hljs-params">()</span>:</span>
     <span class="hljs-keyword">if</span> request.method == <span class="hljs-string">'POST'</span>:            
            raw_text = request.form[<span class="hljs-string">'rawtext'</span>]
            result = <span class="hljs-string">''</span>
            <span class="hljs-keyword">if</span> len(raw_text.strip()) &gt; <span class="hljs-number">0</span>:
               <span class="hljs-comment"># Load English tokenizer, tagger, parser and NER</span>
               nlp = spacy.load(<span class="hljs-string">'en_core_web_sm'</span>)
               docx = nlp(raw_text)
               html = displacy.render(docx, style=<span class="hljs-string">"ent"</span>)
               html = html.replace(<span class="hljs-string">"\n\n"</span>, <span class="hljs-string">"\n"</span>)
               result = HTML_WRAPPER.format(html)
               <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'ner.html'</span>, user=current_user, result=result,rawtext = raw_text, pst=<span class="hljs-keyword">True</span> )
        
     <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'ner.html'</span>, user=current_user, pst=<span class="hljs-keyword">False</span>)

</code></pre>
<p>Below is the ner.html template file which inhertied from base.html (Change every "[" or "]" below on "{" and "}")</p>
<pre class="codeblock-container" idlang="7" lang="HTML" tabsize="4"><code class="language-html hljs XML">[% extends"base.html" %] [% block title %]Home[% endblock %] 

[% block head %]
      <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"display-4"</span>&gt;</span>Named entity recognition<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>with spaCy, a open-source library for Natural Language Processing (NLP) in Python<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
[% endblock %]


[% block content %]
<span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">method</span>=<span class="hljs-string">"POST"</span>&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">textarea</span> <span class="hljs-attr">rows</span>=<span class="hljs-string">"7"</span> <span class="hljs-attr">required</span>=<span class="hljs-string">"true"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"rawtext"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form-control txtarea-main"</span>&gt;</span>
		[[ rawtext ]]
	<span class="hljs-tag">&lt;/<span class="hljs-name">textarea</span>&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-info"</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fa fa-database"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span> Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-primary waves-effect"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">role</span>=<span class="hljs-string">"button"</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fa fa-eraser"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span> Refresh<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
[% if pst %]
[% filter markdown %]
[% endfilter %]
<span class="hljs-tag">&lt;<span class="hljs-name">hr</span>/&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card shadow-sm"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"custom_card2"</span>&gt;</span>
          	<span class="hljs-tag">&lt;<span class="hljs-name">h4</span>&gt;</span>Result<span class="hljs-tag">&lt;/<span class="hljs-name">h4</span>&gt;</span>
			<span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>[[ result|markdown ]]<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
		<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
[% endif %]
[% endblock %]

</code></pre>
<h2>&nbsp;</h2>
<h2>Application Database</h2>
<p>SQLALchemy will create below tables:</p>
<ul>
<li><strong>user</strong>: To store User information</li>
</ul>
<p>To view table details, navigate to&nbsp;<a href="http://localhost:52775/csp/sys/exp/%25CSP.UI.Portal.SQL.Home.zen?$NAMESPACE=USER#"><strong>http://localhost:52775/csp/sys/exp/%25CSP.UI.Portal.SQL.Home.zen?$NAMESPACE=USER#</strong></a><br><img alt="image" src="https://user-images.githubusercontent.com/18219467/268517007-a4d5d474-65eb-4026-a59b-7727fe7c592d.png"></p>
<p>For more details please visit <a href="https://openexchange.intersystems.com/package/IRIS-GenLab" rel="nofollow"><strong>IRIS-GenLab</strong></a>&nbsp;open exchange application page<br><br>Thanks</p>