File size: 2,620 Bytes
33b4f25
 
 
 
 
 
 
c48ed59
 
 
 
 
33b4f25
 
c48ed59
 
 
 
33b4f25
 
 
 
 
c48ed59
33b4f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
import streamlit as st

# Function to call the OpenAI API and get the email content
def generate_email(api_key, email_type, details):
    openai.api_key = api_key  # Set OpenAI API key provided by the user
    
    # Define the conversation in the chat-based format
    messages = [
        {"role": "system", "content": "You are a helpful assistant that writes professional emails."},
        {"role": "user", "content": f"I need to write a professional email for the following situation:\nEmail Type: {email_type}\nDetails: {details}\nPlease generate the body of the email, including suggestions for the subject line and sign-off. Ensure the email tone is professional and polite."}
    ]
    
    try:
        # Request to OpenAI API (using chat/completions endpoint)
        response = openai.ChatCompletion.create(
            model="gpt-4o",  # You can replace this with any other chat-based model like `gpt-4`
            messages=messages,
            max_tokens=1200,
            temperature=0.7
        )
        
        # Return the generated email content
        return response.choices[0].message['content'].strip()
    except Exception as e:
        return f"Error: {str(e)}"

# Streamlit app
def email_composer():
    st.title("Automated Email Composer")
    st.write("This tool helps you generate professional emails, improve tone and grammar, and suggest appropriate subject lines and sign-offs.")
    
    # Step 1: Enter OpenAI API key
    api_key = st.text_input("Enter your OpenAI API Key", type="password")
    
    # Step 2: Select email type
    email_type = st.selectbox(
        "Select Email Type",
        ["Request for Information", "Follow-Up", "Introduction", "Complaint", "Thank You", "Job Application", "Meeting Request", "Others"]
    )
    
    # Step 3: Enter email details
    details = st.text_area("Enter Email Details", "Please provide the necessary details for the email.")
    
    # Step 4: Generate email button
    if st.button("Generate Email"):
        if api_key and details:
            with st.spinner("Generating email..."):
                # Call the generate_email function to get the email content
                email_content = generate_email(api_key, email_type, details)
                st.subheader("Generated Email")
                st.write(email_content)
        else:
            if not api_key:
                st.error("Please enter your OpenAI API key.")
            if not details:
                st.error("Please enter the email details to generate the email.")

# Run the Streamlit app
if __name__ == "__main__":
    email_composer()