File size: 2,629 Bytes
15b9da3
46f5603
c775910
15b9da3
 
78f5c78
029fc0c
f4a1918
78f5c78
c71a877
46f5603
 
 
c71a877
 
 
 
 
46f5603
 
 
 
 
 
 
 
 
 
 
029fc0c
46f5603
 
 
 
c71a877
46f5603
 
 
c71a877
 
 
 
 
46f5603
 
c71a877
 
f1630aa
46f5603
c71a877
 
 
 
 
46f5603
 
c71a877
 
 
46f5603
 
c71a877
 
 
 
 
 
 
 
 
 
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
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_topic_sentences(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


with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            # basic inputs
            gr.Makdown("## Basic Inputs")
            model = gr.Radio(["gpt-4-1106-preview", "gpt-3.5-turbo"], label="Model")
            max_tokens = gr.Slider(minimum=50, maximum=4000, default=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")
            
            gr.Makdown("## Generate Topic Sentences")
            user_generate_topics_prompt = gr.Textbox(label="User Prompt", value="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.")
            generate_topics_button = gr.Button("Generate Topic Sentences")

            # gr.Makdown("## Generate Points")
            # topic_input = gr.Textbox(label="Topic")
            # generate_points_button = gr.Button("Generate Points")

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

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

demo.launch()