Spaces:
Build error
Build error
Initial commit
Browse files- .gitignore +1 -0
- .streamlit/secrets.toml.template +2 -0
- README.md +12 -1
- app.py +65 -0
- requirements.txt +2 -0
- secrets.toml.template +2 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
/.streamlit/secrets.toml
|
.streamlit/secrets.toml.template
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Everything in this section will be available as an environment variable
|
| 2 |
+
openai-api-key = "your-secret-here"
|
README.md
CHANGED
|
@@ -9,4 +9,15 @@ app_file: app.py
|
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# Generating Emails with GPT-3
|
| 13 |
+
|
| 14 |
+
Email generator using the OpenAI API and the language model GPT-3 davinci-3
|
| 15 |
+
|
| 16 |
+
## How to run this on your local computer:
|
| 17 |
+
* Sign up for an [OpenAI account](https://platform.openai.com/signup)
|
| 18 |
+
* Clone this repo
|
| 19 |
+
* Rename the file `.streamlite/secrets.toml.template` to `.streamlite/secrets.toml`
|
| 20 |
+
* Create an API key and copy the value into `.streamlite/secrets.toml`
|
| 21 |
+
* Install requirements by using `pip install -r requirements`
|
| 22 |
+
* Open a terminal and run the command `streamlit run app.py`
|
| 23 |
+
* Connect locally to http://localhost:8501/
|
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
+
|
| 4 |
+
EXAMPLES = [
|
| 5 |
+
"Write a professional email to the customers of a telco company offering 100 minutes free if they buy 1000 minutes",
|
| 6 |
+
"Write a formal email to the customers of a bank offering a new home insurance",
|
| 7 |
+
"Write an informal email to the customers of a bank, offering a new car insurance, using emojis in the subject",
|
| 8 |
+
]
|
| 9 |
+
|
| 10 |
+
openai.api_key = st.secrets["openai-api-key"]
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def generate_email(prompt: str, max_tokens: int = 256) -> str:
|
| 14 |
+
"""
|
| 15 |
+
Returns a generated an email using GPT3 with a certain prompt and starting sentence
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
completions = openai.Completion.create(
|
| 19 |
+
model="text-davinci-003",
|
| 20 |
+
prompt=prompt,
|
| 21 |
+
temperature=0.7,
|
| 22 |
+
max_tokens=max_tokens,
|
| 23 |
+
top_p=1,
|
| 24 |
+
frequency_penalty=0,
|
| 25 |
+
presence_penalty=0
|
| 26 |
+
)
|
| 27 |
+
message = completions.choices[0].text
|
| 28 |
+
return message
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def example_selected():
|
| 32 |
+
st.session_state['prompt'] = st.session_state['selected_example']
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
st.title("Email Generator")
|
| 36 |
+
st.text("by Marc Puig")
|
| 37 |
+
|
| 38 |
+
st.selectbox(
|
| 39 |
+
label="Examples",
|
| 40 |
+
options=EXAMPLES,
|
| 41 |
+
on_change=example_selected,
|
| 42 |
+
key="selected_example"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
prompt_input = st.text_area(
|
| 46 |
+
label="Describe the type of email you want to be written.",
|
| 47 |
+
key="prompt"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
max_tokens_input = st.slider(
|
| 51 |
+
label="How many characters do you want your email to be? ",
|
| 52 |
+
help="A typical email is usually 100-500 characters",
|
| 53 |
+
min_value=64,
|
| 54 |
+
max_value=750,
|
| 55 |
+
value=200
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
with st.form(key="form"):
|
| 59 |
+
submit_button = st.form_submit_button(label='Generate email', disabled=len(st.session_state["prompt"]) == 0)
|
| 60 |
+
|
| 61 |
+
if submit_button:
|
| 62 |
+
with st.spinner("Generating email..."):
|
| 63 |
+
output = generate_email(prompt_input, max_tokens=max_tokens_input)
|
| 64 |
+
st.markdown("----")
|
| 65 |
+
st.markdown(output)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai==0.26.4
|
| 2 |
+
streamlit==1.17.0
|
secrets.toml.template
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Everything in this section will be available as an environment variable
|
| 2 |
+
openai-api-key = "your-secret-here"
|