siddhartharya commited on
Commit
b020659
·
verified ·
1 Parent(s): b682b3b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -93
app.py CHANGED
@@ -69,10 +69,17 @@ class AutonomousEmailAgent:
69
  if response.status_code == 200:
70
  try:
71
  result = response.json() # Parse the response as JSON
 
 
72
  # Check if 'choices' and the content are correctly structured in the response
73
- content = "".join([chunk.get("choices", [{}])[0].get("message", {}).get("content", "") for chunk in result.get("choices", [])])
74
- print(content)
75
- return self.act_on_llm_instructions(content)
 
 
 
 
 
76
  except json.JSONDecodeError:
77
  print("Error: Response from Groq Cloud LLM is not valid JSON.")
78
  return "Error: Response is not in JSON format."
@@ -82,6 +89,7 @@ class AutonomousEmailAgent:
82
 
83
  # Function to act on the LLM's structured instructions
84
  def act_on_llm_instructions(self, reasoning_output):
 
85
  instruction = reasoning_output.lower().strip()
86
 
87
  if "scrape" in instruction:
@@ -105,103 +113,15 @@ class AutonomousEmailAgent:
105
  print("Error: Unrecognized instruction from LLM. Proceeding with available data.")
106
  return self.generate_email()
107
 
108
- # Fetch company URL using SERP API
109
- def fetch_company_url(self):
110
- serp_api_key = os.getenv("SERP_API_KEY")
111
- print(f"Fetching company URL for {self.company_name} using SERP API...")
112
- serp_url = f"https://serpapi.com/search.json?q={self.company_name}&api_key={serp_api_key}&num=1"
113
- response = requests.get(serp_url)
114
-
115
- if response.status_code == 200:
116
- serp_data = response.json()
117
- if 'organic_results' in serp_data and len(serp_data['organic_results']) > 0:
118
- self.company_url = serp_data['organic_results'][0]['link']
119
- print(f"Found company URL: {self.company_url}")
120
- else:
121
- print("No URL found for the company via SERP API.")
122
- self.company_url = None
123
- else:
124
- print(f"Error fetching company URL: {response.status_code}")
125
-
126
- # Fetch LinkedIn data via Proxycurl
127
- def fetch_linkedin_data(self):
128
- proxycurl_api_key = os.getenv("PROXYCURL_API_KEY")
129
- if not self.linkedin_url:
130
- print("Action: No LinkedIn URL provided, using default bio.")
131
- self.bio = "A professional with diverse experience."
132
- self.skills = ["Adaptable", "Hardworking"]
133
- self.experiences = ["Worked across various industries"]
134
- else:
135
- print("Action: Fetching LinkedIn data via Proxycurl.")
136
- headers = {"Authorization": f"Bearer {proxycurl_api_key}"}
137
- url = f"https://nubela.co/proxycurl/api/v2/linkedin?url={self.linkedin_url}"
138
- response = requests.get(url, headers=headers)
139
- if response.status_code == 200:
140
- data = response.json()
141
- self.bio = data.get("summary", "No bio available")
142
- self.skills = data.get("skills", [])
143
- self.experiences = data.get("experiences", [])
144
- else:
145
- print("Error: Unable to fetch LinkedIn profile. Using default bio.")
146
- self.bio = "A professional with diverse experience."
147
- self.skills = ["Adaptable", "Hardworking"]
148
- self.experiences = ["Worked across various industries"]
149
-
150
- # Fetch company information via Firecrawl API using company URL
151
- def fetch_company_info_with_firecrawl(self, company_url):
152
- firecrawl_api_key = os.getenv("FIRECRAWL_API_KEY")
153
- print(f"Fetching company info for {company_url} using Firecrawl.")
154
- headers = {"Authorization": f"Bearer {firecrawl_api_key}"}
155
- firecrawl_url = "https://api.firecrawl.dev/v1/scrape"
156
- data = {"url": company_url, "patterns": ["description", "about", "careers", "company overview"]}
157
-
158
- response = requests.post(firecrawl_url, json=data, headers=headers)
159
- if response.status_code == 200:
160
- firecrawl_data = response.json()
161
- self.company_info = firecrawl_data.get("description", "No detailed company info available.")
162
- print(f"Company info fetched: {self.company_info}")
163
- else:
164
- print(f"Error: Unable to fetch company info via Firecrawl. Status code: {response.status_code}")
165
- self.company_info = "A leading company in its field."
166
-
167
- # Final Action: Generate the email using Groq Cloud LLM
168
- def generate_email(self):
169
- print("Action: Generating the email using Groq Cloud LLM with the gathered information.")
170
-
171
- prompt = f"""
172
- Write a professional job application email applying for the {self.role} position at {self.company_name}.
173
-
174
- The email should follow the "Start with Why" approach:
175
- 1. **Why**: Explain why the candidate is passionate about this role and company.
176
- 2. **How**: Highlight the candidate’s skills and experiences.
177
- 3. **What**: Provide examples of past achievements.
178
- 4. **Call to Action**: Request a meeting or discussion.
179
-
180
- - LinkedIn bio: {self.bio}
181
- - Skills: {', '.join(self.skills)}
182
- - Experience: {', '.join([exp['title'] for exp in self.experiences])}
183
- - Company information: {self.company_info}
184
-
185
- Signature:
186
- Best regards,
187
- {self.user_name}
188
- Email: {self.email}
189
- Phone: {self.phone}
190
- LinkedIn: {self.linkedin}
191
-
192
- Limit the email to {self.word_limit} words.
193
- """
194
-
195
- return self.send_request_to_llm(prompt)
196
 
197
  # Main loop following ReAct pattern
198
  def run(self):
199
  self.fetch_linkedin_data()
200
  return self.autonomous_reasoning()
201
 
202
- # Gradio UI setup remains the same
203
  def gradio_ui():
204
- # Input fields
205
  name_input = gr.Textbox(label="Your Name", placeholder="Enter your name")
206
  company_input = gr.Textbox(label="Company Name or URL", placeholder="Enter the company name or website URL")
207
  role_input = gr.Textbox(label="Role Applying For", placeholder="Enter the role you are applying for")
 
69
  if response.status_code == 200:
70
  try:
71
  result = response.json() # Parse the response as JSON
72
+ print(f"LLM Response: {json.dumps(result, indent=2)}") # Print the full response for debugging
73
+
74
  # Check if 'choices' and the content are correctly structured in the response
75
+ choices = result.get("choices", [])
76
+ if choices and "message" in choices[0]:
77
+ content = choices[0]["message"]["content"]
78
+ print(f"Content: {content}")
79
+ return self.act_on_llm_instructions(content)
80
+ else:
81
+ print("Error: Unrecognized format in LLM response.")
82
+ return "Error: Unrecognized response format."
83
  except json.JSONDecodeError:
84
  print("Error: Response from Groq Cloud LLM is not valid JSON.")
85
  return "Error: Response is not in JSON format."
 
89
 
90
  # Function to act on the LLM's structured instructions
91
  def act_on_llm_instructions(self, reasoning_output):
92
+ print(f"LLM Instruction: {reasoning_output}") # Print the LLM's instruction for debugging
93
  instruction = reasoning_output.lower().strip()
94
 
95
  if "scrape" in instruction:
 
113
  print("Error: Unrecognized instruction from LLM. Proceeding with available data.")
114
  return self.generate_email()
115
 
116
+ # Other methods (fetch_linkedin_data, fetch_company_url, fetch_company_info_with_firecrawl, generate_email) remain unchanged...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
  # Main loop following ReAct pattern
119
  def run(self):
120
  self.fetch_linkedin_data()
121
  return self.autonomous_reasoning()
122
 
123
+ # Gradio UI setup remains the same as before
124
  def gradio_ui():
 
125
  name_input = gr.Textbox(label="Your Name", placeholder="Enter your name")
126
  company_input = gr.Textbox(label="Company Name or URL", placeholder="Enter the company name or website URL")
127
  role_input = gr.Textbox(label="Role Applying For", placeholder="Enter the role you are applying for")