forked from sliday/resume-job-matcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresume_matcher.py
1170 lines (997 loc) Β· 46.5 KB
/
resume_matcher.py
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import json
import json5
import PyPDF2
import anthropic
import openai
from openai import OpenAI
from openai import OpenAIError
from glob import glob
from multiprocessing import Pool, cpu_count
from tqdm import tqdm
import logging
from termcolor import colored
import time
import json5
import requests
from bs4 import BeautifulSoup
from PIL import Image
import io
import statistics
import base64
import os
# Initialize logging with more detailed format
logging.basicConfig(level=logging.CRITICAL, format='%(asctime)s - %(levelname)s - %(message)s')
# Initialize the Anthropic client globally
default_anthropic_client = anthropic.Anthropic(api_key=os.environ.get("CLAUDE_API_KEY"))
# Initialize the OpenAI client globally
default_openai_client = openai.OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# Global variable to store the chosen API
chosen_api = "anthropic"
import os
from termcolor import colored
def choose_api():
global chosen_api
prompt = "Use OpenAI API instead of Anthropic? [y/N]: "
choice = input(colored(prompt, "cyan")).strip().lower()
if choice in ["y", "yes"]:
chosen_api = "openai"
else:
chosen_api = "anthropic"
print(colored(f"\nSelected API: {chosen_api.capitalize()}", "green", attrs=["bold"]))
def talk_to_ai(prompt, max_tokens=1000, image_data=None, client=None):
global chosen_api
try:
if chosen_api == "anthropic":
response = talk_to_anthropic(prompt, max_tokens, image_data, client)
else:
response = talk_to_openai(prompt, max_tokens, image_data, client)
return response.strip() if response else ""
except Exception as e:
logging.error(f"Error in talk_to_ai: {str(e)}")
return ""
def talk_to_anthropic(prompt, max_tokens=1000, image_data=None, client=None):
if client is None:
client = default_anthropic_client
messages = [{"role": "user", "content": [{"type": "text", "text": prompt}]}]
if image_data:
for img in image_data:
base64_image = base64.b64encode(img).decode('utf-8')
messages[0]["content"].append({
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": base64_image
}
})
try:
response = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=max_tokens,
messages=messages
)
return response.content[0].text.strip()
except Exception as e:
logging.error(f"Error in Anthropic AI communication: {str(e)}")
return ""
def talk_to_openai(prompt, max_tokens=1000, image_data=None, client=None):
if client is None:
client = default_openai_client
messages = [{"role": "user", "content": [{"type": "text", "text": prompt}]}]
if image_data:
model = "gpt-4o"
for img in image_data:
base64_image = base64.b64encode(img).decode('utf-8')
messages[0]["content"].append({
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
})
else:
model = "gpt-4o"
try:
response = client.chat.completions.create(
model=model,
messages=messages,
max_tokens=max_tokens
)
return response.choices[0].message.content.strip()
except Exception as e:
logging.error(f"Error in OpenAI communication: {str(e)}")
return ""
from pydantic import BaseModel, Field
from typing import List, Union
from enum import Enum
class ResponseType(str, Enum):
score = "score"
reasons = "reasons"
url = "url"
email = "email"
class Score(BaseModel):
value: int = Field(..., ge=0, le=100)
class Reasons(BaseModel):
items: List[str] = Field(..., max_items=5)
class URL(BaseModel):
value: str
class Email(BaseModel):
subject: str
body: str
class AIResponse(BaseModel):
response_type: ResponseType
content: Union[Score, Reasons, URL, Email]
def talk_fast(messages, model="gpt-4o-mini", max_tokens=1000, client=None, image_data=None):
import tiktoken # Ensure this package is installed: pip install tiktoken
if client is None:
client = default_openai_client
content = []
if isinstance(messages, str):
content.append({"type": "text", "text": messages})
elif isinstance(messages, list):
content.extend(messages)
else:
raise ValueError("Messages should be a string or a list of message objects")
if image_data:
if isinstance(image_data, list):
for img in image_data:
base64_image = base64.b64encode(img).decode('utf-8')
content.append({
"type": "image",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
})
else:
base64_image = base64.b64encode(image_data).decode('utf-8')
content.append({
"type": "image",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
})
# Estimate token count
encoding = tiktoken.encoding_for_model(model)
content_text = ''
for item in content:
if item['type'] == 'text':
content_text += item['text']
input_tokens = len(encoding.encode(content_text))
# Define the model's context window
model_context_windows = {
"gpt-4": 8192,
"gpt-4o-mini": 4096 # Adjust according to the actual context window
}
context_window = model_context_windows.get(model, 4096)
# Set default max_tokens if not provided
if max_tokens is None:
max_tokens = 1000 # Default value
# Ensure total tokens do not exceed context window
if input_tokens + max_tokens > context_window:
max_tokens = context_window - input_tokens - 1 # Reserve 1 token for safety
# Ensure max_tokens is positive
if max_tokens <= 0:
logging.error("Input text is too long for the model to process.")
return None # Or handle as needed
try:
logging.debug(f"Sending request to OpenAI API with model: {model}")
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": content}],
max_tokens=max_tokens,
response_format={"type": "json_object"},
tools=[{
"type": "function",
"function": {
"name": "AIResponse",
"parameters": AIResponse.model_json_schema()
}
}]
)
logging.debug(f"Received response from OpenAI API: {response}")
if response.choices and response.choices[0].message:
if response.choices[0].message.content:
result = response.choices[0].message.content.strip()
elif response.choices[0].message.tool_calls:
# Handle tool calls (function calls)
tool_call = response.choices[0].message.tool_calls[0]
result = tool_call.function.arguments
else:
raise ValueError("Unexpected response format")
logging.debug(f"Extracted content from OpenAI response: {result}")
try:
parsed_result = json5.loads(result)
return parsed_result
except json.JSONDecodeError as e:
logging.error(f"Error parsing JSON response: {str(e)}")
return None
else:
logging.error("Empty or invalid response from OpenAI API")
logging.debug(f"Full response object: {response}")
return None
except Exception as e:
error_message = str(e)
if hasattr(e, 'response'):
error_message += f"\nResponse content: {e.response.text}"
logging.error(f"Error in talk_fast: {error_message}")
return None
def rank_job_description(job_desc, client=None):
criteria = [
{
'name': 'Clarity and Specificity',
'key': 'clarity_specificity',
'weight': 20,
'description': 'The job description should clearly outline the responsibilities, required qualifications, and expectations without ambiguity.',
'factors': [
'Use of clear and concise language',
'Detailed list of job responsibilities',
'Specific qualifications and experience required',
'Avoidance of vague terms like "sometimes," "maybe," or "as needed"'
]
},
{
'name': 'Inclusivity and Bias-Free Language',
'key': 'inclusivity',
'weight': 20,
'description': 'The job description should use inclusive language that encourages applications from a diverse range of candidates.',
'factors': [
'Gender-neutral pronouns and job titles',
'Avoidance of ageist, ableist, or culturally biased language',
'Inclusion of diversity and inclusion statements'
]
},
{
'name': 'Company Culture and Values Description',
'key': 'company_culture',
'weight': 15,
'description': 'The job description should provide insight into the company\'s culture, mission, and values to help candidates assess cultural fit.',
'factors': [
'Clear statement of company mission and values',
'Description of team dynamics and work environment',
'Emphasis on aspects like innovation, collaboration, or employee development'
]
},
{
'name': 'Realistic and Prioritized Qualifications',
'key': 'realistic_qualifications',
'weight': 15,
'description': 'The qualifications section should distinguish between essential and preferred qualifications to avoid deterring qualified candidates.',
'factors': [
'Separate lists for mandatory and preferred qualifications',
'Realistic experience and education requirements',
'Justification for any stringent requirements'
]
},
{
'name': 'Opportunities for Growth and Development',
'key': 'growth_opportunities',
'weight': 10,
'description': 'The job description should mention any opportunities for career advancement, professional development, or training.',
'factors': [
'Information on potential career paths within the company',
'Availability of training programs or educational assistance',
'Mentorship or leadership development opportunities'
]
},
{
'name': 'Compensation and Benefits Transparency',
'key': 'compensation_transparency',
'weight': 10,
'description': 'Providing information on compensation ranges and benefits can attract candidates aligned with what the company offers.',
'factors': [
'Inclusion of salary range or compensation package details',
'Highlighting key benefits (e.g., health insurance, retirement plans)',
'Mention of unique perks (e.g., remote work options, flexible hours)'
]
},
{
'name': 'Search Engine Optimization (SEO)',
'key': 'seo',
'weight': 5,
'description': 'The job description should be optimized with relevant keywords to improve visibility in job searches.',
'factors': [
'Use of industry-standard job titles',
'Inclusion of relevant keywords and phrases'
]
},
{
'name': 'Legal Compliance',
'key': 'legal_compliance',
'weight': 5,
'description': 'Ensure the job description complies with employment laws and regulations.',
'factors': [
'Compliance with labor laws',
'Non-discriminatory language',
'Properly stated equal opportunity statements'
]
}
]
scores = {}
total_weight = sum(criterion['weight'] for criterion in criteria)
total_score = 0
for criterion in criteria:
prompt = f"""
Evaluate the job description based on the criterion: "{criterion['name']}".
Criterion Description:
{criterion['description']}
Factors to consider:
{', '.join(criterion['factors'])}
Job Description:
{job_desc}
Provide your evaluation as an integer score from 0 to 100, where 0 is the lowest and 100 is the highest.
Only return the integer score, nothing else.
"""
response = talk_fast(prompt, client=client)
try:
if isinstance(response, dict) and 'content' in response and 'value' in response['content']:
score = response['content']['value']
else:
raise ValueError("Unexpected response format")
if 0 <= score <= 100:
criterion['score'] = score
else:
raise ValueError("Score out of range")
except ValueError as ve:
logging.error(f"Error parsing score for criterion {criterion['name']}: {ve}")
criterion['score'] = 0
except Exception as e:
logging.error(f"Unexpected error for criterion {criterion['name']}: {e}")
criterion['score'] = 0
scores[criterion['key']] = criterion['score']
weighted_score = (criterion['score'] * criterion['weight']) / 100
total_score += weighted_score
overall_score = int((total_score / total_weight) * 100) # Normalize to 0-100 scale
# Collect improvement tips
tips_prompt = f"""
Based on your evaluation of the job description, provide 3-5 tips for improvement.
Job Description:
{job_desc}
Focus on areas that can be enhanced according to modern best practices.
Output your response as a JSON array of strings, e.g.:
[
"Tip 1",
"Tip 2",
"Tip 3"
]
"""
tips_text = talk_fast(tips_prompt, max_tokens=150, client=client)
try:
improvement_tips = json5.loads(tips_text)
if not isinstance(improvement_tips, list):
raise ValueError("Improvement tips should be a list.")
# Ensure tips are strings
improvement_tips = [str(tip) for tip in improvement_tips]
except Exception as e:
logging.error(f"Error parsing improvement tips: {str(e)}")
improvement_tips = []
result = {
"scores": scores,
"overall_score": overall_score,
"improvement_tips": improvement_tips[:5] # Limit to 5 tips
}
return result
def extract_text_and_image_from_pdf(file_path):
import pytesseract
from pdf2image import convert_from_path
from PyPDF2 import PdfReader
try:
text = ""
resume_images = []
# Extract text from the PDF using PyPDF2
reader = PdfReader(file_path)
first_page_text = ""
if reader.pages:
first_page = reader.pages[0]
first_page_text = first_page.extract_text()
if first_page_text:
text += first_page_text
# Extract image from the first page
images = convert_from_path(file_path, first_page=1, last_page=1)
if images:
img = images[0]
# Convert to grayscale and compress image
img_gray = img.convert('L')
img_buffer = io.BytesIO()
img_gray.save(img_buffer, format='JPEG', quality=51)
img_buffer.seek(0)
# Add image data to resume_images list
resume_images.append(img_buffer.getvalue())
# If text extraction is insufficient, perform OCR
if not first_page_text or len(first_page_text.strip()) < 500:
ocr_text = pytesseract.image_to_string(Image.open(img_buffer))
text += ocr_text
else:
logging.error(f"No images found in PDF {file_path}")
return text, resume_images
except Exception as e:
logging.error(f"Error extracting text and image from PDF {file_path}: {str(e)}")
return "", []
def assess_resume_quality(resume_images, client=None):
# Ensure resume_images is a list of base64 encoded strings
if not isinstance(resume_images, list) or not resume_images:
logging.error("Invalid resume_images format")
return 0
# Use only the first image (front page)
front_page_image = resume_images[0]
criteria = [
{
'name': 'Formatting and Layout',
'key': 'formatting_layout',
'weight': 10,
'description': 'Assess the overall formatting and layout of the resume.',
'factors': [
'Consistent font styles and sizes',
'Proper alignment of text and sections',
'Effective use of white space to enhance readability',
'Appropriate margins and spacing'
]
},
{
'name': 'Section Organization and Headings',
'key': 'section_organization',
'weight': 15,
'description': 'Evaluate the organization of content into clear sections with appropriate headings.',
'factors': [
'Clear and descriptive section headings',
'Logical sequence of sections (e.g., summary, experience, education)',
'Use of subheadings where appropriate',
'Ease of locating key information'
]
},
{
'name': 'Clarity and Conciseness of Content',
'key': 'content_clarity',
'weight': 25,
'description': 'Assess the clarity and conciseness of the information presented.',
'factors': [
'Use of clear and straightforward language',
'Concise bullet points',
'Avoidance of unnecessary jargon or buzzwords',
'Focus on relevant information'
]
},
{
'name': 'Visual Elements and Design',
'key': 'visual_design',
'weight': 20,
'description': 'Evaluate the visual appeal of the resume, including the use of visual elements.',
'factors': [
'Appropriate use of color accents',
'Inclusion of relevant visual elements (e.g., icons, charts)',
'Consistency in design elements',
'Professional appearance suitable for the industry'
]
},
{
'name': 'Grammar and Spelling',
'key': 'grammar_spelling',
'weight': 20,
'description': 'Assess the resume for grammatical correctness and spelling accuracy.',
'factors': [
'Correct grammar usage',
'Accurate spelling throughout',
'Proper punctuation',
'Professional tone and language'
]
},
{
'name': 'Length and Completeness',
'key': 'length_completeness',
'weight': 10,
'description': 'Evaluate whether the resume is of appropriate length and includes all necessary sections.',
'factors': [
'Resume length appropriate for experience level (typically 1-2 pages)',
'Inclusion of all relevant sections',
'Absence of irrelevant or redundant information'
]
}
]
scores = {}
total_weight = sum(criterion['weight'] for criterion in criteria)
total_score = 0
for criterion in criteria:
prompt = f"""
Evaluate the resume image based on the criterion: "{criterion['name']}".
Criterion Description:
{criterion['description']}
Factors to consider:
{', '.join(criterion['factors'])}
Provide your evaluation as an integer score from 0 to 100, where 0 is the lowest and 100 is the highest.
Only return the integer score, nothing else.
"""
response = talk_fast(prompt, max_tokens=200, image_data=front_page_image, client=client)
try:
if isinstance(response, dict) and 'content' in response and 'value' in response['content']:
score = response['content']['value']
else:
raise ValueError("Unexpected response format")
if 0 <= score <= 100:
scores[criterion['key']] = score
else:
raise ValueError("Score out of range")
except Exception as e:
logging.error(f"Error parsing score for criterion {criterion['name']}: {str(e)}")
scores[criterion['key']] = 0
weighted_score = (score * criterion['weight']) / 100
total_score += weighted_score
overall_score = int((total_score / total_weight) * 100) # Normalize to 0-100 scale
return overall_score
def extract_job_requirements(job_desc, client=None):
prompt = f"""
Extract the key requirements from the following job description.
Job Description:
{job_desc}
Provide the output in the following JSON format:
{{
"required_experience_years": integer,
"required_education_level": string,
"required_skills": [list of strings],
"optional_skills": [list of strings],
"certifications_preferred": [list of strings],
"soft_skills": [list of strings],
"keywords_to_match": [list of strings],
"emphasis": {{
"technical_skills_weight": integer,
"soft_skills_weight": integer,
"experience_weight": integer,
"education_weight": integer,
"language_proficiency_weight": integer,
"certifications_weight": integer
}}
}}
Only output valid JSON.
You can only speak JSON. You can only output valid JSON. Strictly No explanation, no comments, no intro. No \`\`\`json\`\`\` wrapper.
"""
response = talk_to_ai(prompt, max_tokens=2000, client=client)
try:
if isinstance(response, dict):
job_requirements = response
else:
job_requirements = json5.loads(response)
return job_requirements
except json.JSONDecodeError as e:
logging.error(f"Error parsing job requirements: {str(e)}")
logging.error(f"Response: {response}")
return None
import sys # Make sure this import is at the top of your file
def match_resume_to_job(resume_text, job_desc, file_path, resume_images, client=None):
# Extract job requirements and wait for completion
job_requirements = extract_job_requirements(job_desc, client)
if not job_requirements:
logging.error("Failed to extract job requirements")
print(colored("Error: Failed to extract job requirements. Exiting program.", 'red'))
sys.exit(1) # Exit the script with an error code
# Check if job_requirements contains expected keys
if 'emphasis' not in job_requirements:
logging.error("Job requirements missing 'emphasis' key")
print(colored("Error: Invalid job requirements format. Exiting program.", 'red'))
sys.exit(1) # Exit the script with an error code
criteria = [
{
'name': 'Language Proficiency',
'key': 'language_proficiency',
'weight': job_requirements['emphasis'].get('language_proficiency_weight', 5),
'description': 'Assign points based on the candidate\'s proficiency in languages relevant to the job.',
'factors': [
'Proficiency in required languages',
'Multilingual abilities relevant to the job'
]
},
{
'name': 'Education Level',
'key': 'education_level',
'weight': job_requirements['emphasis'].get('education_weight', 10),
'description': 'Assign points based on the candidate\'s highest level of education or equivalent experience.',
'factors': [
'Highest education level attained',
'Relevance of degree to the job',
'Alternative education paths (certifications, bootcamps, self-learning)'
]
},
{
'name': 'Years of Experience',
'key': 'experience_years',
'weight': job_requirements['emphasis'].get('experience_weight', 20),
'description': 'Assign points based on the relevance and quality of experience.',
'factors': [
'Total years of relevant experience',
'Quality and relevance of previous roles',
'Significant achievements in previous positions'
]
},
{
'name': 'Technical Skills',
'key': 'technical_skills',
'weight': job_requirements['emphasis'].get('technical_skills_weight', 40),
'description': 'Assign points for each required and optional skill, considering proficiency level.',
'factors': [
'Proficiency in required technical skills',
'Proficiency in optional technical skills',
'Transferable skills and learning ability',
'Keywords matched in resume'
]
},
{
'name': 'Certifications',
'key': 'certifications',
'weight': job_requirements['emphasis'].get('certifications_weight', 5),
'description': 'Assign points for each relevant certification.',
'factors': [
'Possession of preferred certifications',
'Equivalent practical experience',
'Self-learning projects demonstrating expertise'
]
},
{
'name': 'Soft Skills',
'key': 'soft_skills',
'weight': job_requirements['emphasis'].get('soft_skills_weight', 20),
'description': 'Assign points for each soft skill demonstrated through examples or achievements.',
'factors': [
'Demonstrated soft skills in resume',
'Examples of teamwork, leadership, problem-solving, etc.'
]
}
]
scores = {}
total_weight = sum(criterion['weight'] for criterion in criteria)
if total_weight == 0:
logging.error("Total weight of criteria is zero")
return {'score': 0, 'match_reasons': "Error: Invalid criteria weights", 'website': ''}
total_score = 0
for criterion in criteria:
prompt = f"""
Evaluate the candidate's resume based on the criterion: "{criterion['name']}".
Criterion Description:
{criterion['description']}
Factors to consider:
{', '.join(criterion['factors'])}
Job Requirements:
{json.dumps(job_requirements, indent=2)}
Resume:
{resume_text}
Provide your evaluation as an integer score from 0 to 100, where 0 is the lowest and 100 is the highest.
Only return the integer score, nothing else. No explanation, no comments, no intro. No \`\`\`json\`\`\` wrapper.
"""
response = talk_fast(prompt, client=client)
try:
if isinstance(response, dict) and 'content' in response and 'value' in response['content']:
score = response['content']['value']
else:
raise ValueError("Unexpected response format")
if 0 <= score <= 100:
criterion['score'] = score
else:
raise ValueError("Score out of range")
except ValueError as ve:
logging.error(f"Error parsing score for criterion {criterion['name']}: {ve}")
criterion['score'] = 0
except Exception as e:
logging.error(f"Unexpected error for criterion {criterion['name']}: {e}")
criterion['score'] = 0
scores[criterion['key']] = criterion['score']
weighted_score = (criterion['score'] * criterion['weight']) / 100
total_score += weighted_score
# Normalize total score to 0 - 100 scale
final_score = int((total_score / total_weight) * 100)
# Generate match reasons
reasons_prompt = f"""
Based on the evaluation, provide 3-4 key reasons for the match between the candidate's resume and the job requirements.
Resume:
{resume_text}
Job Requirements:
{json.dumps(job_requirements, indent=2)}
Provide the reasons in telegraphic English, max 10 words per reason, separated by ' | '.
Only output the reasons as a single string. No explanation, no comments, no intro. No \`\`\`json\`\`\` wrapper.
"""
reasons_response = talk_fast(reasons_prompt, max_tokens=100, client=client)
if isinstance(reasons_response, dict) and 'content' in reasons_response:
match_reasons = reasons_response['content'].get('value', '')
else:
logging.error(f"Unexpected format for reasons response: {reasons_response}")
match_reasons = ''
# Extract website from resume (simple extraction)
website = ''
website_prompt = f"""
Extract the candidate's personal website URL from the resume if available.
Resume:
{resume_text}
Only output the URL or an empty string.
You can only speak URL. You can only output valid URL. Strictly No explanation, no comments, no intro. No \`\`\`json\`\`\` wrapper.
"""
website_response = talk_fast(website_prompt, max_tokens=200, client=client)
if isinstance(website_response, dict) and 'content' in website_response:
website = website_response['content'].get('value', '')
else:
logging.error(f"Unexpected format for website response: {website_response}")
website = ''
# Generate email response and subject
email_prompt = f"""
Compose a professional email response to the candidate based on their match score.
Score: {final_score}
If the score is below 90, politely reject the candidate. If the score is 90 or above, invite them to the next stage.
Use personal details and make it personalized.
Provide the output in the following JSON format:
{{
"email_response": "Email body",
"subject_response": "Email subject"
}}
You can only speak JSON. You can only output valid JSON. Strictly No explanation, no comments, no intro. No \`\`\`json\`\`\` wrapper.
"""
email_text = talk_to_ai(email_prompt, max_tokens=200, client=client)
try:
email_response = json5.loads(email_text)
email_body = email_response.get('email_response', '')
email_subject = email_response.get('subject_response', '')
# Save email to file
os.makedirs('out', exist_ok=True)
file_name = f"out/{os.path.splitext(os.path.basename(file_path))[0]}_response.txt"
with open(file_name, 'w') as f:
f.write(f"Subject: {email_subject}\n\n{email_body}")
except ValueError as e:
logging.error(f"Error parsing email response: {str(e)}")
logging.error(f"Raw email text: {email_text}")
email_body = ''
email_subject = ''
return {'score': final_score, 'match_reasons': match_reasons, 'website': website}
def get_score_details(score):
if not isinstance(score, int):
raise ValueError(f"Invalid score type: {type(score)}. Expected int.")
score_ranges = [
{"min_score": 98, "max_score": 101, "label": 'Cosmic Perfection', "color": 'magenta', "emoji": 'π'},
{"min_score": 95, "max_score": 98, "label": 'Unicorn Candidate', "color": 'blue', "emoji": 'π¦'},
{"min_score": 93, "max_score": 95, "label": 'Superstar Plus', "color": 'cyan', "emoji": 'π '},
{"min_score": 90, "max_score": 93, "label": 'Coding Wizard', "color": 'green', "emoji": 'π§'},
{"min_score": 87, "max_score": 90, "label": 'Rockstar Coder', "color": 'cyan', "emoji": 'πΈ'},
{"min_score": 85, "max_score": 87, "label": 'Coding Virtuoso', "color": 'cyan', "emoji": 'π'},
{"min_score": 83, "max_score": 85, "label": 'Tech Maestro', "color": 'green', "emoji": 'π'},
{"min_score": 80, "max_score": 83, "label": 'Awesome Fit', "color": 'green', "emoji": 'π'},
{"min_score": 78, "max_score": 80, "label": 'Stellar Match', "color": 'green', "emoji": 'π«'},
{"min_score": 75, "max_score": 78, "label": 'Great Prospect', "color": 'green', "emoji": 'π'},
{"min_score": 73, "max_score": 75, "label": 'Very Promising', "color": 'light_green', "emoji": 'π'},
{"min_score": 70, "max_score": 73, "label": 'Solid Contender', "color": 'light_green', "emoji": 'π΄'},
{"min_score": 68, "max_score": 70, "label": 'Strong Potential', "color": 'yellow', "emoji": 'π±'},
{"min_score": 65, "max_score": 68, "label": 'Good Fit', "color": 'yellow', "emoji": 'π'},
{"min_score": 63, "max_score": 65, "label": 'Promising Talent', "color": 'yellow', "emoji": 'π»'},
{"min_score": 60, "max_score": 63, "label": 'Worthy Consideration', "color": 'yellow', "emoji": 'π€'},
{"min_score": 58, "max_score": 60, "label": 'Potential Diamond', "color": 'yellow', "emoji": 'π'},
{"min_score": 55, "max_score": 58, "label": 'Decent Prospect', "color": 'yellow', "emoji": 'π'},
{"min_score": 53, "max_score": 55, "label": 'Worth a Look', "color": 'yellow', "emoji": 'π'},
{"min_score": 50, "max_score": 53, "label": 'Average Candidate', "color": 'yellow', "emoji": 'πΌ'},
{"min_score": 48, "max_score": 50, "label": 'Middling Match', "color": 'yellow', "emoji": 'π―'},
{"min_score": 45, "max_score": 48, "label": 'Fair Possibility', "color": 'yellow', "emoji": 'πΎ'},
{"min_score": 43, "max_score": 45, "label": 'Needs Polish', "color": 'yellow', "emoji": 'πͺ'},
{"min_score": 40, "max_score": 43, "label": 'Diamond in the Rough', "color": 'yellow', "emoji": 'π£'},
{"min_score": 38, "max_score": 40, "label": 'Underdog Contender', "color": 'light_yellow',"emoji": 'π'},
{"min_score": 35, "max_score": 38, "label": 'Needs Work', "color": 'light_yellow',"emoji": 'π '},
{"min_score": 33, "max_score": 35, "label": 'Significant Gap', "color": 'light_yellow',"emoji": 'π'},
{"min_score": 30, "max_score": 33, "label": 'Mismatch Alert', "color": 'light_yellow',"emoji": 'π¨'},
{"min_score": 25, "max_score": 30, "label": 'Back to Drawing Board', "color": 'red', "emoji": 'π¨'},
{"min_score": 20, "max_score": 25, "label": 'Way Off Track', "color": 'red', "emoji": 'π'},
{"min_score": 15, "max_score": 20, "label": 'Resume Misfire', "color": 'red', "emoji": 'π―'},
{"min_score": 10, "max_score": 15, "label": 'Cosmic Mismatch', "color": 'red', "emoji": 'β'},
{"min_score": 5, "max_score": 10, "label": 'Did You Mean to Apply?', "color": 'red', "emoji": 'π€·'},
{"min_score": 0, "max_score": 5, "label": 'Oops! Wrong Universe', "color": 'red', "emoji": 'π'},
]
for range_info in score_ranges:
min_score = range_info["min_score"]
max_score = range_info["max_score"]
if min_score <= score < max_score:
return range_info["emoji"], range_info["color"], range_info["label"]
return "π", "red", "Unable to score" # Fallback for any unexpected scores
def check_website(url):
if not url.startswith(('http://', 'https://')):
url = 'https://' + url
try:
response = requests.get(url, timeout=5)
return response.status_code == 200, url
except requests.RequestException:
return False, url
def worker(args):
file, job_desc = args
try:
resume_text, resume_images = extract_text_and_image_from_pdf(file)
if not resume_text:
return (os.path.basename(file), 0, "π΄", "red", "Error: Failed to extract text from PDF", "", "")
result = match_resume_to_job(resume_text, job_desc, file, resume_images)
# Use json5 to parse the result
if isinstance(result, str):
result = json5.loads(result)
score = result.get('score', 0)
match_reasons = result.get('match_reasons', '')
website = result.get('website', '')
# Check if the website is accessible
if website:
is_accessible, updated_url = check_website(website)
if not is_accessible:
score = max(0, score - 25) # Reduce score, but not below 0
website = f"{updated_url} (inactive)"
else:
website = updated_url
# Fetch website content
try:
response = requests.get(website, timeout=5)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
website_text = soup.get_text(separator=' ', strip=True)
# Combine resume_text and website_text
combined_text = f"{resume_text}\n\nWebsite Content:\n{website_text}"
# Re-run match_resume_to_job with combined_text
result = match_resume_to_job(combined_text, job_desc, file, resume_images)
if isinstance(result, str):
result = json5.loads(result)
score = result.get('score', 0)
match_reasons = result.get('match_reasons', '')
except Exception as e:
logging.error(f"Error fetching website content for {file}: {str(e)}")
emoji, color, label = get_score_details(score)
return (os.path.basename(file), score, emoji, color, label, match_reasons, website)
except json5.JSONDecodeError as je:
error_msg = f"JSON5 Decode Error: {str(je)}"
logging.error(f"Error processing {file}: {error_msg}")
return (os.path.basename(file), 0, "π΄", "red", error_msg, "", "")
except Exception as e:
error_msg = f"Unexpected Error: {str(e)}"
logging.error(f"Error processing {file}: {error_msg}")
return (os.path.basename(file), 0, "π΄", "red", error_msg, "", "")
def process_resumes(job_desc, pdf_files):
with Pool(processes=cpu_count()) as pool:
results = list(tqdm(pool.imap(worker, [(file, job_desc) for file in pdf_files]), total=len(pdf_files), desc=f"Processing {len(pdf_files)} resumes"))
return results
def analyze_overall_matches(job_desc, results):
# Prepare data for analysis
match_data = []
for filename, score, _, _, _, match_reasons, _ in results:
match_data.append({
"filename": filename,
"score": score,
"match_reasons": match_reasons
})
# Create a prompt for Claude AI
prompt = f"""
As a hiring consultant, analyze the following resume match data and suggest adjustments to the job description to attract better candidates.
Job Description:
{job_desc}
Resume Match Data:
{json.dumps(match_data, indent=2)}
Provide a detailed analysis highlighting common strengths and weaknesses among the candidates. Suggest specific changes to the job description to improve candidate matches.
Output format, no more than 5 suggestions, 1-sentence long each:
- Observation or suggestion
...
Only output the suggestions, no intro, no explanations, no comments.
"""
try: