Spaces:
Runtime error
Runtime error
Owen
commited on
Commit
·
9bc85e2
1
Parent(s):
59a5e01
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Set OpenAI API key from Streamlit Secrets
|
| 6 |
+
OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
|
| 7 |
+
openai.api_key = OPENAI_API_KEY
|
| 8 |
+
|
| 9 |
+
# Set maximum token length
|
| 10 |
+
MAX_TOKENS = 1024
|
| 11 |
+
|
| 12 |
+
# Set OpenAI model
|
| 13 |
+
MODEL = "openai-davinci-003"
|
| 14 |
+
|
| 15 |
+
# Set Streamlit page configuration
|
| 16 |
+
st.set_page_config(layout="centered")
|
| 17 |
+
|
| 18 |
+
# Set Streamlit title and description
|
| 19 |
+
st.title("Email Response Generator")
|
| 20 |
+
st.markdown("Generate a response to an email.")
|
| 21 |
+
|
| 22 |
+
# Define the email input form
|
| 23 |
+
st.subheader("Input Email")
|
| 24 |
+
email_text = st.text_area("Enter your email here", height=200)
|
| 25 |
+
|
| 26 |
+
# Define the response generation function
|
| 27 |
+
@st.cache(show_spinner=False)
|
| 28 |
+
def generate_response(email_text):
|
| 29 |
+
prompt = "You are sharing an email. You have very little time in your schedule but want to be polite in your response. You are expecting a response.\n\nEmail:\n" + email_text
|
| 30 |
+
response = openai.Completion.create(
|
| 31 |
+
engine=MODEL,
|
| 32 |
+
prompt=prompt,
|
| 33 |
+
max_tokens=MAX_TOKENS
|
| 34 |
+
)
|
| 35 |
+
return response.choices[0].text.strip()
|
| 36 |
+
|
| 37 |
+
# Define the submit button
|
| 38 |
+
if st.button("Generate Response"):
|
| 39 |
+
if email_text.strip() == "":
|
| 40 |
+
st.warning("Please enter your email.")
|
| 41 |
+
else:
|
| 42 |
+
with st.spinner("Generating response..."):
|
| 43 |
+
response_text = generate_response(email_text)
|
| 44 |
+
st.subheader("Generated Response")
|
| 45 |
+
st.markdown(response_text)
|