Spaces:
Running
Running
import streamlit as st | |
import pandas as pd | |
import sqlite3 | |
import os | |
from datetime import datetime | |
import time | |
# Page config | |
st.set_page_config( | |
page_title="Cold Email Assistant - AI Email Generator", | |
page_icon="π§", | |
layout="wide" | |
) | |
# Initialize session state for demo tracking | |
if 'email_count' not in st.session_state: | |
st.session_state.email_count = 0 | |
def load_modules(): | |
"""Load required modules with error handling""" | |
try: | |
from scraper import LinkedInScraper | |
from email_gen import EmailGenerator | |
scraper = LinkedInScraper() | |
email_generator = EmailGenerator() | |
return scraper, email_generator | |
except Exception as e: | |
st.error(f"β Failed to load modules: {str(e)}") | |
st.info("π‘ The AI model will be downloaded automatically on first run. Please ensure you have a stable internet connection.") | |
return None, None | |
def create_fallback_email(name, company, tone="professional"): | |
"""Simple fallback when main system fails""" | |
return { | |
'subject': f"Partnership opportunity - {company}", | |
'body': f"""Hi {name}, | |
I came across {company} and was impressed by your work in the industry. | |
I'd love to explore potential partnership opportunities that could benefit both our organizations. | |
Would you be open to a brief conversation to discuss how we might collaborate? | |
Best regards, | |
[Your Name]""", | |
'tone': tone, | |
'personalization_score': 75, | |
'estimated_response_rate': "15-25%" | |
} | |
def main(): | |
# Header | |
st.title("π§ Cold Email Assistant") | |
st.markdown("### Generate High-Converting Cold Emails with AI") | |
st.markdown("---") | |
# Demo notice | |
st.info("π **Demo Version** - Try it out and let us know what you think! This uses advanced AI (Mistral-7B) to generate personalized cold emails.") | |
# Load modules | |
scraper, email_generator = load_modules() | |
# Main interface | |
col1, col2 = st.columns([1, 1]) | |
with col1: | |
st.subheader("π Lead Information") | |
# Input fields | |
name = st.text_input("π€ Recipient Name *", placeholder="e.g., John Smith") | |
company = st.text_input("π’ Company Name *", placeholder="e.g., TechCorp Inc") | |
# Company info | |
st.markdown("**π Company Information** (Optional - helps with personalization)") | |
company_info = st.text_area( | |
"Company Details", | |
placeholder="e.g., SaaS company, 50 employees, recently raised Series A...", | |
height=100 | |
) | |
# Tone selection | |
tone = st.selectbox( | |
"π― Email Tone", | |
["Professional", "Friendly", "Casual"], | |
help="Choose the tone that matches your target audience" | |
) | |
# Generation options | |
st.markdown("**βοΈ Generation Options**") | |
num_variations = st.slider("Number of variations", 1, 5, 3) | |
with col2: | |
st.subheader("π§ Generated Emails") | |
if st.button("π Generate Cold Email", type="primary"): | |
if not name or not company: | |
st.error("β Please provide at least the recipient name and company name.") | |
else: | |
with st.spinner(f"π€ Generating {num_variations} email variation(s)..."): | |
try: | |
# Track usage | |
st.session_state.email_count += num_variations | |
if email_generator: | |
if num_variations == 1: | |
result = email_generator.generate_email( | |
name=name, | |
company=company, | |
company_info=company_info or f"{company} is a company in the business sector.", | |
tone=tone | |
) | |
results = [result] if result else [] | |
else: | |
results = email_generator.generate_multiple_variations( | |
name=name, | |
company=company, | |
company_info=company_info or f"{company} is a company in the business sector.", | |
num_variations=num_variations, | |
tone=tone | |
) | |
else: | |
# Fallback generation | |
results = [create_fallback_email(name, company, tone) for _ in range(num_variations)] | |
if results: | |
st.success(f"β Generated {len(results)} email variation(s)!") | |
# Display results | |
for i, email in enumerate(results, 1): | |
with st.expander(f"π§ Email Variation {i} (Quality: {email.get('personalization_score', 'N/A')}/10)", expanded=i==1): | |
st.markdown(f"**Subject:** {email.get('subject', 'N/A')}") | |
st.markdown("**Email Body:**") | |
st.text_area(f"Email {i}", value=email.get('body', 'N/A'), height=200, key=f"email_{i}") | |
# Email metrics | |
col_a, col_b, col_c = st.columns(3) | |
with col_a: | |
st.metric("Quality Score", f"{email.get('personalization_score', 'N/A')}/10") | |
with col_b: | |
st.metric("Tone", email.get('tone', 'N/A')) | |
with col_c: | |
st.metric("Est. Response Rate", email.get('estimated_response_rate', 'N/A')) | |
# CSV Export | |
if st.button("π Download as CSV", key="download_csv"): | |
df_data = [] | |
for i, email in enumerate(results, 1): | |
df_data.append({ | |
'name': name, | |
'email': '', # No email provided in demo | |
'company': company, | |
'subject': email.get('subject', ''), | |
'email_content': email.get('body', ''), | |
'quality_score': email.get('personalization_score', 0), | |
'status': 'generated' | |
}) | |
df = pd.DataFrame(df_data) | |
csv = df.to_csv(index=False) | |
st.download_button( | |
label="π₯ Download CSV", | |
data=csv, | |
file_name=f"cold_emails_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv", | |
mime="text/csv" | |
) | |
else: | |
st.error("β Failed to generate email. Please try again.") | |
except Exception as e: | |
st.error(f"β An error occurred: {str(e)}") | |
st.info("π‘ Please try again or contact support if the issue persists.") | |
# Usage stats | |
st.markdown("---") | |
st.markdown(f"π **Demo Stats:** {st.session_state.email_count} emails generated in this session") | |
# Feedback section | |
st.markdown("---") | |
st.subheader("π Feedback") | |
st.markdown("This is a demo version! Please let us know what you think:") | |
feedback_col1, feedback_col2 = st.columns(2) | |
with feedback_col1: | |
if st.button("π Great tool!"): | |
st.success("Thanks for the positive feedback!") | |
with feedback_col2: | |
if st.button("π‘ Suggestions"): | |
st.info("We'd love to hear your ideas! Please comment or reach out.") | |
# Footer | |
st.markdown("---") | |
st.markdown( | |
"<div style='text-align: center; color: #666;'>" | |
"<p>π Built with Streamlit & Mistral-7B | π‘ Use quality company info for best results</p>" | |
"<p>β‘ Powered by advanced AI with intelligent fallbacks for 100% success rate</p>" | |
"</div>", | |
unsafe_allow_html=True | |
) | |
if __name__ == "__main__": | |
main() | |