curiousgeorge1292's picture
Update app.py
20d517d verified
import os
import requests
from bs4 import BeautifulSoup
import gradio as gr
from groq import Groq
# Initialize Groq client
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
# Function to extract content from a URL
def extract_content(url):
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
paragraphs = soup.find_all('p')
content = ' '.join([para.get_text() for para in paragraphs])
return content[:2000] # Limit content to 2000 characters to avoid overload
except Exception as e:
return f"Error extracting content from {url}: {str(e)}"
# Function to fetch LinkedIn profile insights using Proxycurl API
def fetch_linkedin_insights(profile_url):
api_key = os.environ.get("PROXYCURL_API_KEY")
api_endpoint = "https://nubela.co/proxycurl/api/v2/linkedin"
headers = {"Authorization": f"Bearer {api_key}"}
params = {"url": profile_url, "fallback_to_cache": "on-error"}
try:
response = requests.get(api_endpoint, headers=headers, params=params, timeout=10)
response.raise_for_status()
profile_data = response.json()
insights = f"{profile_data.get('headline', '')}. {profile_data.get('summary', '')}"
return insights
except Exception as e:
return f"Error fetching LinkedIn insights: {str(e)}"
# Function to generate email using Llama
def generate_email(name, linkedin_url, website_url, context_url, word_count):
# Fetch insights from LinkedIn and reference URLs
linkedin_insights = fetch_linkedin_insights(linkedin_url)
website_content = extract_content(website_url)
context_content = extract_content(context_url) if context_url else ""
# Fetch details from AdTech company website
adtech_content = extract_content("https://www.abcd.com")
# Construct the prompt for Llama
prompt = f"""
You are an AI assistant helping an AdTech company draft personalized sales emails.
Here are the details of the prospect:
- Name: {name}
- LinkedIn Insights: {linkedin_insights}
- Website Content: {website_content}
- Additional Context: {context_content}
The company provides the following offerings:
{adtech_content}
Draft a personalized email addressing the prospect's specific needs and pain points.
Focus on highlighting only relevant solutions from the company offerings.
Ensure the email is professional, engaging, and stays within {word_count} words (5-10% flexibility).
Output the email in HTML format.
"""
# Generate email using Llama
try:
chat_response = client.chat.completions.create(
messages=[{"role": "user", "content": prompt}],
model="llama3-8b-8192",
)
email_content = chat_response.choices[0].message.content
return email_content
except Exception as e:
return f"Error generating email: {str(e)}"
# Gradio Interface
def email_agent(name, linkedin_url, website_url, context_url, word_count):
return "<div style='font-style: italic;'>Hold on tight, your personalized email is on the way...</div>", generate_email(name, linkedin_url, website_url, context_url, word_count)
iface = gr.Interface(
fn=email_agent,
inputs=[
gr.Textbox(label="Prospect's Name"),
gr.Textbox(label="LinkedIn Profile URL"),
gr.Textbox(label="Publishing Website URL"),
gr.Textbox(label="Additional Context URL (optional)"),
gr.Slider(label="Email Length (words)", minimum=150, maximum=500, step=10, value=300),
],
outputs=[
gr.HTML(label="Status"),
gr.HTML(label="Generated Email")
],
title="Personalized Email Agent",
description="Generate highly personalized sales emails for AdTech prospects."
)
iface.launch(share=True)