diff options
Diffstat (limited to 'docs/feed.rss')
-rw-r--r-- | docs/feed.rss | 158 |
1 files changed, 141 insertions, 17 deletions
diff --git a/docs/feed.rss b/docs/feed.rss index 06f953c..9a44450 100644 --- a/docs/feed.rss +++ b/docs/feed.rss @@ -4,8 +4,8 @@ <title>Navan's Archive</title> <description>Rare Tips, Tricks and Posts</description> <link>https://web.navan.dev/</link><language>en</language> - <lastBuildDate>Sat, 26 Jun 2021 18:27:33 -0000</lastBuildDate> - <pubDate>Sat, 26 Jun 2021 18:27:33 -0000</pubDate> + <lastBuildDate>Mon, 28 Jun 2021 00:47:49 -0000</lastBuildDate> + <pubDate>Mon, 28 Jun 2021 00:47:49 -0000</pubDate> <ttl>250</ttl> <atom:link href="https://web.navan.dev/feed.rss" rel="self" type="application/rss+xml"/> @@ -2023,6 +2023,133 @@ Configure failed due to the errors above! <item> <guid isPermaLink="true"> + https://web.navan.dev/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html + </guid> + <title> + Making a Crude ML Powered Chatbot in Swift using CoreML + </title> + <description> + Writing a simple Machine-Learning powered Chatbot (or, daresay virtual personal assistant ) in Swift using CoreML. + </description> + <link>https://web.navan.dev/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html</link> + <pubDate>Sun, 27 Jun 2021 23:26:00 -0000</pubDate> + <content:encoded><![CDATA[<h1>Making a Crude ML Powered Chatbot in Swift using CoreML</h1> + +<p>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.</p> + +<h2>Text Classifier</h2> + +<p>I opened a CSV file and added some sample entries, with a corresponding label.</p> + +<p><img src="/assets/posts/swift-chatbot/intent-csv.png" alt="Screenshot of Sample Dataset" /> +<img src="/assets/posts/swift-chatbot/create-intent.png" alt="Screenshot of Create ML Text Classifier" /></p> + +<h2>Word Tagging</h2> + +<p>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).</p> + +<p><img src="/assets/posts/swift-chatbot/drugs-json.png" alt="Screenshot of Sample Dataset" /> +<img src="/assets/posts/swift-chatbot/create-tagger.png" alt="Screenshot of Create ML Text Classifier" /></p> + +<h2>Time to Get Swift-y</h2> + +<p>The initial part is easy, importing CoreML and NaturalLanguage and then initializing the models and the tagger.</p> + +<p><img src="/assets/posts/swift-chatbot/carbon.png" alt="Screenshot" /></p> + +<div class="codehilite"><pre><span></span><code><span class="kd">import</span> <span class="nc">CoreML</span> +<span class="kd">import</span> <span class="nc">NaturalLanguage</span> + +<span class="kd">let</span> <span class="nv">mlModelClassifier</span> <span class="p">=</span> <span class="k">try</span> <span class="n">IntentDetection_1</span><span class="p">(</span><span class="n">configuration</span><span class="p">:</span> <span class="bp">MLModelConfiguration</span><span class="p">()).</span><span class="n">model</span> +<span class="kd">let</span> <span class="nv">mlModelTagger</span> <span class="p">=</span> <span class="k">try</span> <span class="n">CompoundTagger</span><span class="p">(</span><span class="n">configuration</span><span class="p">:</span> <span class="bp">MLModelConfiguration</span><span class="p">()).</span><span class="n">model</span> + +<span class="kd">let</span> <span class="nv">intentPredictor</span> <span class="p">=</span> <span class="k">try</span> <span class="bp">NLModel</span><span class="p">(</span><span class="n">mlModel</span><span class="p">:</span> <span class="n">mlModelClassifier</span><span class="p">)</span> +<span class="kd">let</span> <span class="nv">tagPredictor</span> <span class="p">=</span> <span class="k">try</span> <span class="bp">NLModel</span><span class="p">(</span><span class="n">mlModel</span><span class="p">:</span> <span class="n">mlModelTagger</span><span class="p">)</span> + +<span class="kd">let</span> <span class="nv">tagger</span> <span class="p">=</span> <span class="bp">NLTagger</span><span class="p">(</span><span class="n">tagSchemes</span><span class="p">:</span> <span class="p">[.</span><span class="n">nameType</span><span class="p">,</span> <span class="n">NLTagScheme</span><span class="p">(</span><span class="s">"Apple"</span><span class="p">)])</span> +<span class="n">tagger</span><span class="p">.</span><span class="n">setModels</span><span class="p">([</span><span class="n">tagPredictor</span><span class="p">],</span> <span class="n">forTagScheme</span><span class="p">:</span> <span class="n">NLTagScheme</span><span class="p">(</span><span class="s">"Apple"</span><span class="p">))</span> +</code></pre></div> + +<p>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. </p> + +<p><img src="/assets/posts/swift-chatbot/carbon-2.png" alt="Screenshot" /></p> + +<div class="codehilite"><pre><span></span><code><span class="kd">struct</span> <span class="nc">User</span> <span class="p">{</span> + <span class="kd">static</span> <span class="kd">var</span> <span class="nv">message</span> <span class="p">=</span> <span class="s">""</span> +<span class="p">}</span> + +<span class="kd">func</span> <span class="nf">customAction</span><span class="p">()</span> <span class="p">-></span> <span class="nb">String</span> <span class="p">{</span> + <span class="kd">let</span> <span class="nv">sampleMessage</span> <span class="p">=</span> <span class="n">User</span><span class="p">.</span><span class="n">message</span> + <span class="kd">var</span> <span class="nv">actionable_item</span> <span class="p">=</span> <span class="s">""</span> + <span class="n">tagger</span><span class="p">.</span><span class="n">string</span> <span class="p">=</span> <span class="n">sampleMessage</span> + <span class="n">tagger</span><span class="p">.</span><span class="n">enumerateTags</span><span class="p">(</span><span class="k">in</span><span class="p">:</span> <span class="n">sampleMessage</span><span class="p">.</span><span class="n">startIndex</span><span class="p">..<</span><span class="n">sampleMessage</span><span class="p">.</span><span class="n">endIndex</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="p">.</span><span class="n">word</span><span class="p">,</span> + <span class="n">scheme</span><span class="p">:</span> <span class="n">NLTagScheme</span><span class="p">(</span><span class="s">"Apple"</span><span class="p">),</span> <span class="n">options</span><span class="p">:</span> <span class="p">.</span><span class="n">omitWhitespace</span><span class="p">)</span> <span class="p">{</span> <span class="n">tag</span><span class="p">,</span> <span class="n">tokenRange</span> <span class="k">in</span> + <span class="k">if</span> <span class="kd">let</span> <span class="nv">tag</span> <span class="p">=</span> <span class="n">tag</span> <span class="p">{</span> + <span class="k">if</span> <span class="n">tag</span><span class="p">.</span><span class="n">rawValue</span> <span class="p">==</span> <span class="s">"COMPOUND"</span> <span class="p">{</span> + <span class="n">actionable_item</span> <span class="o">+=</span> <span class="n">sampleMessage</span><span class="p">[</span><span class="n">tokenRange</span><span class="p">]</span> + <span class="p">}</span> + <span class="p">}</span> + <span class="k">return</span> <span class="kc">true</span> + <span class="p">}</span> + <span class="k">if</span> <span class="n">actionable_item</span> <span class="p">==</span> <span class="s">""</span> <span class="p">{</span> + <span class="k">return</span> <span class="s">"You did not provide any input"</span> + <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> + <span class="k">return</span> <span class="s">"You provided input </span><span class="si">\(</span><span class="n">actionable_item</span><span class="si">)</span><span class="s"> for performing custom action"</span> + <span class="p">}</span> + +<span class="p">}</span> +</code></pre></div> + +<p>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.</p> + +<p><img src="/assets/posts/swift-chatbot/carbon-3.png" alt="Screenshot" /></p> + +<div class="codehilite"><pre><span></span><code><span class="kd">let</span> <span class="nv">defaultResponses</span> <span class="p">=</span> <span class="p">[</span> + <span class="s">"greetings"</span><span class="p">:</span> <span class="s">"Hello"</span><span class="p">,</span> + <span class="s">"banter"</span><span class="p">:</span> <span class="s">"no, plix no"</span> +<span class="p">]</span> + +<span class="kd">let</span> <span class="nv">customActions</span> <span class="p">=</span> <span class="p">[</span> + <span class="s">"deez-drug"</span><span class="p">:</span> <span class="n">customAction</span> +<span class="p">]</span> +</code></pre></div> + +<p>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.</p> + +<p><img src="/assets/posts/swift-chatbot/carbon-4.png" alt="Screenshot" /></p> + +<div class="codehilite"><pre><span></span><code><span class="kd">let</span> <span class="nv">defaultResponses</span> <span class="p">=</span> <span class="p">[</span> + <span class="s">"greetings"</span><span class="p">:</span> <span class="s">"Hello"</span><span class="p">,</span> + <span class="s">"banter"</span><span class="p">:</span> <span class="s">"no, plix no"</span> +<span class="p">]</span> + +<span class="kd">let</span> <span class="nv">customActions</span> <span class="p">=</span> <span class="p">[</span> + <span class="s">"deez-drug"</span><span class="p">:</span> <span class="n">customAction</span> +<span class="p">]</span> +</code></pre></div> + +<p><img src="/assets/posts/swift-chatbot/output.png" alt="Output" /></p> + +<p>So easy.</p> + +<p>If I ever release a part-2, it will either be about implementing this in Tensorflow.JS or an iOS app using SwiftUI ;)</p> +]]></content:encoded> + </item> + + <item> + <guid isPermaLink="true"> https://web.navan.dev/posts/2019-12-10-TensorFlow-Model-Prediction.html </guid> <title> @@ -3342,24 +3469,21 @@ new Dics({ <pubDate>Sat, 26 Jun 2021 13:04:00 -0000</pubDate> <content:encoded><![CDATA[<h1>Cheminformatics on the Web (2021)</h1> -<p>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. +<p>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.</p> <h2>RDKit</h2> <p>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</p> +This is perfect for generating 2D Figures (HTML5 Canva/SVGs), Canonical SMILES, Descriptors e.t.c</p> <h3>Substructure Matching</h3> <p>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.</p> <p><img src="/assets/posts/cheminformatics-web/postera-demo.png" alt="PostEra Demo" /></p> @@ -3379,22 +3503,22 @@ Thus, something like PostEra's Medicinal Chemistry Alert can be done with RDKit- <p>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.</p> +Currently, only Chromium-based and Firefox browsers have reimplemented and enabled it. +Hopefully, soon, this will be again supported by all major browsers.</p> <h2>Machine Learning</h2> <p>Frameworks have now evolved enough to allow exporting models to be able to run them through JavaScript/Wasm backend. An example task can be <strong>NER</strong> 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: <a rel="noopener" target="_blank" href="http://chembl.blogspot.com/2021/03/target-predictions-in-browser-with.html">CHEMBL - Target Prediction in Browser</a></p> <p>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</p> +A model like this can be directly implemented in TensorFlow, and then exported to be able to run with TensorFlow.js</p> <h2>Cheminfo-to-web</h2> -<p>The project aims to port chemoinformatics libraries into JavaScript via Emscripten. +<p>The project aims to port cheminformatics libraries into JavaScript via Emscripten. They have ported InChI, Indigo, OpenBabel, and OpenMD</p> <h3>Kekule.js</h3> @@ -3409,7 +3533,7 @@ They have ported InChI, Indigo, OpenBabel, and OpenMD</p> <p>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</p> +You can quickly create an extension to convert PDB codes into links to RCSB, highlight SMILES, highlight output of NER models, e.t.c</p> <h2>Conclusion</h2> |