File size: 18,741 Bytes
a028cde
 
 
 
 
 
 
e1106c7
 
 
a028cde
 
e1106c7
a028cde
e1106c7
a028cde
 
 
 
 
 
 
 
 
 
 
e1106c7
 
 
 
 
a028cde
e1106c7
 
 
 
 
 
 
a028cde
e1106c7
a028cde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1106c7
a028cde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1106c7
a028cde
 
 
 
 
 
 
 
e1106c7
 
a028cde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1106c7
 
 
 
 
a028cde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1106c7
a028cde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1106c7
a028cde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1106c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import os, time, wave
import openai
import gradio as gr
import requests
from pydub import AudioSegment as am
from xml.etree import ElementTree

aoai_url = "https://mvp-azureopenai.openai.azure.com/"
aoai_key = ""
stts_key = ""

openai.api_type = "azure"
openai.api_base = "https://mvp-azureopenai.openai.azure.com/"
openai.api_version = "2023-03-15-preview"
# openai.api_key = aoai_key

messages_gpt = []
messages_chat = [
    {"role": "system", "content": "You are an AI assistant that helps people find information."},
]
prompts = ""
response_walle = []
messages_vchat = [
    {"role": "system", "content": "You are an AI assistant that helps people find information and just response with SSML."},
]

def get_aoai_key(txt_inpout):
    if txt_inpout:
        openai.api_key = txt_inpout
    return gr.update(value=txt_inpout)

with gr.Blocks() as page:
    with gr.Row():
        with gr.Column(scale=0.4):
            gr.Markdown("Your Key will not be saved or viewed by anyone.")
        with gr.Column(scale=0.6):
            ui_aoai_key = gr.Textbox(placeholder="Please enter your Azure OpenAI API key here.", lines=1, show_label=False, type='password').style(container=False)
            ui_aoai_key.submit(get_aoai_key, ui_aoai_key, ui_aoai_key)
        
    with gr.Tabs():
        with gr.TabItem("GPT Playground"):
            ui_chatbot_gpt = gr.Chatbot(label="GPT Playground:")
            with gr.Row():
                with gr.Column(scale=0.9):
                    ui_prompt_gpt = gr.Textbox(placeholder="Please enter your prompt here.", show_label=False).style(container=False)
                with gr.Column(scale=0.1, min_width=100):
                    ui_clear_gpt = gr.Button("Clear Input", )
            with gr.Accordion("Expand to config parameters:", open=False):
                gr.Markdown("Look at me...")
                with gr.Row():
                    ui_temp_gpt = gr.Slider(0.1, 1.0, 0.9, step=0.1, label="Temperature", interactive=True)
                    ui_max_tokens_gpt = gr.Slider(100, 4000, 1000, step=100, label="Max Tokens", interactive=True)
                    ui_top_p_gpt = gr.Slider(0.1, 1.0, 0.5, step=0.1, label="Top P", interactive=True)
            with gr.Accordion("Select radio button to see detail:", open=False):
                ui_res_radio_gpt = gr.Radio(["Response from OpenAI Model", "Prompt messages history"], label="Show OpenAI response:", interactive=True)
                ui_response_gpt = gr.TextArea(show_label=False, interactive=False).style(container=False)

            def get_parameters_gpt(slider_1, slider_2, slider_3):
                ui_temp_gpt.value = slider_1
                ui_max_tokens_gpt.value = slider_2
                ui_top_p_gpt.value = slider_3
                print("Log - Updated GPT parameters: Temperature=", ui_temp_gpt.value,
                    " Max Tokens=", ui_max_tokens_gpt.value, " Top_P=", ui_top_p_gpt.value)
            
            def select_response_gpt(radio):
                if radio == "Response from OpenAI Model":
                    return gr.update(value=gpt_x)
                else:
                    return gr.update(value=messages_gpt)

            def user_gpt(user_message, history):
                global prompts 
                prompts = user_message
                messages_gpt.append(prompts)
                return "", history + [[user_message, None]]
            
            def bot_gpt(history):
                global gpt_x
                gpt_x = openai.Completion.create(
                    engine="mvp-text-davinci-003",
                    prompt=prompts,
                    temperature=0.6,
                    max_tokens=1000,
                    top_p=1,
                    frequency_penalty=0,
                    presence_penalty=0,
                    best_of=1,
                    stop=None
                )
                gpt_reply = gpt_x.choices[0].text
                messages_gpt.append(gpt_reply)
                history[-1][1] = gpt_reply
                return history
            
        ui_temp_gpt.change(get_parameters_gpt, [ui_temp_gpt, ui_max_tokens_gpt, ui_top_p_gpt])
        ui_max_tokens_gpt.change(get_parameters_gpt, [ui_temp_gpt, ui_max_tokens_gpt, ui_top_p_gpt])
        ui_top_p_gpt.change(get_parameters_gpt, [ui_temp_gpt, ui_max_tokens_gpt, ui_top_p_gpt])

        ui_prompt_gpt.submit(user_gpt, [ui_prompt_gpt, ui_chatbot_gpt], [ui_prompt_gpt, ui_chatbot_gpt], queue=False).then(
            bot_gpt, ui_chatbot_gpt, ui_chatbot_gpt
                )
        ui_clear_gpt.click(lambda: None, None, ui_chatbot_gpt, queue=False)
        ui_res_radio_gpt.change(select_response_gpt, ui_res_radio_gpt, ui_response_gpt)

        with gr.TabItem("ChatGPT"):
            ui_chatbot_chat = gr.Chatbot(label="ChatGPT:")
            with gr.Row():
                with gr.Column(scale=0.9):
                    ui_prompt_chat = gr.Textbox(placeholder="Please enter your prompt here.", show_label=False).style(container=False)
                with gr.Column(scale=0.1, min_width=100):
                    ui_clear_chat = gr.Button("Clear Chat")
            with gr.Blocks():
                with gr.Accordion("Expand to config parameters:", open=False):
                    gr.Markdown("Here is the default system prompt, you can change it to your own prompt.")
                    ui_prompt_sys = gr.Textbox(value="You are an AI assistant that helps people find information.", show_label=False, interactive=True).style(container=False)
                    with gr.Row():
                        ui_temp_chat = gr.Slider(0.1, 1.0, 0.7, step=0.1, label="Temperature", interactive=True)
                        ui_max_tokens_chat = gr.Slider(100, 8000, 2000, step=100, label="Max Tokens", interactive=True)
                        ui_top_p_chat = gr.Slider(0.05, 1.0, 0.9, step=0.1, label="Top P", interactive=True)
                with gr.Accordion("Select radio button to see detail:", open=False):
                    ui_res_radio_chat = gr.Radio(["Response from OpenAI Model", "Prompt messages history"], label="Show OpenAI response:", interactive=True)
                    ui_response_chat = gr.TextArea(show_label=False, interactive=False).style(container=False)

            def get_parameters_chat(slider_1, slider_2, slider_3):
                ui_temp_chat.value = slider_1
                ui_max_tokens_chat.value = slider_2
                ui_top_p_chat.value = slider_3
                print("Log - Updated chatGPT parameters: Temperature=", ui_temp_chat.value,
                    " Max Tokens=", ui_max_tokens_chat.value, " Top_P=", ui_top_p_chat.value)
            
            def select_response_chat(radio):
                if radio == "Response from OpenAI Model":
                    return gr.update(value=chat_x)
                else:
                    return gr.update(value=messages_chat)
                                                
            def user_chat(user_message, history):
                messages_chat.append({"role": "user", "content": user_message})
                return "", history + [[user_message, None]]

            def bot_chat(history):
                global chat_x
                chat_x = openai.ChatCompletion.create(
                    engine="mvp-gpt-35-turbo", messages=messages_chat,
                    temperature=ui_temp_chat.value,
                    max_tokens=ui_max_tokens_chat.value,
                    top_p=ui_top_p_chat.value,
                    frequency_penalty=0,
                    presence_penalty=0,
                    stop=None
                )
                
                ui_response_chat.value= chat_x
                print(ui_response_chat.value)

                chat_reply = chat_x.choices[0].message.content
                messages_chat.append({"role": "assistant", "content": chat_reply})
                
                history[-1][1] = chat_reply
                return history
                    
            def reset_sys(sysmsg):
                global messages_chat
                messages_chat = [
                    {"role": "system", "content": sysmsg},
                ]
        
        ui_res_radio_chat.change(select_response_chat, ui_res_radio_chat, ui_response_chat)
        ui_temp_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
        ui_max_tokens_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
        ui_top_p_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
        ui_prompt_sys.submit(reset_sys, ui_prompt_sys)
        ui_prompt_chat.submit(user_chat, [ui_prompt_chat, ui_chatbot_chat], [ui_prompt_chat, ui_chatbot_chat], queue=False).then(
                bot_chat, ui_chatbot_chat, ui_chatbot_chat
                )
        ui_clear_chat.click(lambda: None, None, ui_chatbot_chat, queue=False).then(reset_sys, ui_prompt_sys)


        with gr.TabItem("DALL路E 2"):
            ui_prompt_walle = gr.Textbox(placeholder="Please enter your prompt here to generate image.", show_label=False).style(container=False)
            ui_image_walle = gr.Image()
            with gr.Accordion("Select radio button to see detail:", open=False):            
                ui_response_walle = gr.TextArea(show_label=False, interactive=False).style(container=False)

            def get_image_walle(prompt_walle):
                global response_walle
                walle_api_version = '2022-08-03-preview'
                url = "{}dalle/text-to-image?api-version={}".format(openai.api_base, walle_api_version)
                headers= { "api-key": openai.api_key, "Content-Type": "application/json" }
                body = {
                    "caption": prompt_walle,
                    "resolution": "1024x1024"
                }
                submission = requests.post(url, headers=headers, json=body)
                response_walle.append(submission.json())
                print("Log - WALL路E status: {}".format(submission.json()))
                operation_location = submission.headers['Operation-Location']
                retry_after = submission.headers['Retry-after']
                status = ""
                while (status != "Succeeded"):
                    time.sleep(int(retry_after))
                    response = requests.get(operation_location, headers=headers)
                    response_walle.append(response.json())
                    print("Log - WALL路E status: {}".format(response.json()))
                    status = response.json()['status']
                image_url_walle = response.json()['result']['contentUrl']
                return gr.update(value=image_url_walle)

            def get_response_walle():
                global response_walle
                return gr.update(value=response_walle)

        ui_prompt_walle.submit(get_image_walle, ui_prompt_walle, ui_image_walle, queue=False).then(get_response_walle, None, ui_response_walle)

        with gr.TabItem("VoiceChat"):
            with gr.Row():
                with gr.Column():
                    with gr.Accordion("Expand to config parameters:", open=False):
                        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)
                        ui_stts_key = gr.Textbox(placeholder="Input your Azure Speech service API key", show_label=False, interactive=True).style(container=False)
                        ui_stts_key.submit(lambda x: None, ui_stts_key)
                        stts_key = ui_aoai_key.value
                        print(stts_key)

                    ui_voice_inc_vchat = gr.Audio(source="microphone", type="filepath")
                    ui_voice_out_vchat = gr.Audio(value=None, type="filepath", interactive=False).style(container=False)
                    with gr.Accordion("Expand to config parameters:", open=False):
                        with gr.Row():
                            ui_temp_vchat = gr.Slider(0.1, 1.0, 0.7, step=0.1, label="Temperature", interactive=True)
                            ui_max_tokens_vchat = gr.Slider(100, 8000, 800, step=100, label="Max Tokens", interactive=True)
                            ui_top_p_vchat = gr.Slider(0.05, 1.0, 0.9, step=0.1, label="Top P", interactive=True)
                with gr.Column():
                    ui_chatbot_vchat = gr.Chatbot(label="Voice to ChatGPT:")
            with gr.Accordion("Select radio button to see detail:", open=False):
                ui_res_radio_vchat = gr.Radio(["Response from OpenAI Model", "Prompt messages history"], label="Show OpenAI response:", interactive=True)
                ui_response_vchat = gr.TextArea(show_label=False, interactive=False).style(container=False)

            def get_parameters_vchat(slider_1, slider_2, slider_3):
                ui_temp_vchat.value = slider_1
                ui_max_tokens_vchat.value = slider_2
                ui_top_p_vchat.value = slider_3
                print("Log - Updated chatGPT parameters: Temperature=", ui_temp_vchat.value,
                    " Max Tokens=", ui_max_tokens_vchat.value, " Top_P=", ui_top_p_vchat.value)
            
            def select_response_vchat(radio):
                if radio == "Response from OpenAI Model":
                    return gr.update(value=vchat_x)
                else:
                    return gr.update(value=messages_vchat)

            

            def speech_to_text(voice_message):
                # Downsample input voice to 16kHz
                voice_wav = am.from_file(voice_message, format='wav')
                voice_wav = voice_wav.set_frame_rate(16000)
                voice_wav.export(voice_message, format='wav')
                # STT
                OASK = stts_key
                service_region = "westus"

                base_url = "https://"+service_region+".stt.speech.microsoft.com/"
                path = 'speech/recognition/conversation/cognitiveservices/v1'
                constructed_url = base_url + path
                params = {
                        'language': 'zh-CN',
                        'format': 'detailed'
                        }
                headers = {
                        'Ocp-Apim-Subscription-Key': OASK_Speech,
                        'Content-Type': 'audio/wav; codecs=audio/pcm; samplerate=16000',
                        'Accept': 'application/json;text/xml'
                        }
                body = open(voice_message,'rb').read()
                response = requests.post(constructed_url, params=params, headers=headers, data=body)
                if response.status_code == 200:
                    rs = response.json()
                    if rs != '': 
                        print(rs)
                else:
                    print("\nLog - Status code: " + str(response.status_code) + "\nSomething went wrong. Check your subscription key and headers.\n")
                    print("Reason: " + str(response.reason) + "\n")
                
                sst_text = rs['DisplayText']
                return sst_text  
            
            def text_to_speech():
                OASK_Speech = stts_key
                service_region = "westus"

                base_url = "https://"+service_region+".tts.speech.microsoft.com/"
                path = 'cognitiveservices/v1'
                constructed_url = base_url + path
                headers = {
                        'Ocp-Apim-Subscription-Key': OASK_Speech,
                        'Content-Type': 'application/ssml+xml',
                        'X-Microsoft-OutputFormat': 'riff-24khz-16bit-mono-pcm',
                        'User-Agent': 'Voice ChatGPT'
                        }
                xml_body = ElementTree.Element('speak', version='1.0')
                xml_body.set('{http://www.w3.org/XML/1998/namespace}lang', 'zh-cn')
                voice = ElementTree.SubElement(xml_body, 'voice')
                voice.set('{http://www.w3.org/XML/1998/namespace}lang', 'zh-cn')
                voice.set('name', 'zh-CN-XiaoxiaoNeural') 
                voice.text = vchat_reply
                body = ElementTree.tostring(xml_body)
                response = requests.post(constructed_url, headers=headers, data=body)
                if response.status_code == 200:
                    with open('chatgpt.wav', 'wb') as audio:
                        audio.write(response.content)
                        print("\nStatus code: " + str(response.status_code) + "\nYour TTS is ready for playback.\n")
                else:
                    print("\nStatus code: " + str(response.status_code) + "\nSomething went wrong. Check your subscription key and headers.\n")
                    print("Reason: " + str(response.reason) + "\n")

                tts_file = "chatgpt.wav"
                return gr.update(value=tts_file, interactive=True)

            def user_vchat(user_voice_message, history):
                user_message = speech_to_text(user_voice_message)
                messages_vchat.append({"role": "user", "content": user_message})
                return history + [[user_message, None]]

            def bot_vchat(history):
                global vchat_x, vchat_reply
                vchat_x = openai.ChatCompletion.create(
                    engine="mvp-gpt-35-turbo", messages=messages_vchat,
                    temperature=ui_temp_chat.value,
                    max_tokens=ui_max_tokens_chat.value,
                    top_p=ui_top_p_chat.value,
                    frequency_penalty=0,
                    presence_penalty=0,
                    stop=None
                )
                ui_response_vchat.value= vchat_x
                print(ui_response_vchat.value)
                vchat_reply = vchat_x.choices[0].message.content
                messages_vchat.append({"role": "assistant", "content": vchat_reply})
                history[-1][1] = vchat_reply
                return history
            
        ui_res_radio_chat.change(select_response_chat, ui_res_radio_chat, ui_response_chat)
        ui_temp_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
        ui_max_tokens_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
        ui_top_p_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])    
        ui_voice_inc_vchat.change(user_vchat, [ui_voice_inc_vchat, ui_chatbot_vchat], ui_chatbot_vchat, queue=False).then(
                bot_vchat, ui_chatbot_vchat, ui_chatbot_vchat, queue=False).then(text_to_speech, None, ui_voice_out_vchat)


page.launch(share=False)