Fognitive Services: F# and Azure Cognitive Services

Machine Learning is revolutionizing how we think about processing data. But machine learning algorithms can have a steep learning curve. Luckily, Microsoft has released an easy way to access very powerful machine learning models through Azure Cognitive Services. They expose the power of machine learning through the ease of calling a REST API.

Microsoft provides good samples for calling into the API in C#, Java, JavaScript, PHP, Python, and Ruby. But I wanted to try it from F#. At the MVP Summit, I talked with Reed Copsey about this idea and he suggested I that I create an F# wrapper for the API. The wrapper could allow someone to call into Cognitive Services using idiomatic F# code. Thus the idea for Fognitive Services was born.

With help from Reed and Ryan Riley, I created the start of this project. It is built on the existing NuGet package for Cognitive Services .NET SDK. We started with the C# sample code and did a basic translation of it into F#. Then we refactored it to be idiomatic F# code. For example, the API now takes an F# list of tuples instead of a C# list of a custom data structure. This means that the API integrates smoothly with the other F# language tools like piping and pattern matching. Ryan and I recorded a screencast while we did part of this refactoring.

The GitHub repo contains some sample apps for each API. But I also put together a slightly more interesting demo project. The Cognitive Services Text API has the ability to pick out key phrases from a block of text. There is a wrapper in Fognitive Services for this in FognitiveServices.Text and there is a NuGet package for it. I thought it would be interesting to use Canopy to screenscrape the session list from the StirTrek conference page and pick out the key phrases from each session. This turned out to be incredibly easy. The entire project is available on GitHub. Here is the meat of the code.

All of the real work is done in the analyzeStirTrekTags function. Lines 9 and 10 load up the page in the browser. Line 12 initializes the Cognitive Services client. Lines 15 and 16 scrape the session titles off of the web page. The list of titles is seamlessly piped into the FognitiveServices API call on line 17 which returns the results. Line 19 prints the results.

This easy integration between two F# libraries was one of the main goals behind Fognitivie services. It combines the ease and power of Cognitive Services with the power and elegance of F#.

March 22, 2018 |
Tags : FSharp CognitiveServices Canopy

Comments Section

Feel free to comment on the post but keep it clean and on topic.

comments powered by Disqus

About Me

Eric Potter My name is Eric Potter. I have an amazing wife and 5 wonderful children. I am a Microsoft MVP for Developer Tools and Technologies, the Director of Technical Education for Sweetwater in Ft. Wayne Indiana, and an adjunct professor for Indiana Tech. I am a humble toolsmith.

Microsoft MVP Award