Skip to content

Commit d591f90

Browse files
committed
add ai speech services
1 parent c2adbf6 commit d591f90

File tree

4 files changed

+108
-18
lines changed

4 files changed

+108
-18
lines changed

.env.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ AZURE_BLOB_CONTAINER_NAME="audio"
3131
# Azure AI Speech
3232
AZURE_AI_SPEECH_API_ENDPOINT="https://<speech-api-name>.cognitiveservices.azure.com/"
3333
AZURE_AI_SPEECH_API_SUBSCRIPTION_KEY="<speech-api-subscription-key>"
34+
AZURE_AI_SPEECH_REGION="southeastasia"
3435

3536
# Bing search resource
3637
BING_SUBSCRIPTION_KEY="<bing-subscription-key>"
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from os import getenv
2+
3+
import azure.cognitiveservices.speech as speechsdk
4+
import streamlit as st
5+
from dotenv import load_dotenv
6+
7+
load_dotenv()
8+
9+
with st.sidebar:
10+
service_type = st.selectbox(
11+
label="Service type",
12+
options=[
13+
"Local",
14+
"Azure",
15+
],
16+
)
17+
if service_type == "Azure":
18+
azure_ai_services_api_key = st.text_input(
19+
label="AZURE_AI_SERVICES_API_KEY",
20+
key="AZURE_AI_SERVICES_API_KEY",
21+
type="password",
22+
)
23+
azure_ai_speech_region = st.text_input(
24+
label="AZURE_AI_SPEECH_REGION",
25+
value=getenv("AZURE_AI_SPEECH_REGION"),
26+
key="AZURE_AI_SPEECH_REGION",
27+
type="default",
28+
)
29+
if service_type == "Local":
30+
host = st.text_input(
31+
label="Host",
32+
value="ws://localhost:5000",
33+
key="host",
34+
type="default",
35+
)
36+
"[Azure Portal](https://portal.azure.com/)"
37+
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/13_Azure_AI_Speech.py)"
38+
"[Speech to text containers with Docker](https://learn.microsoft.com/azure/ai-services/speech-service/speech-container-stt?tabs=container&pivots=programming-language-python)"
39+
40+
41+
def is_configured():
42+
if service_type == "Azure":
43+
return azure_ai_services_api_key and azure_ai_speech_region
44+
if service_type == "Local":
45+
return host != ""
46+
return False
47+
48+
49+
def get_speech_config():
50+
if service_type == "Azure":
51+
return speechsdk.SpeechConfig(
52+
subscription=azure_ai_services_api_key,
53+
region=azure_ai_speech_region,
54+
)
55+
if service_type == "Local":
56+
return speechsdk.SpeechConfig(
57+
endpoint=host,
58+
)
59+
60+
61+
def from_mic():
62+
speech_recognizer = speechsdk.SpeechRecognizer(
63+
speech_config=get_speech_config(),
64+
)
65+
66+
print("Speak into your microphone.")
67+
speech_recognition_result = speech_recognizer.recognize_once_async().get()
68+
print(speech_recognition_result.text)
69+
70+
71+
st.title("Azure AI Speech Services")
72+
73+
if not is_configured():
74+
st.warning("Please fill in the required fields at the sidebar.")
75+
76+
st.info("Transcribe your speech.")
77+
78+
if st.button("Transcribe", disabled=not is_configured()):
79+
from_mic()

poetry.lock

Lines changed: 27 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ playwright = "^1.47.0"
3636
lxml = "^5.3.0"
3737
nest-asyncio = "^1.6.0"
3838
typer = "^0.12.5"
39+
azure-cognitiveservices-speech = "^1.40.0"
3940

4041
[tool.poetry.group.dev.dependencies]
4142
pre-commit = "^3.8.0"

0 commit comments

Comments
 (0)