13
13
st .session_state ["transcribed_result" ] = ""
14
14
15
15
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" ,
54
18
options = [
55
- "en-US " ,
56
- "ja-JP " ,
19
+ "local " ,
20
+ "azure " ,
57
21
],
58
- key = "AZURE_AI_SPEECH_API_LANGUAGE " ,
22
+ key = "INEFERENCE_TYPE " ,
59
23
)
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
+ )
60
71
"[Azure Portal](https://portal.azure.com/)"
61
72
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
62
73
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/14_streamlit_azure_ai_speech/main.py)"
63
74
64
75
65
76
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
67
81
68
82
69
83
st .title ("transcribe text" )
@@ -103,7 +117,7 @@ def is_configured():
103
117
"Task" ,
104
118
[
105
119
"Create summaries from the following text" ,
106
- "Translate the following text into English " ,
120
+ "Extract 3 main points from the following text" ,
107
121
# Add more tasks here
108
122
],
109
123
key = "selected_task" ,
@@ -118,32 +132,41 @@ def is_configured():
118
132
119
133
def start_recognition ():
120
134
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 )
123
141
124
142
125
143
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
+ )
131
153
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 } " )
147
170
148
171
149
172
def load_transcribed_text ():
@@ -153,7 +176,7 @@ def load_transcribed_text():
153
176
154
177
if start_transcribe_button :
155
178
if not st .session_state .get ("process" ):
156
- transcription_status .info ("Transcribing..." )
179
+ transcription_status .info (f "Transcribing... (language= { azure_ai_speech_api_language } ) " )
157
180
start_recognition ()
158
181
else :
159
182
transcription_status .warning ("Transcription is already running." )
@@ -165,10 +188,9 @@ def load_transcribed_text():
165
188
st .rerun ()
166
189
167
190
if run_task_button :
168
- transcribed_text = load_transcribed_text ()
169
191
with st .spinner ("Running..." ):
170
192
output = run_task (
171
193
selected_task = selected_task ,
172
- input = transcribed_text ,
194
+ input = input ,
173
195
)
174
196
st .write (output )
0 commit comments