mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-25 14:55:55 +01:00
[watsonstt] reuse token (#12800)
* [watsonstt] reuse token Signed-off-by: Miguel Álvarez Díez <miguelwork92@gmail.com>
This commit is contained in:
parent
98af136e4a
commit
1b5471cf65
@ -77,15 +77,26 @@ public class WatsonSTTService implements STTService {
|
|||||||
private final Set<Locale> supportedLocales = models.stream().map(name -> name.split("_")[0])
|
private final Set<Locale> supportedLocales = models.stream().map(name -> name.split("_")[0])
|
||||||
.map(Locale::forLanguageTag).collect(Collectors.toSet());
|
.map(Locale::forLanguageTag).collect(Collectors.toSet());
|
||||||
private WatsonSTTConfiguration config = new WatsonSTTConfiguration();
|
private WatsonSTTConfiguration config = new WatsonSTTConfiguration();
|
||||||
|
private @Nullable SpeechToText speechToText = null;
|
||||||
|
|
||||||
@Activate
|
@Activate
|
||||||
protected void activate(Map<String, Object> config) {
|
protected void activate(Map<String, Object> config) {
|
||||||
this.config = new Configuration(config).as(WatsonSTTConfiguration.class);
|
modified(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Modified
|
@Modified
|
||||||
protected void modified(Map<String, Object> config) {
|
protected void modified(Map<String, Object> config) {
|
||||||
this.config = new Configuration(config).as(WatsonSTTConfiguration.class);
|
this.config = new Configuration(config).as(WatsonSTTConfiguration.class);
|
||||||
|
if (this.config.apiKey.isBlank() || this.config.instanceUrl.isBlank()) {
|
||||||
|
this.speechToText = null;
|
||||||
|
} else {
|
||||||
|
var speechToText = new SpeechToText(new IamAuthenticator.Builder().apikey(this.config.apiKey).build());
|
||||||
|
speechToText.setServiceUrl(this.config.instanceUrl);
|
||||||
|
if (this.config.optOutLogging) {
|
||||||
|
speechToText.setDefaultHeaders(Map.of("X-Watson-Learning-Opt-Out", "1"));
|
||||||
|
}
|
||||||
|
this.speechToText = speechToText;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -112,7 +123,8 @@ public class WatsonSTTService implements STTService {
|
|||||||
@Override
|
@Override
|
||||||
public STTServiceHandle recognize(STTListener sttListener, AudioStream audioStream, Locale locale, Set<String> set)
|
public STTServiceHandle recognize(STTListener sttListener, AudioStream audioStream, Locale locale, Set<String> set)
|
||||||
throws STTException {
|
throws STTException {
|
||||||
if (config.apiKey.isBlank() || config.instanceUrl.isBlank()) {
|
var stt = this.speechToText;
|
||||||
|
if (stt == null) {
|
||||||
throw new STTException("service is not correctly configured");
|
throw new STTException("service is not correctly configured");
|
||||||
}
|
}
|
||||||
String contentType = getContentType(audioStream);
|
String contentType = getContentType(audioStream);
|
||||||
@ -120,11 +132,6 @@ public class WatsonSTTService implements STTService {
|
|||||||
throw new STTException("Unsupported format, unable to resolve audio content type");
|
throw new STTException("Unsupported format, unable to resolve audio content type");
|
||||||
}
|
}
|
||||||
logger.debug("Content-Type: {}", contentType);
|
logger.debug("Content-Type: {}", contentType);
|
||||||
var speechToText = new SpeechToText(new IamAuthenticator.Builder().apikey(config.apiKey).build());
|
|
||||||
speechToText.setServiceUrl(config.instanceUrl);
|
|
||||||
if (config.optOutLogging) {
|
|
||||||
speechToText.setDefaultHeaders(Map.of("X-Watson-Learning-Opt-Out", "1"));
|
|
||||||
}
|
|
||||||
RecognizeWithWebsocketsOptions wsOptions = new RecognizeWithWebsocketsOptions.Builder().audio(audioStream)
|
RecognizeWithWebsocketsOptions wsOptions = new RecognizeWithWebsocketsOptions.Builder().audio(audioStream)
|
||||||
.contentType(contentType).redaction(config.redaction).smartFormatting(config.smartFormatting)
|
.contentType(contentType).redaction(config.redaction).smartFormatting(config.smartFormatting)
|
||||||
.model(locale.toLanguageTag() + "_BroadbandModel").interimResults(true)
|
.model(locale.toLanguageTag() + "_BroadbandModel").interimResults(true)
|
||||||
@ -134,7 +141,7 @@ public class WatsonSTTService implements STTService {
|
|||||||
final AtomicReference<@Nullable WebSocket> socketRef = new AtomicReference<>();
|
final AtomicReference<@Nullable WebSocket> socketRef = new AtomicReference<>();
|
||||||
final AtomicBoolean aborted = new AtomicBoolean(false);
|
final AtomicBoolean aborted = new AtomicBoolean(false);
|
||||||
executor.submit(() -> {
|
executor.submit(() -> {
|
||||||
socketRef.set(speechToText.recognizeUsingWebSocket(wsOptions,
|
socketRef.set(stt.recognizeUsingWebSocket(wsOptions,
|
||||||
new TranscriptionListener(socketRef, sttListener, config, aborted)));
|
new TranscriptionListener(socketRef, sttListener, config, aborted)));
|
||||||
});
|
});
|
||||||
return new STTServiceHandle() {
|
return new STTServiceHandle() {
|
||||||
|
Loading…
Reference in New Issue
Block a user