Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211import streamlit as stimport osfrom groq import Groqfrom streamlit_extras.switch_page_button import switch_page# Initialize Groq client with API keyclient = Groq(api_key=os.environ.get("GROQ_API_KEY"),)# Configure Streamlit page settingsst.set_page_config(page_title="EmailGenie",page_icon="📧",layout="centered",initial_sidebar_state="collapsed",)# Hide the sidebarst.markdown("""<style>[data-testid="collapsedControl"] {display: none}</style>""",unsafe_allow_html=True,)# Hide Streamlit's default stylinghide_st_style = """<style>#MainMenu {visibility: hidden;}footer {visibility: hidden;}header {visibility: hidden;}</style>"""st.markdown(hide_st_style, unsafe_allow_html=True)# Display the page title and descriptionst.markdown("<h1 style='text-align: center;'>Welcome to 📧EmailGenie</h1>", unsafe_allow_html=True)st.markdown("<p style='text-align: center;'>AI-Powered Cold Email Generator</p>",unsafe_allow_html=True,)# Initialize conversation history in session state if it doesn't existif "conversation_history" not in st.session_state:st.session_state.conversation_history = []# Initialize session state variables for input fieldsif 'recipient_name' not in st.session_state:st.session_state.recipient_name = ""if 'recipient_role' not in st.session_state:st.session_state.recipient_role = ""if 'industry' not in st.session_state:st.session_state.industry = ""if 'company_name' not in st.session_state:st.session_state.company_name = ""if 'sender_name' not in st.session_state:st.session_state.sender_name = ""if 'sender_role' not in st.session_state:st.session_state.sender_role = ""if 'specific_goal' not in st.session_state:st.session_state.specific_goal = ""# Function to generate the email promptdef generate_prompt(email_type, recipient_name, industry, recipient_role, company_name, sender_name, sender_role, specific_goal, email_tone, email_style):prompt = f"""Instructions to draft the email:1. Create a {email_type} email based on the following details.2. Generate an appropriate subject line for the email.3. Format the email properly with greetings, body, and closing.4. Ensure the email has a {email_tone} tone and follows a {email_style} style.Email details:- Recipient Name: {recipient_name}- Recipient Role: {recipient_role}- Recipient Company: {company_name}- Industry: {industry}- Sender Name: {sender_name}- Sender Role: {sender_role}- Specific Goal: {specific_goal}Important:- If any of the above fields are empty, do not make assumptions. Use only the provided information.- Do not include any commentary or explanations outside of the email content.- Avoid using variable names or placeholders in the final email.Please generate a personalized {email_type} email using the above details."""# Add system prompt to conversation history if it doesn't existif not any(msg['role'] == 'system' for msg in st.session_state.conversation_history):st.session_state.conversation_history += [{"role":"system", "content":"You are an expert at writing personalized emails. Don't answer questions that are not related to generating or modifying emails. For irrelevant questions just respond something like you can only help with writing personalized emails."}]st.session_state.conversation_history += [{"role": "user", "content": prompt}]return prompt# Function to get response from the AI modeldef get_response(prompt):messages = st.session_state.conversation_historymodel = 'llama-3.1-8b-instant'chat_completion = client.chat.completions.create(messages=messages,model= model)response = chat_completion.choices[0].message.contentst.session_state.conversation_history += [{"role": "assistant", "content": response}]return response# CSS to make radio button options appear side by sidest.markdown("""<style>.stRadio > div {flex-direction: row;}</style>""",unsafe_allow_html=True,)# Input Fieldsst.markdown("<span style='color: #c94d4d; font-weight: bold;'>Email Type</span>", unsafe_allow_html=True)email_type = st.radio("", options=["Sales Outreach", "Networking", "Job Application", "Partnership Proposal", "Follow-Up"], index=["Sales Outreach", "Networking", "Job Application", "Partnership Proposal", "Follow-Up"].index(st.session_state.get('email_type', "Sales Outreach")), label_visibility="collapsed")# Create text input fields for user informationst.markdown("<span style='color: #c94d4d; font-weight: bold;'>Recipient Name</span>", unsafe_allow_html=True)recipient_name = st.text_input("Recipient Name", value=st.session_state.get('recipient_name', ""), placeholder="e.g. John Doe", label_visibility="collapsed")st.markdown("<span style='color: #c94d4d; font-weight: bold;'>Recipient's Role</span>", unsafe_allow_html=True)recipient_role = st.text_input("Recipient's Role", value=st.session_state.get('recipient_role', ""), placeholder="e.g. Marketing Manager, CEO, Software Engineer", label_visibility="collapsed")st.markdown("<span style='color: #c94d4d; font-weight: bold;'>Industry</span>", unsafe_allow_html=True)industry = st.text_input("Industry", value=st.session_state.get('industry', ""), placeholder="e.g. Technology, Healthcare, Finance", label_visibility="collapsed")st.markdown("<span style='color: #c94d4d; font-weight: bold;'>Company Name</span>", unsafe_allow_html=True)company_name = st.text_input("Company Name", value=st.session_state.get('company_name', ""), placeholder="e.g. Acme Corporation", label_visibility="collapsed")st.markdown("<span style='color: #c94d4d; font-weight: bold;'>Your Name</span>", unsafe_allow_html=True)sender_name = st.text_input("Your Name", value=st.session_state.get('sender_name', ""), placeholder="e.g. Jane Smith", label_visibility="collapsed")st.markdown("<span style='color: #c94d4d; font-weight: bold;'>Your Role</span>", unsafe_allow_html=True)sender_role = st.text_input("Your Role", value=st.session_state.get('sender_role', ""), placeholder="e.g. Sales Representative", label_visibility="collapsed")st.markdown("<span style='color: #c94d4d; font-weight: bold;'>Specific Goal of the Email</span>", unsafe_allow_html=True)specific_goal = st.text_input("Specific Goal of the Email", value=st.session_state.get('specific_goal', ""), placeholder="e.g. Schedule a product demo", label_visibility="collapsed")# Email Tone and Style Selectionst.markdown("<span style='color: #c94d4d; font-weight: bold;'>Email Tone</span>", unsafe_allow_html=True)email_tone = st.radio("", options=["Professional", "Friendly", "Formal", "Casual", "Enthusiastic"], index=["Professional", "Friendly", "Formal", "Casual", "Enthusiastic"].index(st.session_state.get('email_tone', "Professional")), label_visibility="collapsed")st.markdown("<span style='color: #c94d4d; font-weight: bold;'>Email Style</span>", unsafe_allow_html=True)email_style = st.radio("", options=["Concise", "Detailed", "Persuasive", "Informative"], index=["Concise", "Detailed", "Persuasive", "Informative"].index(st.session_state.get('email_style', "Concise")), label_visibility="collapsed")# Update session state with current input valuesst.session_state.email_type = email_typest.session_state.recipient_name = recipient_namest.session_state.recipient_role = recipient_rolest.session_state.industry = industryst.session_state.company_name = company_namest.session_state.sender_name = sender_namest.session_state.sender_role = sender_rolest.session_state.specific_goal = specific_goalst.session_state.email_tone = email_tonest.session_state.email_style = email_style# Generate Email Buttonif st.button("Generate Email", use_container_width=True, type='primary'):# Check if all fields are filledrequired_fields = [(st.session_state.email_type, "Email Type"),(st.session_state.recipient_name, "Recipient Name"),(st.session_state.recipient_role, "Recipient's Role"),(st.session_state.industry, "Industry"),(st.session_state.company_name, "Company Name"),(st.session_state.sender_name, "Your Name"),(st.session_state.sender_role, "Your Role"),(st.session_state.specific_goal, "Specific Goal of the Email"),(st.session_state.email_tone, "Email Tone"),(st.session_state.email_style, "Email Style")]empty_fields = [field_name for field_value, field_name in required_fields if not field_value.strip()]if empty_fields:st.error(f"Please fill in all required fields: {', '.join(empty_fields)}")else:# Generate email prompt and get AI responseemail_prompt = generate_prompt(st.session_state.email_type, st.session_state.recipient_name, st.session_state.industry, st.session_state.recipient_role, st.session_state.company_name, st.session_state.sender_name, st.session_state.sender_role, st.session_state.specific_goal, st.session_state.email_tone, st.session_state.email_style)email_content = get_response(email_prompt)# Store the generated email in session statest.session_state.generated_email = email_content# Switch to the follow-up pageswitch_page("follow_up")# Preview generated email if it existsif 'generated_email' in st.session_state:st.subheader("Generated Email Preview")st.write(st.session_state.generated_email)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211import streamlit as stimport osfrom groq import Groqfrom streamlit_extras.switch_page_button import switch_page# Initialize Groq client with API keyclient = Groq(api_key=os.environ.get("GROQ_API_KEY"),)# Configure Streamlit page settingsst.set_page_config(page_title="EmailGenie",page_icon="📧",layout="centered",initial_sidebar_state="collapsed",)# Hide the sidebarst.markdown("""<style>[data-testid="collapsedControl"] {display: none}</style>""",unsafe_allow_html=True,)# Hide Streamlit's default stylinghide_st_style = """<style>#MainMenu {visibility: hidden;}footer {visibility: hidden;}header {visibility: hidden;}</style>"""st.markdown(hide_st_style, unsafe_allow_html=True)# Display the page title and descriptionst.markdown("<h1 style='text-align: center;'>Welcome to 📧EmailGenie</h1>", unsafe_allow_html=True)st.markdown("<p style='text-align: center;'>AI-Powered Cold Email Generator</p>",unsafe_allow_html=True,)# Initialize conversation history in session state if it doesn't existif "conversation_history" not in st.session_state:st.session_state.conversation_history = []# Initialize session state variables for input fieldsif 'recipient_name' not in st.session_state:st.session_state.recipient_name = ""if 'recipient_role' not in st.session_state:st.session_state.recipient_role = ""if 'industry' not in st.session_state:st.session_state.industry = ""if 'company_name' not in st.session_state:st.session_state.company_name = ""if 'sender_name' not in st.session_state:st.session_state.sender_name = ""if 'sender_role' not in st.session_state:st.session_state.sender_role = ""if 'specific_goal' not in st.session_state:st.session_state.specific_goal = ""# Function to generate the email promptdef generate_prompt(email_type, recipient_name, industry, recipient_role, company_name, sender_name, sender_role, specific_goal, email_tone, email_style):prompt = f"""Instructions to draft the email:1. Create a {email_type} email based on the following details.2. Generate an appropriate subject line for the email.3. Format the email properly with greetings, body, and closing.4. Ensure the email has a {email_tone} tone and follows a {email_style} style.Email details:- Recipient Name: {recipient_name}- Recipient Role: {recipient_role}- Recipient Company: {company_name}- Recipient Industry: {industry}- Sender Name: {sender_name}- Sender Role: {sender_role}- Specific Reason for which sender is reaching out to Recipient: {specific_goal}Important:- If any of the above fields are empty, do not make assumptions. Use only the provided information.- Do not include any commentary or explanations outside of the email content.- Avoid using variable names or placeholders in the final email.Please generate a personalized {email_type} email using the above details."""# Add system prompt to conversation history if it doesn't existif not any(msg['role'] == 'system' for msg in st.session_state.conversation_history):st.session_state.conversation_history += [{"role":"system", "content":"You are an expert at writing personalized emails. Don't answer questions that are not related to generating or modifying emails. For irrelevant questions just respond something like you can only help with writing personalized emails."}]st.session_state.conversation_history += [{"role": "user", "content": prompt}]return prompt# Function to get response from the AI modeldef get_response(prompt):messages = st.session_state.conversation_historymodel = 'llama-3.1-8b-instant'chat_completion = client.chat.completions.create(messages=messages,model= model)response = chat_completion.choices[0].message.contentst.session_state.conversation_history += [{"role": "assistant", "content": response}]return response# CSS to make radio button options appear side by sidest.markdown("""<style>.stRadio > div {flex-direction: row;}</style>""",unsafe_allow_html=True,)# Input Fieldsst.markdown("<span style='color: #c94d4d; font-weight: bold;'>Email Type</span>", unsafe_allow_html=True)email_type = st.radio("", options=["Sales Outreach", "Networking", "Job Application", "Partnership Proposal", "Follow-Up"], index=["Sales Outreach", "Networking", "Job Application", "Partnership Proposal", "Follow-Up"].index(st.session_state.get('email_type', "Sales Outreach")), label_visibility="collapsed")# Create text input fields for user informationst.markdown("<span style='color: #c94d4d; font-weight: bold;'>Recipient Name</span>", unsafe_allow_html=True)recipient_name = st.text_input("Recipient Name", value=st.session_state.get('recipient_name', ""), placeholder="e.g. John Doe", label_visibility="collapsed")st.markdown("<span style='color: #c94d4d; font-weight: bold;'>Recipient's Role</span>", unsafe_allow_html=True)recipient_role = st.text_input("Recipient's Role", value=st.session_state.get('recipient_role', ""), placeholder="e.g. Marketing Manager, CEO, Software Engineer", label_visibility="collapsed")st.markdown("<span style='color: #c94d4d; font-weight: bold;'>Industry</span>", unsafe_allow_html=True)industry = st.text_input("Industry", value=st.session_state.get('industry', ""), placeholder="e.g. Technology, Healthcare, Finance", label_visibility="collapsed")st.markdown("<span style='color: #c94d4d; font-weight: bold;'>Company Name</span>", unsafe_allow_html=True)company_name = st.text_input("Company Name", value=st.session_state.get('company_name', ""), placeholder="e.g. Acme Corporation", label_visibility="collapsed")st.markdown("<span style='color: #c94d4d; font-weight: bold;'>Your Name</span>", unsafe_allow_html=True)sender_name = st.text_input("Your Name", value=st.session_state.get('sender_name', ""), placeholder="e.g. Jane Smith", label_visibility="collapsed")st.markdown("<span style='color: #c94d4d; font-weight: bold;'>Your Role</span>", unsafe_allow_html=True)sender_role = st.text_input("Your Role", value=st.session_state.get('sender_role', ""), placeholder="e.g. Sales Representative", label_visibility="collapsed")st.markdown("<span style='color: #c94d4d; font-weight: bold;'>Specific Goal of the Email</span>", unsafe_allow_html=True)specific_goal = st.text_input("Specific Goal of the Email", value=st.session_state.get('specific_goal', ""), placeholder="e.g. Schedule a product demo", label_visibility="collapsed")# Email Tone and Style Selectionst.markdown("<span style='color: #c94d4d; font-weight: bold;'>Email Tone</span>", unsafe_allow_html=True)email_tone = st.radio("", options=["Professional", "Friendly", "Formal", "Casual", "Enthusiastic"], index=["Professional", "Friendly", "Formal", "Casual", "Enthusiastic"].index(st.session_state.get('email_tone', "Professional")), label_visibility="collapsed")st.markdown("<span style='color: #c94d4d; font-weight: bold;'>Email Style</span>", unsafe_allow_html=True)email_style = st.radio("", options=["Concise", "Detailed", "Persuasive", "Informative"], index=["Concise", "Detailed", "Persuasive", "Informative"].index(st.session_state.get('email_style', "Concise")), label_visibility="collapsed")# Update session state with current input valuesst.session_state.email_type = email_typest.session_state.recipient_name = recipient_namest.session_state.recipient_role = recipient_rolest.session_state.industry = industryst.session_state.company_name = company_namest.session_state.sender_name = sender_namest.session_state.sender_role = sender_rolest.session_state.specific_goal = specific_goalst.session_state.email_tone = email_tonest.session_state.email_style = email_style# Generate Email Buttonif st.button("Generate Email", use_container_width=True, type='primary'):# Check if all fields are filledrequired_fields = [(st.session_state.email_type, "Email Type"),(st.session_state.recipient_name, "Recipient Name"),(st.session_state.recipient_role, "Recipient's Role"),(st.session_state.industry, "Industry"),(st.session_state.company_name, "Company Name"),(st.session_state.sender_name, "Your Name"),(st.session_state.sender_role, "Your Role"),(st.session_state.specific_goal, "Specific Goal of the Email"),(st.session_state.email_tone, "Email Tone"),(st.session_state.email_style, "Email Style")]empty_fields = [field_name for field_value, field_name in required_fields if not field_value.strip()]if empty_fields:st.error(f"Please fill in all required fields: {', '.join(empty_fields)}")else:# Generate email prompt and get AI responseemail_prompt = generate_prompt(st.session_state.email_type, st.session_state.recipient_name, st.session_state.industry, st.session_state.recipient_role, st.session_state.company_name, st.session_state.sender_name, st.session_state.sender_role, st.session_state.specific_goal, st.session_state.email_tone, st.session_state.email_style)email_content = get_response(email_prompt)# Store the generated email in session statest.session_state.generated_email = email_content# Switch to the follow-up pageswitch_page("follow_up")# Preview generated email if it existsif 'generated_email' in st.session_state:st.subheader("Generated Email Preview")st.write(st.session_state.generated_email)
app.py+211-211