import gradio as gr from huggingface_hub import InferenceClient import requests from bs4 import BeautifulSoup import re import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") def scrape_prospect_website(url): try: response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') company_name = soup.title.string if soup.title else "" meta_desc = soup.find('meta', attrs={'name': 'description'}) description = meta_desc['content'] if meta_desc else "" about_section = soup.find('div', string=re.compile(r'About Us|About|Who We Are', re.I)) about_text = about_section.text if about_section else "" contact_info = soup.find('div', string=re.compile(r'Contact|Email|Phone', re.I)) contact_text = contact_info.text if contact_info else "" return f"Company: {company_name}\nDescription: {description}\nAbout: {about_text[:300]}...\nContact: {contact_text}" except Exception as e: return f"Error scraping website: {str(e)}" def generate_email(input_type, profile_input, recipient_name, project_name, key_features): system_message = "You are an AI assistant that generates personalized emails based on given information about a prospect." if input_type == "URL": prospect_info = scrape_prospect_website(profile_input) else: prospect_info = profile_input prompt = f"""Generate a personalized email with the following details: Recipient: {recipient_name} Project Name: {project_name} Key Features of Your Project: {key_features} Use the following information about the prospect to personalize the email: {prospect_info} The email should: 1. Be professional and engaging 2. Show understanding of the prospect's business based on their website information 3. Explain how your project's key features can benefit their specific business 4. Include a clear call-to-action Keep the email concise and focused on value proposition. """ messages = [ {"role": "system", "content": system_message}, {"role": "user", "content": prompt} ] response = client.chat_completion( messages, max_tokens=500, temperature=0.7, top_p=0.95, ) return response.choices[0].message.content def send_email(sender_email, sender_password, recipient_email, subject, body): try: msg = MIMEMultipart() msg['From'] = sender_email msg['To'] = recipient_email msg['Subject'] = subject msg.attach(MIMEText(body, 'plain')) server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(sender_email, sender_password) text = msg.as_string() server.sendmail(sender_email, recipient_email, text) server.quit() return "Email sent successfully!" except Exception as e: return f"Error sending email: {str(e)}" def email_generator_interface(input_type, profile_input, recipient_name, recipient_email, project_name, key_features, sender_email, sender_password): if not all([profile_input, recipient_name, recipient_email, project_name, key_features, sender_email, sender_password]): return "Please fill in all fields." generated_email = generate_email(input_type, profile_input, recipient_name, project_name, key_features) subject = f"Introducing {project_name} - A Perfect Fit for {recipient_name}" send_result = send_email(sender_email, sender_password, recipient_email, subject, generated_email) return f"Generated Email:\n\n{generated_email}\n\n{send_result}" # Create the Gradio interface demo = gr.Interface( fn=email_generator_interface, inputs=[ gr.Radio(["URL", "Text"], label="Input Type"), gr.Textbox(lines=3, label="Profile URL of the prospect or their Profile Text"), gr.Textbox(label="What would you like to address the recipient as:"), gr.Textbox(label="Recipient's Email Address"), gr.Textbox(label="Name of the project you are pitching"), gr.Textbox(label="Key Features of your project (comma-separated)"), gr.Textbox(label="Your Email Address"), gr.Textbox(label="Your Email Password", type="password"), ], outputs=gr.Textbox(label="Generated Email and Sending Result"), title="Personalized Email Generator and Sender", description="Enter the prospect's details, your project info, and your email credentials to generate and send a personalized email.", ) if __name__ == "__main__": demo.launch()