Microsoft Polly

 admin

You can create an instant, real time poll in seconds within an email message. In the body of your Outlook email, add questions and options for single or multiple answers. Once you send the message, recipients vote directly in email or click on a handy included link and vote in a browser window. The Microsoft early announcement speaks more to these topics, and Steve Gordon's pair of blog posts (1; 2) are also an excellent read for deeper background and some great worked examples. Polly and Polly policies work great with ASP.NET Core 2.1 and integrated nicely.

  1. Microsoft Polly Extensions
  2. Microsoft Teams Polly Bot
  3. Microsoft Polly Httpclient
  4. Microsoft Polly App

Posted by Abhishek on February 19, 2020

In this blog, we will look at a common scenario where we have to call an API endpoint and handle retries efficiently in .NET Core. For this purpose, we will be using a library called as “Polly“.

From Poly Studio X30 and X50 Microsoft Teams Rooms on Android for smaller meeting spaces to Poly G-Series Microsoft Teams Rooms on Windows that support up to large rooms and allow room AV integrations, Poly room solutions for Microsoft Teams deliver simple yet powerful collaboration experiences that will get the best out of your meetings. Since I'm passionate about the possibilities of AI-assisted creative automation, I tested the three leading text-to-speech engines: Amazon Polly, Microsoft Azure Cognitive Services and Google Cloud. The purpose of this article is to give you my honest opinion about the way they render human voice based on the same text prompt.

Polly is a library that allows developers to express resilience and transient fault handling policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner.

Pre-Requisites

  • Open Visual Studio
  • Create a New Project of type “ASP.NET Core Web Application”
  • Give a good name for your project
  • Choose the “MVC” template
  • Hit “Create” button

What am I trying to achieve?

The goal that I am trying to achieve is to call the GitHub Users endpoint and retrieve the data for any user in github. I want to make sure that the solution uses memory optimally & also ensures that my application retries if the API fails for some reason.

A traditional way the developers would code is to create an HttpClient, set the properties with the Uri and invoke the endpoint (as shown below).

Problems with this traditional approach

  • Every time you want the user data, the HttpClient is newly initialized
  • Everytime a new HttpClient is initialized, the HTTP Handlers for the client object is also newly instantiated.
  • We already know that BaseAddress and DefaultRequestHeaders are the same and do not change. So, the handlers could have been re-used but with this approach, you can’t do that.
  • It would slow down the execution of this logic because all these objects have to be created in memory and then executed.

Solution to this problem

From .NET Core 2.1 onwards, Microsoft introduced IHttpClientFactory with which we can set the handlers prior and re-use them while creating the client objects. As we all know, we can set the handlers in the Startup.cs and dependency inject it to the controller so that we can use the handlers for creating the client object.

Microsoft Polly Extensions

Polly

In Startup.cs, we can specify a named HttpClient called “GitHub”

In HomeController.cs, we can use httpClientFactory to create the client object using the named Client that we had specified in Startup.cs

Perfect !! We now understood the optimal way to invoke API endpoints using IHttpClientFactory.

But, there can be instances where the API endpoints can fail temporarily for a fraction of a second but is likely to disappear if we invoke the API endpoint again. These kinds of temporary errors that will disappear on retry are called as “Transient Errors“.

Technically, Transient Errors are one of the following.

  • Network failures (System.Net.Http.HttpRequestException)
  • HTTP 5XX status codes (server errors)
  • HTTP 408 status code (request timeout)In our case, if a transient error occurred, this call would fail naturally because there is no retry mechanism built/coded.

Microsoft provides an elegant solution to this problem with the NuGet package named “Microsoft.Extensions.Http.Polly“. It integrates the Polly library with IHttpClientFactory.

Steps to implement Retry

Alright, go ahead and install the “Microsoft.Extensions.Http.Polly” NuGet package in the web application project.

In Startup.cs, specify the Transient Error Policy as shown below

Here, we are telling the IHttpClientFactory that in case of a transient error, the policy is to wait for 3 seconds (300 milliseconds) and retry the API endpoint for a maximum of 3 times. If we get the response before max retry times of 3, send the response back. If not, send the error as it is. Therefore, if this was really a transient error that would disappear on retry, essentially the user doesn’t even realize that a transient error just happened because the IHttpClientFactory immediately retried invoking the API endpoint and send back the response.

I hope this blog was useful to all .NET Core Developers. This is a common pattern that we see everyday in our coding life and you can follow the techniques mentioned here to make your applications more robust.


Thanks for reading this post. Enjoy !!Share on: Please enable JavaScript to view the comments powered by Disqus.
Turn text into lifelike speech using deep learning

Amazon Polly is a service that turns text into lifelike speech, allowing you to create applications that talk, and build entirely new categories of speech-enabled products. Polly's Text-to-Speech (TTS) service uses advanced deep learning technologies to synthesize natural sounding human speech. With dozens of lifelike voices across a broad set of languages, you can build speech-enabled applications that work in many different countries.
In addition to Standard TTS voices, Amazon Polly offers Neural Text-to-Speech (NTTS) voices that deliver advanced improvements in speech quality through a new machine learning approach. Polly’s Neural TTS technology also supports two speaking styles that allow you to better match the delivery style of the speaker to the application: a Newscaster reading style that is tailored to news narration use cases, and a Conversational speaking style that is ideal for two-way communication like telephony applications.
Finally, Amazon Polly Brand Voice can create a custom voice for your organization. This is a custom engagement where you will work with the Amazon Polly team to build an NTTS voice for the exclusive use of your organization. Learn more here.

Listen to Amazon Polly’s Newscaster voice » Listen to Amazon Polly’s Conversational voice »

per month for 12 months with the AWS Free Tier

LanguageFemaleMaleSample Text
EnglishHello. Do you speak a foreign language? One language is never enough.
Brazilian PortugueseVitóriaRicardoOi. Você fala algum idioma estrangeiro? Somente um idioma nunca é bastante.
DanishNajaMadsHej. Taler du et fremmed sprog? Et sprog er aldrig nok.
FrenchLéaMathieuBonjour. Parlez-vous une autre langue que le français? Une langue n'est jamais assez.
JapaneseMizuki
Takumiこんにちは、外国語を話せますか? 世界にはたくさんの言語があります。
KoreanSeoyeon (Standard)
Seoyeon (Neural)
안녕하세요? 외국어를 구사하십니까? 이 세상에는 수많은 언어들이 있답니다.
Mandarin ChineseZhiyu你好,你会不会说其他外语?只会一种语言是不够的。
SpanishPenélopeMiguelHola. ¿Hablas algún idioma extranjero? Un solo idioma no es suficiente.
Check our full list of Amazon Polly voices and supported languages here.

Benefits

Natural sounding voices

Amazon Polly provides dozens of languages and a wide selection of natural-sounding male and female voices. Amazon Polly's fluid pronunciation of text enables you to deliver high-quality voice output for a global audience.

Store & redistribute speech

Amazon Polly allows for unlimited replays of generated speech without any additional fees. You can create speech files in standard formats like MP3 and OGG, and serve them from the cloud or locally with apps or devices for offline playback.

Real-time streaming

Delivering lifelike voices and conversational user experiences requires consistently fast response times. When you send text to Amazon Polly’s API, it returns the audio to your application as a stream so you can play the voices immediately.

Customize & control speech output

Modify Amazon Polly voices to best suit your needs – Amazon Polly supports lexicons and SSML tags which enable you to control aspects of speech, such as pronunciation, volume, pitch, speed rate, etc.

Low cost

Amazon Polly’s pay-as-you-go pricing, low cost per character converted, and unlimited replays make it a cost-effective way to voice your applications.

Use cases

Content Creation

Audio can be used as a complementary media to written and/or visual communication. By voicing your content, you can provide your audience with an alternative way to consume information and meet the needs of a larger pool of readers. Amazon Polly can generate speech in dozens of languages, making it easy to add speech to applications with a global audience, such as RSS feeds, websites, or videos.

Example: Convert an article to speech and download as MP3

“Amazon Polly gives GoAnimate users the ability to immediately give voice to the characters they animate using our platform. This is especially helpful in scenarios where live voice-over is either resource or time prohibitive, such as when developing a video in many languages or within pre-production to speed the approval process. The speech is integrated seamlessly with our rich set of pre-animated assets, which reinforces GoAnimate’s ease-of-use and affords our customers both efficiency and speed to market.”

Microsoft Polly

– Alvin Hung, CEO and founder, GoAnimate

Refer to case study >>Microsoft Polly

Microsoft Teams Polly Bot

E-learning

Amazon Polly enables developers to provide their applications with an enhanced visual experience such as speech-synchronized facial animation or karaoke-style word highlighting. Amazon Polly makes it easy to request an additional stream of metadata with information about when particular sentences, words and sounds are being pronounced. Using this metadata stream alongside the synthesized speech audio stream, customers can animate avatars and highlight text as it is currently spoken text in their app.

“I can't think of many use cases where accurate pronunciation is more important than when you're learning a new language. We have found that the Amazon Polly voices are not just high in quality, but are as good as natural human speech for teaching a language.”

– Severin Hacker, CTO, Duolingo

Read their blog post >>

Telephony

With Amazon Polly, your contact centers can engage customers with natural sounding voices. You can cache and replay Amazon Polly’s speech output to prompt callers through interactive voice response (IVR) systems, such as Amazon Connect. Additionally, you can leverage Amazon Polly’s API to deliver automated real-time information such as service status, account and billing inquiries, addresses, and contact information.

“Bandwidth built a small, reliable, and fast TTS application that is backed by Amazon Polly. We are now using high-quality voices at low cost. The developer effort required to build this new service was surprisingly minimal.”

– Yiannis Philipopoulos, Sr. Software Developer, Bandwidth

Read their blog post >>

Microsoft Polly Httpclient

Get started with Amazon Polly

Microsoft Polly App

Have more questions?
Contact us