-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
387 lines (306 loc) · 14.2 KB
/
app.py
File metadata and controls
387 lines (306 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# LangGraph + LangChain + Tools
# High-level: Agent to login to WorkAtAStartup, fetch job links, extract job descriptions,
# embed + match to resume, generate optional cover letter, and log results.
import os
from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
import uuid
import asyncio
from langgraph.graph import StateGraph, END
from langchain_core.runnables import RunnableLambda
from sentence_transformers import SentenceTransformer, util
from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeoutError
from summarize import summarize_text
# === Graph State ===
from typing import TypedDict, List, Dict
class JobResult(TypedDict):
description: str
similarity: float
cover_letter: str | None
apply_decision: bool | None # User's decision to apply
applied: bool # Whether the application was successfully submitted
class AgentState(TypedDict):
user_id: str
no_jobs: int
username: str
password: str
resume_text: str
filter_url: str
job_links: list[str | None]
current_job_index: int
job_results: Dict[str, JobResult]
model = SentenceTransformer("all-MiniLM-L6-v2")
resume_embedding_store = {}
def embed_resume(
state: AgentState
) -> AgentState:
"""Embed the user's resume and store it by user_id."""
user_id = state["user_id"]
resume_text = state["resume_text"]
embedding = model.encode(resume_text, convert_to_tensor=True)
resume_embedding_store[user_id] = embedding
return state
async def get_job_links(state: AgentState) -> AgentState:
"""Log into WorkAtAStartup and return a list of job posting URLs."""
username = state["username"]
password = state["password"]
filter_url = state["filter_url"]
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
context = await browser.new_context()
# Block fonts/images for speed
await context.route("**/*", lambda route, request: (
route.abort() if request.resource_type in ["image", "font"] else route.continue_()
))
page = await context.new_page()
try:
print("🔐 Navigating to login...")
await page.goto("https://account.ycombinator.com/?continue=https%3A%2F%2Fwww.workatastartup.com%2F", wait_until= "domcontentloaded")
await page.fill('input[name="username"]', username)
await page.fill('input[name="password"]', password)
await page.click('button[type="submit"]')
print("✅ Login successful. Current URL:", page.url)
except Exception as e:
print(f"❌ Login flow failed: {e}")
await page.screenshot(path="login_error.png", full_page=True)
await browser.close()
return state
await page.wait_for_timeout(10000)
if page.url != filter_url:
print("➡️ Navigating to filter URL...")
try:
await page.goto(filter_url, timeout=60000)
except Exception as e:
print(f"❌ Failed to load filter URL: {e}")
await browser.close()
return state
else:
print("🟢 Already on filter URL.")
# Ensure job links are loaded
try:
await page.wait_for_selector("a:has-text('View Job')", timeout=10000)
except PlaywrightTimeoutError:
print("❌ No job links found.")
await browser.close()
return state
# Infinite scroll
print("📜 Scrolling to load job listings...")
for _ in range(30):
prev_height = await page.evaluate("document.body.scrollHeight")
await page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
await page.wait_for_timeout(1000)
new_height = await page.evaluate("document.body.scrollHeight")
if new_height == prev_height:
break
anchors = await page.locator("a:has-text('View Job')").all()
hrefs = [await a.get_attribute("href") for a in anchors if await a.get_attribute("href")]
await browser.close()
state["job_links"] = hrefs[:state["no_jobs"]] # or more
print(f"✅ Found {len(state['job_links'])} job links:", state["job_links"])
return state
async def get_job_description(job_url: str) -> str:
"""Fetch the job description from a specific job page using custom classnames."""
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
page = await browser.new_page()
try:
await page.goto(job_url, timeout=45000)
await page.wait_for_timeout(2000)
# Extract top title (e.g., company name or job title)
title = await page.locator(".company-title").all_inner_texts()
title_text = "\n".join(title).strip()
# Extract repeated section + prose blocks
sections = await page.locator(".company-section").all_inner_texts()
contents = await page.locator(".prose").all_inner_texts()
# Combine section titles and prose content
job_description_parts = []
for sec, con in zip(sections, contents):
job_description_parts.append(f"## {sec.strip()}\n{con.strip()}")
full_description = f"# {title_text}\n\n" + "\n\n".join(job_description_parts)
return full_description
except Exception as e:
await page.screenshot(path="job_error.png")
return f"Error scraping job description: {str(e)}"
finally:
await browser.close()
def compare_job_with_resume(job_text: str, user_id: str) -> float:
"""Compare each job with resume using Cosine similarity."""
if user_id not in resume_embedding_store:
return 0.0
job_embedding = model.encode(job_text, convert_to_tensor=True)
similarity = util.cos_sim(resume_embedding_store[user_id], job_embedding).item()
return similarity
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4.1-nano")
def generate_cover_letter(resume: str, job_desc: str) -> str:
"""Generate cover letter using OpenAI API."""
prompt = f"""
Based on the following resume:
{resume}
Write a tailored cover letter of around 200 words for this job description:
{job_desc}
"""
cover_letter = llm.invoke(prompt).content
return str(cover_letter)
async def fetch_all_job_descriptions(state: AgentState) -> AgentState:
descriptions = {}
for url in state["job_links"]:
if url is None:
continue
desc = await get_job_description(job_url=url)
descriptions[url] = {"description": desc}
state["job_results"] = descriptions
return state
def compare_all_jobs(state: AgentState) -> AgentState:
for url, entry in state["job_results"].items():
desc = entry["description"]
similarity = compare_job_with_resume(
job_text=desc,
user_id=state["user_id"]
)
entry["similarity"] = similarity
return state
def generate_all_cover_letters(state: AgentState) -> AgentState:
for url, entry in state["job_results"].items():
if entry["similarity"] > 0.4:
print(f"📝 Generating cover letter for {url} (similarity: {entry['similarity']:.2f})")
summarized_resume = summarize_text(state["resume_text"], 10)
summarized_job_desc = summarize_text(entry["description"], 10)
cover_letter = generate_cover_letter(
resume=summarized_resume,
job_desc=summarized_job_desc
)
entry["cover_letter"] = cover_letter
entry["apply_decision"] = None
entry["applied"] = False
else:
print(f"❌ Skipping {url} (similarity: {entry['similarity']:.2f}) — not a good match")
entry["cover_letter"] = "Not a good match"
entry["apply_decision"] = False
entry["applied"] = False
return state
async def should_apply_to_job(state: AgentState) -> AgentState:
for url, entry in state["job_results"].items():
if entry.get("apply_decision") is None and entry.get("cover_letter") != "Not a good match":
print(f"\n🔗 Job URL: {url}")
print(f"\n📄 Cover Letter:\n{entry['cover_letter']}\n")
while True:
user_input = input("👉 Do you want to apply to this job? (y/n): ").strip().lower()
if user_input in ["y", "yes"]:
entry["apply_decision"] = True
break
elif user_input in ["n", "no"]:
entry["apply_decision"] = False
break
else:
print("⚠️ Please enter 'y' or 'n'.")
return state
async def auto_apply_to_job(state: AgentState) -> AgentState:
username = state["username"]
password = state["password"]
job_results = state["job_results"]
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
context = await browser.new_context()
page = await context.new_page()
# Block fonts/images for speed
await page.route("**/*", lambda route, request: (
route.abort() if request.resource_type in ["image", "font"] else route.continue_()
))
try:
print("🔐 Navigating to login...")
await page.goto("https://account.ycombinator.com/?continue=https%3A%2F%2Fwww.workatastartup.com%2F", wait_until= "domcontentloaded")
await page.fill('input[name="username"]', username)
await page.fill('input[name="password"]', password)
await page.click('button[type="submit"]')
print("✅ Login successful. Current URL:", page.url)
except Exception as e:
print(f"❌ Login flow failed: {e}")
await page.screenshot(path="login_error.png", full_page=True)
await browser.close()
return state
await page.wait_for_timeout(10000)
for url, entry in job_results.items():
if entry.get("apply_decision") is not True:
continue
print(f"🚀 Applying to job: {url}")
if page.url != url:
try:
await page.goto(url, timeout=10000)
await page.wait_for_selector("text=Apply", timeout=10000)
await page.click("text=Apply")
# Wait for modal with textarea and Send button
await page.wait_for_selector("textarea", timeout=10000)
await page.fill("textarea", str(entry["cover_letter"]))
await page.click("button:has-text('Send')")
await page.wait_for_timeout(5000) # Wait for application to process
entry["applied"] = True
print("✅ Successfully applied.")
except PlaywrightTimeoutError:
print(f"❌ Failed to apply to job: {url}")
entry["applied"] = False
except Exception as e:
print(f"⚠️ Unexpected error for {url}: {e}")
entry["applied"] = False
else:
print(f"🟢 Already on job page: {url}")
await browser.close()
return state
# === Graph Definition ===
workflow = StateGraph(AgentState)
workflow.add_node("EmbedResume", RunnableLambda(embed_resume))
workflow.add_node("GetJobLinks", RunnableLambda(get_job_links))
workflow.add_node("FetchJobDesc", RunnableLambda(fetch_all_job_descriptions))
workflow.add_node("Compare", RunnableLambda(compare_all_jobs))
workflow.add_node("GenCover", RunnableLambda(generate_all_cover_letters))
workflow.add_node("ShouldApplyToJob", RunnableLambda(should_apply_to_job))
workflow.add_node("AutoApplyToJob", RunnableLambda(auto_apply_to_job))
workflow.set_entry_point("EmbedResume")
workflow.add_edge("EmbedResume", "GetJobLinks")
workflow.add_edge("GetJobLinks", "FetchJobDesc")
workflow.add_edge("FetchJobDesc", "Compare")
workflow.add_edge("Compare", "GenCover")
workflow.add_edge("GenCover", "ShouldApplyToJob")
workflow.add_edge("ShouldApplyToJob", "AutoApplyToJob")
workflow.add_edge("AutoApplyToJob", END)
graph = workflow.compile()
async def run_graph(pdf_text, user_id, username, password, filter_url, no_jobs):
result = await graph.ainvoke({
"user_id": user_id,
"no_jobs": no_jobs,
"username": username,
"password": password,
"resume_text": pdf_text,
"filter_url": filter_url,
"current_job_index": 0,
"job_results": {}
})
print("✅ Graph execution completed successfully!")
for idx, (url, job) in enumerate(result['job_results'].items(), 1):
print(f"\nJob {idx}:")
print(f"URL: {url}")
print(f"Similarity Score: {job.get('similarity', 'N/A')}")
print(f"Applied: {'Yes' if job.get('applied', False) else 'No'}")
print("Cover Letter:")
print(job.get('cover_letter', 'No cover letter generated'))
print("-" * 40)
if __name__ == "__main__":
import getpass
from read_pdf import read_pdf
username = input("Enter your ycombinator username: ")
password = getpass.getpass("Enter your ycombinator password: ")
filter_url = input("Enter the Job Portal URL (e.g., https://www.workatastartup.com/companies?demographic=any&hasEquity=any&hasSalary=any&industry=any&interviewProcess=any&jobType=fulltime&layout=list-compact&minExperience=0&minExperience=1&remote=yes&role=eng&role_type=fe&role_type=fs&sortBy=created_desc&tab=any&usVisaNotRequired=any): ")
resume_file = input("Enter your resume file name (e.g., resume.pdf): ")
while True:
try:
no_jobs = int(input("Enter the number of jobs to fetch (max 10, default 10): ") or 10)
if no_jobs <= 10:
break
else:
print("Please enter a number less than or equal to 10.")
except ValueError:
print("Please enter a valid integer.")
user_id = str(uuid.uuid4())
pdf_text = read_pdf(resume_file)
asyncio.run(run_graph(pdf_text, user_id, username, password, filter_url, no_jobs))