File size: 44,018 Bytes
a928595
db455d9
9f115d1
 
c1efc08
 
 
e84a893
c1efc08
fbc936b
 
c1efc08
 
 
 
 
 
 
 
 
e84a893
 
 
 
 
 
 
 
 
 
 
 
c1efc08
 
 
 
 
 
 
 
 
a928595
7044586
c1efc08
7044586
 
 
 
fbc936b
 
 
 
 
 
 
 
 
 
 
7044586
 
 
fbc936b
c1efc08
 
 
 
 
 
 
 
e84a893
7044586
 
e84a893
7044586
 
 
 
 
 
c1efc08
 
 
 
 
 
 
 
7044586
 
 
 
c1efc08
 
 
 
 
 
 
 
7044586
 
 
 
c1efc08
 
 
 
 
 
 
 
 
7044586
 
 
 
 
db455d9
 
 
7044586
 
c1efc08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7044586
 
 
 
 
 
 
 
c1efc08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7044586
 
 
 
 
 
 
c1efc08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e84a893
 
 
 
 
 
 
 
 
 
 
 
 
c1efc08
e84a893
 
 
 
 
 
 
c1efc08
e84a893
 
 
 
 
 
c1efc08
e84a893
 
 
 
 
 
c1efc08
e84a893
 
 
 
 
 
c1efc08
e84a893
 
 
 
 
 
 
 
 
 
 
 
 
c1efc08
e84a893
 
 
 
 
 
c1efc08
e84a893
 
 
 
 
 
c1efc08
 
e84a893
 
 
 
 
c1efc08
e84a893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7044586
 
c1efc08
7044586
 
 
c1efc08
 
 
 
 
 
 
 
7044586
 
 
 
 
 
 
5bae67b
7044586
 
 
 
 
 
 
c1efc08
7044586
 
 
 
 
c1efc08
5bae67b
 
7044586
 
c1efc08
 
 
 
 
7044586
 
c1efc08
 
 
 
 
 
 
 
7044586
 
c1efc08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7044586
 
c1efc08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7044586
 
c1efc08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7044586
c1efc08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e84a893
c1efc08
7044586
 
 
c1efc08
7044586
 
 
c1efc08
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
import streamlit as st
import pdfplumber
import io
import spacy
import re
import pandas as pd
import matplotlib.pyplot as plt
from transformers import pipeline
from sentence_transformers import SentenceTransformer, util
import subprocess
import sys
import torch
import nltk
from nltk.tokenize import word_tokenize
from datetime import datetime
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
from collections import defaultdict

# Fix for huggingface_hub import issue
try:
    # For newer versions of huggingface_hub
    from huggingface_hub import hf_hub_download
except ImportError:
    try:
        # For older versions of huggingface_hub
        from huggingface_hub import cached_download as hf_hub_download
    except ImportError:
        st.error("Could not import required functions from huggingface_hub. Please check your installation.")
        hf_hub_download = None

# Initialize NLTK
@st.cache_resource
def download_nltk_resources():
    try:
        nltk.data.find('tokenizers/punkt')
    except LookupError:
        nltk.download('punkt')

download_nltk_resources()

st.set_page_config(
    page_title="Comprehensive Resume Screener & Skill Extractor",
    page_icon="πŸ“„",
    layout="wide"
)

# Download spaCy model if not already downloaded
@st.cache_resource
def download_spacy_model():
    try:
        nlp = spacy.load("en_core_web_sm")
    except OSError:
        subprocess.check_call([sys.executable, "-m", "spacy", "download", "en_core_web_sm"])
        nlp = spacy.load("en_core_web_sm")
    return nlp

# Load the NLP models
@st.cache_resource
def load_models():
    summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
    nlp = download_spacy_model()
    
    # Load sentence transformer for semantic matching
    try:
        sentence_model = SentenceTransformer('all-MiniLM-L6-v2')
    except Exception as e:
        st.error(f"Failed to load sentence transformer: {str(e)}")
        sentence_model = None
    
    return summarizer, nlp, sentence_model

# Initialize models
summarizer, nlp, sentence_model = load_models()

# Job descriptions and required skills
job_descriptions = {
    "Software Engineer": {
        "skills": ["python", "java", "javascript", "sql", "algorithms", "data structures", 
                  "git", "cloud", "web development", "software development", "coding"],
        "description": "Looking for software engineers with strong programming skills and experience in software development.",
        "semantic_description": """
        We're seeking a talented Software Engineer to design, develop, and maintain high-quality software solutions.
        The ideal candidate has strong programming skills in languages like Python, Java, or JavaScript, and experience with
        SQL databases. You should be proficient in algorithms, data structures, and version control systems like Git.
        Experience with cloud platforms and web development frameworks is a plus. You'll be responsible for the full
        software development lifecycle, from requirements gathering to deployment and maintenance.
        """
    },
    "Interaction Designer": {
        "skills": ["ui", "ux", "user research", "wireframing", "prototyping", "figma", 
                  "sketch", "adobe", "design thinking", "interaction design"],
        "description": "Seeking interaction designers with expertise in user experience and interface design.",
        "semantic_description": """
        We're looking for a creative Interaction Designer to craft intuitive and engaging user experiences.
        You should have expertise in UI/UX design principles and methods, with a portfolio demonstrating your
        ability to conduct user research, create wireframes, and develop interactive prototypes. Proficiency
        with design tools like Figma, Sketch, and Adobe Creative Suite is required. You'll collaborate with
        product managers and developers to iterate on designs based on user feedback and business requirements.
        """
    },
    "Data Scientist": {
        "skills": ["python", "r", "statistics", "machine learning", "data analysis", 
                  "sql", "tensorflow", "pytorch", "pandas", "numpy"],
        "description": "Looking for data scientists with strong analytical and machine learning skills.",
        "semantic_description": """
        We're seeking a skilled Data Scientist to extract insights from complex datasets and build predictive models.
        The ideal candidate has strong programming skills in Python or R, expertise in statistical analysis, and
        experience with machine learning algorithms. You should be proficient in SQL for data extraction and tools
        like TensorFlow or PyTorch for deep learning. Experience with data manipulation libraries like Pandas and NumPy
        is essential. You'll work on projects from exploratory data analysis to model deployment, collaborating with
        stakeholders to solve business problems through data-driven approaches.
        """
    }
}

def extract_text_from_pdf(pdf_file):
    text = ""
    with pdfplumber.open(pdf_file) as pdf:
        for page in pdf.pages:
            text += page.extract_text() or ""
    return text

def extract_work_experience(text):
    """Extract work experience details including company names, job titles, and dates"""
    # Find common section headers for work experience
    work_exp_patterns = [
        r"(?i)WORK EXPERIENCE|PROFESSIONAL EXPERIENCE|EMPLOYMENT HISTORY|EXPERIENCE",
        r"(?i)EDUCATION|ACADEMIC|QUALIFICATIONS"
    ]
    
    # Find the start of work experience section
    work_exp_start = None
    for pattern in work_exp_patterns[:1]:  # Use only the work experience patterns
        match = re.search(pattern, text)
        if match:
            work_exp_start = match.end()
            break
    
    if work_exp_start is None:
        return []
    
    # Find the end of work experience section (start of education or next major section)
    work_exp_end = len(text)
    for pattern in work_exp_patterns[1:]:  # Use only the education pattern
        match = re.search(pattern, text)
        if match and match.start() > work_exp_start:
            work_exp_end = match.start()
            break
    
    work_exp_text = text[work_exp_start:work_exp_end]
    
    # Extract job entries
    # Look for patterns of job titles, company names, and dates
    job_entries = []
    
    # Pattern for dates (MM/YYYY or Month YYYY)
    date_pattern = r"(?i)(Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)[,\s]+\d{4}|\d{1,2}/\d{4}|\d{4}"
    
    # Pattern for common job title indicators
    job_title_pattern = r"(?i)(Senior|Lead|Principal|Junior|Associate)?\s*(Software Engineer|Developer|Designer|Analyst|Manager|Director|Consultant|Specialist|Coordinator|Administrator)"
    
    # Split into paragraphs which often represent job entries
    paragraphs = re.split(r'\n\s*\n', work_exp_text)
    
    for paragraph in paragraphs:
        # Skip short paragraphs that are likely not job entries
        if len(paragraph.strip()) < 30:
            continue
        
        # Extract dates
        dates = re.findall(date_pattern, paragraph)
        start_date = dates[0] if dates else "Unknown"
        end_date = dates[-1] if len(dates) > 1 else "Present"
        
        # Extract job title
        title_match = re.search(job_title_pattern, paragraph)
        job_title = title_match.group(0) if title_match else "Unknown Position"
        
        # Extract company name (typically near the job title or at the start of the paragraph)
        lines = paragraph.split('\n')
        company = lines[0].strip() if lines else "Unknown Company"
        if job_title in company:
            company = company.replace(job_title, "").strip()
        
        # Clean company name
        for date in dates:
            company = company.replace(date, "").strip()
        company = re.sub(r'[,\.\|\-]', ' ', company).strip()
        
        job_entries.append({
            "company": company,
            "title": job_title,
            "start_date": start_date,
            "end_date": end_date,
            "description": paragraph
        })
    
    return job_entries

def estimate_skill_proficiency(text, skill):
    """Estimate proficiency level for a skill"""
    # Define proficiency indicators
    basic_indicators = ["familiar with", "basic knowledge", "understanding of", "exposure to"]
    intermediate_indicators = ["experience with", "proficient in", "worked with", "2-3 years", "2 years", "3 years"]
    advanced_indicators = ["expert in", "advanced", "extensive experience", "lead", "architected", "designed", "5+ years", "4+ years"]
    
    # Convert to lowercase for matching
    text_lower = text.lower()
    
    # Find skill mentions and surrounding context
    skill_lower = skill.lower()
    skill_index = text_lower.find(skill_lower)
    
    if skill_index == -1:
        return None
    
    # Extract context (100 characters before and after the skill mention)
    start = max(0, skill_index - 100)
    end = min(len(text_lower), skill_index + len(skill_lower) + 100)
    context = text_lower[start:end]
    
    # Check for proficiency indicators
    for indicator in advanced_indicators:
        if indicator in context:
            return "Advanced"
    
    for indicator in intermediate_indicators:
        if indicator in context:
            return "Intermediate"
    
    for indicator in basic_indicators:
        if indicator in context:
            return "Basic"
    
    # Default to basic if skill is mentioned but no proficiency indicators are found
    return "Basic"

def calculate_seniority_score(job_entries):
    """Calculate a seniority score based on job titles and years of experience"""
    # Define seniority levels for common job titles
    seniority_levels = {
        "intern": 1,
        "junior": 2,
        "associate": 3,
        "developer": 4,
        "engineer": 4,
        "designer": 4,
        "analyst": 4,
        "senior": 6,
        "lead": 7,
        "manager": 7,
        "principal": 8,
        "director": 9,
        "vp": 10,
        "cto": 10,
        "cio": 10,
        "ceo": 10
    }
    
    # Calculate total years of experience
    total_years = 0
    
    for job in job_entries:
        # Parse start and end dates
        try:
            start_year = re.search(r'\d{4}', job["start_date"])
            end_year = re.search(r'\d{4}', job["end_date"]) if job["end_date"] != "Present" else None
            
            if start_year:
                start_year = int(start_year.group(0))
                end_year = int(end_year.group(0)) if end_year else datetime.now().year
                years = end_year - start_year
                if 0 <= years <= 30:  # Sanity check
                    total_years += years
        except Exception:
            # Skip if there's an issue with date parsing
            pass
    
    # Calculate title-based seniority
    highest_seniority = 0
    
    for job in job_entries:
        title_lower = job["title"].lower()
        for level_title, score in seniority_levels.items():
            if level_title in title_lower and score > highest_seniority:
                highest_seniority = score
    
    # Combine years of experience and title-based seniority
    # Years of experience factor: 0-2 years (1), 3-5 years (2), 6-10 years (3), 11+ years (4)
    years_factor = 1
    if total_years >= 3:
        years_factor = 2
    if total_years >= 6:
        years_factor = 3
    if total_years >= 11:
        years_factor = 4
    
    # Final seniority score (1-10 scale)
    seniority_score = min(10, max(1, (highest_seniority * 0.6) + (years_factor * 1.0)))
    
    return round(seniority_score, 1), total_years

def detect_fraud_signals(text, job_entries):
    """Detect potential fraud signals in the resume"""
    fraud_signals = []
    
    # Check for impossible timelines (overlapping full-time roles)
    if len(job_entries) >= 2:
        for i in range(len(job_entries) - 1):
            for j in range(i+1, len(job_entries)):
                # Check if both jobs have date information
                if (job_entries[i]["start_date"] != "Unknown" and 
                    job_entries[i]["end_date"] != "Unknown" and
                    job_entries[j]["start_date"] != "Unknown" and
                    job_entries[j]["end_date"] != "Unknown"):
                    
                    # Get years for comparison
                    i_start = re.search(r'\d{4}', job_entries[i]["start_date"])
                    i_end = re.search(r'\d{4}', job_entries[i]["end_date"]) if job_entries[i]["end_date"] != "Present" else None
                    j_start = re.search(r'\d{4}', job_entries[j]["start_date"])
                    j_end = re.search(r'\d{4}', job_entries[j]["end_date"]) if job_entries[j]["end_date"] != "Present" else None
                    
                    # Convert to integers for comparison
                    if i_start and j_start:
                        i_start = int(i_start.group(0))
                        i_end = int(i_end.group(0)) if i_end else datetime.now().year
                        j_start = int(j_start.group(0))
                        j_end = int(j_end.group(0)) if j_end else datetime.now().year
                        
                        # Check for significant overlap (more than 6 months)
                        if ((i_start <= j_start < i_end) or (j_start <= i_start < j_end)) and job_entries[i]["company"] != job_entries[j]["company"]:
                            overlap_years = min(i_end, j_end) - max(i_start, j_start)
                            if overlap_years > 0.5:  # More than 6 months overlap
                                fraud_signals.append(f"Potential timeline inconsistency: Overlapping roles at {job_entries[i]['company']} and {job_entries[j]['company']} for {overlap_years:.1f} years")
    
    # Check for suspicious keywords or phrases
    suspicious_phrases = [
        "self-employed", 
        "freelance",
        "consultant",
        "entrepreneur",
        "founder",
        "ceo of own company"
    ]
    
    # Look for suspicious gap filling
    for phrase in suspicious_phrases:
        if phrase in text.lower():
            # Not all of these are fraudulent, but they warrant verification
            fraud_signals.append(f"Verification recommended: Contains '{phrase}' which may need additional verification")
    
    # Check for unexplained gaps in employment history
    if len(job_entries) >= 2:
        for i in range(len(job_entries) - 1):
            # Sort entries by start date
            if "Unknown" not in job_entries[i]["end_date"] and "Unknown" not in job_entries[i+1]["start_date"]:
                end_match = re.search(r'\d{4}', job_entries[i]["end_date"])
                start_match = re.search(r'\d{4}', job_entries[i+1]["start_date"])
                
                if end_match and start_match:
                    end_year = int(end_match.group(0))
                    start_year = int(start_match.group(0))
                    
                    # If there's more than a 1-year gap
                    if start_year - end_year > 1:
                        fraud_signals.append(f"Employment gap: {end_year} to {start_year} ({start_year - end_year} years)")
    
    return fraud_signals

def predict_career_trajectory(job_entries, current_skills):
    """Predict logical next roles based on career progression"""
    # Career path mappings based on common progressions
    career_paths = {
        "software engineer": ["Senior Software Engineer", "Lead Developer", "Software Architect", "Engineering Manager", "CTO"],
        "developer": ["Senior Developer", "Technical Lead", "Software Architect", "Development Manager", "CTO"],
        "designer": ["Senior Designer", "Lead Designer", "Design Manager", "Creative Director", "VP of Design"],
        "data scientist": ["Senior Data Scientist", "Lead Data Scientist", "Data Science Manager", "Director of Analytics", "Chief Data Officer"]
    }
    
    # Extract current role from latest job entry
    current_role = job_entries[0]["title"].lower() if job_entries else "unknown"
    
    # Find the best matching career path
    best_match = None
    for role_key in career_paths:
        if role_key in current_role:
            best_match = role_key
            break
    
    if not best_match:
        return ["Career path prediction requires more information"]
    
    # Find current position in the career path
    current_index = 0
    for i, role in enumerate(career_paths[best_match]):
        if any(indicator in current_role for indicator in ["senior", "lead", "manager", "director", "vp", "chief"]):
            # If current role contains seniority indicators, advance the index
            if "senior" in current_role and "senior" in role.lower():
                current_index = i
                break
            elif "lead" in current_role and "lead" in role.lower():
                current_index = i
                break
            elif "manager" in current_role and "manager" in role.lower():
                current_index = i
                break
            elif "director" in current_role and "director" in role.lower():
                current_index = i
                break
    
    # Get next potential roles (up to 3)
    next_roles = []
    for i in range(current_index + 1, min(current_index + 4, len(career_paths[best_match]))):
        next_roles.append(career_paths[best_match][i])
    
    if not next_roles:
        next_roles = ["You're at a senior level in your career path. Consider lateral moves or industry specialization."]
    
    return next_roles

def analyze_resume(text, job_title, sentence_model):
    # Extract work experience
    job_entries = extract_work_experience(text)
    
    # Sort job entries by start date (most recent first)
    job_entries.sort(key=lambda x: "9999" if x["start_date"] == "Unknown" else x["start_date"], reverse=True)
    
    # Extract relevant skills with basic keyword matching
    doc = nlp(text.lower())
    found_skills = []
    required_skills = job_descriptions[job_title]["skills"]
    
    for skill in required_skills:
        if skill in text.lower():
            found_skills.append(skill)
    
    # Determine skill proficiency levels
    skill_proficiencies = {}
    for skill in found_skills:
        proficiency = estimate_skill_proficiency(text, skill)
        if proficiency:
            skill_proficiencies[skill] = proficiency
    
    # Calculate seniority score
    seniority_score, years_experience = calculate_seniority_score(job_entries)
    
    # Detect fraud signals
    fraud_signals = detect_fraud_signals(text, job_entries)
    
    # Predict career trajectory
    next_roles = predict_career_trajectory(job_entries, found_skills)
    
    # Generate summary
    chunks = [text[i:i + 1000] for i in range(0, len(text), 1000)]
    summaries = []
    for chunk in chunks[:3]:  # Process first 3000 characters to avoid token limits
        summary = summarizer(chunk, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
        summaries.append(summary)
    
    # Semantic matching with job description
    semantic_score = 0
    if sentence_model:
        try:
            resume_embedding = sentence_model.encode(text[:5000])  # Limit to first 5000 chars to avoid memory issues
            job_embedding = sentence_model.encode(job_descriptions[job_title]["semantic_description"])
            semantic_score = float(util.pytorch_cos_sim(resume_embedding, job_embedding)[0][0])
        except Exception as e:
            st.error(f"Error in semantic matching: {str(e)}")
    
    return {
        "found_skills": found_skills,
        "skill_proficiencies": skill_proficiencies,
        "summary": " ".join(summaries),
        "job_entries": job_entries,
        "seniority_score": seniority_score,
        "years_experience": years_experience,
        "fraud_signals": fraud_signals,
        "next_roles": next_roles,
        "semantic_score": semantic_score
    }

def generate_career_advice(resume_text, job_title, found_skills, missing_skills):
    """
    Generate career advice using a template-based approach instead of Qwen3-8B
    to avoid dependency issues
    """
    # Template-based advice generation
    advice = f"""## Career Development Plan for {job_title} Position

### Skills to Develop

The following skills would strengthen your resume for this position:

"""
    
    # Add advice for each missing skill
    for skill in missing_skills:
        if skill == "python":
            advice += f"""#### Python
- **How to develop**: Take online courses focused on Python for {job_title.lower()} applications
- **Project idea**: Build a data analysis tool or web application using Python and popular frameworks
- **Resources**: Coursera's Python for Everybody, Python.org tutorials, Real Python website

"""
        elif skill == "java":
            advice += f"""#### Java
- **How to develop**: Complete a comprehensive Java course with practical exercises
- **Project idea**: Develop a backend service with Spring Boot
- **Resources**: Oracle's Java tutorials, Udemy courses on Java, "Effective Java" by Joshua Bloch

"""
        elif skill == "javascript":
            advice += f"""#### JavaScript
- **How to develop**: Practice with modern JavaScript frameworks
- **Project idea**: Create an interactive web application with React or Vue.js
- **Resources**: MDN Web Docs, freeCodeCamp, "Eloquent JavaScript" by Marijn Haverbeke

"""
        elif skill == "sql":
            advice += f"""#### SQL
- **How to develop**: Practice with database design and complex queries
- **Project idea**: Design a database system for a small business with reports and analytics
- **Resources**: SQLZoo, Mode Analytics SQL tutorial, W3Schools SQL course

"""
        elif "algorithms" in skill or "data structures" in skill:
            advice += f"""#### Algorithms & Data Structures
- **How to develop**: Solve coding problems regularly on platforms like LeetCode
- **Project idea**: Implement classic algorithms and optimize them for specific use cases
- **Resources**: "Cracking the Coding Interview" book, AlgoExpert, Coursera Algorithms specialization

"""
        elif "git" in skill:
            advice += f"""#### Git & Version Control
- **How to develop**: Contribute to open source projects to practice Git workflows
- **Project idea**: Set up a personal project with proper branching strategies and CI/CD
- **Resources**: Git documentation, GitHub Learning Lab, Atlassian Git tutorials

"""
        elif "cloud" in skill:
            advice += f"""#### Cloud Technologies
- **How to develop**: Get hands-on experience with a major cloud provider (AWS, Azure, GCP)
- **Project idea**: Deploy an application to the cloud with proper infrastructure as code
- **Resources**: Cloud provider documentation, A Cloud Guru courses, free tier accounts

"""
        elif "ui" in skill or "ux" in skill:
            advice += f"""#### UI/UX Design
- **How to develop**: Study design principles and practice creating user interfaces
- **Project idea**: Redesign an existing website or app with focus on user experience
- **Resources**: Nielsen Norman Group articles, Interaction Design Foundation, Figma tutorials

"""
        elif "machine learning" in skill:
            advice += f"""#### Machine Learning
- **How to develop**: Take courses on ML fundamentals and practice with datasets
- **Project idea**: Build a predictive model to solve a real-world problem
- **Resources**: Andrew Ng's Coursera courses, Kaggle competitions, "Hands-On Machine Learning" book

"""
        elif "data analysis" in skill:
            advice += f"""#### Data Analysis
- **How to develop**: Practice analyzing datasets and creating visualizations
- **Project idea**: Perform an exploratory data analysis on a public dataset
- **Resources**: DataCamp courses, Kaggle datasets, "Python for Data Analysis" by Wes McKinney

"""
        else:
            advice += f"""#### {skill.title()}
- **How to develop**: Research industry best practices and take relevant courses
- **Project idea**: Create a portfolio piece that showcases this skill
- **Resources**: Online courses, industry blogs, and practice projects

"""
    
    # Add project recommendations based on job title
    advice += f"""
### Recommended Projects for {job_title}

Based on the target position and the skills needed, here are some project ideas:

"""
    if job_title == "Software Engineer":
        advice += """
1. **Full-Stack Web Application**: Build a complete web app with frontend, backend, and database
2. **API Service**: Create a RESTful or GraphQL API with proper authentication and documentation
3. **Mobile Application**: Develop a cross-platform mobile app using React Native or Flutter
4. **Automation Tools**: Build scripts or applications that automate repetitive tasks
5. **Contribution to Open Source**: Find a project aligned with your skills and contribute meaningfully

"""
    elif job_title == "Interaction Designer":
        advice += """
1. **Design System**: Create a comprehensive design system with components and usage guidelines
2. **Website Redesign**: Redesign an existing website with focus on improved UX
3. **Mobile App Prototype**: Design a fully interactive mobile app prototype
4. **User Research Project**: Conduct user research and create a report with insights and recommendations
5. **Design Case Study**: Document your design process for solving a specific problem

"""
    elif job_title == "Data Scientist":
        advice += """
1. **Predictive Model**: Build a machine learning model that solves a real-world problem
2. **Data Visualization Dashboard**: Create an interactive dashboard to visualize complex data
3. **Natural Language Processing**: Develop a text analysis or sentiment analysis project
4. **Time Series Analysis**: Analyze time-based data and build forecasting models
5. **A/B Testing Framework**: Design and implement a framework for testing hypotheses

"""
    
    # General advice for all positions
    advice += """
### Learning Resources

- **Online Platforms**: Coursera, Udemy, Pluralsight, LinkedIn Learning
- **Documentation**: Official language and framework documentation
- **Communities**: Stack Overflow, GitHub, Reddit programming communities
- **Books**: O'Reilly publications specific to your target technologies
- **YouTube Channels**: Traversy Media, Tech With Tim, freeCodeCamp

### Positioning Your Experience

- Highlight transferable skills from your current experience
- Quantify achievements with metrics where possible
- Frame previous work in terms relevant to the target position
- Create a tailored resume that emphasizes relevant projects and responsibilities
"""
    
    return advice

# Streamlit UI
st.title("πŸ“„ Comprehensive Resume Analyzer")

# Add description
st.markdown("""
This app helps recruiters and job seekers analyze resumes with advanced features:

- **Semantic Job Matching**: Uses AI to match resumes to job descriptions beyond keywords
- **Skill Proficiency Detection**: Identifies skill levels from context
- **Career Progression Analysis**: Visualizes job history and seniority
- **Fraud Detection**: Flags potential inconsistencies for verification
- **Career Path Prediction**: Suggests logical next roles based on experience
- **Personalized Development Advice**: Recommends skills, projects, and resources
""")

# Create two columns
col1, col2 = st.columns([2, 1])

with col1:
    # File upload
    uploaded_file = st.file_uploader("Upload Resume (PDF)", type=["pdf"])

with col2:
    # Job selection
    job_title = st.selectbox("Select Job Position", list(job_descriptions.keys()))
    
    # Show job description
    if job_title:
        st.info(f"**Job Description:**\n{job_descriptions[job_title]['description']}\n\n**Required Skills:**\n" + 
                "\n".join([f"- {skill.title()}" for skill in job_descriptions[job_title]["skills"]]))

if uploaded_file and job_title:
    try:
        # Show spinner while processing
        with st.spinner("Analyzing resume with advanced AI..."):
            # Extract text from PDF
            text = extract_text_from_pdf(uploaded_file)
            
            # Analyze resume
            analysis_results = analyze_resume(text, job_title, sentence_model)
            
            # Calculate missing skills
            missing_skills = [skill for skill in job_descriptions[job_title]["skills"] 
                            if skill not in analysis_results["found_skills"]]
        
        # Display results in tabs
        tab1, tab2, tab3, tab4, tab5, tab6 = st.tabs([
            "πŸ“Š Match Score", 
            "🎯 Skills Analysis", 
            "πŸ‘¨β€πŸ’Ό Experience", 
            "πŸ“ˆ Career Path", 
            "🚩 Verification", 
            "πŸš€ Career Advice"
        ])
        
        with tab1:
            # Display match scores
            st.subheader("πŸ“Š Job Match Analysis")
            
            # Calculate match scores
            keyword_match = len(analysis_results["found_skills"]) / len(job_descriptions[job_title]["skills"]) * 100
            semantic_match = analysis_results["semantic_score"] * 100
            
            # Display scores with gauges
            col1, col2 = st.columns(2)
            
            with col1:
                # Keyword match gauge
                fig = go.Figure(go.Indicator(
                    mode = "gauge+number",
                    value = keyword_match,
                    title = {'text': "Keyword Match"},
                    gauge = {
                        'axis': {'range': [0, 100]},
                        'bar': {'color': "darkblue"},
                        'steps': [
                            {'range': [0, 30], 'color': "lightgray"},
                            {'range': [30, 70], 'color': "gray"},
                            {'range': [70, 100], 'color': "lightblue"}
                        ],
                        'threshold': {
                            'line': {'color': "red", 'width': 4},
                            'thickness': 0.75,
                            'value': 70
                        }
                    }
                ))
                st.plotly_chart(fig, use_container_width=True)
            
            with col2:
                # Semantic match gauge
                fig = go.Figure(go.Indicator(
                    mode = "gauge+number",
                    value = semantic_match,
                    title = {'text': "Semantic Match"},
                    gauge = {
                        'axis': {'range': [0, 100]},
                        'bar': {'color': "darkgreen"},
                        'steps': [
                            {'range': [0, 30], 'color': "lightgray"},
                            {'range': [30, 70], 'color': "gray"},
                            {'range': [70, 100], 'color': "lightgreen"}
                        ],
                        'threshold': {
                            'line': {'color': "red", 'width': 4},
                            'thickness': 0.75,
                            'value': 70
                        }
                    }
                ))
                st.plotly_chart(fig, use_container_width=True)
            
            # Calculate overall match score (weighted average)
            overall_match = (keyword_match * 0.4) + (semantic_match * 0.6)
            
            # Create overall score gauge
            fig = go.Figure(go.Indicator(
                mode = "gauge+number+delta",
                value = overall_match,
                title = {'text': "Overall Match Score"},
                delta = {'reference': 75, 'increasing': {'color': "green"}},
                gauge = {
                    'axis': {'range': [0, 100]},
                    'bar': {'color': "darkblue"},
                    'steps': [
                        {'range': [0, 50], 'color': "lightgray"},
                        {'range': [50, 75], 'color': "gray"},
                        {'range': [75, 100], 'color': "darkblue"}
                    ],
                    'threshold': {
                        'line': {'color': "red", 'width': 4},
                        'thickness': 0.75,
                        'value': 75
                    }
                }
            ))
            
            st.plotly_chart(fig, use_container_width=True)
            
            # Display resume summary
            st.subheader("πŸ“ Resume Summary")
            st.write(analysis_results["summary"])
        
        with tab2:
            # Display skills analysis
            st.subheader("🎯 Skills Analysis")
            
            # Create two columns
            col1, col2 = st.columns(2)
            
            with col1:
                # Display matched skills with proficiency levels
                st.subheader("🟒 Skills Present")
                
                # Create a DataFrame for the skills table
                skills_data = []
                for skill in analysis_results["found_skills"]:
                    proficiency = analysis_results["skill_proficiencies"].get(skill, "Basic")
                    skills_data.append({
                        "Skill": skill.title(),
                        "Proficiency": proficiency
                    })
                
                if skills_data:
                    skills_df = pd.DataFrame(skills_data)
                    
                    # Add proficiency color coding
                    def color_proficiency(val):
                        if val == "Advanced":
                            return 'background-color: #d4f7d4'
                        elif val == "Intermediate":
                            return 'background-color: #fff2cc'
                        else:
                            return 'background-color: #f2f2f2'
                    
                    st.dataframe(skills_df.style.applymap(color_proficiency, subset=['Proficiency']), 
                                 use_container_width=True)
                else:
                    st.warning("No direct skill matches found.")
            
            with col2:
                # Display missing skills
                st.subheader("πŸ”΄ Skills to Develop")
                if missing_skills:
                    missing_df = pd.DataFrame({"Skill": [skill.title() for skill in missing_skills]})
                    st.dataframe(missing_df, use_container_width=True)
                else:
                    st.success("Great! The candidate has all the required skills!")
            
            # Create a radar chart for skills coverage
            st.subheader("Skills Coverage")
            
            # Prepare data for radar chart
            categories = job_descriptions[job_title]["skills"]
            values = [1 if skill in analysis_results["found_skills"] else 0 for skill in categories]
            
            # Create radar chart
            fig = go.Figure()
            
            fig.add_trace(go.Scatterpolar(
                r=values,
                theta=categories,
                fill='toself',
                name='Present Skills'
            ))
            
            fig.add_trace(go.Scatterpolar(
                r=[1] * len(categories),
                theta=categories,
                fill='toself',
                name='Required Skills',
                opacity=0.3
            ))
            
            fig.update_layout(
                polar=dict(
                    radialaxis=dict(
                        visible=True,
                        range=[0, 1]
                    )),
                showlegend=True
            )
            
            st.plotly_chart(fig, use_container_width=True)
        
        with tab3:
            # Display experience analysis
            st.subheader("πŸ‘¨β€πŸ’Ό Experience Analysis")
            
            # Display seniority metrics
            col1, col2 = st.columns(2)
            
            with col1:
                # Seniority score gauge
                fig = go.Figure(go.Indicator(
                    mode="gauge+number",
                    value=analysis_results["seniority_score"],
                    title={'text': "Seniority Score"},
                    gauge={
                        'axis': {'range': [0, 10]},
                        'bar': {'color': "darkblue"},
                        'steps': [
                            {'range': [0, 3], 'color': "lightgray"},
                            {'range': [3, 7], 'color': "gray"},
                            {'range': [7, 10], 'color': "lightblue"}
                        ],
                        'threshold': {
                            'line': {'color': "red", 'width': 4},
                            'thickness': 0.75,
                            'value': 7
                        }
                    }
                ))
                st.plotly_chart(fig, use_container_width=True)
            
            with col2:
                # Years of experience
                fig = go.Figure(go.Indicator(
                    mode="number+delta",
                    value=analysis_results["years_experience"],
                    number={'suffix': " years"},
                    title={"text": "Years of Experience"},
                    delta={'reference': 5, 'relative': False}
                ))
                st.plotly_chart(fig, use_container_width=True)
            
            # Display career progression timeline
            st.subheader("Career Progression Timeline")
            
            if analysis_results["job_entries"]:
                # Create timeline data
                timeline_data = []
                
                for job in analysis_results["job_entries"]:
                    # Extract years for visualization
                    start_year = re.search(r'\d{4}', job["start_date"])
                    end_year = re.search(r'\d{4}', job["end_date"]) if job["end_date"] != "Present" else None
                    
                    if start_year:
                        start_year = int(start_year.group(0))
                        end_year = int(end_year.group(0)) if end_year else datetime.now().year
                        
                        timeline_data.append({
                            "Role": job["title"],
                            "Company": job["company"],
                            "Start": start_year,
                            "End": end_year,
                            "Duration": end_year - start_year
                        })
                
                if timeline_data:
                    # Create DataFrame for timeline
                    timeline_df = pd.DataFrame(timeline_data)
                    
                    # Sort by start date (ascending)
                    timeline_df = timeline_df.sort_values(by="Start")
                    
                    # Create Gantt chart
                    fig = px.timeline(
                        timeline_df, 
                        x_start="Start", 
                        x_end="End", 
                        y="Company",
                        color="Role",
                        hover_data=["Duration"],
                        labels={"Company": "Employer"}
                    )
                    
                    fig.update_layout(
                        xaxis_title="Year",
                        yaxis_title="Employer",
                        title="Career Progression"
                    )
                    
                    st.plotly_chart(fig, use_container_width=True)
                else:
                    st.warning("Couldn't extract timeline data from the resume.")
            else:
                st.warning("No work experience entries found in the resume.")
        
        with tab4:
            # Display career path analysis
            st.subheader("πŸ“ˆ Career Path Analysis")
            
            # Display next role suggestions
            st.subheader("Suggested Next Roles")
            
            for i, role in enumerate(analysis_results["next_roles"]):
                st.info(f"**Option {i+1}:** {role}")
            
            # Add simple career progression visualization
            st.subheader("Career Progression Path")
            
            # Extract current role from latest job entry
            current_role = analysis_results["job_entries"][0]["title"] if analysis_results["job_entries"] else "Current Position"
            
            # Create nodes for career path
            career_nodes = [current_role] + analysis_results["next_roles"]
            
            # Create a simple digraph visualization
            career_df = pd.DataFrame({
                "From": [career_nodes[i] for i in range(len(career_nodes)-1)],
                "To": [career_nodes[i+1] for i in range(len(career_nodes)-1)],
                "Value": [10 for _ in range(len(career_nodes)-1)]
            })
            
            # Create a Sankey diagram
            fig = go.Figure(data=[go.Sankey(
                node=dict(
                    pad=15,
                    thickness=20,
                    line=dict(color="black", width=0.5),
                    label=career_nodes,
                    color="blue"
                ),
                link=dict(
                    source=[i for i in range(len(career_nodes)-1)],
                    target=[i+1 for i in range(len(career_nodes)-1)],
                    value=[1 for _ in range(len(career_nodes)-1)]
                )
            )])
            
            fig.update_layout(title_text="Potential Career Path", font_size=12)
            st.plotly_chart(fig, use_container_width=True)
        
        with tab5:
            # Display fraud detection analysis
            st.subheader("🚩 Verification Points")
            
            if analysis_results["fraud_signals"]:
                st.warning("The following points may require verification:")
                for signal in analysis_results["fraud_signals"]:
                    st.markdown(f"- {signal}")
            else:
                st.success("No significant inconsistencies detected in the resume.")
            
            # Add common verification tips
            st.subheader("Recommended Verification Steps")
            st.markdown("""
            Even when no inconsistencies are detected, consider these verification steps:
            
            1. **Reference Checks**: Contact previous employers to confirm employment dates and responsibilities
            2. **Skills Assessment**: Use technical interviews or tests to verify claimed skills
            3. **Education Verification**: Confirm degrees and certifications with educational institutions
            4. **Portfolio Review**: Examine work samples or project contributions
            5. **Online Presence**: Check LinkedIn, GitHub, or other professional profiles for consistency
            """)
        
        with tab6:
            # Display career advice
            st.subheader("πŸš€ Career Advice and Development Plan")
            
            if st.button("Generate Personalized Career Advice"):
                with st.spinner("Generating detailed career advice and development plan..."):
                    advice = generate_career_advice(text, job_title, analysis_results["found_skills"], missing_skills)
                    st.markdown(advice)
        
    except Exception as e:
        st.error(f"An error occurred while processing the resume: {str(e)}")
        st.exception(e)

# Add footer
st.markdown("---")
st.markdown("Made with ❀️ using Streamlit, Hugging Face, and Advanced AI")