Spaces:
Sleeping
Sleeping
Commit
路
625a371
0
Parent(s):
Duplicate from haohoo/Azure-OpenAI-QuickDemo
Browse filesCo-authored-by: Hao Hu <[email protected]>
- .gitattributes +34 -0
- README.md +14 -0
- app.py +411 -0
- requirements.txt +3 -0
.gitattributes
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Azure OpenAI QuickDemo
|
| 3 |
+
emoji: 馃殌
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: gray
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.24.1
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
duplicated_from: haohoo/Azure-OpenAI-QuickDemo
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,411 @@
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
openai.api_type = "azure"
|
| 10 |
+
|
| 11 |
+
prompts = ""
|
| 12 |
+
model_gpt = ""
|
| 13 |
+
messages_gpt = []
|
| 14 |
+
model_chat = ""
|
| 15 |
+
messages_chat = [
|
| 16 |
+
{"role": "system", "content": "You are an AI assistant that helps people find information."},
|
| 17 |
+
]
|
| 18 |
+
response_walle = []
|
| 19 |
+
model_vchat = ""
|
| 20 |
+
messages_vchat = [
|
| 21 |
+
{"role": "system", "content": "You are an AI assistant that helps people find information and just respond with SSML."},
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
def get_aoai_set(get_aoai_url, get_aoai_key, get_aoai_API):
|
| 25 |
+
if get_aoai_url:
|
| 26 |
+
openai.api_base = get_aoai_url
|
| 27 |
+
if get_aoai_key:
|
| 28 |
+
openai.api_key = get_aoai_key
|
| 29 |
+
if get_aoai_API:
|
| 30 |
+
openai.api_version = get_aoai_API
|
| 31 |
+
return gr.update(value=get_aoai_url), gr.update(value=get_aoai_key), gr.update(value=get_aoai_API)
|
| 32 |
+
|
| 33 |
+
def get_stts_set(get_stts_key, get_stts_region):
|
| 34 |
+
global stts_key, stts_region
|
| 35 |
+
if get_stts_key:
|
| 36 |
+
stts_key = get_stts_key
|
| 37 |
+
if get_stts_region:
|
| 38 |
+
stts_region = get_stts_region
|
| 39 |
+
return gr.update(value=get_stts_key), gr.update(value=get_stts_region)
|
| 40 |
+
|
| 41 |
+
with gr.Blocks() as page:
|
| 42 |
+
with gr.Tabs():
|
| 43 |
+
with gr.TabItem("Settings"):
|
| 44 |
+
gr.HTML("""
|
| 45 |
+
<p>Please read and set parameters before switching to another tab.</p> <br>Your Azure OpenAI Key and other Azure Cognitive Service Keys
|
| 46 |
+
will not be saved or viewed by anyone. <br><br>
|
| 47 |
+
You can find these parameters in Azure Portal. Select Azure OpenAI resource or Cognitive Service resource like Speech, and then select
|
| 48 |
+
'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
|
| 49 |
+
need to set the API version or just use the default value. The Azure OpenAI model which is deployed needs to be set in each tab. Because
|
| 50 |
+
you may need to run different models at the same time. Don't forget to hit 'Enter' with every input. <br> For Azure Cognitive services,
|
| 51 |
+
you need to provide a Key for REST API, and also need to provide a service region, for example, 'westus'. The app will create the endpoint
|
| 52 |
+
URL by itself. <br><br>Thank you.<br><br><br> Azure OpenAI Service parameters for ChatGPT/GPT. Please input these settings and hit the
|
| 53 |
+
'Enter' key.
|
| 54 |
+
""")
|
| 55 |
+
with gr.Row():
|
| 56 |
+
with gr.Column(scale=0.6):
|
| 57 |
+
ui_aoai_url = gr.Textbox(placeholder="Like https://your-url-base.openai.azure.com , etc.",
|
| 58 |
+
label="- Azure OpenAI service API endpoint:", lines=1).style(container=False)
|
| 59 |
+
with gr.Column(scale=0.2):
|
| 60 |
+
ui_aoai_key = gr.Textbox(placeholder="Please enter your Azure OpenAI API key here.",
|
| 61 |
+
label="- Azure OpenAI service API Key: ", lines=1, type='password').style(container=False)
|
| 62 |
+
with gr.Column(scale=0.2):
|
| 63 |
+
ui_aoai_api = gr.Textbox(value="2023-03-15-preview", label="路 Azure OpenAI service API version: ",
|
| 64 |
+
lines=1, interactive=True).style(container=False)
|
| 65 |
+
gr.HTML("Azure Cognitive Speech Service parameters to use VoiceChat. ")
|
| 66 |
+
with gr.Row():
|
| 67 |
+
with gr.Column(scale=0.6):
|
| 68 |
+
ui_stts_key = gr.Textbox(placeholder="Please enter your speech service API key if you want to try VoiceChat. " +
|
| 69 |
+
"Please input these settings and hit 'Enter' key.",
|
| 70 |
+
label="- Azure Cognitive Speech service API Key: ", interactive=True, type='password').style(container=False)
|
| 71 |
+
with gr.Column(scale=0.4):
|
| 72 |
+
ui_stts_loc = gr.Textbox(placeholder="Please enter your speech service region.",
|
| 73 |
+
label="- Azure Cognitive Speech service region: ", interactive=True).style(container=False)
|
| 74 |
+
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])
|
| 75 |
+
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])
|
| 76 |
+
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])
|
| 77 |
+
ui_stts_key.submit(get_stts_set, [ui_stts_key, ui_stts_loc], [ui_stts_key, ui_stts_loc])
|
| 78 |
+
ui_stts_loc.submit(get_stts_set, [ui_stts_key, ui_stts_loc], [ui_stts_key, ui_stts_loc])
|
| 79 |
+
|
| 80 |
+
with gr.TabItem("GPT-3.5 Playground"):
|
| 81 |
+
ui_chatbot_gpt = gr.Chatbot(label="GPT Playground:")
|
| 82 |
+
with gr.Row():
|
| 83 |
+
with gr.Column(scale=0.9):
|
| 84 |
+
ui_prompt_gpt = gr.Textbox(placeholder="Please enter your prompt here.", show_label=False).style(container=False)
|
| 85 |
+
with gr.Column(scale=0.1, min_width=100):
|
| 86 |
+
ui_clear_gpt = gr.Button("Clear Input", )
|
| 87 |
+
with gr.Accordion("Expand to config parameters:", open=True):
|
| 88 |
+
ui_memo_gpt = gr.HTML("GPT-3.5 playground use Completion(). So you just need to provide model name as engine parameter.")
|
| 89 |
+
ui_model_gpt = gr.Textbox(placeholder="Azure OpenAI GPT model deployment name. ",
|
| 90 |
+
label="- Azure OpenAI deployment name:", lines=1).style(container=False)
|
| 91 |
+
with gr.Row():
|
| 92 |
+
ui_temp_gpt = gr.Slider(0.1, 1.0, 0.9, step=0.1, label="Temperature", interactive=True)
|
| 93 |
+
ui_max_tokens_gpt = gr.Slider(100, 4000, 1000, step=100, label="Max Tokens", interactive=True)
|
| 94 |
+
ui_top_p_gpt = gr.Slider(0.1, 1.0, 0.5, step=0.1, label="Top P", interactive=True)
|
| 95 |
+
with gr.Accordion("Select radio button to see detail:", open=False):
|
| 96 |
+
ui_res_radio_gpt = gr.Radio(["Response from OpenAI Model", "Prompt messages history"], label="Show OpenAI response:", interactive=True)
|
| 97 |
+
ui_response_gpt = gr.TextArea(show_label=False, interactive=False).style(container=False)
|
| 98 |
+
|
| 99 |
+
def get_parameters_gpt(slider_1, slider_2, slider_3):
|
| 100 |
+
ui_temp_gpt.value = slider_1
|
| 101 |
+
ui_max_tokens_gpt.value = slider_2
|
| 102 |
+
ui_top_p_gpt.value = slider_3
|
| 103 |
+
print("Log - Updated GPT parameters: Temperature=", ui_temp_gpt.value,
|
| 104 |
+
" Max Tokens=", ui_max_tokens_gpt.value, " Top_P=", ui_top_p_gpt.value)
|
| 105 |
+
|
| 106 |
+
def get_engine_gpt(get_aoai_model):
|
| 107 |
+
global model_gpt
|
| 108 |
+
model_gpt = get_aoai_model
|
| 109 |
+
return gr.update(value=get_aoai_model)
|
| 110 |
+
|
| 111 |
+
def select_response_gpt(radio):
|
| 112 |
+
if radio == "Response from OpenAI Model":
|
| 113 |
+
return gr.update(value=gpt_x)
|
| 114 |
+
else:
|
| 115 |
+
return gr.update(value=messages_gpt)
|
| 116 |
+
|
| 117 |
+
def user_gpt(user_message, history):
|
| 118 |
+
global prompts
|
| 119 |
+
prompts = user_message
|
| 120 |
+
messages_gpt.append(prompts)
|
| 121 |
+
return "", history + [[user_message, None]]
|
| 122 |
+
|
| 123 |
+
def bot_gpt(history):
|
| 124 |
+
global gpt_x
|
| 125 |
+
print(ui_model_gpt.value)
|
| 126 |
+
gpt_x = openai.Completion.create(
|
| 127 |
+
engine=model_gpt,
|
| 128 |
+
prompt=prompts,
|
| 129 |
+
temperature=0.6,
|
| 130 |
+
max_tokens=1000,
|
| 131 |
+
top_p=1,
|
| 132 |
+
frequency_penalty=0,
|
| 133 |
+
presence_penalty=0,
|
| 134 |
+
best_of=1,
|
| 135 |
+
stop=None
|
| 136 |
+
)
|
| 137 |
+
gpt_reply = gpt_x.choices[0].text
|
| 138 |
+
messages_gpt.append(gpt_reply)
|
| 139 |
+
history[-1][1] = gpt_reply
|
| 140 |
+
return history
|
| 141 |
+
|
| 142 |
+
ui_model_gpt.submit(get_engine_gpt, ui_model_gpt , ui_model_gpt)
|
| 143 |
+
ui_temp_gpt.change(get_parameters_gpt, [ui_temp_gpt, ui_max_tokens_gpt, ui_top_p_gpt])
|
| 144 |
+
ui_max_tokens_gpt.change(get_parameters_gpt, [ui_temp_gpt, ui_max_tokens_gpt, ui_top_p_gpt])
|
| 145 |
+
ui_top_p_gpt.change(get_parameters_gpt, [ui_temp_gpt, ui_max_tokens_gpt, ui_top_p_gpt])
|
| 146 |
+
ui_prompt_gpt.submit(user_gpt, [ui_prompt_gpt, ui_chatbot_gpt], [ui_prompt_gpt, ui_chatbot_gpt], queue=False).then(
|
| 147 |
+
bot_gpt, ui_chatbot_gpt, ui_chatbot_gpt
|
| 148 |
+
)
|
| 149 |
+
ui_clear_gpt.click(lambda: None, None, ui_chatbot_gpt, queue=False)
|
| 150 |
+
ui_res_radio_gpt.change(select_response_gpt, ui_res_radio_gpt, ui_response_gpt)
|
| 151 |
+
|
| 152 |
+
with gr.TabItem("ChatGPT on GPT-4"):
|
| 153 |
+
ui_chatbot_chat = gr.Chatbot(label="ChatGPT:")
|
| 154 |
+
with gr.Row():
|
| 155 |
+
with gr.Column(scale=0.9):
|
| 156 |
+
ui_prompt_chat = gr.Textbox(placeholder="Please enter your prompt here.", show_label=False).style(container=False)
|
| 157 |
+
with gr.Column(scale=0.1, min_width=100):
|
| 158 |
+
ui_clear_chat = gr.Button("Clear Chat")
|
| 159 |
+
with gr.Blocks():
|
| 160 |
+
with gr.Accordion("Expand to config parameters:", open=True):
|
| 161 |
+
gr.HTML("ChatGPT use ChatCompletion(). Here is the default system prompt, you can change it to your own prompt.")
|
| 162 |
+
ui_prompt_sys = gr.Textbox(value="You are an AI assistant that helps people find information.",
|
| 163 |
+
label="- Here is the default system prompt, you can change it to your own prompt.",
|
| 164 |
+
interactive=True).style(container=False)
|
| 165 |
+
ui_model_chat = gr.Textbox(placeholder="Azure OpenAI model deployment name. ",
|
| 166 |
+
label="- Azure OpenAI GPT-3.5/4 deployment name:", lines=1).style(container=False)
|
| 167 |
+
with gr.Row():
|
| 168 |
+
ui_temp_chat = gr.Slider(0.1, 1.0, 0.7, step=0.1, label="Temperature", interactive=True)
|
| 169 |
+
ui_max_tokens_chat = gr.Slider(100, 8000, 2000, step=100, label="Max Tokens", interactive=True)
|
| 170 |
+
ui_top_p_chat = gr.Slider(0.05, 1.0, 0.9, step=0.1, label="Top P", interactive=True)
|
| 171 |
+
with gr.Accordion("Select radio button to see detail:", open=False):
|
| 172 |
+
ui_res_radio_chat = gr.Radio(["Response from OpenAI Model", "Prompt messages history"], label="Show OpenAI response:", interactive=True)
|
| 173 |
+
ui_response_chat = gr.TextArea(show_label=False, interactive=False).style(container=False)
|
| 174 |
+
|
| 175 |
+
def get_parameters_chat(slider_1, slider_2, slider_3):
|
| 176 |
+
ui_temp_chat.value = slider_1
|
| 177 |
+
ui_max_tokens_chat.value = slider_2
|
| 178 |
+
ui_top_p_chat.value = slider_3
|
| 179 |
+
print("Log - Updated chatGPT parameters: Temperature=", ui_temp_chat.value,
|
| 180 |
+
" Max Tokens=", ui_max_tokens_chat.value, " Top_P=", ui_top_p_chat.value)
|
| 181 |
+
|
| 182 |
+
def get_engine_chat(get_aoai_model):
|
| 183 |
+
global model_chat
|
| 184 |
+
model_chat = get_aoai_model
|
| 185 |
+
return gr.update(value=get_aoai_model)
|
| 186 |
+
|
| 187 |
+
def select_response_chat(radio):
|
| 188 |
+
if radio == "Response from OpenAI Model":
|
| 189 |
+
return gr.update(value=chat_x)
|
| 190 |
+
else:
|
| 191 |
+
return gr.update(value=messages_chat)
|
| 192 |
+
|
| 193 |
+
def user_chat(user_message, history):
|
| 194 |
+
messages_chat.append({"role": "user", "content": user_message})
|
| 195 |
+
return "", history + [[user_message, None]]
|
| 196 |
+
|
| 197 |
+
def bot_chat(history):
|
| 198 |
+
global chat_x
|
| 199 |
+
chat_x = openai.ChatCompletion.create(
|
| 200 |
+
engine=model_chat, messages=messages_chat,
|
| 201 |
+
temperature=ui_temp_chat.value,
|
| 202 |
+
max_tokens=ui_max_tokens_chat.value,
|
| 203 |
+
top_p=ui_top_p_chat.value,
|
| 204 |
+
frequency_penalty=0,
|
| 205 |
+
presence_penalty=0,
|
| 206 |
+
stop=None
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
ui_response_chat.value= chat_x
|
| 210 |
+
print(ui_response_chat.value)
|
| 211 |
+
|
| 212 |
+
chat_reply = chat_x.choices[0].message.content
|
| 213 |
+
messages_chat.append({"role": "assistant", "content": chat_reply})
|
| 214 |
+
|
| 215 |
+
history[-1][1] = chat_reply
|
| 216 |
+
return history
|
| 217 |
+
|
| 218 |
+
def reset_sys(sysmsg):
|
| 219 |
+
global messages_chat
|
| 220 |
+
messages_chat = [
|
| 221 |
+
{"role": "system", "content": sysmsg},
|
| 222 |
+
]
|
| 223 |
+
|
| 224 |
+
ui_model_chat.submit(get_engine_chat, ui_model_chat, ui_model_chat)
|
| 225 |
+
ui_res_radio_chat.change(select_response_chat, ui_res_radio_chat, ui_response_chat)
|
| 226 |
+
ui_temp_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
|
| 227 |
+
ui_max_tokens_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
|
| 228 |
+
ui_top_p_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
|
| 229 |
+
ui_prompt_sys.submit(reset_sys, ui_prompt_sys)
|
| 230 |
+
ui_prompt_chat.submit(user_chat, [ui_prompt_chat, ui_chatbot_chat], [ui_prompt_chat, ui_chatbot_chat], queue=False).then(
|
| 231 |
+
bot_chat, ui_chatbot_chat, ui_chatbot_chat
|
| 232 |
+
)
|
| 233 |
+
ui_clear_chat.click(lambda: None, None, ui_chatbot_chat, queue=False).then(reset_sys, ui_prompt_sys)
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
with gr.TabItem("DALL路E 2 Painting"):
|
| 237 |
+
ui_prompt_walle = gr.Textbox(placeholder="Please enter your prompt here to generate image.",
|
| 238 |
+
show_label=False).style(container=False)
|
| 239 |
+
ui_image_walle = gr.Image()
|
| 240 |
+
with gr.Accordion("Select radio button to see detail:", open=False):
|
| 241 |
+
ui_response_walle = gr.TextArea(show_label=False, interactive=False).style(container=False)
|
| 242 |
+
|
| 243 |
+
def get_image_walle(prompt_walle):
|
| 244 |
+
global response_walle
|
| 245 |
+
walle_api_version = '2022-08-03-preview'
|
| 246 |
+
url = "{}dalle/text-to-image?api-version={}".format(openai.api_base, walle_api_version)
|
| 247 |
+
headers= { "api-key": openai.api_key, "Content-Type": "application/json" }
|
| 248 |
+
body = {
|
| 249 |
+
"caption": prompt_walle,
|
| 250 |
+
"resolution": "1024x1024"
|
| 251 |
+
}
|
| 252 |
+
submission = requests.post(url, headers=headers, json=body)
|
| 253 |
+
response_walle.append(submission.json())
|
| 254 |
+
print("Log - WALL路E status: {}".format(submission.json()))
|
| 255 |
+
operation_location = submission.headers['Operation-Location']
|
| 256 |
+
retry_after = submission.headers['Retry-after']
|
| 257 |
+
status = ""
|
| 258 |
+
while (status != "Succeeded"):
|
| 259 |
+
time.sleep(int(retry_after))
|
| 260 |
+
response = requests.get(operation_location, headers=headers)
|
| 261 |
+
response_walle.append(response.json())
|
| 262 |
+
print("Log - WALL路E status: {}".format(response.json()))
|
| 263 |
+
status = response.json()['status']
|
| 264 |
+
image_url_walle = response.json()['result']['contentUrl']
|
| 265 |
+
return gr.update(value=image_url_walle)
|
| 266 |
+
|
| 267 |
+
def get_response_walle():
|
| 268 |
+
global response_walle
|
| 269 |
+
return gr.update(value=response_walle)
|
| 270 |
+
|
| 271 |
+
ui_prompt_walle.submit(get_image_walle, ui_prompt_walle, ui_image_walle, queue=False).then(get_response_walle, None, ui_response_walle)
|
| 272 |
+
|
| 273 |
+
with gr.TabItem("VoiceChat on GPT"):
|
| 274 |
+
with gr.Row():
|
| 275 |
+
with gr.Column():
|
| 276 |
+
with gr.Accordion("Expand to config parameters:", open=True):
|
| 277 |
+
ui_prompt_sys_vchat = gr.Textbox(value="You are an AI assistant that helps people find information and just respond with SSML.",
|
| 278 |
+
label="- Here is the default system prompt, you can change it to your own prompt.",
|
| 279 |
+
interactive=True).style(container=False)
|
| 280 |
+
ui_model_vchat = gr.Textbox(placeholder="- Azure OpenAI model deployment name. ",
|
| 281 |
+
label="- Azure OpenAI GPT-3.5/4 deployment name:", lines=1).style(container=False)
|
| 282 |
+
|
| 283 |
+
ui_voice_inc_vchat = gr.Audio(source="microphone", type="filepath")
|
| 284 |
+
ui_voice_out_vchat = gr.Audio(value=None, type="filepath", interactive=False).style(container=False)
|
| 285 |
+
with gr.Accordion("Expand to config parameters:", open=False):
|
| 286 |
+
with gr.Row():
|
| 287 |
+
ui_temp_vchat = gr.Slider(0.1, 1.0, 0.7, step=0.1, label="Temperature", interactive=True)
|
| 288 |
+
ui_max_tokens_vchat = gr.Slider(100, 8000, 800, step=100, label="Max Tokens", interactive=True)
|
| 289 |
+
ui_top_p_vchat = gr.Slider(0.05, 1.0, 0.9, step=0.1, label="Top P", interactive=True)
|
| 290 |
+
with gr.Column():
|
| 291 |
+
ui_chatbot_vchat = gr.Chatbot(label="Voice to ChatGPT:")
|
| 292 |
+
with gr.Accordion("Select radio button to see detail:", open=False):
|
| 293 |
+
ui_res_radio_vchat = gr.Radio(["Response from OpenAI Model", "Prompt messages history"], label="Show OpenAI response:", interactive=True)
|
| 294 |
+
ui_response_vchat = gr.TextArea(show_label=False, interactive=False).style(container=False)
|
| 295 |
+
|
| 296 |
+
def get_parameters_vchat(slider_1, slider_2, slider_3):
|
| 297 |
+
ui_temp_vchat.value = slider_1
|
| 298 |
+
ui_max_tokens_vchat.value = slider_2
|
| 299 |
+
ui_top_p_vchat.value = slider_3
|
| 300 |
+
print("Log - Updated chatGPT parameters: Temperature=", ui_temp_vchat.value,
|
| 301 |
+
" Max Tokens=", ui_max_tokens_vchat.value, " Top_P=", ui_top_p_vchat.value)
|
| 302 |
+
|
| 303 |
+
def get_engine_vchat(get_aoai_model):
|
| 304 |
+
global model_vchat
|
| 305 |
+
model_vchat = get_aoai_model
|
| 306 |
+
return gr.update(value=get_aoai_model)
|
| 307 |
+
|
| 308 |
+
def select_response_vchat(radio):
|
| 309 |
+
if radio == "Response from OpenAI Model":
|
| 310 |
+
return gr.update(value=vchat_x)
|
| 311 |
+
else:
|
| 312 |
+
return gr.update(value=messages_vchat)
|
| 313 |
+
|
| 314 |
+
def speech_to_text(voice_message):
|
| 315 |
+
# Downsample input voice to 16kHz
|
| 316 |
+
voice_wav = am.from_file(voice_message, format='wav')
|
| 317 |
+
voice_wav = voice_wav.set_frame_rate(16000)
|
| 318 |
+
voice_wav.export(voice_message, format='wav')
|
| 319 |
+
# STT
|
| 320 |
+
service_region = stts_region
|
| 321 |
+
|
| 322 |
+
base_url = "https://"+service_region+".stt.speech.microsoft.com/"
|
| 323 |
+
path = 'speech/recognition/conversation/cognitiveservices/v1'
|
| 324 |
+
constructed_url = base_url + path
|
| 325 |
+
params = {
|
| 326 |
+
'language': 'zh-CN',
|
| 327 |
+
'format': 'detailed'
|
| 328 |
+
}
|
| 329 |
+
headers = {
|
| 330 |
+
'Ocp-Apim-Subscription-Key': stts_key,
|
| 331 |
+
'Content-Type': 'audio/wav; codecs=audio/pcm; samplerate=16000',
|
| 332 |
+
'Accept': 'application/json;text/xml'
|
| 333 |
+
}
|
| 334 |
+
body = open(voice_message,'rb').read()
|
| 335 |
+
response = requests.post(constructed_url, params=params, headers=headers, data=body)
|
| 336 |
+
if response.status_code == 200:
|
| 337 |
+
rs = response.json()
|
| 338 |
+
if rs != '':
|
| 339 |
+
print(rs)
|
| 340 |
+
else:
|
| 341 |
+
print("\nLog - Status code: " + str(response.status_code) + "\nSomething went wrong. Check your subscription key and headers.\n")
|
| 342 |
+
print("Reason: " + str(response.reason) + "\n")
|
| 343 |
+
|
| 344 |
+
sst_text = rs['DisplayText']
|
| 345 |
+
return sst_text
|
| 346 |
+
|
| 347 |
+
def text_to_speech():
|
| 348 |
+
service_region = stts_region
|
| 349 |
+
# test
|
| 350 |
+
print(stts_key)
|
| 351 |
+
base_url = "https://"+service_region+".tts.speech.microsoft.com/"
|
| 352 |
+
path = 'cognitiveservices/v1'
|
| 353 |
+
constructed_url = base_url + path
|
| 354 |
+
headers = {
|
| 355 |
+
'Ocp-Apim-Subscription-Key': stts_key,
|
| 356 |
+
'Content-Type': 'application/ssml+xml',
|
| 357 |
+
'X-Microsoft-OutputFormat': 'riff-24khz-16bit-mono-pcm',
|
| 358 |
+
'User-Agent': 'Voice ChatGPT'
|
| 359 |
+
}
|
| 360 |
+
xml_body = ElementTree.Element('speak', version='1.0')
|
| 361 |
+
xml_body.set('{http://www.w3.org/XML/1998/namespace}lang', 'zh-cn')
|
| 362 |
+
voice = ElementTree.SubElement(xml_body, 'voice')
|
| 363 |
+
voice.set('{http://www.w3.org/XML/1998/namespace}lang', 'zh-cn')
|
| 364 |
+
voice.set('name', 'zh-CN-XiaoxiaoNeural')
|
| 365 |
+
voice.text = vchat_reply
|
| 366 |
+
body = ElementTree.tostring(xml_body)
|
| 367 |
+
response = requests.post(constructed_url, headers=headers, data=body)
|
| 368 |
+
if response.status_code == 200:
|
| 369 |
+
with open('chatgpt.wav', 'wb') as audio:
|
| 370 |
+
audio.write(response.content)
|
| 371 |
+
print("\nStatus code: " + str(response.status_code) + "\nYour TTS is ready for playback.\n")
|
| 372 |
+
else:
|
| 373 |
+
print("\nStatus code: " + str(response.status_code) + "\nSomething went wrong. Check your subscription key and headers.\n")
|
| 374 |
+
print("Reason: " + str(response.reason) + "\n")
|
| 375 |
+
|
| 376 |
+
tts_file = "chatgpt.wav"
|
| 377 |
+
return gr.update(value=tts_file, interactive=True)
|
| 378 |
+
|
| 379 |
+
def user_vchat(user_voice_message, history):
|
| 380 |
+
user_message = speech_to_text(user_voice_message)
|
| 381 |
+
messages_vchat.append({"role": "user", "content": user_message})
|
| 382 |
+
return history + [[user_message, None]]
|
| 383 |
+
|
| 384 |
+
def bot_vchat(history):
|
| 385 |
+
global vchat_x, vchat_reply
|
| 386 |
+
vchat_x = openai.ChatCompletion.create(
|
| 387 |
+
engine=model_vchat, messages=messages_vchat,
|
| 388 |
+
temperature=ui_temp_chat.value,
|
| 389 |
+
max_tokens=ui_max_tokens_chat.value,
|
| 390 |
+
top_p=ui_top_p_chat.value,
|
| 391 |
+
frequency_penalty=0,
|
| 392 |
+
presence_penalty=0,
|
| 393 |
+
stop=None
|
| 394 |
+
)
|
| 395 |
+
ui_response_vchat.value= vchat_x
|
| 396 |
+
print(ui_response_vchat.value)
|
| 397 |
+
vchat_reply = vchat_x.choices[0].message.content
|
| 398 |
+
messages_vchat.append({"role": "assistant", "content": vchat_reply})
|
| 399 |
+
history[-1][1] = vchat_reply
|
| 400 |
+
return history
|
| 401 |
+
|
| 402 |
+
ui_model_vchat.submit(get_engine_vchat, ui_model_vchat, ui_model_vchat)
|
| 403 |
+
ui_res_radio_vchat.change(select_response_vchat, ui_res_radio_vchat, ui_response_vchat)
|
| 404 |
+
ui_temp_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
|
| 405 |
+
ui_max_tokens_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
|
| 406 |
+
ui_top_p_chat.change(get_parameters_chat, [ui_temp_chat, ui_max_tokens_chat, ui_top_p_chat])
|
| 407 |
+
ui_voice_inc_vchat.change(user_vchat, [ui_voice_inc_vchat, ui_chatbot_vchat], ui_chatbot_vchat, queue=False).then(
|
| 408 |
+
bot_vchat, ui_chatbot_vchat, ui_chatbot_vchat, queue=False).then(text_to_speech, None, ui_voice_out_vchat)
|
| 409 |
+
|
| 410 |
+
|
| 411 |
+
page.launch(share=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai==0.27.4
|
| 2 |
+
gradio==3.24.1
|
| 3 |
+
requests==2.28.2
|