How to use 'Microsoft.CognitiveServices.Speech' namespace with Azure Functions?

1

I'm testing the Microsoft Azure Speech services, specifically trying to use Text-To-Speech. So, I'm using a free layer of Azure, and created a TimeTrigger Azure Function to read an e-mail, traverse through HTML and then call the Speech Service with the SDK Microsoft.CognitiveServices.Speech. I'm using the function.proj to load the nuget packages, loading S22.Imap and HtmlAgilityPack without any issues. But the speech package is triggering an exception: Unable to load DLL 'Microsoft.CognitiveServices.Speech.core.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E). Am I able to use this package in an Azure Function? If so, what am I doing wrong?

I tried to remove the <PackageReference Include="Microsoft.CognitiveServices.Speech" Version="1.6.0" /> line from function.proj and deleted project.assets.json to reload the package but it didn't work.

This is my function.proj:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="S22.Imap" Version="3.6.0" />
        <PackageReference Include="HtmlAgilityPack" Version="1.11.9" />
        <PackageReference Include="Microsoft.CognitiveServices.Speech" Version="1.6.0" />
    </ItemGroup>
</Project>

And this is my run.csx:

using System;
using S22.Imap;
using System.Net.Mail;
using HtmlAgilityPack;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using System.Diagnostics;

public static void Run(TimerInfo myTimer, ILogger log)
{
    var username = "sample@gmail.com";
    var password = "sample";
    var subsKey = "sample";

    using(ImapClient client = new ImapClient("imap.gmail.com", 993, username, password, AuthMethod.Login, true))
    {
        IEnumerable<uint> uids = client.Search(SearchCondition.From("sample@sample.com"));
        IEnumerable<MailMessage> messages = client.GetMessages(uids);
        log.LogInformation($"Count: {messages.Count()}.");

        var msg = messages.FirstOrDefault();
        if(msg != null)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(msg.Body);

            var paragraphs = doc.DocumentNode.Descendants()
                .Where(x => x.Name == "p" && !string.IsNullOrEmpty(x.InnerText.Trim()))
                .ToList();

            var mailText = string.Empty;
            foreach(var par in paragraphs)
                mailText += par.InnerText;

            if(!string.IsNullOrEmpty(mailText))
            {
                var config = SpeechConfig.FromSubscription(subsKey, "myregion");
                config.SetSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Audio24Khz160KBitRateMonoMp3);
                config.SpeechSynthesisLanguage = "pt-BR";

                using (var synthesizer = new SpeechSynthesizer(config))
                {
                    using (var result = synthesizer.SpeakTextAsync(mailText).Result)
                    {
                        if (result.Reason == ResultReason.SynthesizingAudioCompleted)
                        {
                            //Do something with it
                        }
                        else if (result.Reason == ResultReason.Canceled)
                        {
                            var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
                            log.LogError($"CANCELED: Reason={cancellation.Reason}");

                            if (cancellation.Reason == CancellationReason.Error)
                            {
                                log.LogError($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                                log.LogError($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
                            }
                        }
                    }
                }
            }
        }
    }
}
c#
azure
azure-functions
text-to-speech
asked on Stack Overflow Jul 11, 2019 by Cristian Porto

1 Answer

1

You could try to delete the function.proj then recreate one and add Microsoft.CognitiveServices.Speech at first.

Make sure the Microsoft.CognitiveServices.Speech.core.dll has been installed in win-x86 and win-x64. Please refer to this issue.

enter image description here

answered on Stack Overflow Jul 12, 2019 by Joey Cai

User contributions licensed under CC BY-SA 3.0