-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
686 lines (578 loc) · 23.3 KB
/
Copy pathapp.py
File metadata and controls
686 lines (578 loc) · 23.3 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
"""
LLMployable - Resume Builder Application
This application scrapes LinkedIn and GitHub profiles, analyzes job descriptions,
and generates tailored resumes as PDFs using LaTeX.
"""
from flask import Flask, request, jsonify, send_file, render_template_string
from flask_cors import CORS
import os
import os.path
import uuid
from dotenv import load_dotenv
# Import our modules
from database import init_db
from database.repositories import UserRepository, ResumeRepository, JobApplicationRepository
from scrapers.github_scraper import GitHubScraper
from scrapers.linkedin_scraper import LinkedInScraper
from analyzer.job_analyzer import JobAnalyzer
from analyzer.interview_generator import InterviewGenerator
from generator.resume_generator import ResumeGenerator
from generator.latex_compiler import LaTeXCompiler
from config.logging_config import setup_logging
from config.config import Config
# Load environment variables
load_dotenv()
Config.from_env()
# Base directory of the application
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
app = Flask(__name__)
CORS(app)
# Setup logging
logger = setup_logging(__name__)
api_logger = setup_logging("api")
# Initialize database
init_db()
# Initialize components
github_scraper = GitHubScraper()
linkedin_scraper = LinkedInScraper()
job_analyzer = JobAnalyzer()
interview_generator = InterviewGenerator()
resume_generator = ResumeGenerator()
latex_compiler = LaTeXCompiler()
# Middleware for request tracking
@app.before_request
def before_request():
"""Track request metadata"""
# Use str(uuid.uuid4())[:8] as a simple request ID
try:
request.request_id = str(uuid.uuid4())[:8]
except Exception:
pass
@app.after_request
def after_request(response):
"""Log response details"""
rid = "unknown"
try:
rid = getattr(request, "request_id", "unknown")
except Exception:
pass
api_logger.info(f"[{rid}] {request.method} {request.path} {response.status_code}")
return response
# Error handlers
@app.errorhandler(Exception)
def handle_exception(e):
"""Handle all unhandled exceptions"""
rid = "unknown"
try:
rid = getattr(request, "request_id", "unknown")
except Exception:
pass
logger.error(f"[{rid}] Unhandled exception: {str(e)}", exc_info=True)
return jsonify({
"error": "Internal Server Error",
"message": str(e)
}), 500
# Simple HTML interface
HTML_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<title>LLMployable - AI Resume Builder</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 900px;
margin: 0 auto;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
}
.container {
background: white;
border-radius: 10px;
padding: 30px;
box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}
h1 {
color: #333;
text-align: center;
margin-bottom: 10px;
}
.subtitle {
text-align: center;
color: #666;
margin-bottom: 30px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #333;
}
input[type="text"], textarea {
width: 100%;
padding: 10px;
border: 2px solid #ddd;
border-radius: 5px;
font-size: 14px;
box-sizing: border-box;
}
textarea {
min-height: 150px;
resize: vertical;
}
button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 12px 30px;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
width: 100%;
font-weight: bold;
}
button:hover {
opacity: 0.9;
}
button:disabled {
background: #ccc;
cursor: not-allowed;
}
.status {
margin-top: 20px;
padding: 15px;
border-radius: 5px;
display: none;
}
.status.info {
background: #e3f2fd;
color: #1565c0;
display: block;
}
.status.success {
background: #e8f5e9;
color: #2e7d32;
display: block;
}
.status.error {
background: #ffebee;
color: #c62828;
display: block;
}
.example {
font-size: 12px;
color: #666;
margin-top: 5px;
}
</style>
</head>
<body>
<div class="container">
<h1>🚀 LLMployable</h1>
<p class="subtitle">AI-Powered Resume Builder for Hackathons & Job Applications</p>
<form id="resumeForm">
<div class="form-group">
<label for="github">GitHub Username:</label>
<input type="text" id="github" name="github" placeholder="octocat">
<div class="example">Example: torvalds, gvanrossum</div>
</div>
<div class="form-group">
<label for="linkedinFile">Upload LinkedIn Data (ZIP):</label>
<input type="file" id="linkedinFile" name="linkedinFile" accept=".zip">
<div class="example">Settings > Data Privacy > Get a copy of your data > Download ZIP</div>
</div>
<div class="form-group">
<label for="jobDescription">Job Description:</label>
<textarea id="jobDescription" name="jobDescription" placeholder="Paste the job description here..."></textarea>
<div class="example">Paste the full job description including requirements and responsibilities</div>
</div>
<button type="submit" id="submitBtn">Generate Tailored Resume</button>
</form>
<div id="status" class="status"></div>
</div>
<script>
document.getElementById('resumeForm').addEventListener('submit', async (e) => {
e.preventDefault();
const submitBtn = document.getElementById('submitBtn');
const status = document.getElementById('status');
const github = document.getElementById('github').value;
const linkedinFile = document.getElementById('linkedinFile').files[0];
const jobDescription = document.getElementById('jobDescription').value;
if (!github && !linkedinFile) {
status.className = 'status error';
status.textContent = 'Please provide at least one profile (GitHub username or LinkedIn Data Export)';
return;
}
if (!jobDescription) {
status.className = 'status error';
status.textContent = 'Please provide a job description';
return;
}
submitBtn.disabled = true;
submitBtn.textContent = 'Generating Resume...';
status.className = 'status info';
status.textContent = '⏳ Scraping profiles and analyzing job description...';
try {
let response;
if (linkedinFile) {
const formData = new FormData();
formData.append('github_username', github);
formData.append('job_description', jobDescription);
formData.append('linkedin_data', linkedinFile);
response = await fetch('/api/generate-resume', {
method: 'POST',
body: formData
});
} else {
response = await fetch('/api/generate-resume', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
github_username: github,
job_description: jobDescription
})
});
}
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || 'Failed to generate resume');
}
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'tailored_resume.pdf';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
status.className = 'status success';
status.textContent = '✅ Resume generated successfully! Check your downloads.';
} catch (error) {
status.className = 'status error';
status.textContent = '❌ Error: ' + error.message;
} finally {
submitBtn.disabled = false;
submitBtn.textContent = 'Generate Tailored Resume';
}
});
</script>
</body>
</html>
"""
@app.route("/")
def index():
"""Serve the main HTML interface"""
return render_template_string(HTML_TEMPLATE)
@app.route("/api/generate-resume", methods=["POST"])
@app.route("/api/v1/generate-resume", methods=["POST"])
def generate_resume():
"""
Main endpoint to generate a tailored resume
Expected payload:
- JSON: {"github_username": "...", "job_description": "..."}
- OR Multipart: form fields github_username, job_description AND optional file linkedin_data
"""
rid = getattr(request, "request_id", "unknown")
try:
linkedin_file = None
user_id = None
content_type = request.content_type or ""
if "multipart/form-data" in content_type:
github_username = request.form.get("github_username", "").strip()
job_description = request.form.get("job_description", "").strip()
linkedin_file = request.files.get("linkedin_data")
user_id = request.form.get("user_id")
else:
data = request.json
github_username = data.get("github_username", "").strip()
job_description = data.get("job_description", "").strip()
user_id = data.get("user_id")
logger.info(f"[{rid}] Resume generation request for user: {user_id}")
if not job_description:
return jsonify({"error": "Job description is required"}), 400
if not github_username and not linkedin_file:
return (
jsonify({"error": "At least one profile or data export is required"}),
400,
)
# Step 1: Scrape profiles
profile_data = {}
if github_username:
logger.debug(f"[{rid}] Scraping GitHub: {github_username}")
github_data = github_scraper.scrape_profile(github_username)
profile_data["github"] = github_data
# LinkedIn Data Handling (File Export or URL)
if linkedin_file and linkedin_file.filename:
logger.debug(f"[{rid}] Processing LinkedIn file")
# Save file temporarily
upload_dir = "uploads"
os.makedirs(upload_dir, exist_ok=True)
temp_filename = f"{uuid.uuid4()}_{linkedin_file.filename}"
file_path = os.path.join(upload_dir, temp_filename)
linkedin_file.save(file_path)
linkedin_data = linkedin_scraper.parse_export(file_path)
profile_data["linkedin"] = linkedin_data
# Cleanup
if os.path.exists(file_path):
os.remove(file_path)
# Step 2: Analyze job description
logger.debug(f"[{rid}] Analyzing job description")
job_requirements = job_analyzer.analyze(job_description)
# Step 3: Refine GitHub projects based on job requirements
if "github" in profile_data and "repositories" in profile_data["github"]:
job_skills = job_requirements.get("skills", {})
relevant_projects = github_scraper.select_relevant_projects(
profile_data["github"]["repositories"], job_skills
)
profile_data["github"]["top_projects"] = relevant_projects
# Step 4: Generate tailored resume content
logger.debug(f"[{rid}] Generating tailored content")
resume_content = resume_generator.generate(profile_data, job_requirements, user_id=user_id)
# Step 5: Compile to PDF
logger.debug(f"[{rid}] Compiling LaTeX to PDF")
pdf_path = latex_compiler.compile(resume_content)
# Step 6: Move PDF to permanent storage and save to database if user is logged in
if user_id:
try:
# Move PDF to a more permanent 'uploads/resumes' folder
# Use absolute paths for file system operations but keep DB path portable
resumes_dir = os.path.join(BASE_DIR, "uploads", "resumes")
os.makedirs(resumes_dir, exist_ok=True)
permanent_pdf_name = f"resume_{user_id}_{uuid.uuid4().hex[:8]}.pdf"
permanent_pdf_path = os.path.join(resumes_dir, permanent_pdf_name)
# The path we store in DB (relative for portability across environments)
db_pdf_path = os.path.join("uploads", "resumes", permanent_pdf_name)
import shutil
shutil.copy2(pdf_path, permanent_pdf_path)
# Create Resume record
resume_doc = ResumeRepository.create_resume(
user_id=user_id,
github_username=github_username,
job_title=resume_content.get("basics", {}).get("label", "Software Engineer"),
job_description=job_description,
tailored_content=resume_content,
pdf_path=db_pdf_path
)
# Create Job Application record
company = resume_content.get("basics", {}).get("company", "Target Company")
is_multipart = "multipart/form-data" in content_type
job_url = request.form.get("job_url") if is_multipart else (data.get("job_url") if 'data' in locals() else None)
JobApplicationRepository.create_application(
user_id=user_id,
job_title=resume_content.get("basics", {}).get("label", "Software Engineer"),
company=company,
resume_id=str(resume_doc.id),
job_url=job_url,
job_description=job_description
)
logger.info(f"[{rid}] Stored resume and application for user {user_id}")
except Exception as e:
logger.error(f"[{rid}] Failed to store resume/application: {e}")
# Validate that the PDF path is within the temp directory (security check)
abs_temp_dir = os.path.abspath("temp")
abs_pdf_path = os.path.abspath(pdf_path)
if not abs_pdf_path.startswith(abs_temp_dir):
return jsonify({"error": "Invalid file path"}), 500
# Return the PDF file
logger.info(f"[{rid}] Resume generation successful")
return send_file(
pdf_path,
mimetype="application/pdf",
as_attachment=False,
)
except Exception as e:
logger.error(f"[{rid}] Generation failed: {e}", exc_info=True)
return jsonify({"error": str(e)}), 500
@app.route("/api/config/elevenlabs", methods=["GET"])
@app.route("/api/v1/config/elevenlabs", methods=["GET"])
def get_elevenlabs_config():
"""Return ElevenLabs Agent ID for the mock interview"""
agent_id = Config.ELEVENLABS_AGENT_ID
if not agent_id:
# Check env directly if not in Config for some reason
agent_id = os.getenv("ELEVENLABS_AGENT_ID")
if not agent_id:
return jsonify({"error": "ElevenLabs Agent ID not configured"}), 404
return jsonify({"agentId": agent_id})
@app.route("/api/interview-prep", methods=["POST"])
@app.route("/api/v1/interview-prep", methods=["POST"])
def interview_prep():
"""
Endpoint to generate interview preparation tips and questions
"""
rid = getattr(request, "request_id", "unknown")
try:
data = request.get_json(silent=True) or {}
job_description = data.get("job_description", "").strip()
if not job_description:
return jsonify({"error": "Job description is required"}), 400
logger.info(f"[{rid}] Interview prep request")
# Analyze job description
job_requirements = job_analyzer.analyze(job_description)
# Generate interview prep
prep_data = interview_generator.generate(job_requirements)
return jsonify(prep_data)
except Exception as e:
logger.error(f"[{rid}] Interview prep failed: {e}", exc_info=True)
return jsonify({"error": str(e)}), 500
# --- Authentication & User Endpoints ---
@app.route("/api/auth/register", methods=["POST"])
@app.route("/api/v1/auth/register", methods=["POST"])
def register():
"""Register a new user"""
try:
data = request.json
email = data.get("email")
username = data.get("username")
password = data.get("password")
first_name = data.get("first_name", "")
last_name = data.get("last_name", "")
if not email or not username or not password:
return jsonify({"error": "Email, username, and password are required"}), 400
if UserRepository.get_user_by_email(email):
return jsonify({"error": "Email already registered"}), 400
if UserRepository.get_user_by_username(username):
return jsonify({"error": "Username already taken"}), 400
user = UserRepository.create_user(email, username, password, first_name, last_name)
return jsonify({
"message": "User registered successfully",
"user": {
"id": str(user.id),
"username": user.username,
"email": user.email
}
}), 201
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/auth/login", methods=["POST"])
@app.route("/api/v1/auth/login", methods=["POST"])
def login():
"""Authenticate user"""
try:
data = request.json
username = data.get("username")
password = data.get("password")
if not username or not password:
return jsonify({"error": "Username and password are required"}), 400
user = UserRepository.authenticate(username, password)
if not user:
return jsonify({"error": "Invalid username or password"}), 401
return jsonify({
"message": "Login successful",
"user": {
"id": str(user.id),
"username": user.username,
"email": user.email
}
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/user/resumes/<user_id>", methods=["GET"])
@app.route("/api/v1/user/resumes/<user_id>", methods=["GET"])
def get_user_resumes(user_id):
"""Get all resumes for a user"""
try:
resumes = ResumeRepository.get_user_resumes(user_id)
return jsonify([{
"id": str(r.id),
"job_title": r.job_title,
"job_description": r.job_description or "",
"github_username": r.github_username,
"created_at": r.created_at.isoformat(),
"is_archived": r.is_archived
} for r in resumes])
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/v1/user/resumes/download/<resume_id>", methods=["GET"])
def download_resume(resume_id):
"""Download a previously generated resume PDF"""
try:
resume = ResumeRepository.get_resume(resume_id)
if not resume or not resume.pdf_path:
return jsonify({"error": "Resume not found"}), 404
# Try multiple path resolutions
possible_paths = [
resume.pdf_path,
os.path.join(BASE_DIR, resume.pdf_path),
os.path.join(os.getcwd(), resume.pdf_path)
]
target_path = None
for p in possible_paths:
if p and os.path.exists(p):
target_path = p
break
if not target_path:
return jsonify({"error": "PDF file not found on server"}), 404
return send_file(
target_path,
mimetype="application/pdf",
as_attachment=True,
download_name=f"resume_{resume.job_title.replace(' ', '_')}.pdf"
)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/v1/user/resumes/preview/<resume_id>", methods=["GET"])
def preview_resume(resume_id):
"""Preview a previously generated resume PDF inline"""
try:
resume = ResumeRepository.get_resume(resume_id)
if not resume or not resume.pdf_path:
return jsonify({"error": "Resume not found"}), 404
# Try multiple path resolutions
possible_paths = [
resume.pdf_path,
os.path.join(BASE_DIR, resume.pdf_path),
os.path.join(os.getcwd(), resume.pdf_path)
]
target_path = None
for p in possible_paths:
if p and os.path.exists(p):
target_path = p
break
if not target_path:
return jsonify({"error": "PDF file not found on server"}), 404
return send_file(
target_path,
mimetype="application/pdf",
as_attachment=False
)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/user/applications/<user_id>", methods=["GET"])
@app.route("/api/v1/user/applications/<user_id>", methods=["GET"])
def get_user_applications(user_id):
"""Get all job applications for a user"""
try:
applications = JobApplicationRepository.get_user_applications(user_id)
return jsonify([{
"id": str(a.id),
"job_title": a.job_title,
"company": a.company,
"status": a.status,
"applied_date": a.applied_date.isoformat(),
"job_description": a.job_description or ""
} for a in applications])
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/health", methods=["GET"])
@app.route("/api/v1/health", methods=["GET"])
def health():
"""Health check endpoint"""
return jsonify({"status": "healthy"})
if __name__ == "__main__":
# Create necessary directories
os.makedirs("temp", exist_ok=True)
# Get configuration from environment
debug_mode = os.getenv("FLASK_DEBUG", "False").lower() == "true"
# Default to localhost for security, can be overridden with FLASK_HOST=0.0.0.0
host = os.getenv("FLASK_HOST", "127.0.0.1")
port = int(os.getenv("FLASK_PORT", os.getenv("PORT", "5001")))
# Run the application
app.run(debug=debug_mode, host=host, port=port)