Download app.py from kkAIGC/SQLBot: direct link, hf CLI and curl.
- Browser
- Download file 3.61 kB
-
https://huggingface.co/spaces/kkAIGC/SQLBot/resolve/1d4d1933b076249ee96a69e4caa2ff8ff76ef82b/app.py
- Command line
-
hf download hf://spaces/kkAIGC/SQLBot@1d4d1933b076249ee96a69e4caa2ff8ff76ef82b/app.py
-
curl -L -o app.py https://huggingface.co/spaces/kkAIGC/SQLBot/resolve/1d4d1933b076249ee96a69e4caa2ff8ff76ef82b/app.py
3.61 kB
| import os | |
| import openai | |
| #获取api_key函数 | |
| def get_api_key(key): | |
| if key == '': | |
| openai.api_key = os.environ.get('OPENAI_API_KEY') | |
| openai.api_key = key | |
| #对话函数 | |
| def get_completion_from_messages( | |
| messages, | |
| model="gpt-3.5-turbo-16k", | |
| temperature=0, | |
| max_tokens=3000): | |
| response = openai.ChatCompletion.create( | |
| model=model, | |
| messages=messages, | |
| temperature=temperature, | |
| max_tokens=max_tokens, | |
| ) | |
| return response.choices[0].message["content"] | |
| #详细解释 | |
| def sql_explain(user_input, | |
| model="gpt-3.5-turbo-16k", | |
| temperature=0, | |
| max_tokens=3000): | |
| system_message = """ | |
| 详细解释用户提供的SQL语句: | |
| """ | |
| messages = [ | |
| {'role':'system', 'content': system_message}, | |
| {'role':'user', 'content': user_input}, | |
| ] | |
| response = get_completion_from_messages(messages, | |
| model, | |
| temperature, | |
| max_tokens) | |
| return response | |
| #自然语言解释 | |
| def sql_translate(user_input, | |
| model="gpt-3.5-turbo-16k", | |
| temperature=0, | |
| max_tokens=3000): | |
| system_message = """ | |
| 将下面的SQL语句翻译为自然语言: | |
| """ | |
| messages = [ | |
| {'role':'system', 'content': system_message}, | |
| {'role':'user', 'content': user_input}, | |
| ] | |
| response = get_completion_from_messages(messages, | |
| model, | |
| temperature, | |
| max_tokens) | |
| return response | |
| #block构建界面 | |
| import gradio as gr | |
| def function_select(input_text, | |
| model, | |
| temperature, | |
| max_token, | |
| flag = False): | |
| if flag: | |
| response = sql_explain(input_text,model,temperature,max_token) | |
| return response | |
| else: | |
| response = sql_translate(input_text,model,temperature,max_token) | |
| return response | |
| with gr.Blocks() as demo: | |
| gr.Markdown("本页面对SQL代码进行文字说明or翻译为自然语言") | |
| with gr.Row(): | |
| with gr.Column(scale=2): | |
| sql_text = gr.Textbox(lines=21,label="输入SQL代码") | |
| sql_button = gr.Button('确认输入') | |
| with gr.Column(scale=2): | |
| output_text = gr.Textbox(lines=24,label="输出", | |
| show_label=True,show_copy_button=True, | |
| ) | |
| with gr.Column(scale=1): | |
| apikey_text = gr.Textbox(type='password',label='请输入api_key:') | |
| model_select = gr.Dropdown( | |
| choices=["gpt-3.5-turbo-16k","gpt-3.5-turbo"], | |
| value='gpt-3.5-turbo', | |
| label='选择模型' | |
| ) | |
| temperature = gr.Slider(minimum=0, maximum=1,value=0,label='temperature') | |
| maxtoken = gr.Slider(minimum=0, maximum=3000, value=300,label='max_token') | |
| translate_select = gr.Checkbox(label='需要详细解释') | |
| apikey_text.change(fn=get_api_key,inputs= apikey_text) | |
| sql_button.click(fn=function_select, | |
| inputs=[sql_text, | |
| model_select, | |
| temperature, | |
| maxtoken, | |
| translate_select | |
| ], | |
| outputs=output_text) | |
| demo.launch() |