siddhartharya
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
import openai
|
4 |
+
import gradio as gr
|
5 |
+
from openai_swarm import Agent, Swarm
|
6 |
+
|
7 |
+
# Fetch API keys from environment variables
|
8 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
9 |
+
PROXYCURL_API_KEY = os.getenv("PROXYCURL_API_KEY")
|
10 |
+
FIRECRAWL_API_KEY = os.getenv("FIRECRAWL_API_KEY")
|
11 |
+
|
12 |
+
# Function to fetch LinkedIn data using Proxycurl API
|
13 |
+
def fetch_linkedin_data(linkedin_url):
|
14 |
+
headers = {'Authorization': f'Bearer {PROXYCURL_API_KEY}'}
|
15 |
+
response = requests.get(f"https://api.proxycurl.com/v1/linkedin/{linkedin_url}", headers=headers)
|
16 |
+
if response.status_code == 200:
|
17 |
+
return response.json()
|
18 |
+
else:
|
19 |
+
return {"error": "Unable to fetch LinkedIn data"}
|
20 |
+
|
21 |
+
# Function to fetch company information using Firecrawl API
|
22 |
+
def fetch_company_info(company_url):
|
23 |
+
headers = {'Authorization': f'Bearer {FIRECRAWL_API_KEY}'}
|
24 |
+
response = requests.get(f"https://api.firecrawl.com/v1/scrape?url={company_url}", headers=headers)
|
25 |
+
if response.status_code == 200:
|
26 |
+
return response.json()
|
27 |
+
else:
|
28 |
+
return {"error": "Unable to fetch company information"}
|
29 |
+
|
30 |
+
# Function to structure the email using the "Start with Why" model
|
31 |
+
def structure_email(user_data, linkedin_info, company_info):
|
32 |
+
why = f"I am passionate about {company_info.get('mission', 'your mission')} because it aligns with my experience as {linkedin_info.get('current_role', 'a professional')}."
|
33 |
+
how = f"My skills in {user_data['role']} match the requirements and goals of your organization."
|
34 |
+
what = f"I can bring my experience in {linkedin_info.get('skills', 'relevant skills')} to help achieve {company_info.get('goal', 'your company goals')}."
|
35 |
+
structured_input = f"{why}\n\n{how}\n\n{what}"
|
36 |
+
return structured_input
|
37 |
+
|
38 |
+
# Function to generate email content using Nvidia Nemotron LLM
|
39 |
+
def generate_email_content(api_key, prompt):
|
40 |
+
openai.api_key = api_key
|
41 |
+
response = openai.Completion.create(
|
42 |
+
model="nemotron-70b",
|
43 |
+
prompt=prompt,
|
44 |
+
max_tokens=500
|
45 |
+
)
|
46 |
+
return response.choices[0].text
|
47 |
+
|
48 |
+
# Function to validate the generated email for professional tone and completeness
|
49 |
+
def validate_email(email_content):
|
50 |
+
# Basic validation: check if the email contains key sections (can be expanded)
|
51 |
+
return "Why" in email_content and "How" in email_content and "What" in email_content
|
52 |
+
|
53 |
+
# Define the ReAct Agent using Swarm framework
|
54 |
+
class EmailAgent(Agent):
|
55 |
+
def __init__(self, user_data):
|
56 |
+
super().__init__()
|
57 |
+
self.user_data = user_data
|
58 |
+
self.iterations = 0
|
59 |
+
|
60 |
+
def act(self):
|
61 |
+
linkedin_info = fetch_linkedin_data(self.user_data['linkedin_url'])
|
62 |
+
company_info = fetch_company_info(self.user_data['company_url'])
|
63 |
+
if "error" in linkedin_info or "error" in company_info:
|
64 |
+
return "Error fetching data. Please check the LinkedIn and company URLs."
|
65 |
+
|
66 |
+
prompt = structure_email(self.user_data, linkedin_info, company_info)
|
67 |
+
email_content = generate_email_content(OPENAI_API_KEY, prompt)
|
68 |
+
return email_content
|
69 |
+
|
70 |
+
def react(self, output):
|
71 |
+
# React based on the generated email's validity
|
72 |
+
if validate_email(output):
|
73 |
+
return output
|
74 |
+
else:
|
75 |
+
# If invalid, refine prompt or retry (up to 3 iterations)
|
76 |
+
self.iterations += 1
|
77 |
+
if self.iterations < 3:
|
78 |
+
refined_prompt = f"Refined: {output}"
|
79 |
+
return generate_email_content(OPENAI_API_KEY, refined_prompt)
|
80 |
+
else:
|
81 |
+
return "Unable to generate a valid email after 3 attempts."
|
82 |
+
|
83 |
+
# Define the main function to run the agent within the Swarm
|
84 |
+
def run_agent(name, email, phone, linkedin_url, company_url, role):
|
85 |
+
user_data = {
|
86 |
+
"name": name,
|
87 |
+
"email": email,
|
88 |
+
"phone": phone,
|
89 |
+
"linkedin_url": linkedin_url,
|
90 |
+
"company_url": company_url,
|
91 |
+
"role": role
|
92 |
+
}
|
93 |
+
|
94 |
+
# Initialize the swarm and add the EmailAgent
|
95 |
+
email_swarm = Swarm()
|
96 |
+
agent = EmailAgent(user_data)
|
97 |
+
email_swarm.add_agent(agent)
|
98 |
+
|
99 |
+
# Run the swarm to get the final email content
|
100 |
+
result = email_swarm.run()
|
101 |
+
return result
|
102 |
+
|
103 |
+
# Set up the Gradio interface
|
104 |
+
final_interface = gr.Interface(
|
105 |
+
fn=run_agent,
|
106 |
+
inputs=[
|
107 |
+
gr.Textbox(label="Name"),
|
108 |
+
gr.Textbox(label="Email"),
|
109 |
+
gr.Textbox(label="Phone Number"),
|
110 |
+
gr.Textbox(label="LinkedIn Profile URL"),
|
111 |
+
gr.Textbox(label="Company URL or Name"),
|
112 |
+
gr.Textbox(label="Role Being Applied For")
|
113 |
+
],
|
114 |
+
outputs="text",
|
115 |
+
title="Email Writing AI Agent",
|
116 |
+
description="Autonomously generate a professional email tailored to the job application."
|
117 |
+
)
|
118 |
+
|
119 |
+
if __name__ == "__main__":
|
120 |
+
final_interface.launch()
|