mpuig's picture
Initial commit
a37b99e
raw
history blame
1.83 kB
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)