Skip to content

Commit

Permalink
better handling of failed speech token acquisition
Browse files Browse the repository at this point in the history
  • Loading branch information
bkrabach committed Nov 11, 2024
1 parent eb7568e commit 3a2c40d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
18 changes: 10 additions & 8 deletions workbench-app/src/components/Conversations/SpeechButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface SpeechButtonProps {
export const SpeechButton: React.FC<SpeechButtonProps> = (props) => {
const { disabled, onListeningChange, onSpeechRecognizing, onSpeechRecognized } = props;
const [recognizer, setRecognizer] = React.useState<speechSdk.SpeechRecognizer>();
const [isFetching, setIsFetching] = React.useState(false);
const [isInitialized, setIsInitialized] = React.useState(false);
const [isListening, setIsListening] = React.useState(false);
const [lastSpeechResultTimestamp, setLastSpeechResultTimestamp] = React.useState(0);

Expand Down Expand Up @@ -115,15 +115,17 @@ export const SpeechButton: React.FC<SpeechButtonProps> = (props) => {
}, [getAzureSpeechTokenAsync, onSpeechRecognized, onSpeechRecognizing]);

React.useEffect(() => {
// If the recognizer is already available or we are fetching it, do nothing
if (recognizer || isFetching) return;
// If the recognizer is already initialized, return
if (isInitialized) return;

// Indicate that we are fetching the recognizer to prevent multiple fetches
setIsFetching(true);
// Set the recognizer as initialized
setIsInitialized(true);

// Fetch the recognizer, then indicate that we are no longer fetching even if the fetch fails
getRecognizer().finally(() => setIsFetching(false));
}, [getRecognizer, isFetching, recognizer]);
(async () => {
// Fetch the recognizer
await getRecognizer();
})();
}, [getRecognizer, isInitialized, recognizer]);

React.useEffect(() => {
onListeningChange(isListening);
Expand Down
10 changes: 9 additions & 1 deletion workbench-service/semantic_workbench_service/azure_speech.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import logging

from azure.identity import DefaultAzureCredential

from . import settings

logger = logging.getLogger(__name__)


def get_token() -> dict[str, str]:
if settings.azure_speech.resource_id == "" or settings.azure_speech.region == "":
return {}

credential = DefaultAzureCredential()
token = credential.get_token("https://cognitiveservices.azure.com/.default").token
try:
token = credential.get_token("https://cognitiveservices.azure.com/.default").token
except Exception as e:
logger.error(f"Failed to get token: {e}")
return {}

return {
"token": f"aad#{settings.azure_speech.resource_id}#{token}",
Expand Down

0 comments on commit 3a2c40d

Please sign in to comment.