Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from groq import Groq
|
| 6 |
+
|
| 7 |
+
# Initialize Groq client
|
| 8 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 9 |
+
|
| 10 |
+
# Function to extract content from a URL
|
| 11 |
+
def extract_content(url):
|
| 12 |
+
try:
|
| 13 |
+
response = requests.get(url, timeout=10)
|
| 14 |
+
response.raise_for_status()
|
| 15 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 16 |
+
paragraphs = soup.find_all('p')
|
| 17 |
+
content = ' '.join([para.get_text() for para in paragraphs])
|
| 18 |
+
return content[:2000] # Limit content to 2000 characters to avoid overload
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return f"Error extracting content from {url}: {str(e)}"
|
| 21 |
+
|
| 22 |
+
# Function to fetch LinkedIn profile insights using Proxycurl API
|
| 23 |
+
def fetch_linkedin_insights(profile_url):
|
| 24 |
+
api_key = os.environ.get("PROXYCURL_API_KEY")
|
| 25 |
+
api_endpoint = "https://nubela.co/proxycurl/api/v2/linkedin"
|
| 26 |
+
headers = {"Authorization": f"Bearer {api_key}"}
|
| 27 |
+
params = {"url": profile_url, "fallback_to_cache": "on-error"}
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
response = requests.get(api_endpoint, headers=headers, params=params, timeout=10)
|
| 31 |
+
response.raise_for_status()
|
| 32 |
+
profile_data = response.json()
|
| 33 |
+
insights = f"{profile_data.get('headline', '')}. {profile_data.get('summary', '')}"
|
| 34 |
+
return insights
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"Error fetching LinkedIn insights: {str(e)}"
|
| 37 |
+
|
| 38 |
+
# Function to generate email using Llama
|
| 39 |
+
def generate_email(name, linkedin_url, website_url, context_url, word_count):
|
| 40 |
+
# Fetch insights from LinkedIn and reference URLs
|
| 41 |
+
linkedin_insights = fetch_linkedin_insights(linkedin_url)
|
| 42 |
+
website_content = extract_content(website_url)
|
| 43 |
+
context_content = extract_content(context_url) if context_url else ""
|
| 44 |
+
|
| 45 |
+
# Fetch details from AdTech company website
|
| 46 |
+
adtech_content = extract_content("https://www.abcd.com")
|
| 47 |
+
|
| 48 |
+
# Construct the prompt for Llama
|
| 49 |
+
prompt = f"""
|
| 50 |
+
You are an AI assistant helping an AdTech company draft personalized sales emails.
|
| 51 |
+
Here are the details of the prospect:
|
| 52 |
+
- Name: {name}
|
| 53 |
+
- LinkedIn Insights: {linkedin_insights}
|
| 54 |
+
- Website Content: {website_content}
|
| 55 |
+
- Additional Context: {context_content}
|
| 56 |
+
|
| 57 |
+
The company provides the following offerings:
|
| 58 |
+
{adtech_content}
|
| 59 |
+
|
| 60 |
+
Draft a personalized email addressing the prospect's specific needs and pain points.
|
| 61 |
+
Focus on highlighting only relevant solutions from the company offerings.
|
| 62 |
+
Ensure the email is professional, engaging, and stays within {word_count} words (5-10% flexibility).
|
| 63 |
+
Output the email in HTML format.
|
| 64 |
+
"""
|
| 65 |
+
|
| 66 |
+
# Generate email using Llama
|
| 67 |
+
try:
|
| 68 |
+
chat_response = client.chat.completions.create(
|
| 69 |
+
messages=[{"role": "user", "content": prompt}],
|
| 70 |
+
model="llama3-8b-8192",
|
| 71 |
+
)
|
| 72 |
+
email_content = chat_response.choices[0].message.content
|
| 73 |
+
return email_content
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return f"Error generating email: {str(e)}"
|
| 76 |
+
|
| 77 |
+
# Gradio Interface
|
| 78 |
+
def email_agent(name, linkedin_url, website_url, context_url, word_count):
|
| 79 |
+
return generate_email(name, linkedin_url, website_url, context_url, word_count)
|
| 80 |
+
|
| 81 |
+
iface = gr.Interface(
|
| 82 |
+
fn=email_agent,
|
| 83 |
+
inputs=[
|
| 84 |
+
gr.Textbox(label="Prospect's Name"),
|
| 85 |
+
gr.Textbox(label="LinkedIn Profile URL"),
|
| 86 |
+
gr.Textbox(label="Publishing Website URL"),
|
| 87 |
+
gr.Textbox(label="Additional Context URL (optional)"),
|
| 88 |
+
gr.Slider(label="Email Length (words)", minimum=150, maximum=500, step=10, value=300),
|
| 89 |
+
],
|
| 90 |
+
outputs="html",
|
| 91 |
+
title="Personalized Email Agent",
|
| 92 |
+
description="Generate highly personalized sales emails for AdTech prospects.",
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
iface.launch(share=True)
|