From b5c4bdce27ca7bc75c91dc28223e12ec1be2ea47 Mon Sep 17 00:00:00 2001 From: navanchauhan Date: Mon, 28 Jun 2021 00:48:18 +0530 Subject: added CoreML Chatbot --- ...2021-06-26-Cheminformatics-On-The-Web-2021.html | 27 ++-- ...21-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html | 161 +++++++++++++++++++++ docs/posts/index.html | 15 ++ 3 files changed, 188 insertions(+), 15 deletions(-) create mode 100644 docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html (limited to 'docs/posts') diff --git a/docs/posts/2021-06-26-Cheminformatics-On-The-Web-2021.html b/docs/posts/2021-06-26-Cheminformatics-On-The-Web-2021.html index 885c7b5..3324928 100644 --- a/docs/posts/2021-06-26-Cheminformatics-On-The-Web-2021.html +++ b/docs/posts/2021-06-26-Cheminformatics-On-The-Web-2021.html @@ -41,24 +41,21 @@

Cheminformatics on the Web (2021)

-

Here, I have compiled a list of some tools and possible solutions. -The web is a nice platform, it is available anywhere and just requires an internet connection. -I, personally like static websites which don't require a server side application and can be hosted on platforms like GitHub Pages. -Or, just open the HTML file and run it in your browser. -No data is required to be sent to any server and your device's computational power is used. -Even our phones have a lot of computational power now, which allows the user to run tasks on the go without needing to worry about managing dependencies. -WebAssembly (Wasm) has made running code written for other platfroms on the web relativevly easier. +

Here, I have compiled a list of some libraries and possible ideas. +I, personally, like static websites which don't require a server side application and can be hosted on platforms like GitHub Pages. +Or, just by opening the HTML file and running it in your browser. +WebAssembly (Wasm) has made running code written for other platforms on the web relatively easier. Combine Wasm with some pure JavaScript libraries, and you get a platform to quickly amp up your speed in some common tasks.

RDKit

RDKit bundles a minimal JavaScript Wrapper in their core RDKit suite. -This is perfect for generating 2D Figures (HTML5 Canva/SVGs), Cannonical SMILES, Descriptors e.t.c

+This is perfect for generating 2D Figures (HTML5 Canva/SVGs), Canonical SMILES, Descriptors e.t.c

Substructure Matching

This can be used to flag undesirable functional groups in a given compound. -Create a simple key:value pair of name:SMARTS and use it to highlight substructure matches. +Create a simple key:value pairs of name:SMARTS and use it to highlight substructure matches. Thus, something like PostEra's Medicinal Chemistry Alert can be done with RDKit-JS alone.

PostEra Demo

@@ -78,22 +75,22 @@ Thus, something like PostEra's Medicinal Chemistry Alert can be done with RDKit-

Obviously, it takes a few hits in the time to complete the docking because the code is transpiled from C++ to Wasm. But, the only major drawback (for now) is that it uses SharedArrayBuffer. Due to Spectre, this feature was disabled on all browsers. -Currently, only Chromium-based and Firefox browsers have reimplemented and renabled it. -Hopefully, soon this will be again supported by all major browsers.

+Currently, only Chromium-based and Firefox browsers have reimplemented and enabled it. +Hopefully, soon, this will be again supported by all major browsers.

Machine Learning

Frameworks have now evolved enough to allow exporting models to be able to run them through JavaScript/Wasm backend. An example task can be NER or Named-entity Recognition. -It can be used to extract compounds or diseases from a large blob of text and then matched with external refferences. +It can be used to extract compounds or diseases from a large blob of text and then matched with external references. Another example is target-prediction right in the browser: CHEMBL - Target Prediction in Browser

CHEMBL Group is first training the model using PyTorch (A Python ML Library), then converting it to the ONNX runtime. -A model like this can be directly implemented in Tensorflow, and then exported to be able to run with TensorFlow.js

+A model like this can be directly implemented in TensorFlow, and then exported to be able to run with TensorFlow.js

Cheminfo-to-web

-

The project aims to port chemoinformatics libraries into JavaScript via Emscripten. +

The project aims to port cheminformatics libraries into JavaScript via Emscripten. They have ported InChI, Indigo, OpenBabel, and OpenMD

Kekule.js

@@ -108,7 +105,7 @@ They have ported InChI, Indigo, OpenBabel, and OpenMD

The previous machine learning examples can be packaged as browser-extensions to perform tasks on the article you are reading. With iOS 15 bringing WebExtensions to iOS/iPadOS, the same browser extension source code can be now used on Desktop and Mobile Phones. -You can quickly create an extenison to convert PDB codes into links to RCSB, highlight SMILES, highlight output of NER models, e.t.c

+You can quickly create an extension to convert PDB codes into links to RCSB, highlight SMILES, highlight output of NER models, e.t.c

Conclusion

diff --git a/docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html b/docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html new file mode 100644 index 0000000..e4e4d1d --- /dev/null +++ b/docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html @@ -0,0 +1,161 @@ + + + + + + + + + Hey - Post + + + + + + + + + + + + + + + + + + + + + + + +
+

Making a Crude ML Powered Chatbot in Swift using CoreML

+ +

A chatbot/virtual assistant, on paper, looks easy to build. +The user says something, the programs finds the best action, checks if additional input is required and sends back the output. +To do this in Swift, I used two separate ML Models created using Apple's Create ML App. +First is a Text Classifier to classify intent, and the other a word tagger for extracting input from the input message. +Disclaimer: This is a very crude proof-of-concept, but it does work.

+ +

Text Classifier

+ +

I opened a CSV file and added some sample entries, with a corresponding label.

+ +

Screenshot of Sample Dataset +Screenshot of Create ML Text Classifier

+ +

Word Tagging

+ +

This is useful to extract the required variables directly from the user's input. +This model will be only called if the intent from the classifier is a custom action. +I created a sample JSON with only 3 examples (I know, very less, but works for a crude PoC).

+ +

Screenshot of Sample Dataset +Screenshot of Create ML Text Classifier

+ +

Time to Get Swift-y

+ +

The initial part is easy, importing CoreML and NaturalLanguage and then initializing the models and the tagger.

+ +

Screenshot

+ +
import CoreML
+import NaturalLanguage
+
+let mlModelClassifier = try IntentDetection_1(configuration:  MLModelConfiguration()).model
+let mlModelTagger = try CompoundTagger(configuration: MLModelConfiguration()).model
+
+let intentPredictor = try NLModel(mlModel: mlModelClassifier)
+let tagPredictor = try NLModel(mlModel: mlModelTagger)
+
+let tagger = NLTagger(tagSchemes: [.nameType, NLTagScheme("Apple")])
+tagger.setModels([tagPredictor], forTagScheme: NLTagScheme("Apple"))
+
+ +

Now, we define a simple structure which the custom function(s) can use to access the provided input. +It can also be used to hold additional variables. +This custom action for our third label, uses the Word Tagger model to check for the compound in the user's message. +If it is present then it displays the name, otherwise it tells the user that they have not provided the input. +The latter can be replaced with a function which asks the user for the input.

+ +

Screenshot

+ +
struct User {
+    static var message = ""
+}
+
+func customAction() -> String {
+    let sampleMessage = User.message
+    var actionable_item = ""
+    tagger.string = sampleMessage
+    tagger.enumerateTags(in: sampleMessage.startIndex..<sampleMessage.endIndex, unit: .word,
+                             scheme: NLTagScheme("Apple"), options: .omitWhitespace) { tag, tokenRange  in
+            if let tag = tag {
+                if tag.rawValue == "COMPOUND" {
+                    actionable_item += sampleMessage[tokenRange]
+                }
+            }
+        return true
+    }
+    if actionable_item == "" {
+        return "You did not provide any input"
+    } else {
+        return "You provided input \(actionable_item) for performing custom action"
+    }
+
+}
+
+ +

Sometimes, no action needs to be performed, and the bot can use a predefined set of responses. +Otherwise, if an action is required, it can call the custom action.

+ +

Screenshot

+ +
let defaultResponses = [
+    "greetings": "Hello",
+    "banter": "no, plix no"
+]
+
+let customActions = [
+    "deez-drug": customAction
+]
+
+ +

In the sample input, the program is updating the User.message and checking if it has a default response. +Otherwise, it calls the custom action.

+ +

Screenshot

+ +
let defaultResponses = [
+    "greetings": "Hello",
+    "banter": "no, plix no"
+]
+
+let customActions = [
+    "deez-drug": customAction
+]
+
+ +

Output

+ +

So easy.

+ +

If I ever release a part-2, it will either be about implementing this in Tensorflow.JS or an iOS app using SwiftUI ;)

+ +
+ + + + + + \ No newline at end of file diff --git a/docs/posts/index.html b/docs/posts/index.html index 119b7e6..6ee3224 100644 --- a/docs/posts/index.html +++ b/docs/posts/index.html @@ -48,6 +48,21 @@