prograk commited on
Commit
62db7d5
·
verified ·
1 Parent(s): 7f00177

Create app.py

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