Spaces:
Sleeping
Sleeping
import gradio as gr | |
import openai | |
# 设置OpenAI API密钥 | |
openai.api_key = '你的OpenAI API密钥' | |
def generate_topic_sentences(sys_content, user_content, model="gpt-4-1106-preview", max_tokens=4000): | |
""" | |
根据系统提示和用户输入的情境及主题,调用OpenAI API生成相关的主题句。 | |
""" | |
messages = [ | |
{"role": "system", "content": sys_content}, | |
{"role": "user", "content": user_content} | |
] | |
request_payload = { | |
"model": model, | |
"messages": messages, | |
"max_tokens": max_tokens, | |
} | |
response = openai.ChatCompletion.create(**request_payload) | |
content = response.choices[0].message.content.strip() | |
return content | |
with gr.Blocks() as demo: | |
with gr.Row(): | |
with gr.Column(): | |
scenario_input = gr.Textbox(label="Scenario") | |
topic_input = gr.Textbox(label="Topic") | |
eng_level_input = gr.Radio(["beginner", "intermediate", "advanced"], label="English Level") | |
sys_content_input = gr.Textbox(label="System Prompt", value="You are an English teacher who is practicing with me to improve my English writing skill.") | |
user_content_template = "Give me 10 topics relevant to {}, for a paragraph. Just the topics, no explanation, use simple English language. Make sure the vocabulary you use is at {}." | |
generate_topics_button = gr.Button("Generate Topic Sentences") | |
with gr.Column(): | |
output = gr.Textbox(label="Generated Topic Sentences") | |
# 当用户点击生成按钮时执行的操作 | |
def on_generate_button_click(scenario, topic, eng_level, sys_content): | |
user_content = user_content_template.format(scenario, eng_level) | |
return generate_topic_sentences(sys_content, user_content) | |
generate_topics_button.click( | |
fn=on_generate_button_click, | |
inputs=[scenario_input, topic_input, eng_level_input, sys_content_input], | |
outputs=output | |
) | |
demo.launch() | |