Skip to content

Commit 4423067

Browse files
committed
w
1 parent 1523e68 commit 4423067

File tree

1 file changed

+90
-68
lines changed
  • apps/14_streamlit_azure_ai_speech

1 file changed

+90
-68
lines changed

apps/14_streamlit_azure_ai_speech/main.py

Lines changed: 90 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,71 @@
1313
st.session_state["transcribed_result"] = ""
1414

1515
with st.sidebar:
16-
azure_openai_endpoint = st.text_input(
17-
label="AZURE_OPENAI_ENDPOINT",
18-
value=getenv("AZURE_OPENAI_ENDPOINT"),
19-
key="AZURE_OPENAI_ENDPOINT",
20-
type="default",
21-
)
22-
azure_openai_api_key = st.text_input(
23-
label="AZURE_OPENAI_API_KEY",
24-
value=getenv("AZURE_OPENAI_API_KEY"),
25-
key="AZURE_OPENAI_API_KEY",
26-
type="password",
27-
)
28-
azure_openai_api_version = st.text_input(
29-
label="AZURE_OPENAI_API_VERSION",
30-
value=getenv("AZURE_OPENAI_API_VERSION"),
31-
key="AZURE_OPENAI_API_VERSION",
32-
type="default",
33-
)
34-
azure_openai_gpt_model = st.text_input(
35-
label="AZURE_OPENAI_GPT_MODEL",
36-
value=getenv("AZURE_OPENAI_GPT_MODEL"),
37-
key="AZURE_OPENAI_GPT_MODEL",
38-
type="default",
39-
)
40-
azure_ai_speech_api_subscription_key = st.text_input(
41-
label="AZURE_AI_SPEECH_API_SUBSCRIPTION_KEY",
42-
value=getenv("AZURE_AI_SPEECH_API_SUBSCRIPTION_KEY"),
43-
key="AZURE_AI_SPEECH_API_SUBSCRIPTION_KEY",
44-
type="password",
45-
)
46-
azure_ai_speech_api_region = st.text_input(
47-
label="AZURE_AI_SPEECH_API_REGION",
48-
value=getenv("AZURE_AI_SPEECH_API_REGION"),
49-
key="AZURE_AI_SPEECH_API_REGION",
50-
type="default",
51-
)
52-
azure_ai_speech_api_language = st.selectbox(
53-
label="AZURE_AI_SPEECH_API_LANGUAGE",
16+
inference_type = st.selectbox(
17+
label="INEFERENCE_TYPE",
5418
options=[
55-
"en-US",
56-
"ja-JP",
19+
"local",
20+
"azure",
5721
],
58-
key="AZURE_AI_SPEECH_API_LANGUAGE",
22+
key="INEFERENCE_TYPE",
5923
)
24+
if inference_type == "local":
25+
st.warning("yet to be implemented")
26+
if inference_type == "azure":
27+
azure_openai_endpoint = st.text_input(
28+
label="AZURE_OPENAI_ENDPOINT",
29+
value=getenv("AZURE_OPENAI_ENDPOINT"),
30+
key="AZURE_OPENAI_ENDPOINT",
31+
type="default",
32+
)
33+
azure_openai_api_key = st.text_input(
34+
label="AZURE_OPENAI_API_KEY",
35+
value=getenv("AZURE_OPENAI_API_KEY"),
36+
key="AZURE_OPENAI_API_KEY",
37+
type="password",
38+
)
39+
azure_openai_api_version = st.text_input(
40+
label="AZURE_OPENAI_API_VERSION",
41+
value=getenv("AZURE_OPENAI_API_VERSION"),
42+
key="AZURE_OPENAI_API_VERSION",
43+
type="default",
44+
)
45+
azure_openai_gpt_model = st.text_input(
46+
label="AZURE_OPENAI_GPT_MODEL",
47+
value=getenv("AZURE_OPENAI_GPT_MODEL"),
48+
key="AZURE_OPENAI_GPT_MODEL",
49+
type="default",
50+
)
51+
azure_ai_speech_api_subscription_key = st.text_input(
52+
label="AZURE_AI_SPEECH_API_SUBSCRIPTION_KEY",
53+
value=getenv("AZURE_AI_SPEECH_API_SUBSCRIPTION_KEY"),
54+
key="AZURE_AI_SPEECH_API_SUBSCRIPTION_KEY",
55+
type="password",
56+
)
57+
azure_ai_speech_api_region = st.text_input(
58+
label="AZURE_AI_SPEECH_API_REGION",
59+
value=getenv("AZURE_AI_SPEECH_API_REGION"),
60+
key="AZURE_AI_SPEECH_API_REGION",
61+
type="default",
62+
)
63+
azure_ai_speech_api_language = st.selectbox(
64+
label="AZURE_AI_SPEECH_API_LANGUAGE",
65+
options=[
66+
"en-US",
67+
"ja-JP",
68+
],
69+
key="AZURE_AI_SPEECH_API_LANGUAGE",
70+
)
6071
"[Azure Portal](https://portal.azure.com/)"
6172
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
6273
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/14_streamlit_azure_ai_speech/main.py)"
6374

6475

6576
def is_configured():
66-
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model
77+
if inference_type == "local":
78+
return False
79+
if inference_type == "azure":
80+
return azure_openai_api_key and azure_openai_endpoint and azure_openai_api_version and azure_openai_gpt_model
6781

6882

6983
st.title("transcribe text")
@@ -103,7 +117,7 @@ def is_configured():
103117
"Task",
104118
[
105119
"Create summaries from the following text",
106-
"Translate the following text into English",
120+
"Extract 3 main points from the following text",
107121
# Add more tasks here
108122
],
109123
key="selected_task",
@@ -118,32 +132,41 @@ def is_configured():
118132

119133
def start_recognition():
120134
global process
121-
command = f"python apps/14_streamlit_azure_ai_speech/speech_to_text.py --output {path_to_transcribed_text} --subscription {azure_ai_speech_api_subscription_key} --region {azure_ai_speech_api_region} --language {azure_ai_speech_api_language} --verbose" # noqa
122-
process = subprocess.Popen(command, shell=True)
135+
if inference_type == "local":
136+
st.warning("Local inference is not yet implemented.")
137+
return
138+
if inference_type == "azure":
139+
command = f"python apps/14_streamlit_azure_ai_speech/speech_to_text.py --output {path_to_transcribed_text} --subscription {azure_ai_speech_api_subscription_key} --region {azure_ai_speech_api_region} --language {azure_ai_speech_api_language} --verbose" # noqa
140+
process = subprocess.Popen(command, shell=True)
123141

124142

125143
def run_task(selected_task: str, input: str) -> str:
126-
client = AzureOpenAI(
127-
api_key=azure_openai_api_key,
128-
api_version=azure_openai_api_version,
129-
azure_endpoint=azure_openai_endpoint,
130-
)
144+
if inference_type == "local":
145+
st.warning("Local inference is not yet implemented.")
146+
return
147+
if inference_type == "azure":
148+
client = AzureOpenAI(
149+
api_key=azure_openai_api_key,
150+
api_version=azure_openai_api_version,
151+
azure_endpoint=azure_openai_endpoint,
152+
)
131153

132-
response = client.chat.completions.create(
133-
model=azure_openai_gpt_model,
134-
messages=[
135-
{
136-
"role": "system",
137-
"content": f"""
138-
Task: {selected_task}.
139-
---
140-
{input}
141-
---
142-
""",
143-
},
144-
],
145-
)
146-
return response.choices[0].message.content
154+
response = client.chat.completions.create(
155+
model=azure_openai_gpt_model,
156+
messages=[
157+
{
158+
"role": "system",
159+
"content": f"""
160+
Task: {selected_task}.
161+
---
162+
{input}
163+
---
164+
""",
165+
},
166+
],
167+
)
168+
return response.choices[0].message.content
169+
raise ValueError(f"Inference type is not supported: {inference_type}")
147170

148171

149172
def load_transcribed_text():
@@ -153,7 +176,7 @@ def load_transcribed_text():
153176

154177
if start_transcribe_button:
155178
if not st.session_state.get("process"):
156-
transcription_status.info("Transcribing...")
179+
transcription_status.info(f"Transcribing... (language={azure_ai_speech_api_language})")
157180
start_recognition()
158181
else:
159182
transcription_status.warning("Transcription is already running.")
@@ -165,10 +188,9 @@ def load_transcribed_text():
165188
st.rerun()
166189

167190
if run_task_button:
168-
transcribed_text = load_transcribed_text()
169191
with st.spinner("Running..."):
170192
output = run_task(
171193
selected_task=selected_task,
172-
input=transcribed_text,
194+
input=input,
173195
)
174196
st.write(output)

0 commit comments

Comments
 (0)