siddhartharya commited on
Commit
5e249c4
·
verified ·
1 Parent(s): 4e099a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -30
app.py CHANGED
@@ -22,7 +22,7 @@ def fetch_linkedin_data(linkedin_url):
22
  response = requests.get(api_endpoint,
23
  params={'url': linkedin_url},
24
  headers=headers,
25
- timeout=10) # Adding a timeout for safety
26
  if response.status_code == 200:
27
  logging.info("LinkedIn data fetched successfully.")
28
  return response.json()
@@ -48,7 +48,7 @@ def fetch_company_info(company_url):
48
  }
49
 
50
  logging.info("Fetching company information...")
51
- response = requests.post(api_endpoint, json=data, headers=headers, timeout=15) # Adding a timeout for safety
52
  if response.status_code == 200:
53
  logging.info("Company information fetched successfully.")
54
  return response.json()
@@ -56,13 +56,45 @@ def fetch_company_info(company_url):
56
  logging.error(f"Error fetching company information: {response.text}")
57
  return {"error": f"Error fetching company information: {response.text}"}
58
 
59
- # Function to structure the email using the "Start with Why" model
60
  def structure_email(user_data, linkedin_info, company_info):
61
- 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')}."
62
- how = f"My skills in {user_data['role']} match the requirements and goals of your organization."
63
- what = f"I can bring my experience in {linkedin_info.get('skills', 'relevant skills')} to help achieve {company_info.get('goal', 'your company goals')}."
64
- structured_input = f"{why}\n\n{how}\n\n{what}"
65
- return structured_input
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  # Function to generate email content using Nvidia Nemotron LLM (non-streaming for simplicity)
68
  def generate_email_content(api_key, prompt):
@@ -84,7 +116,6 @@ def generate_email_content(api_key, prompt):
84
  stream=False # Disable streaming for simplicity
85
  )
86
 
87
- # Access the response content correctly based on the Nvidia API structure
88
  if hasattr(response, 'choices') and len(response.choices) > 0:
89
  email_content = response.choices[0].message.content
90
  logging.info("Email content generated successfully.")
@@ -97,21 +128,6 @@ def generate_email_content(api_key, prompt):
97
  logging.error(f"Error generating email content: {e}")
98
  return "Error generating email content."
99
 
100
- # Function to validate the generated email for professional tone and completeness
101
- def validate_email(email_content):
102
- logging.info("Validating email content...")
103
- logging.info(f"Email Content for Validation: {email_content}")
104
-
105
- # Adjust validation to check for structure rather than exact keywords
106
- if ("passionate" in email_content and
107
- "skills" in email_content and
108
- "experience" in email_content):
109
- logging.info("Email content validation passed.")
110
- return True
111
- else:
112
- logging.info("Email content validation failed.")
113
- return False
114
-
115
  # Custom Agent class to simulate behavior similar to OpenAI's Swarm framework
116
  class Agent:
117
  def __init__(self, name, instructions, user_data):
@@ -159,28 +175,23 @@ def run_agent(name, email, phone, linkedin_url, company_url, role):
159
  "role": role
160
  }
161
 
162
- # Create a Swarm and add the Data Collection Agent
163
  email_swarm = Swarm()
164
  data_collection_agent = Agent("Data Collection Agent", "Collect user inputs and relevant data", user_data)
165
  email_swarm.add_agent(data_collection_agent)
166
 
167
- # Get data from the Data Collection Agent
168
  linkedin_info, company_info = email_swarm.run()
169
- if isinstance(linkedin_info, str): # If an error message is returned
170
  return linkedin_info
171
 
172
- # Create a structured dictionary for the Email Generation Agent
173
  agent_data = {
174
  "user_data": user_data,
175
  "linkedin_info": linkedin_info,
176
  "company_info": company_info
177
  }
178
 
179
- # Pass the collected data to the Email Generation Agent
180
  email_agent = Agent("Email Generation Agent", "Generate the email content", agent_data)
181
  email_content = email_agent.act()
182
 
183
- # Validate and refine the email using a ReAct pattern with a maximum of 3 iterations
184
  for i in range(3):
185
  if validate_email(email_content):
186
  return email_content
 
22
  response = requests.get(api_endpoint,
23
  params={'url': linkedin_url},
24
  headers=headers,
25
+ timeout=10)
26
  if response.status_code == 200:
27
  logging.info("LinkedIn data fetched successfully.")
28
  return response.json()
 
48
  }
49
 
50
  logging.info("Fetching company information...")
51
+ response = requests.post(api_endpoint, json=data, headers=headers, timeout=15)
52
  if response.status_code == 200:
53
  logging.info("Company information fetched successfully.")
54
  return response.json()
 
56
  logging.error(f"Error fetching company information: {response.text}")
57
  return {"error": f"Error fetching company information: {response.text}"}
58
 
59
+ # Function to structure the email dynamically based on inputs and fetched data
60
  def structure_email(user_data, linkedin_info, company_info):
61
+ # Fetch relevant information from the LinkedIn profile and company information
62
+ linkedin_role = linkedin_info.get('current_role', 'a professional')
63
+ linkedin_skills = linkedin_info.get('skills', 'various relevant skills')
64
+ company_mission = company_info.get('mission', 'your mission')
65
+ company_goal = company_info.get('goal', 'achieving excellence in the field')
66
+
67
+ # Construct the dynamic email content based on the provided and fetched information
68
+ email_content = (
69
+ f"Dear Hiring Manager,\n\n"
70
+ f"I am writing to express my enthusiasm for the {user_data['role']} position at {user_data['company_url']}. "
71
+ f"The mission of {company_mission} resonates deeply with me, as my professional experience aligns closely with this vision.\n\n"
72
+ f"Having worked as {linkedin_role}, I have developed skills in {linkedin_skills}. These skills match the needs of your organization, "
73
+ f"and I am confident in my ability to contribute effectively to {company_goal}.\n\n"
74
+ f"I am eager to bring my expertise in {linkedin_skills} to your team, focusing on achieving key objectives and supporting your projects. "
75
+ f"My goal is to make a meaningful impact and collaborate with like-minded professionals committed to excellence.\n\n"
76
+ f"I would appreciate the opportunity to discuss how my background and skills align with the needs of {user_data['company_url']}. "
77
+ f"Please find my resume attached for a more detailed overview of my qualifications.\n\n"
78
+ f"Thank you for your time and consideration. I look forward to the possibility of contributing to your team.\n\n"
79
+ f"Best regards,\n"
80
+ f"{user_data['name']}"
81
+ )
82
+ return email_content
83
+
84
+ # Function to validate the generated email for professional tone and completeness
85
+ def validate_email(email_content):
86
+ logging.info("Validating email content...")
87
+ logging.info(f"Email Content for Validation: {email_content}")
88
+
89
+ # Check if the generated email contains essential elements
90
+ if ("enthusiasm" in email_content and
91
+ "skills" in email_content and
92
+ "contribute" in email_content):
93
+ logging.info("Email content validation passed.")
94
+ return True
95
+ else:
96
+ logging.info("Email content validation failed.")
97
+ return False
98
 
99
  # Function to generate email content using Nvidia Nemotron LLM (non-streaming for simplicity)
100
  def generate_email_content(api_key, prompt):
 
116
  stream=False # Disable streaming for simplicity
117
  )
118
 
 
119
  if hasattr(response, 'choices') and len(response.choices) > 0:
120
  email_content = response.choices[0].message.content
121
  logging.info("Email content generated successfully.")
 
128
  logging.error(f"Error generating email content: {e}")
129
  return "Error generating email content."
130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  # Custom Agent class to simulate behavior similar to OpenAI's Swarm framework
132
  class Agent:
133
  def __init__(self, name, instructions, user_data):
 
175
  "role": role
176
  }
177
 
 
178
  email_swarm = Swarm()
179
  data_collection_agent = Agent("Data Collection Agent", "Collect user inputs and relevant data", user_data)
180
  email_swarm.add_agent(data_collection_agent)
181
 
 
182
  linkedin_info, company_info = email_swarm.run()
183
+ if isinstance(linkedin_info, str):
184
  return linkedin_info
185
 
 
186
  agent_data = {
187
  "user_data": user_data,
188
  "linkedin_info": linkedin_info,
189
  "company_info": company_info
190
  }
191
 
 
192
  email_agent = Agent("Email Generation Agent", "Generate the email content", agent_data)
193
  email_content = email_agent.act()
194
 
 
195
  for i in range(3):
196
  if validate_email(email_content):
197
  return email_content