This commit is contained in:
Zeeshaun
2026-04-23 14:16:17 +00:00
parent 62384ef28c
commit 5533fea21f
2 changed files with 75 additions and 3 deletions
@@ -5,6 +5,8 @@ namespace LCU.Webapps.Marketing.SPIT.Model
{
public class SpitConfiguration
{
private const string DefaultSpeechSynthesisPath = "/tts/cognitiveservices/v1";
public string AppName { get; set; }
public string SeqApiKey { get; set; }
public string SeqUrl { get; set; }
@@ -36,6 +38,45 @@ namespace LCU.Webapps.Marketing.SPIT.Model
return new Uri(SpeechEndpoint.Trim(), UriKind.Absolute);
}
public Uri ResolveSpeechSynthesisEndpointUri()
{
var endpoint = ResolveSpeechEndpointUri();
if (endpoint == null)
{
return null;
}
var endpointPath = endpoint.AbsolutePath?.Trim() ?? string.Empty;
if (string.IsNullOrWhiteSpace(endpointPath) || endpointPath == "/")
{
return new Uri(endpoint, ResolveSpeechSynthesisPath());
}
if (endpointPath.EndsWith("/websocket/v1", StringComparison.OrdinalIgnoreCase))
{
return new UriBuilder(endpoint)
{
Scheme = Uri.UriSchemeHttps,
Port = -1,
Path = endpointPath.Replace("/websocket/v1", "/v1", StringComparison.OrdinalIgnoreCase),
Query = string.Empty
}.Uri;
}
if (endpointPath.EndsWith("/voices/list", StringComparison.OrdinalIgnoreCase))
{
return new UriBuilder(endpoint)
{
Scheme = Uri.UriSchemeHttps,
Port = -1,
Path = endpointPath.Replace("/voices/list", "/v1", StringComparison.OrdinalIgnoreCase),
Query = string.Empty
}.Uri;
}
return endpoint;
}
public Uri ResolveSpeechTokenUri()
{
return new Uri(ResolveSpeechEndpointUri(), FetchTokenUri);
@@ -45,5 +86,20 @@ namespace LCU.Webapps.Marketing.SPIT.Model
{
return new Uri(ResolveSpeechEndpointUri(), VoicesUrl);
}
private string ResolveSpeechSynthesisPath()
{
if (!string.IsNullOrWhiteSpace(WebsocketUrl))
{
return WebsocketUrl.Replace("/websocket/v1", "/v1", StringComparison.OrdinalIgnoreCase);
}
if (!string.IsNullOrWhiteSpace(VoicesUrl))
{
return VoicesUrl.Replace("/voices/list", "/v1", StringComparison.OrdinalIgnoreCase);
}
return DefaultSpeechSynthesisPath;
}
}
}
@@ -107,9 +107,25 @@ namespace LCU.Webapps.Marketing.SPIT.Services
private SpeechConfig BuildSpeechConfig()
{
return SpeechConfig.FromEndpoint(
_spitConfiguration.ResolveSpeechEndpointUri(),
_spitConfiguration.SpeechApiKey);
var synthesisEndpoint = _spitConfiguration.ResolveSpeechSynthesisEndpointUri();
if (synthesisEndpoint != null)
{
Log.Information(
"Using speech synthesis endpoint {SpeechSynthesisEndpoint} for file generation.",
synthesisEndpoint);
return SpeechConfig.FromEndpoint(
synthesisEndpoint,
_spitConfiguration.SpeechApiKey);
}
Log.Information(
"SpeechEndpoint is not configured for file generation. Falling back to region {Region}.",
_spitConfiguration.Region);
return SpeechConfig.FromSubscription(
_spitConfiguration.SpeechApiKey,
_spitConfiguration.Region);
}
}
}