File size: 8,225 Bytes
15b9da3
46f5603
c775910
15b9da3
 
78f5c78
029fc0c
f4a1918
78f5c78
d947472
46f5603
 
 
c71a877
 
 
 
 
46f5603
 
 
 
 
 
 
 
 
 
 
029fc0c
46f5603
 
 
 
8a473de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11b1dbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
877f289
 
11b1dbb
 
 
 
 
877f289
11b1dbb
 
 
c9e97ad
 
11b1dbb
 
c71a877
c9e97ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46f5603
 
 
c71a877
d947472
0e5dd94
8c415e0
c71a877
46f5603
0e5dd94
c71a877
d947472
0e5dd94
 
 
 
 
 
46f5603
c71a877
d947472
8a473de
0e5dd94
 
 
 
 
 
8a473de
c71a877
11b1dbb
 
 
 
 
 
 
 
 
 
c9e97ad
11b1dbb
 
 
 
 
 
 
 
 
d947472
46f5603
 
dad95ce
8a473de
dad95ce
c71a877
46f5603
 
d947472
c71a877
 
 
 
 
 
 
 
 
46f5603
 
8a473de
 
 
 
 
 
 
 
 
 
 
 
 
 
11b1dbb
 
 
 
 
 
 
 
 
 
 
 
dad95ce
11b1dbb
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import os
import gradio as gr
from openai import OpenAI


OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
OPEN_AI_CLIENT = OpenAI(api_key=OPEN_AI_KEY)


def generate_topics(model, max_tokens, sys_content, scenario, eng_level, user_generate_topics_prompt):
    """
    根据系统提示和用户输入的情境及主题,调用OpenAI API生成相关的主题句。
    """
    user_content = f"""
        scenario is {scenario}
        english level is {eng_level}
        {user_generate_topics_prompt}
    """
    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

def generate_points(model, max_tokens, sys_content, scenario, eng_level, topic, user_generate_points_prompt):
    """
    根据系统提示和用户输入的情境、主题,调用OpenAI API生成相关的主题句。
    """
    user_content = f"""
        scenario is {scenario}
        english level is {eng_level}
        topic is {topic}
        {user_generate_points_prompt}
    """
    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

def generate_topic_sentences(model, max_tokens, sys_content, scenario, eng_level, topic, points, user_generate_topic_sentences_prompt):
    """
    根据系统提示和用户输入的情境及要点,调用OpenAI API生成相关的主题句及其合理性解释。
    """
    user_content = f"""
        scenario is {scenario}
        english level is {eng_level}
        topic is {topic}
        points is {points}
        {user_generate_topic_sentences_prompt}
    """
    messages = [
        {"role": "system", "content": sys_content},
        {"role": "user", "content": user_content}
    ]
    response_format = { "type": "json_object" }


    request_payload = {
        "model": model,
        "messages": messages,
        "max_tokens": max_tokens,
        "response_format": response_format
    }

    response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
    json_content = response.choices[0].message.content
    content = parse_and_display_topic_sentences(json_content)

    return content

def parse_and_display_topic_sentences(json_data):
    """
    解析JSON格式的主题句数据,并转换成易于阅读的格式。
    """
    # 将JSON字符串解析成Python字典
    data = json.loads(json_data)
    
    # 初始化一个空字符串用于存放最终的格式化文本
    formatted_text = ""
    
    # 遍历每个主题句及其评价
    for key, value in data.items():
        topic_sentence = value[0]['topic-sentence']
        appropriate = "適當" if value[0]['appropriate'] == "Y" else "不適當"
        reason = value[0]['reason']
        
        # 将每个主题句的信息添加到格式化文本中
        formatted_text += f"主题句 {int(key)+1}: {topic_sentence}\n"
        formatted_text += f"是否適當: {appropriate}\n"
        formatted_text += f"原因: {reason}\n\n"
    
    return formatted_text

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            # basic inputs
            gr.Markdown("## 1. Basic Inputs")
            model = gr.Radio(["gpt-4-1106-preview", "gpt-3.5-turbo"], label="Model", value="gpt-4-1106-preview")
            max_tokens = gr.Slider(minimum=50, maximum=4000, value=1000, label="Max Tokens")
            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.")
            scenario_input = gr.Textbox(label="Scenario")
            eng_level_input = gr.Radio(["beginner", "intermediate", "advanced"], label="English Level", value="beginner")
            
            gr.Markdown("## 2. Generate Topic")
            default_generate_topics_prompt = """
                Give me 10 topics relevant to Scenario, 
                for a paragraph. Just the topics, no explanation, use simple English language. 
                Make sure the vocabulary you use is at english level.
            """
            user_generate_topics_prompt = gr.Textbox(label="Topics Prompt", value=default_generate_topics_prompt)
            generate_topics_button = gr.Button("Generate Topic Sentences")

            gr.Markdown("## 3. Generate Points")
            topic_input = gr.Textbox(label="Topic")
            default_generate_points_prompt = """
                Please provide main points to develop in a paragraph about topic in the context of scenario, 
                use simple English language and make sure the vocabulary you use is at eng_level.
                No more explanation either no developing these points into a simple paragraph.
            """  
            user_generate_points_prompt = gr.Textbox(label="Points Prompt", value=default_generate_points_prompt)
            generate_points_button = gr.Button("Generate Points")

            gr.Markdown("## 4. Generate Topic Sentences")
            points_input = gr.Textbox(label="Points")
            default_generate_topic_sentences_prompt = """
                Please provide one appropriate topic sentence that aptly introduces the subject for the given scenario and topic. 
                Additionally, provide two topic sentences that, while related to the topic, 
                would be considered inappropriate or less effective for the specified context. 
                Those sentences must include the three main points:". 
                Use English language and each sentence should not be too long.
                For each sentence, explain the reason in Traditional Chinese, Taiwan, 繁體中文 zh-TW. 
                Make sure the vocabulary you use is at level.

                Only return the result in JSON format starting as: 
                {{ 
                    "0": [ {{ "topic-sentence": "#","appropriate": "Y/N", "reason": "#中文解釋" }} ], 
                    "1": [ {{ "topic-sentence": "#","appropriate": "Y/N", "reason": "#中文解釋" }} ],
                    "2": [ {{ "topic-sentence": "#","appropriate": "Y/N", "reason": "#中文解釋" }} ] 
                }}            
            """
            user_generate_topic_sentences_prompt = gr.Textbox(label="Topic Sentences Prompt", value=default_generate_topic_sentences_prompt)
            generate_topic_sentences_button = gr.Button("Generate Topic Sentences")

        
        with gr.Column():
            topic_output = gr.Textbox(label="Generated Topic")
            points_output = gr.Textbox(label="Generated Points")
            topic_sentence_output = gr.Textbox(label="Generated Topic Sentences")

    
    generate_topics_button.click(
        fn=generate_topics,
        inputs=[
            model, 
            max_tokens, 
            sys_content_input, 
            scenario_input, 
            eng_level_input,
            user_generate_topics_prompt
        ],
        outputs=topic_output
    )

    generate_points_button.click(
        fn=generate_points,
        inputs=[
            model, 
            max_tokens, 
            sys_content_input, 
            scenario_input, 
            eng_level_input,
            topic_input,
            user_generate_points_prompt
        ],
        outputs=points_output
    )

    generate_topic_sentences_button.click(
        fn=generate_topic_sentences,
        inputs=[
            model, 
            max_tokens, 
            sys_content_input, 
            scenario_input, 
            eng_level_input,
            topic_input,
            points_input,
            user_generate_topic_sentences_prompt
        ],
        outputs=topic_sentence_output
    )

demo.launch()