gpt4 book ai didi

c# - Azure 认知服务无法在生产模式下工作

转载 作者:行者123 更新时间:2023-12-03 03:36:18 28 4
gpt4 key购买 nike

我已经集成了 Azure 认知服务的文本转语音功能。它在 Debug模式下工作得很好。但生产模式下什么也没有发生。当我从 Debug模式转到生产模式时,有什么需要更改的吗?是因为这个吗?

来自Official Documentation

Important

Remember to remove the key from your code when you're done, and never post it publicly. For production, use a secure way of storing and accessing your credentials like Azure Key Vault. See the Cognitive Services security article for more information.

代码

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;

class Program
{
static string YourSubscriptionKey = "YourSubscriptionKey";
static string YourServiceRegion = "YourServiceRegion";

static void OutputSpeechSynthesisResult(SpeechSynthesisResult speechSynthesisResult, string text)
{
switch (speechSynthesisResult.Reason)
{
case ResultReason.SynthesizingAudioCompleted:
Console.WriteLine($"Speech synthesized for text: [{text}]");
break;
case ResultReason.Canceled:
var cancellation = SpeechSynthesisCancellationDetails.FromResult(speechSynthesisResult);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
Console.WriteLine($"CANCELED: Did you set the speech resource key and region values?");
}
break;
default:
break;
}
}

async static Task Main(string[] args)
{
var speechConfig = SpeechConfig.FromSubscription(YourSubscriptionKey, YourServiceRegion);

// The language of the voice that speaks.
speechConfig.SpeechSynthesisVoiceName = "en-US-JennyNeural";

using (var speechSynthesizer = new SpeechSynthesizer(speechConfig))
{
// Get text from the console and synthesize to the default speaker.
Console.WriteLine("Enter some text that you want to speak >");
string text = Console.ReadLine();

var speechSynthesisResult = await speechSynthesizer.SpeakTextAsync(text);
OutputSpeechSynthesisResult(speechSynthesisResult, text);
}

Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}

最佳答案

当使用 Azure Key Vault 安全存储环境变量 key 时,生产模式将正常运行。

第一步是使用以下命令获取环境变量 key

using static System.Environment;

class Program
{
static void Main()
{
// Get the named env var, and assign it to the value variable
var value =
GetEnvironmentVariable(
"ENVIRONMENT_VARIABLE_KEY");
}
}

由于环境变量 key 缺乏安全性,生产模式无法工作。如果环境变量 key 值存储在 Azure key 保管库中,它也将在生产模式下运行。

关于c# - Azure 认知服务无法在生产模式下工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73206649/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com