Spaces:
Sleeping
Sleeping
import streamlit as st | |
import json | |
import schedule | |
import time | |
from datetime import datetime | |
# Function to Generate Email Content | |
def generate_email(template, customer, product): | |
try: | |
email_content = template.format( | |
name=customer['name'], | |
product_name=product['name'], | |
features=product['features'], | |
benefits=product['benefits'] | |
) | |
except KeyError as e: | |
st.error(f"KeyError: {e} in template: {template}") | |
raise | |
return email_content | |
# Function to Send Email (for demonstration purposes, it just prints the email) | |
def send_email(email): | |
st.write(f"Subject: {email['subject']}") | |
st.write(email['content']) | |
st.write("\n---\n") | |
# Streamlit UI | |
st.title(" Cold Email Sequence Generator") | |
uploaded_file = st.file_uploader("Upload a JSON file input structured like the 'example.json' in this App's files section", type="json") | |
if uploaded_file is not None: | |
# Parse JSON Input | |
data = json.load(uploaded_file) | |
customer = data['customer'] | |
product = data['product'] | |
sequence = data['sequence'] | |
emails = [] | |
# Generate Emails Based on the Sequence | |
for email in sequence: | |
email_content = generate_email(email['template'], customer, product) | |
emails.append({ | |
"day": email['day'], | |
"subject": email['subject'], | |
"content": email_content | |
}) | |
# Display Generated Emails | |
st.subheader("Generated Emails") | |
for email in emails: | |
send_email(email) | |
# Schedule Emails (for demonstration purposes, it just prints the emails) | |
start_time = datetime.now() | |
emails_sent = set() | |
emails_sent_count = 0 | |
max_emails_to_send = len(emails) | |
def get_email_identifier(email): | |
return (email['subject'], email['content']) | |
def schedule_email(e=email): | |
global emails_sent_count | |
if emails_sent_count < max_emails_to_send: | |
if get_email_identifier(e) not in emails_sent: | |
send_email(e) | |
emails_sent.add(get_email_identifier(e)) | |
emails_sent_count += 1 | |
if emails_sent_count >= max_emails_to_send: | |
return schedule.CancelJob | |
if st.button("Start Email Scheduling"): | |
for i, email in enumerate(emails): | |
schedule.every(10 * (i + 1)).seconds.do(schedule_email, e=email) | |
while emails_sent_count < max_emails_to_send: | |
schedule.run_pending() | |
time.sleep(1) | |