Spaces:
Sleeping
Sleeping
File size: 2,071 Bytes
46f5603 c775910 46f5603 78f5c78 029fc0c f4a1918 78f5c78 46f5603 029fc0c 46f5603 |
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 |
import gradio as gr
from openai import OpenAI
# 设置OpenAI API密钥
OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
OPEN_AI_CLIENT = OpenAI(api_key=OPEN_AI_KEY)
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 = OPEN_AI_CLIENT.chat.completions.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()
|