File size: 16,697 Bytes
a910e98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88708bb
a910e98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a479fbf
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import streamlit as st
import os
from groq import Groq


client = Groq(
    api_key=os.environ.get("GROQ_API_KEY"),
)



# -- page config
st.set_page_config(
    page_title="TailorMail",
    page_icon="📧",
    layout="centered",
    initial_sidebar_state="collapsed",
)

# -- hiding the sidebar
st.markdown(
    """
<style>
    [data-testid="collapsedControl"] {
        display: none
    }
</style>
""",
    unsafe_allow_html=True,
)


# variable to check button status
if 'access' not in st.session_state:
    st.session_state.access = False

if 'conversation_history' not in st.session_state:
    st.session_state.conversation_history = []


system_prompt = {"role": "system", 
                     "content": "You are an expert at writing personalized emails. Your task is to write a personalized email in a mail format given some details of the email like recipient details, sender details, reason of email and some other attributes of mail. And make changes based on user's follow up requests."}



# welcome page
def welcome_page():

    # -- page title
    st.markdown(
        "<h1 style='text-align: center;'>Welcome to TailorMail📧</h1>", unsafe_allow_html=True
    )
    st.markdown(
        "<h5 style='text-align: center;'>Personalize Your Emails with Ease</h5>",
        unsafe_allow_html=True,
    )

    # -- adding space b/w texts
    st.title("\n\n")

    button_style = """
    <style>
    .stButton>button{
        width: 180px;
        height: 80px;
        font-size: 100px;
    }
    </style>"""

    

    cols = st.columns((1,3,1))
    
    with cols[1]: 

        access_button = st.button("Access the tool", type = "primary", use_container_width=True)

        if access_button:
            st.session_state['access'] = True
            st.rerun()  # This can be replaced with navigation to the main tool interface



def generate_user_prompt(recipient_name, recipient_role, company_name, sender_name, sender_role, sender_contact, 
                    sender_company, email_reason, specific_details, language, tone_style, cta_outcome, 
                    attachment_info, additional_instructions):
    
    prompt = f"""Instructions to draft the mail: 
    1. Understand the details about recipient, sender and other attributes of email like reason, tone, language, etc.
    2. The subject of the mail will not be provided to you, so once you understand the reason and other details about the mail, create a good subject for the mail that meets the user requirements and is in aligned with the details mentioned.
    3. The output returned need to be of the email format and not as one big long paragraph.
    4. Do add greetings based on the type of the mail.

    IMPORTANT: User might not provide us with some these details : recipient_role, company_name, sender_role, sender_contact, sender_company, specific_details, cta_outcome, attachment_info, additional_instructions. So in that case don't interpret things and add up on your own. You need to generate content based on the content user has provided and ignore the fields which user has left blank (empty string).
    
    Also, don't show variable names in the response, use the names and details which are provided and leave the ones which are blank or empty strings.
    Make sure to not add any extra commentary in the output other than the complete mail. And avoid using special symbols like * etc on the places where its not required.

    Here are the details of the email provided by the user. Use these details to draft a mail, remember some of these could be empty strings: 

    Recipient Name : {recipient_name},

    Recipient Role: {recipient_role},

    Recipient Company Name: {company_name}, 

    Sender Name: {sender_name}, 

    Sender Role: {sender_role}, 

    Sender Contact: {sender_contact}, 

    Sender Company: {sender_company}, 

    Reason of writing the mail: {email_reason}, 

    Specific details about the mail, if any: {specific_details}, 

    Formality Type: {language}, 

    Tone of the mail: {tone_style}, 

    Call to action, if any: {cta_outcome}, 

    Information about the file or document attached, if any: {attachment_info}, 

    These are some special instructions from the user that we need to consider while drafting the mail: {additional_instructions}"""

    return prompt


def generate_response(user_prompt, model, temperature, tokens):
    messages = st.session_state.conversation_history + [{"role": "user", "content": user_prompt}]
    
    if model=='Mistral':
        model = 'mixtral-8x7b-32768'
    else:
        model = 'llama-3.1-8b-instant'

    chat_completion = client.chat.completions.create(
        messages=messages,
        model= model,
        temperature = temperature, 
        max_tokens = tokens
    )
    
    response = chat_completion.choices[0].message.content
    
    # Append the assistant's response to the conversation history
    st.session_state.conversation_history.append({"role": "assistant", "content": response})
    
    return response



def run_email_tool(st):
    
    # Apply custom CSS to style the Streamlit app
    st.markdown(
        """
        <style>
        .stTextInput > div > input {
            color: #000000;
            background-color: #f2f2f2;
            border-radius: 5px;
            border: 1px solid #d9d9d9;
        }
        .stTextArea > div > textarea {
            color: #000000;
            background-color: #f2f2f2;
            border-radius: 5px;
            border: 1px solid #d9d9d9;
        }
        .stRadio > div > div > label > div {
            color: #688da2;
        }
        .stButton > button {
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            padding: 10px 24px;
        }
        .stButton > button:hover {
            background-color: #45a049;
        }
        </style>
        """,
        unsafe_allow_html=True
    )

    # UI Title
    st.markdown("<h1 style='color: #2180b9; text-align: center;'>TailorMail📧</h1>", unsafe_allow_html=True)

    # Section 1: Recipient Information
    st.markdown("<h2 style='color: #688da2;'>Recipient Information</h2>", unsafe_allow_html=True)

    # Recipient's Name Field
    st.markdown("""
        <div style="display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0px; padding-bottom: 0px">
            <span style="color: #c94d4d; font-weight: bold;">Recipient's Name</span>
            <span style="color: grey; font-size: 0.9em;">(Required)</span>
        </div>
        """, unsafe_allow_html=True)
    recipient_name = st.text_input("", placeholder="e.g., John Doe", key="recipient_name", label_visibility="collapsed")

    # Add a small margin between the input field and the next heading
    st.markdown("<div style='margin-bottom: 15px;'></div>", unsafe_allow_html=True)

    # Recipient's Job Title/Role Field
    st.markdown("""
        <div style="display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0px; padding-bottom: 0px">
            <span style="font-weight: bold;">Recipient's Job Title/Role</span>
            <span style="color: grey; font-size: 0.9em;">(Optional)</span>
        </div>
        """, unsafe_allow_html=True)
    recipient_role = st.text_input("", placeholder="e.g., Marketing Manager", key="recipient_role", label_visibility="collapsed")


    # Company Name Field
    st.markdown("""
        <div style="display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0px; padding-bottom: 0px">
            <span style="font-weight: bold;">Company Name</span>
            <span style="color: grey; font-size: 0.9em;">(Optional)</span>
        </div>
        """, unsafe_allow_html=True)
    company_name = st.text_input("", placeholder="e.g., ABC Corporation", label_visibility="collapsed")

    # Section 2: Sender Information
    st.markdown("<h2 style='color: #688da2;'>Sender Information</h2>", unsafe_allow_html=True)

    # Sender's Name Field
    st.markdown("""
        <div style="display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0px; padding-bottom: 0px">
            <span style="color: #c94d4d; font-weight: bold;">Sender's Name</span>
            <span style="color: grey; font-size: 0.9em;">(Required)</span>
        </div>
        """, unsafe_allow_html=True)
    sender_name = st.text_input("", placeholder="e.g., Jane Smith", label_visibility="collapsed")

    # Sender's Job Title Field
    st.markdown("""
        <div style="display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0px; padding-bottom: 0px">
            <span style="font-weight: bold;">Sender's Job Title</span>
            <span style="color: grey; font-size: 0.9em;">(Optional)</span>
        </div>
        """, unsafe_allow_html=True)
    sender_role = st.text_input("", placeholder="e.g., Project Lead", label_visibility="collapsed")

    # Sender's Contact Information Field
    st.markdown("""
        <div style="display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0px; padding-bottom: 0px">
            <span style="font-weight: bold;">Sender's Contact Information</span>
            <span style="color: grey; font-size: 0.9em;">(Optional)</span>
        </div>
        """, unsafe_allow_html=True)
    sender_contact = st.text_input("", placeholder="Enter your mail or mobile number", label_visibility="collapsed")

    # Sender's Company Name/Organization Field
    st.markdown("""
        <div style="display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0px; padding-bottom: 0px">
            <span style="font-weight: bold;">Company Name/Organization</span>
            <span style="color: grey; font-size: 0.9em;">(Optional)</span>
        </div>
        """, unsafe_allow_html=True)
    sender_company = st.text_input("", placeholder="e.g., XYZ Ltd.", label_visibility="collapsed")

    # Section 3: Purpose of the Email
    st.markdown("<h2 style='color: #688da2;'>Purpose of the Email</h2>", unsafe_allow_html=True)

    # Reason for the Email Field
    st.markdown("""
        <div style="display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0px; padding-bottom: 0px">
            <span style="color: #c94d4d; font-weight: bold;">Reason for the Email</span>
            <span style="color: grey; font-size: 0.9em;">(Required)</span>
        </div>
        """, unsafe_allow_html=True)
    email_reason = st.text_area("", placeholder="e.g., I want to discuss the project updates", label_visibility="collapsed")

    # Specific Details Field
    st.markdown("""
        <div style="display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0px; padding-bottom: 0px">
            <span style="font-weight: bold;">Specific Details</span>
            <span style="color: grey; font-size: 0.9em;">(Optional)</span>
        </div>
        """, unsafe_allow_html=True)
    specific_details = st.text_area("", placeholder="Enter any specific details that you would like to mention e.g., Provide an update on the project status...", label_visibility="collapsed")

    # Section 4: Tone of the Email
    st.markdown("<h2 style='color: #688da2;'>Tone of the Email</h2>", unsafe_allow_html=True)

    # CSS to make radio button options appear side by side
    st.markdown(
    """
    <style>
    .stRadio > div {
    flex-direction: row;
    }
    """,
        unsafe_allow_html=True,
    )

    language = st.radio("Language", options=["Formal", "Informal"], index=0)
    tone_style = st.radio("Tone", options=["Friendly", "Professional", "Urgent"], index=1)

    # Section 5: Call to Action (CTA)
    st.markdown("<h2 style='color: #688da2;'>Call to Action (CTA)</h2>", unsafe_allow_html=True)

    # Desired Outcome Field
    st.markdown("""
        <div style="display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0px; padding-bottom: 0px">
            <span style="font-weight: bold;">Desired Outcome</span>
            <span style="color: grey; font-size: 0.9em;">(Optional)</span>
        </div>
        """, unsafe_allow_html=True)
    cta_outcome = st.text_input("", placeholder="e.g., Schedule a meeting for next week", label_visibility="collapsed")

    # # Section 6: Sign-Off
    # st.markdown("<h2 style='color: #688da2;'>Sign-Off</h2>", unsafe_allow_html=True)

    # # Preferred Closing Field
    # st.markdown("""
    #     <div style="display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0px; padding-bottom: 0px">
    #         <span style="font-weight: bold;">Preferred Closing</span>
    #         <span style="color: grey; font-size: 0.9em;">(Optional)</span>
    #     </div>
    #     """, unsafe_allow_html=True)
    # closing_text = st.text_input("", placeholder="e.g., Best regards", label_visibility="collapsed")

    # # Sender's Signature Field
    # st.markdown("""
    #     <div style="display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0px; padding-bottom: 0px">
    #         <span style="font-weight: bold;">Sender's Signature</span>
    #         <span style="color: grey; font-size: 0.9em;">(Optional)</span>
    #     </div>
    #     """, unsafe_allow_html=True)
    # signature = st.text_input("", placeholder="e.g., Jane Smith, Project Lead", label_visibility="collapsed")

    # Section 7: Attachments
    st.markdown("<h2 style='color: #688da2;'>Attachments</h2>", unsafe_allow_html=True)

    # Attachment Information Field
    st.markdown("""
        <div style="display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0px; padding-bottom: 0px">
            <span style="font-weight: bold;">Attachment Information</span>
            <span style="color: grey; font-size: 0.9em;">(Optional)</span>
        </div>
        """, unsafe_allow_html=True)
    attachment_info = st.text_area("", placeholder="e.g., I have attached the documentation that explains the workflow...", label_visibility="collapsed")

    # Section 8: Additional Instructions to the Bot
    st.markdown("<h2 style='color: #688da2;'>Additional Instructions to the Bot</h2>", unsafe_allow_html=True)

    # Additional Instructions Field
    st.markdown("""
        <div style="display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0px; padding-bottom: 0px">
            <span style="font-weight: bold;">Additional Instructions</span>
            <span style="color: grey; font-size: 0.9em;">(Optional)</span>
        </div>
        """, unsafe_allow_html=True)
    additional_instructions = st.text_area("", placeholder="e.g., I want the email to be written in 200 words, Add a very catchy subject for this mail,...", label_visibility="collapsed")


    # Section 9: Model Parameters 
    st.markdown("<h2 style='color: #688da2;'>Model Parameters</h2>", unsafe_allow_html=True)
    
    model = st.radio("Select a Model", options=["Llama 3.1", "Mixtral"], index=0)

    temperature = st.slider('Temperature (higher value more random output, lesser value more stable output)', 0.0, 2.0, step=0.01, value = 1.0)
    
    tokens = st.slider('Max words:', 0, 32000, step=20, value = 1024)

    st.markdown("\n\n")

    # Submit Button
    if st.button("Generate Mail", use_container_width=True):
        if not recipient_name:
            st.error("Please provide the recipient's name.")
        elif not sender_name:
            st.error("Please provide the sender's name.")
        elif not email_reason:
            st.error("Please provide the reason for the email.")
        else:
            with st.spinner(text="Generating mail..."):
                
                # Process the input data and generate the email content
                email_prompt = generate_user_prompt(recipient_name, recipient_role, company_name, sender_name, sender_role, 
                                            sender_contact, sender_company, email_reason, specific_details, 
                                            language, tone_style, cta_outcome, attachment_info, additional_instructions)
                
                # Generate and display the email content
                response = generate_response(email_prompt, model, temperature, tokens)
                st.markdown("<h3 style='color: #2180b9;'>Generated Email</h3>", unsafe_allow_html=True)
                st.text_area(label="", value=response, height=400)



if __name__=='__main__':
    
    if not st.session_state['access']:
        welcome_page()
    else:
        run_email_tool(st)