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|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def generate_email_content(api_key, prompt, tone, model="gpt-3.5-turbo"):
|
| 7 |
+
"""Generate email response, subject, and summary using OpenAI"""
|
| 8 |
+
if not api_key:
|
| 9 |
+
return {
|
| 10 |
+
"error": "Please enter your OpenAI API key in the sidebar.",
|
| 11 |
+
"response": None
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
client = OpenAI(api_key=api_key)
|
| 16 |
+
|
| 17 |
+
# Generate email response
|
| 18 |
+
response_messages = [
|
| 19 |
+
{"role": "system", "content": f"You are a professional email assistant. Generate a {tone} tone response."},
|
| 20 |
+
{"role": "user", "content": prompt}
|
| 21 |
+
]
|
| 22 |
+
response = client.chat.completions.create(
|
| 23 |
+
model=model,
|
| 24 |
+
messages=response_messages,
|
| 25 |
+
temperature=0.7,
|
| 26 |
+
)
|
| 27 |
+
email_response = response.choices[0].message.content
|
| 28 |
+
|
| 29 |
+
# Generate subject line
|
| 30 |
+
subject_messages = [
|
| 31 |
+
{"role": "system", "content": "Generate a concise and appropriate subject line for this email."},
|
| 32 |
+
{"role": "user", "content": f"Email content:\n{email_response}"}
|
| 33 |
+
]
|
| 34 |
+
subject = client.chat.completions.create(
|
| 35 |
+
model=model,
|
| 36 |
+
messages=subject_messages,
|
| 37 |
+
temperature=0.7,
|
| 38 |
+
)
|
| 39 |
+
subject_line = subject.choices[0].message.content
|
| 40 |
+
|
| 41 |
+
# Generate thread summary
|
| 42 |
+
summary_messages = [
|
| 43 |
+
{"role": "system", "content": "Provide a concise summary of the email thread."},
|
| 44 |
+
{"role": "user", "content": f"Original thread:\n{prompt}\n\nResponse:\n{email_response}"}
|
| 45 |
+
]
|
| 46 |
+
summary = client.chat.completions.create(
|
| 47 |
+
model=model,
|
| 48 |
+
messages=summary_messages,
|
| 49 |
+
temperature=0.7,
|
| 50 |
+
)
|
| 51 |
+
thread_summary = summary.choices[0].message.content
|
| 52 |
+
|
| 53 |
+
return {
|
| 54 |
+
"error": None,
|
| 55 |
+
"response": email_response,
|
| 56 |
+
"subject": subject_line,
|
| 57 |
+
"summary": thread_summary
|
| 58 |
+
}
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return {
|
| 61 |
+
"error": str(e),
|
| 62 |
+
"response": None
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def main():
|
| 67 |
+
st.set_page_config(page_title="Smart Email Assistant", layout="wide")
|
| 68 |
+
st.markdown("<h2>@GenAILearniverse Project 15: Smart Email Assistant</h2>", unsafe_allow_html=True)
|
| 69 |
+
st.markdown("Generate Professional Email response with different tones")
|
| 70 |
+
|
| 71 |
+
# Initialize session state for API key
|
| 72 |
+
if 'OPENAI_API_KEY' not in st.session_state:
|
| 73 |
+
st.session_state.OPENAI_API_KEY = None
|
| 74 |
+
|
| 75 |
+
# Sidebar
|
| 76 |
+
with st.sidebar:
|
| 77 |
+
st.subheader("Settings")
|
| 78 |
+
api_key = st.text_input(
|
| 79 |
+
"Enter OpenAI API Key",
|
| 80 |
+
type="password",
|
| 81 |
+
help="Get your API key from https://platform.openai.com/account/api-keys"
|
| 82 |
+
)
|
| 83 |
+
if api_key:
|
| 84 |
+
st.session_state.OPENAI_API_KEY = api_key
|
| 85 |
+
st.success("API key set successfully!")
|
| 86 |
+
|
| 87 |
+
st.markdown("---")
|
| 88 |
+
st.markdown("### Features")
|
| 89 |
+
st.markdown("""
|
| 90 |
+
β’ Auto-generate responses
|
| 91 |
+
β’ Multiple tone options
|
| 92 |
+
β’ Subject suggestions
|
| 93 |
+
β’ Thread summarization
|
| 94 |
+
""")
|
| 95 |
+
# Main content area
|
| 96 |
+
col1, col2 = st.columns([1, 1])
|
| 97 |
+
|
| 98 |
+
with col1:
|
| 99 |
+
st.subheader("π Input")
|
| 100 |
+
|
| 101 |
+
# Email thread input
|
| 102 |
+
email_thread = st.text_area(
|
| 103 |
+
"Enter the email thread (most recent at top)",
|
| 104 |
+
height=200,
|
| 105 |
+
placeholder="""Example email:
|
| 106 |
+
|
| 107 |
+
Hi team,
|
| 108 |
+
Can we schedule a meeting to discuss the quarterly report? I've noticed some interesting trends that I'd like to explore further.
|
| 109 |
+
|
| 110 |
+
Best regards,
|
| 111 |
+
John"""
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# Tone selection
|
| 115 |
+
tone_options = ["Professional", "Casual", "Friendly", "Formal"]
|
| 116 |
+
selected_tone = st.selectbox("Select tone", tone_options)
|
| 117 |
+
|
| 118 |
+
# Context input
|
| 119 |
+
additional_context = st.text_area(
|
| 120 |
+
"Additional context or specific points to address (optional)",
|
| 121 |
+
height=100,
|
| 122 |
+
placeholder="Example: Need to highlight the positive growth trend in Q3..."
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# Generate button
|
| 126 |
+
if st.button("Generate Response", type="primary"):
|
| 127 |
+
if not email_thread:
|
| 128 |
+
st.error("Please enter an email thread.")
|
| 129 |
+
return
|
| 130 |
+
|
| 131 |
+
if not st.session_state.OPENAI_API_KEY:
|
| 132 |
+
st.error("Please enter your OpenAI API key in the sidebar.")
|
| 133 |
+
return
|
| 134 |
+
|
| 135 |
+
with st.spinner("Generating response..."):
|
| 136 |
+
# Prepare prompt
|
| 137 |
+
prompt = f"Email Thread:\n{email_thread}\n\nAdditional Context:\n{additional_context}\n\nGenerate a {selected_tone.lower()} tone response."
|
| 138 |
+
|
| 139 |
+
# Generate content
|
| 140 |
+
result = generate_email_content(st.session_state.OPENAI_API_KEY, prompt, selected_tone)
|
| 141 |
+
|
| 142 |
+
if result["error"]:
|
| 143 |
+
st.error(result["error"])
|
| 144 |
+
else:
|
| 145 |
+
st.session_state.current_response = result
|
| 146 |
+
st.success("Response generated successfully!")
|
| 147 |
+
with col2:
|
| 148 |
+
st.subheader("β¨ Generated Content")
|
| 149 |
+
|
| 150 |
+
if 'current_response' in st.session_state:
|
| 151 |
+
content = st.session_state.current_response
|
| 152 |
+
|
| 153 |
+
st.markdown("**π Subject Line:**")
|
| 154 |
+
st.code(content["subject"], language=None)
|
| 155 |
+
|
| 156 |
+
st.markdown("**π Email Response:**")
|
| 157 |
+
st.code(content["response"], language=None)
|
| 158 |
+
|
| 159 |
+
st.markdown("**π Thread Summary:**")
|
| 160 |
+
st.code(content["summary"], language=None)
|
| 161 |
+
|
| 162 |
+
st.markdown(f"*Tone: {selected_tone}*")
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
if __name__ == "__main__":
|
| 166 |
+
main()
|