Skip to content

Commit f788684

Browse files
committed
wip
1 parent cef03ee commit f788684

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

apps/99_streamlit_examples/pages/13_Azure_AI_Speech.py

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
from enum import Enum
23
from os import getenv
34

45
import azure.cognitiveservices.speech as speechsdk
@@ -7,18 +8,43 @@
78

89
load_dotenv()
910

11+
12+
class ServiceType(Enum):
13+
Azure = "Azure"
14+
Local = "Local"
15+
16+
17+
class TranscriptionStatus(Enum):
18+
NotStarted = "Not started"
19+
InProgress = "In progress"
20+
Completed = "Completed"
21+
22+
23+
if "transcription_status" not in st.session_state:
24+
st.session_state.transcription_status = TranscriptionStatus.NotStarted
25+
26+
1027
with st.sidebar:
28+
speech_recognition_language = st.selectbox(
29+
label="Speech recognition language",
30+
options=[
31+
"en-US",
32+
"ja-JP",
33+
"zh-CN",
34+
],
35+
)
1136
service_type = st.selectbox(
1237
label="Service type",
1338
options=[
14-
"Local",
15-
"Azure",
39+
ServiceType.Local.value,
40+
ServiceType.Azure.value,
1641
],
1742
)
18-
if service_type == "Azure":
19-
azure_ai_services_api_key = st.text_input(
20-
label="AZURE_AI_SERVICES_API_KEY",
21-
key="AZURE_AI_SERVICES_API_KEY",
43+
if service_type == ServiceType.Azure.value:
44+
azure_ai_speech_api_subscription_key = st.text_input(
45+
label="AZURE_AI_SPEECH_API_SUBSCRIPTION_KEY",
46+
value=getenv("AZURE_AI_SPEECH_API_SUBSCRIPTION_KEY"),
47+
key="AZURE_AI_SPEECH_API_SUBSCRIPTION_KEY",
2248
type="password",
2349
)
2450
azure_ai_speech_region = st.text_input(
@@ -27,7 +53,7 @@
2753
key="AZURE_AI_SPEECH_REGION",
2854
type="default",
2955
)
30-
if service_type == "Local":
56+
if service_type == ServiceType.Local.value:
3157
host = st.text_input(
3258
label="Host",
3359
value="ws://localhost:5000",
@@ -40,22 +66,24 @@
4066

4167

4268
def is_configured():
43-
if service_type == "Azure":
44-
return azure_ai_services_api_key and azure_ai_speech_region
45-
if service_type == "Local":
46-
return host != ""
69+
if service_type == ServiceType.Azure.value:
70+
return azure_ai_speech_api_subscription_key and azure_ai_speech_region and speech_recognition_language
71+
if service_type == ServiceType.Local.value:
72+
return host != "" and speech_recognition_language
4773
return False
4874

4975

5076
def get_speech_config():
51-
if service_type == "Azure":
77+
if service_type == ServiceType.Azure.value:
5278
return speechsdk.SpeechConfig(
53-
subscription=azure_ai_services_api_key,
79+
subscription=azure_ai_speech_api_subscription_key,
5480
region=azure_ai_speech_region,
81+
speech_recognition_language=speech_recognition_language,
5582
)
56-
if service_type == "Local":
83+
if service_type == ServiceType.Local.value:
5784
return speechsdk.SpeechConfig(
5885
endpoint=host,
86+
speech_recognition_language=speech_recognition_language,
5987
)
6088

6189

@@ -113,10 +141,12 @@ def conversation_transcriber_session_started_cb(evt: speechsdk.SessionEventArgs)
113141

114142
st.title("Azure AI Speech Services")
115143

144+
# Show transcription status
145+
st.info(f"Transcription status: {st.session_state.transcription_status}")
146+
116147
if not is_configured():
117148
st.warning("Please fill in the required fields at the sidebar.")
118149

119-
st.info("Transcribe your speech.")
120-
121150
if st.button("Transcribe", disabled=not is_configured()):
151+
st.session_state.transcription_status = TranscriptionStatus.InProgress
122152
from_mic()

0 commit comments

Comments
 (0)