File size: 35,533 Bytes
a928595
db455d9
9f115d1
 
556ee06
fbc936b
 
c1efc08
556ee06
 
 
c1efc08
 
556ee06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e84a893
556ee06
c1efc08
 
556ee06
 
 
 
c1efc08
 
a928595
7044586
556ee06
7044586
 
 
 
fbc936b
 
 
 
 
 
 
 
 
 
 
7044586
 
556ee06
 
c1efc08
556ee06
 
 
 
c1efc08
556ee06
 
 
 
 
 
 
c1efc08
556ee06
 
 
c1efc08
556ee06
7044586
 
556ee06
7044586
 
 
 
 
 
c1efc08
556ee06
 
 
 
 
 
 
7044586
 
 
 
c1efc08
556ee06
 
 
 
 
 
 
7044586
 
 
 
c1efc08
556ee06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7044586
 
 
 
 
db455d9
 
 
7044586
 
556ee06
 
7044586
 
 
 
 
 
 
 
 
 
 
 
 
 
 
556ee06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1efc08
 
556ee06
 
 
 
 
 
 
 
 
 
c1efc08
 
e84a893
556ee06
 
c1efc08
556ee06
 
 
 
 
c1efc08
556ee06
c1efc08
556ee06
c1efc08
556ee06
c1efc08
556ee06
c1efc08
556ee06
e84a893
556ee06
 
 
 
e84a893
 
556ee06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7044586
 
556ee06
7044586
 
 
556ee06
 
 
 
 
7044586
 
 
 
 
 
 
5bae67b
7044586
 
 
 
 
 
 
556ee06
7044586
 
 
 
 
556ee06
5bae67b
 
7044586
 
556ee06
c1efc08
 
 
556ee06
7044586
 
c1efc08
556ee06
 
 
 
 
c1efc08
 
7044586
 
556ee06
c1efc08
 
 
556ee06
 
 
 
 
 
 
 
 
 
 
 
 
 
c1efc08
 
556ee06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7044586
 
556ee06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1efc08
556ee06
 
 
c1efc08
 
 
 
 
556ee06
c1efc08
556ee06
 
c1efc08
 
 
 
556ee06
 
c1efc08
556ee06
 
 
c1efc08
556ee06
 
 
 
c1efc08
556ee06
c1efc08
556ee06
 
 
 
 
 
 
 
 
 
 
 
c1efc08
 
556ee06
 
c1efc08
556ee06
 
c1efc08
556ee06
 
c1efc08
556ee06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1efc08
 
556ee06
 
 
 
 
c1efc08
556ee06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1efc08
556ee06
c1efc08
556ee06
 
c1efc08
556ee06
 
 
 
 
 
c1efc08
 
 
556ee06
c1efc08
556ee06
 
 
c1efc08
556ee06
7044586
 
 
 
 
556ee06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pdfplumber
import io
import spacy
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
import subprocess
import sys
import torch
import re
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from datetime import datetime
import dateparser
from sentence_transformers import SentenceTransformer
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from sklearn.metrics.pairwise import cosine_similarity
import faiss
import requests
from bs4 import BeautifulSoup
import networkx as nx
import Levenshtein
import json
import matplotlib.pyplot as plt
from io import BytesIO
import base64
from sentence_transformers import util

# Download NLTK resources
@st.cache_resource
def download_nltk_resources():
    nltk.download('punkt')
    nltk.download('stopwords')
    nltk.download('wordnet')
    nltk.download('averaged_perceptron_tagger')

download_nltk_resources()

st.set_page_config(
    page_title="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 model for semantic matching
    sentence_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
    
    # Load Qwen3-8B model for career advice
    try:
        device = "cuda" if torch.cuda.is_available() else "cpu"
        qwen_tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-8B")
        qwen_model = AutoModelForCausalLM.from_pretrained(
            "Qwen/Qwen3-8B",
            torch_dtype=torch.float16 if device == "cuda" else torch.float32,
            device_map="auto"
        )
    except Exception as e:
        st.error(f"Failed to load Qwen3-8B model: {str(e)}")
        qwen_tokenizer = None
        qwen_model = None
    
    return summarizer, nlp, qwen_tokenizer, qwen_model, sentence_model

# Initialize models
summarizer, nlp, qwen_tokenizer, qwen_model, 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.",
        "must_have": ["python", "git", "algorithms"],
        "nice_to_have": ["cloud", "java", "javascript"],
        "seniority_levels": {
            "Junior": "0-2 years of experience, familiar with basic programming concepts",
            "Mid-level": "3-5 years of experience, proficient in multiple languages, experience with system design",
            "Senior": "6+ years of experience, expert in software architecture, mentoring, and leading projects"
        }
    },
    "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.",
        "must_have": ["ui", "ux", "prototyping"],
        "nice_to_have": ["figma", "sketch", "user research"],
        "seniority_levels": {
            "Junior": "0-2 years of experience, basic design skills, understanding of UX principles",
            "Mid-level": "3-5 years of experience, strong portfolio, experience with user research",
            "Senior": "6+ years of experience, leadership in design systems, driving design strategy"
        }
    },
    "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.",
        "must_have": ["python", "statistics", "machine learning"],
        "nice_to_have": ["tensorflow", "pytorch", "r"],
        "seniority_levels": {
            "Junior": "0-2 years of experience, basic knowledge of statistics and ML algorithms",
            "Mid-level": "3-5 years of experience, model development, feature engineering",
            "Senior": "6+ years of experience, advanced ML techniques, research experience"
        }
    },
    "Product Manager": {
        "skills": ["product strategy", "roadmap planning", "user stories", "agile", "market research", 
                   "stakeholder management", "analytics", "user experience", "a/b testing", "prioritization"],
        "description": "Seeking product managers who can drive product vision, strategy, and execution.",
        "must_have": ["product strategy", "roadmap planning", "stakeholder management"],
        "nice_to_have": ["agile", "analytics", "a/b testing"],
        "seniority_levels": {
            "Junior": "0-2 years of experience, assisting with feature definition and user stories",
            "Mid-level": "3-5 years of experience, owning products/features, market research",
            "Senior": "6+ years of experience, defining product vision, managing teams, strategic planning"
        }
    },
    "DevOps Engineer": {
        "skills": ["linux", "aws", "docker", "kubernetes", "ci/cd", "terraform", 
                   "ansible", "monitoring", "scripting", "automation", "security"],
        "description": "Looking for DevOps engineers to build and maintain infrastructure and deployment pipelines.",
        "must_have": ["linux", "docker", "ci/cd"],
        "nice_to_have": ["kubernetes", "terraform", "aws"],
        "seniority_levels": {
            "Junior": "0-2 years of experience, basic system administration, scripting",
            "Mid-level": "3-5 years of experience, container orchestration, infrastructure as code",
            "Senior": "6+ years of experience, architecture design, security, team leadership"
        }
    }
}

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 analyze_resume(text, job_title):
    # Extract relevant skills
    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)
    
    # 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)
    
    # Extract experience timeline
    experiences = extract_experience(text)
    
    # Calculate semantic match score
    match_score = semantic_matching(text, job_title)
    
    # Estimate seniority
    seniority, years_experience, leadership_count, must_have_percentage = estimate_seniority(experiences, found_skills, job_title)
    
    # Extract skill levels
    skill_levels = extract_skill_levels(text, found_skills)
    
    # Check for timeline inconsistencies
    inconsistencies = check_timeline_inconsistencies(experiences)
    
    # Verify companies
    company_verification = verify_companies(experiences)
    
    # Predict career trajectory
    career_prediction = predict_career_trajectory(experiences, seniority, job_title)
    
    return {
        'found_skills': found_skills,
        'summary': " ".join(summaries),
        'experiences': experiences,
        'match_score': match_score,
        'seniority': seniority,
        'years_experience': years_experience,
        'skill_levels': skill_levels,
        'inconsistencies': inconsistencies,
        'company_verification': company_verification,
        'career_prediction': career_prediction
    }

def generate_career_advice(resume_text, job_title, found_skills, missing_skills):
    if qwen_model is None or qwen_tokenizer is None:
        return "Career advice model not available. Please check the model installation."
    
    # Create a prompt for the model
    prompt = f"""
You are a professional career advisor. Based on the resume and the target job position, 
provide personalized advice on skills to develop and suggest projects that would help the candidate 
become a better fit for the position.

Resume summary: {resume_text[:1000]}...

Target position: {job_title}

Job requirements: {job_descriptions[job_title]['description']}

Skills the candidate has: {', '.join(found_skills)}

Skills the candidate needs to develop: {', '.join(missing_skills)}

Provide the following:
1. Specific advice on how to develop the missing skills
2. 3-5 project ideas that would showcase these skills
3. Resources for learning (courses, books, websites)
"""

    # Generate advice using Qwen3-8B
    try:
        inputs = qwen_tokenizer(prompt, return_tensors="pt").to(qwen_model.device)
        with torch.no_grad():
            outputs = qwen_model.generate(
                **inputs,
                max_new_tokens=1024,
                temperature=0.7,
                top_p=0.9,
                do_sample=True
            )
        advice = qwen_tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
        return advice
    except Exception as e:
        return f"Failed to generate career advice: {str(e)}"

# Streamlit UI
st.title("πŸ“„ Resume Screener & Skill Extractor")

# Add description
st.markdown("""
This app helps recruiters analyze resumes by:
- Extracting relevant skills for specific job positions
- Generating a concise summary of the candidate's background
- Identifying skill gaps for the selected role
- Providing personalized career advice and project recommendations
""")

# 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"**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..."):
            # Extract text from PDF
            text = extract_text_from_pdf(uploaded_file)
            
            # Analyze resume
            resume_data = analyze_resume(text, job_title)
            
            # Calculate missing skills
            missing_skills = [skill for skill in job_descriptions[job_title]["skills"] 
                            if skill not in resume_data['found_skills']]
        
        # Display results in tabs
        tab1, tab2, tab3, tab4, tab5, tab6 = st.tabs([
            "πŸ“Š Skills Match", 
            "πŸ“ Resume Summary", 
            "🎯 Skills Gap", 
            "πŸ‘¨β€πŸ’Ό Career Path", 
            "πŸ” Authentication", 
            "πŸš€ Career Advice"
        ])
        
        with tab1:
            # First create columns for skill match percentage and semantic match
            col1, col2 = st.columns(2)
            
            with col1:
                # Display matched skills
                st.subheader("🎯 Matched Skills")
                if resume_data['found_skills']:
                    for skill in resume_data['found_skills']:
                        # Show skill with proficiency level
                        level = resume_data['skill_levels'].get(skill, 'intermediate')
                        level_emoji = "🟒" if level == 'advanced' else "🟑" if level == 'intermediate' else "🟠"
                        st.success(f"{level_emoji} {skill.title()} ({level.title()})")
                    
                    # Calculate match percentage
                    match_percentage = len(resume_data['found_skills']) / len(job_descriptions[job_title]["skills"]) * 100
                    st.metric("Skills Match", f"{match_percentage:.1f}%")
                else:
                    st.warning("No direct skill matches found.")
            
            with col2:
                # Display semantic match score
                st.subheader("πŸ’‘ Semantic Match")
                st.metric("Overall Match Score", f"{resume_data['match_score']:.1f}%")
                
                # Display must-have skills match
                must_have_skills = job_descriptions[job_title]["must_have"]
                must_have_count = sum(1 for skill in must_have_skills if skill in resume_data['found_skills'])
                must_have_percentage = (must_have_count / len(must_have_skills)) * 100
                
                st.write("Must-have skills:")
                st.progress(must_have_percentage / 100)
                st.write(f"{must_have_count} out of {len(must_have_skills)} ({must_have_percentage:.1f}%)")
                
                # Professional level assessment
                st.subheader("🧠 Seniority Assessment")
                st.info(f"**{resume_data['seniority']}** ({resume_data['years_experience']:.1f} years equivalent experience)")
                st.write(job_descriptions[job_title]["seniority_levels"][resume_data['seniority']])
        
        with tab2:
            # Display resume summary
            st.subheader("πŸ“ Resume Summary")
            st.write(resume_data['summary'])
            
            # Display experience timeline
            st.subheader("⏳ Experience Timeline")
            if resume_data['experiences']:
                # Convert experiences to dataframe for display
                exp_data = []
                for exp in resume_data['experiences']:
                    if 'start_date' in exp and 'end_date' in exp:
                        exp_data.append({
                            'Company': exp['company'],
                            'Role': exp['role'],
                            'Start Date': exp['start_date'].strftime('%b %Y') if exp['start_date'] else 'Unknown',
                            'End Date': exp['end_date'].strftime('%b %Y') if exp['end_date'] != datetime.now() else 'Present',
                            'Duration (months)': exp.get('duration_months', 'Unknown')
                        })
                    else:
                        exp_data.append({
                            'Company': exp['company'],
                            'Role': exp['role'],
                            'Duration': exp.get('duration', 'Unknown')
                        })
                
                if exp_data:
                    exp_df = pd.DataFrame(exp_data)
                    st.dataframe(exp_df)
                    
                    # Create a timeline visualization if dates are available
                    timeline_data = [exp for exp in resume_data['experiences'] if 'start_date' in exp and 'end_date' in exp]
                    if timeline_data:
                        # Sort by start date
                        timeline_data = sorted(timeline_data, key=lambda x: x['start_date'])
                        
                        # Create figure
                        fig = go.Figure()
                        
                        for i, exp in enumerate(timeline_data):
                            fig.add_trace(go.Bar(
                                x=[(exp['end_date'] - exp['start_date']).days / 30],  # Duration in months
                                y=[exp['company']],
                                orientation='h',
                                name=exp['role'],
                                hovertext=f"{exp['role']} at {exp['company']}<br>{exp['start_date'].strftime('%b %Y')} - {exp['end_date'].strftime('%b %Y') if exp['end_date'] != datetime.now() else 'Present'}<br>Duration: {exp.get('duration_months', 0)} months",
                                marker=dict(color=px.colors.qualitative.Plotly[i % len(px.colors.qualitative.Plotly)])
                            ))
                        
                        fig.update_layout(
                            title="Career Timeline",
                            xaxis_title="Duration (months)",
                            yaxis_title="Company",
                            height=400,
                            margin=dict(l=0, r=0, b=0, t=30)
                        )
                        
                        st.plotly_chart(fig, use_container_width=True)
            else:
                st.warning("No work experience data could be extracted.")
        
        with tab3:
            # Display missing skills
            st.subheader("πŸ“Œ Skills to Develop")
            
            # Create two columns
            col1, col2 = st.columns(2)
            
            with col1:
                # Missing skills
                if missing_skills:
                    for skill in missing_skills:
                        st.warning(f"βž– {skill.title()}")
                else:
                    st.success("Great! The candidate has all the required skills!")
            
            with col2:
                # Skills gap analysis
                st.subheader("πŸ” Gap Analysis")
                
                # Show must-have skills that are missing
                missing_must_have = [skill for skill in job_descriptions[job_title]["must_have"] 
                                   if skill not in resume_data['found_skills']]
                
                if missing_must_have:
                    st.error("**Critical Skills Missing:**")
                    for skill in missing_must_have:
                        st.write(f"- {skill.title()}")
                    
                    st.markdown("These are must-have skills for this position.")
                else:
                    st.success("Candidate has all the must-have skills for this position!")
                
                # Show nice-to-have skills gap
                missing_nice_to_have = [skill for skill in job_descriptions[job_title]["nice_to_have"]
                                      if skill not in resume_data['found_skills']]
                
                if missing_nice_to_have:
                    st.warning("**Nice-to-Have Skills Missing:**")
                    for skill in missing_nice_to_have:
                        st.write(f"- {skill.title()}")
                else:
                    st.success("Candidate has all the nice-to-have skills!")
        
        with tab4:
            # Display career path insights
            st.subheader("πŸ‘¨β€πŸ’Ό Career Trajectory")
            
            # Show career prediction
            st.info(resume_data['career_prediction'])
            
            # Show experience trends
            st.subheader("πŸ“ˆ Experience Analysis")
            
            # Check for job hopping
            if len(resume_data['experiences']) >= 3:
                # Calculate average job duration
                durations = [exp.get('duration_months', 0) for exp in resume_data['experiences'] 
                            if 'duration_months' in exp]
                
                if durations:
                    avg_duration = sum(durations) / len(durations)
                    
                    if avg_duration < 12:
                        st.warning(f"🚩 **Frequent Job Changes**: Average job duration is only {avg_duration:.1f} months")
                    elif avg_duration < 24:
                        st.warning(f"⚠️ **Moderate Job Hopping**: Average job duration is {avg_duration:.1f} months")
                    else:
                        st.success(f"βœ… **Stable Employment**: Average job duration is {avg_duration:.1f} months")
            
            # Show inconsistencies if any
            if resume_data['inconsistencies']:
                st.subheader("⚠️ Timeline Inconsistencies")
                for issue in resume_data['inconsistencies']:
                    if issue['type'] == 'overlap':
                        st.warning(issue['description'])
                    elif issue['type'] == 'gap':
                        st.info(issue['description'])
        
        with tab5:
            # Display authentication signals
            st.subheader("πŸ” Resume Authentication")
            
            # Company verification results
            st.write("**Company Verification Results:**")
            
            if resume_data['company_verification']:
                # Count suspicious companies
                suspicious_count = sum(1 for v in resume_data['company_verification'] 
                                    if v['status'] == 'suspicious')
                
                if suspicious_count == 0:
                    st.success("βœ… All companies mentioned in the resume passed basic verification")
                else:
                    st.warning(f"⚠️ {suspicious_count} companies require further verification")
                
                # Display verification details
                verification_data = [{
                    'Company': v['company'],
                    'Status': v['status'].title(),
                    'Notes': v['reason']
                } for v in resume_data['company_verification']]
                
                st.dataframe(pd.DataFrame(verification_data))
            else:
                st.info("No company information found for verification.")
            
            # Timeline consistency check
            st.write("**Timeline Consistency Check:**")
            
            if not resume_data['inconsistencies']:
                st.success("βœ… No timeline inconsistencies detected")
            else:
                st.warning(f"⚠️ {len(resume_data['inconsistencies'])} timeline inconsistencies found")
                for issue in resume_data['inconsistencies']:
                    st.write(f"- {issue['description']}")
        
        with tab6:
            # Display career advice
            st.subheader("πŸš€ Career Advice and Project Recommendations")
            
            if st.button("Generate Career Advice"):
                with st.spinner("Generating personalized career advice..."):
                    advice = generate_career_advice(text, job_title, resume_data['found_skills'], missing_skills)
                    st.markdown(advice)
    
    except Exception as e:
        st.error(f"An error occurred while processing the resume: {str(e)}")

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

# Semantic matching between resume and job description
def semantic_matching(resume_text, job_title):
    job_desc = job_descriptions[job_title]["description"]
    
    # Encode texts using sentence transformers
    resume_embedding = sentence_model.encode(resume_text, convert_to_tensor=True)
    job_embedding = sentence_model.encode(job_desc, convert_to_tensor=True)
    
    # Calculate cosine similarity
    cos_sim = cosine_similarity(
        resume_embedding.cpu().numpy().reshape(1, -1), 
        job_embedding.cpu().numpy().reshape(1, -1)
    )[0][0]
    
    return cos_sim * 100  # Convert to percentage

# Extract experience timeline from resume
def extract_experience(text):
    # Pattern to find work experience entries
    # Look for patterns like "Company Name | Role | Jan 2020 - Present"
    exp_pattern = r"(?i)(.*?(?:inc|llc|ltd|company|corp|corporation|group)?)\s*(?:[|β€’-]\s*)?(.*?)(?:[|β€’-]\s*)((?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[\w\s,]*\d{4}\s*(?:-|to|–)\s*(?:(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[\w\s,]*\d{4}|present))"
    
    experiences = []
    for match in re.finditer(exp_pattern, text, re.IGNORECASE):
        company = match.group(1).strip()
        role = match.group(2).strip()
        duration = match.group(3).strip()
        
        # Parse dates
        try:
            date_range = duration.split('-') if '-' in duration else duration.split('to') if 'to' in duration else duration.split('–')
            start_date = dateparser.parse(date_range[0].strip())
            
            if 'present' in date_range[1].lower():
                end_date = datetime.now()
            else:
                end_date = dateparser.parse(date_range[1].strip())
                
            if start_date and end_date:
                # Calculate duration in months
                months = (end_date.year - start_date.year) * 12 + (end_date.month - start_date.month)
                
                experiences.append({
                    'company': company,
                    'role': role,
                    'start_date': start_date,
                    'end_date': end_date,
                    'duration_months': months
                })
        except:
            # If date parsing fails, still include the experience without dates
            experiences.append({
                'company': company,
                'role': role,
                'duration': duration
            })
    
    return experiences

# Estimate seniority based on experience and skills
def estimate_seniority(experiences, found_skills, job_title):
    # Calculate total experience in years
    total_months = sum(exp.get('duration_months', 0) for exp in experiences if 'duration_months' in exp)
    total_years = total_months / 12
    
    # Count leadership keywords in roles
    leadership_keywords = ['lead', 'senior', 'manager', 'head', 'principal', 'architect', 'director']
    leadership_count = 0
    
    for exp in experiences:
        role = exp.get('role', '').lower()
        for keyword in leadership_keywords:
            if keyword in role:
                leadership_count += 1
                break
    
    # Calculate skill match percentage for must-have skills
    must_have_skills = job_descriptions[job_title]["must_have"]
    must_have_count = sum(1 for skill in must_have_skills if skill in [s.lower() for s in found_skills])
    must_have_percentage = (must_have_count / len(must_have_skills)) * 100 if must_have_skills else 0
    
    # Determine seniority level
    if total_years < 3:
        seniority = "Junior"
    elif total_years < 6:
        seniority = "Mid-level"
    else:
        seniority = "Senior"
    
    # Adjust based on leadership roles and skill match
    if leadership_count >= 2 and seniority != "Senior":
        seniority = "Senior" if total_years >= 4 else seniority
    if must_have_percentage < 50 and seniority == "Senior":
        seniority = "Mid-level"
    
    return seniority, total_years, leadership_count, must_have_percentage

# Check for timeline inconsistencies
def check_timeline_inconsistencies(experiences):
    if not experiences:
        return []
    
    inconsistencies = []
    sorted_experiences = sorted(
        [exp for exp in experiences if 'start_date' in exp and 'end_date' in exp],
        key=lambda x: x['start_date']
    )
    
    for i in range(len(sorted_experiences) - 1):
        current = sorted_experiences[i]
        next_exp = sorted_experiences[i + 1]
        
        # Check for overlapping full-time roles
        if current['end_date'] > next_exp['start_date']:
            overlap_months = (current['end_date'].year - next_exp['start_date'].year) * 12 + \
                            (current['end_date'].month - next_exp['start_date'].month)
            
            if overlap_months > 1:  # Allow 1 month overlap for transitions
                inconsistencies.append({
                    'type': 'overlap',
                    'description': f"Overlapping roles: {current['company']} and {next_exp['company']} " + 
                                   f"overlap by {overlap_months} months"
                })
    
    # Check for gaps in employment
    for i in range(len(sorted_experiences) - 1):
        current = sorted_experiences[i]
        next_exp = sorted_experiences[i + 1]
        
        gap_months = (next_exp['start_date'].year - current['end_date'].year) * 12 + \
                     (next_exp['start_date'].month - current['end_date'].month)
        
        if gap_months > 3:  # Flag gaps longer than 3 months
            inconsistencies.append({
                'type': 'gap',
                'description': f"Employment gap of {gap_months} months between " + 
                               f"{current['company']} and {next_exp['company']}"
            })
    
    return inconsistencies

# Verify company existence (simplified version)
def verify_companies(experiences):
    verification_results = []
    
    for exp in experiences:
        company = exp.get('company', '')
        if not company:
            continue
            
        # Simple heuristic - companies less than 3 characters are suspicious
        if len(company) < 3:
            verification_results.append({
                'company': company,
                'status': 'suspicious',
                'reason': 'Company name too short'
            })
            continue
            
        # Check if company matches common fake patterns
        fake_patterns = ['abc company', 'xyz corp', 'my company', 'personal project']
        if any(pattern in company.lower() for pattern in fake_patterns):
            verification_results.append({
                'company': company,
                'status': 'suspicious',
                'reason': 'Matches pattern of fake company names'
            })
            continue
            
        # In a real implementation, you'd call an API to check if the company exists
        # For this demo, we'll just mark all others as verified
        verification_results.append({
            'company': company,
            'status': 'verified',
            'reason': 'Passed basic verification checks'
        })
    
    return verification_results

# Extract skill levels from text
def extract_skill_levels(text, skills):
    skill_levels = {}
    proficiency_indicators = {
        'basic': ['basic', 'familiar', 'beginner', 'fundamentals', 'exposure'],
        'intermediate': ['intermediate', 'proficient', 'experienced', 'competent', 'skilled'],
        'advanced': ['advanced', 'expert', 'mastery', 'specialist', 'lead', 'senior']
    }
    
    for skill in skills:
        # Look for sentences containing the skill
        sentences = re.findall(r'[^.!?]*%s[^.!?]*[.!?]' % re.escape(skill), text.lower())
        
        # Default level
        level = 'intermediate'
        
        # Check for years of experience indicators
        years_pattern = re.compile(r'(\d+)\s*(?:\+)?\s*years?(?:\s+of)?\s+(?:experience|exp)?\s+(?:with|in|using)?\s+%s' % re.escape(skill), re.IGNORECASE)
        for sentence in sentences:
            years_match = years_pattern.search(sentence)
            if years_match:
                years = int(years_match.group(1))
                if years < 2:
                    level = 'basic'
                elif years < 5:
                    level = 'intermediate'
                else:
                    level = 'advanced'
                break
        
        # Check for proficiency indicators
        if level == 'intermediate':  # Only override if not already set by years
            for level_name, indicators in proficiency_indicators.items():
                for indicator in indicators:
                    pattern = re.compile(r'%s\s+(?:\w+\s+){0,3}%s' % (indicator, re.escape(skill)), re.IGNORECASE)
                    if any(pattern.search(sentence) for sentence in sentences):
                        level = level_name
                        break
                if level != 'intermediate':
                    break
        
        skill_levels[skill] = level
    
    return skill_levels

# Generate career trajectory prediction
def predict_career_trajectory(experiences, seniority, job_title):
    if not experiences:
        return "Unable to predict trajectory due to insufficient experience data."
    
    # Extract roles in chronological order
    roles = [exp.get('role', '').lower() for exp in experiences if 'role' in exp]
    
    # If less than 2 roles, not enough data for prediction
    if len(roles) < 2:
        if seniority == "Junior":
            next_role = "Mid-level " + job_title
        elif seniority == "Mid-level":
            next_role = "Senior " + job_title
        else:  # Senior
            leadership_titles = {
                "Software Engineer": "Technical Lead or Engineering Manager",
                "Data Scientist": "Lead Data Scientist or Data Science Manager",
                "Interaction Designer": "Design Lead or UX Director",
                "Product Manager": "Senior Product Manager or Director of Product",
                "DevOps Engineer": "DevOps Lead or Infrastructure Architect"
            }
            next_role = leadership_titles.get(job_title, f"Director of {job_title}")
        
        return f"Based on current seniority level, the next logical role could be: {next_role}"
    
    # Check for upward mobility patterns
    progression_indicators = ['junior', 'senior', 'lead', 'manager', 'director', 'vp', 'head', 'chief']
    current_level = -1
    
    for role in roles:
        for i, indicator in enumerate(progression_indicators):
            if indicator in role:
                if i > current_level:
                    current_level = i
    
    # Predict next role based on current level
    if current_level < len(progression_indicators) - 1:
        next_level = progression_indicators[current_level + 1]
        
        # Map to specific job titles
        if next_level == 'senior' and 'senior' not in roles[-1].lower():
            next_role = f"Senior {job_title}"
        elif next_level == 'lead':
            next_role = f"{job_title} Lead" 
        elif next_level == 'manager':
            if job_title == "Software Engineer":
                next_role = "Engineering Manager"
            else:
                next_role = f"{job_title} Manager"
        elif next_level == 'director':
            next_role = f"Director of {job_title}s"
        elif next_level == 'vp':
            next_role = f"VP of {job_title}s"
        elif next_level == 'head':
            next_role = f"Head of {job_title}"
        elif next_level == 'chief':
            if job_title == "Software Engineer":
                next_role = "CTO (Chief Technology Officer)"
            elif job_title == "Data Scientist":
                next_role = "Chief Data Officer"
            elif job_title == "Product Manager":
                next_role = "Chief Product Officer"
            else:
                next_role = f"Chief {job_title} Officer"
        else:
            next_role = f"{next_level.title()} {job_title}"
    else:
        next_role = "Executive Leadership or Strategic Advisory roles"
    
    return f"Based on career progression, the next logical role could be: {next_role}"