haohoo commited on
Commit
e549cbf
1 Parent(s): e1106c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -50
app.py CHANGED
@@ -1,52 +1,98 @@
1
- import os, time, wave
2
  import openai
3
  import gradio as gr
4
  import requests
5
  from pydub import AudioSegment as am
6
  from xml.etree import ElementTree
7
 
8
- aoai_url = "https://mvp-azureopenai.openai.azure.com/"
9
- aoai_key = ""
10
- stts_key = ""
11
 
12
  openai.api_type = "azure"
13
- openai.api_base = "https://mvp-azureopenai.openai.azure.com/"
14
  openai.api_version = "2023-03-15-preview"
15
  # openai.api_key = aoai_key
16
 
 
 
17
  messages_gpt = []
 
18
  messages_chat = [
19
  {"role": "system", "content": "You are an AI assistant that helps people find information."},
20
  ]
21
- prompts = ""
22
  response_walle = []
 
23
  messages_vchat = [
24
- {"role": "system", "content": "You are an AI assistant that helps people find information and just response with SSML."},
25
  ]
26
 
27
- def get_aoai_key(txt_inpout):
28
- if txt_inpout:
29
- openai.api_key = txt_inpout
30
- return gr.update(value=txt_inpout)
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  with gr.Blocks() as page:
33
- with gr.Row():
34
- with gr.Column(scale=0.4):
35
- gr.Markdown("Your Key will not be saved or viewed by anyone.")
36
- with gr.Column(scale=0.6):
37
- ui_aoai_key = gr.Textbox(placeholder="Please enter your Azure OpenAI API key here.", lines=1, show_label=False, type='password').style(container=False)
38
- ui_aoai_key.submit(get_aoai_key, ui_aoai_key, ui_aoai_key)
39
-
40
  with gr.Tabs():
41
- with gr.TabItem("GPT Playground"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  ui_chatbot_gpt = gr.Chatbot(label="GPT Playground:")
43
  with gr.Row():
44
  with gr.Column(scale=0.9):
45
  ui_prompt_gpt = gr.Textbox(placeholder="Please enter your prompt here.", show_label=False).style(container=False)
46
  with gr.Column(scale=0.1, min_width=100):
47
  ui_clear_gpt = gr.Button("Clear Input", )
48
- with gr.Accordion("Expand to config parameters:", open=False):
49
- gr.Markdown("Look at me...")
 
 
50
  with gr.Row():
51
  ui_temp_gpt = gr.Slider(0.1, 1.0, 0.9, step=0.1, label="Temperature", interactive=True)
52
  ui_max_tokens_gpt = gr.Slider(100, 4000, 1000, step=100, label="Max Tokens", interactive=True)
@@ -61,6 +107,11 @@ with gr.Blocks() as page:
61
  ui_top_p_gpt.value = slider_3
62
  print("Log - Updated GPT parameters: Temperature=", ui_temp_gpt.value,
63
  " Max Tokens=", ui_max_tokens_gpt.value, " Top_P=", ui_top_p_gpt.value)
 
 
 
 
 
64
 
65
  def select_response_gpt(radio):
66
  if radio == "Response from OpenAI Model":
@@ -76,8 +127,9 @@ with gr.Blocks() as page:
76
 
77
  def bot_gpt(history):
78
  global gpt_x
 
79
  gpt_x = openai.Completion.create(
80
- engine="mvp-text-davinci-003",
81
  prompt=prompts,
82
  temperature=0.6,
83
  max_tokens=1000,
@@ -92,17 +144,17 @@ with gr.Blocks() as page:
92
  history[-1][1] = gpt_reply
93
  return history
94
 
 
95
  ui_temp_gpt.change(get_parameters_gpt, [ui_temp_gpt, ui_max_tokens_gpt, ui_top_p_gpt])
96
  ui_max_tokens_gpt.change(get_parameters_gpt, [ui_temp_gpt, ui_max_tokens_gpt, ui_top_p_gpt])
97
  ui_top_p_gpt.change(get_parameters_gpt, [ui_temp_gpt, ui_max_tokens_gpt, ui_top_p_gpt])
98
-
99
  ui_prompt_gpt.submit(user_gpt, [ui_prompt_gpt, ui_chatbot_gpt], [ui_prompt_gpt, ui_chatbot_gpt], queue=False).then(
100
  bot_gpt, ui_chatbot_gpt, ui_chatbot_gpt
101
  )
102
  ui_clear_gpt.click(lambda: None, None, ui_chatbot_gpt, queue=False)
103
  ui_res_radio_gpt.change(select_response_gpt, ui_res_radio_gpt, ui_response_gpt)
104
 
105
- with gr.TabItem("ChatGPT"):
106
  ui_chatbot_chat = gr.Chatbot(label="ChatGPT:")
107
  with gr.Row():
108
  with gr.Column(scale=0.9):
@@ -110,9 +162,13 @@ with gr.Blocks() as page:
110
  with gr.Column(scale=0.1, min_width=100):
111
  ui_clear_chat = gr.Button("Clear Chat")
112
  with gr.Blocks():
113
- with gr.Accordion("Expand to config parameters:", open=False):
114
- gr.Markdown("Here is the default system prompt, you can change it to your own prompt.")
115
- ui_prompt_sys = gr.Textbox(value="You are an AI assistant that helps people find information.", show_label=False, interactive=True).style(container=False)
 
 
 
 
116
  with gr.Row():
117
  ui_temp_chat = gr.Slider(0.1, 1.0, 0.7, step=0.1, label="Temperature", interactive=True)
118
  ui_max_tokens_chat = gr.Slider(100, 8000, 2000, step=100, label="Max Tokens", interactive=True)
@@ -127,7 +183,12 @@ with gr.Blocks() as page:
127
  ui_top_p_chat.value = slider_3
128
  print("Log - Updated chatGPT parameters: Temperature=", ui_temp_chat.value,
129
  " Max Tokens=", ui_max_tokens_chat.value, " Top_P=", ui_top_p_chat.value)
130
-
 
 
 
 
 
131
  def select_response_chat(radio):
132
  if radio == "Response from OpenAI Model":
133
  return gr.update(value=chat_x)
@@ -141,7 +202,7 @@ with gr.Blocks() as page:
141
  def bot_chat(history):
142
  global chat_x
143
  chat_x = openai.ChatCompletion.create(
144
- engine="mvp-gpt-35-turbo", messages=messages_chat,
145
  temperature=ui_temp_chat.value,
146
  max_tokens=ui_max_tokens_chat.value,
147
  top_p=ui_top_p_chat.value,
@@ -165,6 +226,7 @@ with gr.Blocks() as page:
165
  {"role": "system", "content": sysmsg},
166
  ]
167
 
 
168
  ui_res_radio_chat.change(select_response_chat, ui_res_radio_chat, ui_response_chat)
169
  ui_temp_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
170
  ui_max_tokens_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
@@ -176,8 +238,9 @@ with gr.Blocks() as page:
176
  ui_clear_chat.click(lambda: None, None, ui_chatbot_chat, queue=False).then(reset_sys, ui_prompt_sys)
177
 
178
 
179
- with gr.TabItem("DALL路E 2"):
180
- ui_prompt_walle = gr.Textbox(placeholder="Please enter your prompt here to generate image.", show_label=False).style(container=False)
 
181
  ui_image_walle = gr.Image()
182
  with gr.Accordion("Select radio button to see detail:", open=False):
183
  ui_response_walle = gr.TextArea(show_label=False, interactive=False).style(container=False)
@@ -212,15 +275,15 @@ with gr.Blocks() as page:
212
 
213
  ui_prompt_walle.submit(get_image_walle, ui_prompt_walle, ui_image_walle, queue=False).then(get_response_walle, None, ui_response_walle)
214
 
215
- with gr.TabItem("VoiceChat"):
216
  with gr.Row():
217
  with gr.Column():
218
- with gr.Accordion("Expand to config parameters:", open=False):
219
- ui_prompt_sys_vchat = gr.Textbox(value="You are an AI assistant that helps people find information and just response with SSML.", show_label=False, interactive=True).style(container=False)
220
- ui_stts_key = gr.Textbox(placeholder="Input your Azure Speech service API key", show_label=False, interactive=True).style(container=False)
221
- ui_stts_key.submit(lambda x: None, ui_stts_key)
222
- stts_key = ui_aoai_key.value
223
- print(stts_key)
224
 
225
  ui_voice_inc_vchat = gr.Audio(source="microphone", type="filepath")
226
  ui_voice_out_vchat = gr.Audio(value=None, type="filepath", interactive=False).style(container=False)
@@ -242,22 +305,24 @@ with gr.Blocks() as page:
242
  print("Log - Updated chatGPT parameters: Temperature=", ui_temp_vchat.value,
243
  " Max Tokens=", ui_max_tokens_vchat.value, " Top_P=", ui_top_p_vchat.value)
244
 
 
 
 
 
 
245
  def select_response_vchat(radio):
246
  if radio == "Response from OpenAI Model":
247
  return gr.update(value=vchat_x)
248
  else:
249
  return gr.update(value=messages_vchat)
250
 
251
-
252
-
253
  def speech_to_text(voice_message):
254
  # Downsample input voice to 16kHz
255
  voice_wav = am.from_file(voice_message, format='wav')
256
  voice_wav = voice_wav.set_frame_rate(16000)
257
  voice_wav.export(voice_message, format='wav')
258
  # STT
259
- OASK = stts_key
260
- service_region = "westus"
261
 
262
  base_url = "https://"+service_region+".stt.speech.microsoft.com/"
263
  path = 'speech/recognition/conversation/cognitiveservices/v1'
@@ -267,7 +332,7 @@ with gr.Blocks() as page:
267
  'format': 'detailed'
268
  }
269
  headers = {
270
- 'Ocp-Apim-Subscription-Key': OASK_Speech,
271
  'Content-Type': 'audio/wav; codecs=audio/pcm; samplerate=16000',
272
  'Accept': 'application/json;text/xml'
273
  }
@@ -285,14 +350,14 @@ with gr.Blocks() as page:
285
  return sst_text
286
 
287
  def text_to_speech():
288
- OASK_Speech = stts_key
289
- service_region = "westus"
290
-
291
  base_url = "https://"+service_region+".tts.speech.microsoft.com/"
292
  path = 'cognitiveservices/v1'
293
  constructed_url = base_url + path
294
  headers = {
295
- 'Ocp-Apim-Subscription-Key': OASK_Speech,
296
  'Content-Type': 'application/ssml+xml',
297
  'X-Microsoft-OutputFormat': 'riff-24khz-16bit-mono-pcm',
298
  'User-Agent': 'Voice ChatGPT'
@@ -324,7 +389,7 @@ with gr.Blocks() as page:
324
  def bot_vchat(history):
325
  global vchat_x, vchat_reply
326
  vchat_x = openai.ChatCompletion.create(
327
- engine="mvp-gpt-35-turbo", messages=messages_vchat,
328
  temperature=ui_temp_chat.value,
329
  max_tokens=ui_max_tokens_chat.value,
330
  top_p=ui_top_p_chat.value,
@@ -338,8 +403,9 @@ with gr.Blocks() as page:
338
  messages_vchat.append({"role": "assistant", "content": vchat_reply})
339
  history[-1][1] = vchat_reply
340
  return history
341
-
342
- ui_res_radio_chat.change(select_response_chat, ui_res_radio_chat, ui_response_chat)
 
343
  ui_temp_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
344
  ui_max_tokens_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
345
  ui_top_p_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
 
1
+ import time
2
  import openai
3
  import gradio as gr
4
  import requests
5
  from pydub import AudioSegment as am
6
  from xml.etree import ElementTree
7
 
8
+ aoai_url, aoai_key, stts_key, stts_region = "", "", "", ""
 
 
9
 
10
  openai.api_type = "azure"
11
+ # openai.api_base = "https://mvp-azureopenai.openai.azure.com/"
12
  openai.api_version = "2023-03-15-preview"
13
  # openai.api_key = aoai_key
14
 
15
+ prompts = ""
16
+ model_gpt = ""
17
  messages_gpt = []
18
+ model_chat = ""
19
  messages_chat = [
20
  {"role": "system", "content": "You are an AI assistant that helps people find information."},
21
  ]
 
22
  response_walle = []
23
+ model_vchat = ""
24
  messages_vchat = [
25
+ {"role": "system", "content": "You are an AI assistant that helps people find information and just respond with SSML."},
26
  ]
27
 
28
+ def get_aoai_set(get_aoai_url, get_aoai_key, get_aoai_API):
29
+ if get_aoai_url:
30
+ openai.api_base = get_aoai_url
31
+ if get_aoai_key:
32
+ openai.api_key = get_aoai_key
33
+ if get_aoai_API:
34
+ openai.api_version = get_aoai_API
35
+ return gr.update(value=get_aoai_url), gr.update(value=get_aoai_key), gr.update(value=get_aoai_API)
36
+
37
+ def get_stts_set(get_stts_key, get_stts_region):
38
+ global stts_key, stts_region
39
+ if get_stts_key:
40
+ stts_key = get_stts_key
41
+ if get_stts_region:
42
+ stts_region = get_stts_region
43
+ return gr.update(value=get_stts_key), gr.update(value=get_stts_region)
44
 
45
  with gr.Blocks() as page:
 
 
 
 
 
 
 
46
  with gr.Tabs():
47
+ with gr.TabItem("Settings"):
48
+ gr.HTML("""
49
+ <p>Please read guide and set parameters before switch to other tab.</p> <br>Your Azure OpenAI Key and other Azure Cognitive Service Keys
50
+ will not be saved or viewed by anyone. <br> You need input parameters to use Azure OpenAI Service. Please input these settings and hit
51
+ 'Enter' key. <br><br>
52
+ You can find these parameters in Azure Portal. Select Azure OpenAI resource or Cognitive Service resource like Speech, and then select
53
+ 'Keys and Endpoint' from left panel. <br> For Azure OpenAI service, you need to provide the resource URL and key for REST API. You also
54
+ need to set the API version or just use the default value. The Azure OpenAI model which be deployment need to set in each tab. Because
55
+ you may need run different models at same time. Don't forget hit 'Enter' by every input. <br> For Azure Cognitive services, you need to
56
+ provide Key for REST API, and also need to provide service region, for example like 'westus'. App will create the endpoint URL by itself.
57
+ <br><br>
58
+ Thank you.
59
+ """)
60
+ with gr.Row():
61
+ with gr.Column(scale=0.6):
62
+ ui_aoai_url = gr.Textbox(placeholder="Like https://your-url-base.openai.azure.com , etc.",
63
+ label="- Azure OpenAI service API endpoint:", lines=1).style(container=False)
64
+ with gr.Column(scale=0.2):
65
+ ui_aoai_key = gr.Textbox(placeholder="Please enter your Azure OpenAI API key here.",
66
+ label="- Azure OpenAI service API Key: ", lines=1, type='password').style(container=False)
67
+ with gr.Column(scale=0.2):
68
+ ui_aoai_api = gr.Textbox(value="2023-03-15-preview", label="路 Azure OpenAI service API version: ",
69
+ lines=1, interactive=True).style(container=False)
70
+ gr.HTML("Azure Cognitive Speech Service parameters to use VoiceChat. ")
71
+ with gr.Row():
72
+ with gr.Column(scale=0.6):
73
+ ui_stts_key = gr.Textbox(placeholder="Please enter your speech service API key if you want to try VoiceChat. " +
74
+ "Please input these settings and hit 'Enter' key.",
75
+ label="路 Azure Cognitive Speech service API Key: ", interactive=True, type='password').style(container=False)
76
+ with gr.Column(scale=0.4):
77
+ ui_stts_loc = gr.Textbox(placeholder="Please enter your speech service region.",
78
+ label="路 Azure Cognitive Speech service region: ", interactive=True).style(container=False)
79
+ ui_aoai_url.submit(get_aoai_set, [ui_aoai_url, ui_aoai_key, ui_aoai_api], [ui_aoai_url, ui_aoai_key, ui_aoai_api])
80
+ ui_aoai_key.submit(get_aoai_set, [ui_aoai_url, ui_aoai_key, ui_aoai_api], [ui_aoai_url, ui_aoai_key, ui_aoai_api])
81
+ ui_aoai_api.submit(get_aoai_set, [ui_aoai_url, ui_aoai_key, ui_aoai_api], [ui_aoai_url, ui_aoai_key, ui_aoai_api])
82
+ ui_stts_key.submit(get_stts_set, [ui_stts_key, ui_stts_loc], [ui_stts_key, ui_stts_loc])
83
+ ui_stts_loc.submit(get_stts_set, [ui_stts_key, ui_stts_loc], [ui_stts_key, ui_stts_loc])
84
+
85
+ with gr.TabItem("GPT-3.5 Playground"):
86
  ui_chatbot_gpt = gr.Chatbot(label="GPT Playground:")
87
  with gr.Row():
88
  with gr.Column(scale=0.9):
89
  ui_prompt_gpt = gr.Textbox(placeholder="Please enter your prompt here.", show_label=False).style(container=False)
90
  with gr.Column(scale=0.1, min_width=100):
91
  ui_clear_gpt = gr.Button("Clear Input", )
92
+ with gr.Accordion("Expand to config parameters:", open=True):
93
+ ui_memo_gpt = gr.HTML("GPT-3.5 playground use Completion(). So you just need to provide model name as engine parameter.")
94
+ ui_model_gpt = gr.Textbox(placeholder="Azure OpenAI GPT model deployment name. ",
95
+ label="- Azure OpenAI deployment name:", lines=1).style(container=False)
96
  with gr.Row():
97
  ui_temp_gpt = gr.Slider(0.1, 1.0, 0.9, step=0.1, label="Temperature", interactive=True)
98
  ui_max_tokens_gpt = gr.Slider(100, 4000, 1000, step=100, label="Max Tokens", interactive=True)
 
107
  ui_top_p_gpt.value = slider_3
108
  print("Log - Updated GPT parameters: Temperature=", ui_temp_gpt.value,
109
  " Max Tokens=", ui_max_tokens_gpt.value, " Top_P=", ui_top_p_gpt.value)
110
+
111
+ def get_engine_gpt(get_aoai_model):
112
+ global model_gpt
113
+ model_gpt = get_aoai_model
114
+ return gr.update(value=get_aoai_model)
115
 
116
  def select_response_gpt(radio):
117
  if radio == "Response from OpenAI Model":
 
127
 
128
  def bot_gpt(history):
129
  global gpt_x
130
+ print(ui_model_gpt.value)
131
  gpt_x = openai.Completion.create(
132
+ engine=model_gpt,
133
  prompt=prompts,
134
  temperature=0.6,
135
  max_tokens=1000,
 
144
  history[-1][1] = gpt_reply
145
  return history
146
 
147
+ ui_model_gpt.submit(get_engine_gpt, ui_model_gpt , ui_model_gpt)
148
  ui_temp_gpt.change(get_parameters_gpt, [ui_temp_gpt, ui_max_tokens_gpt, ui_top_p_gpt])
149
  ui_max_tokens_gpt.change(get_parameters_gpt, [ui_temp_gpt, ui_max_tokens_gpt, ui_top_p_gpt])
150
  ui_top_p_gpt.change(get_parameters_gpt, [ui_temp_gpt, ui_max_tokens_gpt, ui_top_p_gpt])
 
151
  ui_prompt_gpt.submit(user_gpt, [ui_prompt_gpt, ui_chatbot_gpt], [ui_prompt_gpt, ui_chatbot_gpt], queue=False).then(
152
  bot_gpt, ui_chatbot_gpt, ui_chatbot_gpt
153
  )
154
  ui_clear_gpt.click(lambda: None, None, ui_chatbot_gpt, queue=False)
155
  ui_res_radio_gpt.change(select_response_gpt, ui_res_radio_gpt, ui_response_gpt)
156
 
157
+ with gr.TabItem("ChatGPT on GPT-4"):
158
  ui_chatbot_chat = gr.Chatbot(label="ChatGPT:")
159
  with gr.Row():
160
  with gr.Column(scale=0.9):
 
162
  with gr.Column(scale=0.1, min_width=100):
163
  ui_clear_chat = gr.Button("Clear Chat")
164
  with gr.Blocks():
165
+ with gr.Accordion("Expand to config parameters:", open=True):
166
+ gr.HTML("ChatGPT use ChatCompletion(). Here is the default system prompt, you can change it to your own prompt.")
167
+ ui_prompt_sys = gr.Textbox(value="You are an AI assistant that helps people find information.",
168
+ label="Here is the default system prompt, you can change it to your own prompt.",
169
+ interactive=True).style(container=False)
170
+ ui_model_chat = gr.Textbox(placeholder="Azure OpenAI model deployment name. ",
171
+ label="路 Azure OpenAI GPT-3.5/4 deployment name:", lines=1).style(container=False)
172
  with gr.Row():
173
  ui_temp_chat = gr.Slider(0.1, 1.0, 0.7, step=0.1, label="Temperature", interactive=True)
174
  ui_max_tokens_chat = gr.Slider(100, 8000, 2000, step=100, label="Max Tokens", interactive=True)
 
183
  ui_top_p_chat.value = slider_3
184
  print("Log - Updated chatGPT parameters: Temperature=", ui_temp_chat.value,
185
  " Max Tokens=", ui_max_tokens_chat.value, " Top_P=", ui_top_p_chat.value)
186
+
187
+ def get_engine_chat(get_aoai_model):
188
+ global model_chat
189
+ model_chat = get_aoai_model
190
+ return gr.update(value=get_aoai_model)
191
+
192
  def select_response_chat(radio):
193
  if radio == "Response from OpenAI Model":
194
  return gr.update(value=chat_x)
 
202
  def bot_chat(history):
203
  global chat_x
204
  chat_x = openai.ChatCompletion.create(
205
+ engine=model_chat, messages=messages_chat,
206
  temperature=ui_temp_chat.value,
207
  max_tokens=ui_max_tokens_chat.value,
208
  top_p=ui_top_p_chat.value,
 
226
  {"role": "system", "content": sysmsg},
227
  ]
228
 
229
+ ui_model_chat.submit(get_engine_chat, ui_model_chat, ui_model_chat)
230
  ui_res_radio_chat.change(select_response_chat, ui_res_radio_chat, ui_response_chat)
231
  ui_temp_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
232
  ui_max_tokens_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
 
238
  ui_clear_chat.click(lambda: None, None, ui_chatbot_chat, queue=False).then(reset_sys, ui_prompt_sys)
239
 
240
 
241
+ with gr.TabItem("DALL路E 2 Painting"):
242
+ ui_prompt_walle = gr.Textbox(placeholder="Please enter your prompt here to generate image.",
243
+ show_label=False).style(container=False)
244
  ui_image_walle = gr.Image()
245
  with gr.Accordion("Select radio button to see detail:", open=False):
246
  ui_response_walle = gr.TextArea(show_label=False, interactive=False).style(container=False)
 
275
 
276
  ui_prompt_walle.submit(get_image_walle, ui_prompt_walle, ui_image_walle, queue=False).then(get_response_walle, None, ui_response_walle)
277
 
278
+ with gr.TabItem("VoiceChat on GPT"):
279
  with gr.Row():
280
  with gr.Column():
281
+ with gr.Accordion("Expand to config parameters:", open=True):
282
+ ui_prompt_sys_vchat = gr.Textbox(value="You are an AI assistant that helps people find information and just respond with SSML.",
283
+ label="Here is the default system prompt, you can change it to your own prompt.",
284
+ interactive=True).style(container=False)
285
+ ui_model_vchat = gr.Textbox(placeholder="- Azure OpenAI model deployment name. ",
286
+ label="- Azure OpenAI GPT-3.5/4 deployment name:", lines=1).style(container=False)
287
 
288
  ui_voice_inc_vchat = gr.Audio(source="microphone", type="filepath")
289
  ui_voice_out_vchat = gr.Audio(value=None, type="filepath", interactive=False).style(container=False)
 
305
  print("Log - Updated chatGPT parameters: Temperature=", ui_temp_vchat.value,
306
  " Max Tokens=", ui_max_tokens_vchat.value, " Top_P=", ui_top_p_vchat.value)
307
 
308
+ def get_engine_vchat(get_aoai_model):
309
+ global model_vchat
310
+ model_vchat = get_aoai_model
311
+ return gr.update(value=get_aoai_model)
312
+
313
  def select_response_vchat(radio):
314
  if radio == "Response from OpenAI Model":
315
  return gr.update(value=vchat_x)
316
  else:
317
  return gr.update(value=messages_vchat)
318
 
 
 
319
  def speech_to_text(voice_message):
320
  # Downsample input voice to 16kHz
321
  voice_wav = am.from_file(voice_message, format='wav')
322
  voice_wav = voice_wav.set_frame_rate(16000)
323
  voice_wav.export(voice_message, format='wav')
324
  # STT
325
+ service_region = stts_region
 
326
 
327
  base_url = "https://"+service_region+".stt.speech.microsoft.com/"
328
  path = 'speech/recognition/conversation/cognitiveservices/v1'
 
332
  'format': 'detailed'
333
  }
334
  headers = {
335
+ 'Ocp-Apim-Subscription-Key': stts_key,
336
  'Content-Type': 'audio/wav; codecs=audio/pcm; samplerate=16000',
337
  'Accept': 'application/json;text/xml'
338
  }
 
350
  return sst_text
351
 
352
  def text_to_speech():
353
+ service_region = stts_region
354
+ # test
355
+ print(stts_key)
356
  base_url = "https://"+service_region+".tts.speech.microsoft.com/"
357
  path = 'cognitiveservices/v1'
358
  constructed_url = base_url + path
359
  headers = {
360
+ 'Ocp-Apim-Subscription-Key': stts_key,
361
  'Content-Type': 'application/ssml+xml',
362
  'X-Microsoft-OutputFormat': 'riff-24khz-16bit-mono-pcm',
363
  'User-Agent': 'Voice ChatGPT'
 
389
  def bot_vchat(history):
390
  global vchat_x, vchat_reply
391
  vchat_x = openai.ChatCompletion.create(
392
+ engine=model_vchat, messages=messages_vchat,
393
  temperature=ui_temp_chat.value,
394
  max_tokens=ui_max_tokens_chat.value,
395
  top_p=ui_top_p_chat.value,
 
403
  messages_vchat.append({"role": "assistant", "content": vchat_reply})
404
  history[-1][1] = vchat_reply
405
  return history
406
+
407
+ ui_model_vchat.submit(get_engine_vchat, ui_model_vchat, ui_model_vchat)
408
+ ui_res_radio_vchat.change(select_response_vchat, ui_res_radio_vchat, ui_response_vchat)
409
  ui_temp_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
410
  ui_max_tokens_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
411
  ui_top_p_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])