gpt4 book ai didi

c# - 在自定义 WebAPi 内调用时,Azure 语音 API 会陷入无限循环

转载 作者:行者123 更新时间:2023-12-03 03:51:14 26 4
gpt4 key购买 nike

让我解释一下这个场景 -

我为具有多个参数的应用程序创建了一个自定义 Web API,用户通过传递基于某种逻辑连接的多个参数来调用此 api,然后在该自定义 api 内调用 Azure 语音 API 将文本转换为语音(分 2 个步骤 - 1. 我调用 Azure 语音服务来获取 token ,2. 我调用 Azure 语音服务将文本转换为语音)

问题 -当我调用 Azure 语音服务来获取 token 时,它处于无限循环中。

下面是示例代码,任何正确方向的指针都会有帮助 -

public class ConverterController : ApiController
{


/// <summary>
/// default constructor
/// </summary>
public ConverterController()
{
}


[HttpGet]
public HttpResponseMessage ConvertUsingSpeech([FromUri] OutReachMessage outReachMessage)
{

try
{
outReachMessage = new OutReachMessage();
var result = ConvertUsingSpeechApi(outReachMessage);
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StringContent(OutputMp3FilePath);
return response;
}
catch (Exception ex)
{
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
}


async Task ConvertUsingSpeechApi(OutReachMessage outReachMessage)
{
var authObj = new Authentication("key");
var token = authObj.GetAccessToken();


using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", token);
client.DefaultRequestHeaders.Add("Content-Type", contentType);
client.DefaultRequestHeaders.Add("X-Microsoft-OutputFormat", outputFormat);
UriBuilder uriBuilder = new UriBuilder(fetchSpeechUri);

var content = new StringContent("<speak version='1.0' xml:lang='hi-IN'><voice xml:lang='hi-IN' xml:gender='Female' name = 'hi-IN-SwaraNeural'>" +
"Welcome mahesh to the world of Azure - feeling great. </voice></speak>",
Encoding.UTF8, "text/xml");

var result = await client.PostAsync(uriBuilder.Uri.AbsoluteUri, content);

using (var response = client.PostAsync(uriBuilder.Uri.AbsoluteUri, content).Result)
using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync())
{
string fileToWriteTo = OutputMp3FilePath;
using (Stream streamToWriteTo = File.Open(fileToWriteTo, FileMode.Create))
{
await streamToReadFrom.CopyToAsync(streamToWriteTo);
}
}
}
}

}

身份验证类 - 获取 token

public class Authentication
{
public static readonly string FetchTokenUri =
"https://centralindia.api.cognitive.microsoft.com/sts/v1.0/issueToken";
private string subscriptionKey;
private string token;

public Authentication(string subscriptionKey)
{
this.subscriptionKey = subscriptionKey;
this.token = FetchTokenAsync(FetchTokenUri, subscriptionKey).Result;
}

public string GetAccessToken()
{
return this.token;
}

private async Task<string> FetchTokenAsync(string fetchUri, string subscriptionKey)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
UriBuilder uriBuilder = new UriBuilder(fetchUri);

var result = await client.PostAsync(uriBuilder.Uri.AbsoluteUri, null);
Console.WriteLine("Token Uri: {0}", uriBuilder.Uri.AbsoluteUri);
return await result.Content.ReadAsStringAsync();
}
}
}

注意 - 我可以使用 postman 和控制台应用程序调用 Azure 语音 api,但仅当我在自定义 webapi 中调用 azure 服务时,它不起作用。

最佳答案

更改代码如下,删除字符串subscriptionKey。它会起作用的。

public Authentication()
{
this.subscriptionKey = subscriptionKey;
this.token = FetchTokenAsync(FetchTokenUri, subscriptionKey).Result;
}

关于c# - 在自定义 WebAPi 内调用时,Azure 语音 API 会陷入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66616129/

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