File size: 1,825 Bytes
a37b99e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import openai

EXAMPLES = [
    "Write a professional email to the customers of a telco company offering 100 minutes free if they buy 1000 minutes",
    "Write a formal email to the customers of a bank offering a new home insurance",
    "Write an informal email to the customers of a bank, offering a new car insurance, using emojis in the subject",
]

openai.api_key = st.secrets["openai-api-key"]


def generate_email(prompt: str, max_tokens: int = 256) -> str:
    """
    Returns a generated an email using GPT3 with a certain prompt and starting sentence
    """

    completions = openai.Completion.create(
        model="text-davinci-003",
        prompt=prompt,
        temperature=0.7,
        max_tokens=max_tokens,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
    )
    message = completions.choices[0].text
    return message


def example_selected():
    st.session_state['prompt'] = st.session_state['selected_example']


st.title("Email Generator")
st.text("by Marc Puig")

st.selectbox(
    label="Examples",
    options=EXAMPLES,
    on_change=example_selected,
    key="selected_example"
)

prompt_input = st.text_area(
    label="Describe the type of email you want to be written.",
    key="prompt"
)

max_tokens_input = st.slider(
    label="How many characters do you want your email to be? ",
    help="A typical email is usually 100-500 characters",
    min_value=64,
    max_value=750,
    value=200
)

with st.form(key="form"):
    submit_button = st.form_submit_button(label='Generate email', disabled=len(st.session_state["prompt"]) == 0)

    if submit_button:
        with st.spinner("Generating email..."):
            output = generate_email(prompt_input, max_tokens=max_tokens_input)
        st.markdown("----")
        st.markdown(output)