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-27-Crude-ML-AI-Powered-Chatbot-Swift.md | 123 ++++++++++++++++ .../posts/cheminformatics-web/postera-demo.png | Bin 0 -> 983243 bytes .../posts/cheminformatics-web/rdkit-demo.png | Bin 0 -> 893061 bytes .../posts/cheminformatics-web/webina-demo.png | Bin 0 -> 2181135 bytes Resources/assets/posts/swift-chatbot/carbon-2.png | Bin 0 -> 247973 bytes Resources/assets/posts/swift-chatbot/carbon-3.png | Bin 0 -> 94268 bytes Resources/assets/posts/swift-chatbot/carbon-4.png | Bin 0 -> 215477 bytes Resources/assets/posts/swift-chatbot/carbon.png | Bin 0 -> 188994 bytes .../assets/posts/swift-chatbot/create-intent.png | Bin 0 -> 763744 bytes .../assets/posts/swift-chatbot/create-tagger.png | Bin 0 -> 782235 bytes .../assets/posts/swift-chatbot/drugs-json.png | Bin 0 -> 549991 bytes .../assets/posts/swift-chatbot/intent-csv.png | Bin 0 -> 755977 bytes Resources/assets/posts/swift-chatbot/output.png | Bin 0 -> 132696 bytes docs/assets/gciTales/03-regression/1.png | Bin 18561 -> 18561 bytes .../posts/cheminformatics-web/postera-demo.png | Bin 572211 -> 983243 bytes .../posts/cheminformatics-web/rdkit-demo.png | Bin 523533 -> 893061 bytes .../posts/cheminformatics-web/webina-demo.png | Bin 1394971 -> 2181135 bytes docs/assets/posts/swift-chatbot/carbon-2.png | Bin 0 -> 247973 bytes docs/assets/posts/swift-chatbot/carbon-3.png | Bin 0 -> 94268 bytes docs/assets/posts/swift-chatbot/carbon-4.png | Bin 0 -> 215477 bytes docs/assets/posts/swift-chatbot/carbon.png | Bin 0 -> 188994 bytes docs/assets/posts/swift-chatbot/create-intent.png | Bin 0 -> 763744 bytes docs/assets/posts/swift-chatbot/create-tagger.png | Bin 0 -> 782235 bytes docs/assets/posts/swift-chatbot/drugs-json.png | Bin 0 -> 549991 bytes docs/assets/posts/swift-chatbot/intent-csv.png | Bin 0 -> 755977 bytes docs/assets/posts/swift-chatbot/output.png | Bin 0 -> 132696 bytes docs/feed.rss | 158 +++++++++++++++++--- ...80b-d347-476a-232d-9568839851cd.webPlatform.png | Bin 1025 -> 1025 bytes ...f17-2e71-90d8-67a7-587163282ebf.webPlatform.png | Bin 1612 -> 1612 bytes ...e9e-b615-96cd-3e18-ab4307c859a0.webPlatform.png | Bin 1034 -> 1034 bytes ...18b-2a6c-0abc-136c-8c8faf49c71b.webPlatform.png | Bin 945 -> 945 bytes ...ee8-92e3-932f-5388-7731928b5692.webPlatform.png | Bin 786 -> 786 bytes ...996-fd1b-b2d3-3627-cef4fa224e25.webPlatform.png | Bin 755 -> 755 bytes ...729-56cb-2a63-7e8b-ac62a038a023.webPlatform.png | Bin 824 -> 824 bytes ...a46-4612-c284-055f-58850c0730bd.webPlatform.png | Bin 958 -> 958 bytes ...b1d-0299-9db6-3747-c7aeaaaa37d0.webPlatform.png | Bin 1070 -> 1070 bytes ...c61-f17f-ff49-3f97-e942f202bebf.webPlatform.png | Bin 908 -> 908 bytes ...aaa-861c-78c0-0919-07a886e57304.webPlatform.png | Bin 1883 -> 1883 bytes ...765-fff5-aa39-9f7f-fdd3024d4056.webPlatform.png | Bin 902 -> 902 bytes docs/index.html | 15 ++ ...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 ++ 43 files changed, 467 insertions(+), 32 deletions(-) create mode 100644 Content/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.md create mode 100644 Resources/assets/posts/cheminformatics-web/postera-demo.png create mode 100644 Resources/assets/posts/cheminformatics-web/rdkit-demo.png create mode 100644 Resources/assets/posts/cheminformatics-web/webina-demo.png create mode 100644 Resources/assets/posts/swift-chatbot/carbon-2.png create mode 100644 Resources/assets/posts/swift-chatbot/carbon-3.png create mode 100644 Resources/assets/posts/swift-chatbot/carbon-4.png create mode 100644 Resources/assets/posts/swift-chatbot/carbon.png create mode 100644 Resources/assets/posts/swift-chatbot/create-intent.png create mode 100644 Resources/assets/posts/swift-chatbot/create-tagger.png create mode 100644 Resources/assets/posts/swift-chatbot/drugs-json.png create mode 100644 Resources/assets/posts/swift-chatbot/intent-csv.png create mode 100644 Resources/assets/posts/swift-chatbot/output.png create mode 100644 docs/assets/posts/swift-chatbot/carbon-2.png create mode 100644 docs/assets/posts/swift-chatbot/carbon-3.png create mode 100644 docs/assets/posts/swift-chatbot/carbon-4.png create mode 100644 docs/assets/posts/swift-chatbot/carbon.png create mode 100644 docs/assets/posts/swift-chatbot/create-intent.png create mode 100644 docs/assets/posts/swift-chatbot/create-tagger.png create mode 100644 docs/assets/posts/swift-chatbot/drugs-json.png create mode 100644 docs/assets/posts/swift-chatbot/intent-csv.png create mode 100644 docs/assets/posts/swift-chatbot/output.png create mode 100644 docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html diff --git a/Content/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.md b/Content/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.md new file mode 100644 index 0000000..0dcabec --- /dev/null +++ b/Content/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.md @@ -0,0 +1,123 @@ +--- +date: 2021-06-27 23:26 +description: Writing a simple Machine-Learning powered Chatbot (or, daresay virtual personal assistant ) in Swift using CoreML. +tags: Swift, CoreML, NLP +--- + +# 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](/assets/posts/swift-chatbot/intent-csv.png) +![Screenshot of Create ML Text Classifier](/assets/posts/swift-chatbot/create-intent.png) + +## 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](/assets/posts/swift-chatbot/drugs-json.png) +![Screenshot of Create ML Text Classifier](/assets/posts/swift-chatbot/create-tagger.png) + +## Time to Get Swift-y + +The initial part is easy, importing CoreML and NaturalLanguage and then initializing the models and the tagger. + +![Screenshot](/assets/posts/swift-chatbot/carbon.png) + +```swift +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](/assets/posts/swift-chatbot/carbon-2.png) +```swift +struct User { + static var message = "" +} + +func customAction() -> String { + let sampleMessage = User.message + var actionable_item = "" + tagger.string = sampleMessage + tagger.enumerateTags(in: sampleMessage.startIndex..Navan's Archive Rare Tips, Tricks and Posts https://web.navan.dev/en - Sat, 26 Jun 2021 18:27:33 -0000 - Sat, 26 Jun 2021 18:27:33 -0000 + Mon, 28 Jun 2021 00:47:49 -0000 + Mon, 28 Jun 2021 00:47:49 -0000 250 @@ -2021,6 +2021,133 @@ Configure failed due to the errors above! ]]> + + + https://web.navan.dev/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html + + + Making a Crude ML Powered Chatbot in Swift using CoreML + + + Writing a simple Machine-Learning powered Chatbot (or, daresay virtual personal assistant ) in Swift using CoreML. + + https://web.navan.dev/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html + Sun, 27 Jun 2021 23:26:00 -0000 + 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 ;)

+]]>
+
+ https://web.navan.dev/posts/2019-12-10-TensorFlow-Model-Prediction.html @@ -3342,24 +3469,21 @@ new Dics({ Sat, 26 Jun 2021 13:04:00 -0000 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

@@ -3379,22 +3503,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

@@ -3409,7 +3533,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/images/04d0580b-d347-476a-232d-9568839851cd.webPlatform.png b/docs/images/04d0580b-d347-476a-232d-9568839851cd.webPlatform.png index 53777c6..b515ebe 100644 Binary files a/docs/images/04d0580b-d347-476a-232d-9568839851cd.webPlatform.png and b/docs/images/04d0580b-d347-476a-232d-9568839851cd.webPlatform.png differ diff --git a/docs/images/82e24f17-2e71-90d8-67a7-587163282ebf.webPlatform.png b/docs/images/82e24f17-2e71-90d8-67a7-587163282ebf.webPlatform.png index 6fe876b..3f94c60 100644 Binary files a/docs/images/82e24f17-2e71-90d8-67a7-587163282ebf.webPlatform.png and b/docs/images/82e24f17-2e71-90d8-67a7-587163282ebf.webPlatform.png differ diff --git a/docs/images/8c0ffe9e-b615-96cd-3e18-ab4307c859a0.webPlatform.png b/docs/images/8c0ffe9e-b615-96cd-3e18-ab4307c859a0.webPlatform.png index 65f9421..6c0a553 100644 Binary files a/docs/images/8c0ffe9e-b615-96cd-3e18-ab4307c859a0.webPlatform.png and b/docs/images/8c0ffe9e-b615-96cd-3e18-ab4307c859a0.webPlatform.png differ diff --git a/docs/images/9384518b-2a6c-0abc-136c-8c8faf49c71b.webPlatform.png b/docs/images/9384518b-2a6c-0abc-136c-8c8faf49c71b.webPlatform.png index 9d2b352..2b7ecef 100644 Binary files a/docs/images/9384518b-2a6c-0abc-136c-8c8faf49c71b.webPlatform.png and b/docs/images/9384518b-2a6c-0abc-136c-8c8faf49c71b.webPlatform.png differ diff --git a/docs/images/9bf4aee8-92e3-932f-5388-7731928b5692.webPlatform.png b/docs/images/9bf4aee8-92e3-932f-5388-7731928b5692.webPlatform.png index 82ed736..64095e8 100644 Binary files a/docs/images/9bf4aee8-92e3-932f-5388-7731928b5692.webPlatform.png and b/docs/images/9bf4aee8-92e3-932f-5388-7731928b5692.webPlatform.png differ diff --git a/docs/images/9dc22996-fd1b-b2d3-3627-cef4fa224e25.webPlatform.png b/docs/images/9dc22996-fd1b-b2d3-3627-cef4fa224e25.webPlatform.png index 035af6b..e247e3c 100644 Binary files a/docs/images/9dc22996-fd1b-b2d3-3627-cef4fa224e25.webPlatform.png and b/docs/images/9dc22996-fd1b-b2d3-3627-cef4fa224e25.webPlatform.png differ diff --git a/docs/images/b0cac729-56cb-2a63-7e8b-ac62a038a023.webPlatform.png b/docs/images/b0cac729-56cb-2a63-7e8b-ac62a038a023.webPlatform.png index 28edd0f..3ee2410 100644 Binary files a/docs/images/b0cac729-56cb-2a63-7e8b-ac62a038a023.webPlatform.png and b/docs/images/b0cac729-56cb-2a63-7e8b-ac62a038a023.webPlatform.png differ diff --git a/docs/images/bb0aca46-4612-c284-055f-58850c0730bd.webPlatform.png b/docs/images/bb0aca46-4612-c284-055f-58850c0730bd.webPlatform.png index 163fb7a..49d092a 100644 Binary files a/docs/images/bb0aca46-4612-c284-055f-58850c0730bd.webPlatform.png and b/docs/images/bb0aca46-4612-c284-055f-58850c0730bd.webPlatform.png differ diff --git a/docs/images/cbac5b1d-0299-9db6-3747-c7aeaaaa37d0.webPlatform.png b/docs/images/cbac5b1d-0299-9db6-3747-c7aeaaaa37d0.webPlatform.png index 571ac24..030e5fa 100644 Binary files a/docs/images/cbac5b1d-0299-9db6-3747-c7aeaaaa37d0.webPlatform.png and b/docs/images/cbac5b1d-0299-9db6-3747-c7aeaaaa37d0.webPlatform.png differ diff --git a/docs/images/f1579c61-f17f-ff49-3f97-e942f202bebf.webPlatform.png b/docs/images/f1579c61-f17f-ff49-3f97-e942f202bebf.webPlatform.png index 4bda098..7bbb309 100644 Binary files a/docs/images/f1579c61-f17f-ff49-3f97-e942f202bebf.webPlatform.png and b/docs/images/f1579c61-f17f-ff49-3f97-e942f202bebf.webPlatform.png differ diff --git a/docs/images/f400aaaa-861c-78c0-0919-07a886e57304.webPlatform.png b/docs/images/f400aaaa-861c-78c0-0919-07a886e57304.webPlatform.png index 18ba491..27272be 100644 Binary files a/docs/images/f400aaaa-861c-78c0-0919-07a886e57304.webPlatform.png and b/docs/images/f400aaaa-861c-78c0-0919-07a886e57304.webPlatform.png differ diff --git a/docs/images/f7842765-fff5-aa39-9f7f-fdd3024d4056.webPlatform.png b/docs/images/f7842765-fff5-aa39-9f7f-fdd3024d4056.webPlatform.png index 1b6ddb8..1bae893 100644 Binary files a/docs/images/f7842765-fff5-aa39-9f7f-fdd3024d4056.webPlatform.png and b/docs/images/f7842765-fff5-aa39-9f7f-fdd3024d4056.webPlatform.png differ diff --git a/docs/index.html b/docs/index.html index c220d2f..56e642e 100644 --- a/docs/index.html +++ b/docs/index.html @@ -45,6 +45,21 @@
    +
  • Making a Crude ML Powered Chatbot in Swift using CoreML
  • +
      +
    • Writing a simple Machine-Learning powered Chatbot (or, daresay virtual personal assistant ) in Swift using CoreML.
    • +
    • Published On: 2021-06-27 23:26
    • +
    • Tags: + + Swift, + + CoreML, + + NLP, + +
    + +
  • Cheminformatics on the Web (2021)
    • Summarising Cheminformatics on the web in 2021.
    • 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 @@