youngtsai commited on
Commit
46f5603
·
1 Parent(s): 1b1ea59
Files changed (2) hide show
  1. app.py +51 -0
  2. requuirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+
4
+ # 设置OpenAI API密钥
5
+ openai.api_key = '你的OpenAI API密钥'
6
+
7
+ def generate_topic_sentences(sys_content, user_content, model="gpt-4-1106-preview", max_tokens=4000):
8
+ """
9
+ 根据系统提示和用户输入的情境及主题,调用OpenAI API生成相关的主题句。
10
+ """
11
+ messages = [
12
+ {"role": "system", "content": sys_content},
13
+ {"role": "user", "content": user_content}
14
+ ]
15
+
16
+ request_payload = {
17
+ "model": model,
18
+ "messages": messages,
19
+ "max_tokens": max_tokens,
20
+ }
21
+
22
+ response = openai.ChatCompletion.create(**request_payload)
23
+ content = response.choices[0].message.content.strip()
24
+
25
+ return content
26
+
27
+ with gr.Blocks() as demo:
28
+ with gr.Row():
29
+ with gr.Column():
30
+ scenario_input = gr.Textbox(label="Scenario")
31
+ topic_input = gr.Textbox(label="Topic")
32
+ eng_level_input = gr.Radio(["beginner", "intermediate", "advanced"], label="English Level")
33
+ 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.")
34
+ 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 {}."
35
+ generate_topics_button = gr.Button("Generate Topic Sentences")
36
+
37
+ with gr.Column():
38
+ output = gr.Textbox(label="Generated Topic Sentences")
39
+
40
+ # 当用户点击生成按钮时执行的操作
41
+ def on_generate_button_click(scenario, topic, eng_level, sys_content):
42
+ user_content = user_content_template.format(scenario, eng_level)
43
+ return generate_topic_sentences(sys_content, user_content)
44
+
45
+ generate_topics_button.click(
46
+ fn=on_generate_button_click,
47
+ inputs=[scenario_input, topic_input, eng_level_input, sys_content_input],
48
+ outputs=output
49
+ )
50
+
51
+ demo.launch()
requuirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ openai>=1.0.0
3
+