File size: 30,993 Bytes
e536d23
3169aa3
e536d23
9322a5c
9b6107a
a6d809c
e536d23
a6d809c
e536d23
508d57a
af5273e
e7d4b15
7feb796
 
9b6107a
e536d23
bac5ff8
f71992d
e7d4b15
bac5ff8
f71992d
 
 
8a984bd
a6ec92d
d74b1fa
 
bac5ff8
f71992d
9b6107a
 
 
 
 
 
 
 
 
 
d74b1fa
f71992d
 
edcaccf
f71992d
bac5ff8
 
 
9b6107a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b456374
9b6107a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63bd30c
 
 
 
 
edcaccf
9b6107a
e7d4b15
 
 
 
 
 
 
 
 
9b6107a
 
 
e7d4b15
 
 
 
 
 
 
 
 
9b6107a
e7d4b15
 
 
 
d74b1fa
 
 
34cbce4
a6ec92d
9b6107a
d74b1fa
877fe6b
 
 
 
 
 
 
 
 
 
 
 
d74b1fa
e7d4b15
d74b1fa
990e041
9b6107a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f71992d
a6d809c
 
9b6107a
d74b1fa
 
149b30a
9b6107a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
508d57a
81a8057
 
9b6107a
 
 
 
 
 
 
 
 
 
 
 
81a8057
 
d74b1fa
9b6107a
 
 
 
 
 
 
 
 
 
 
 
3b72702
 
 
9b6107a
 
 
 
3b72702
 
cd4a730
9b6107a
a6d809c
9b6107a
 
 
 
 
 
 
 
 
 
 
a6ec92d
9b6107a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4299b38
9b6107a
 
 
5fd9f8e
9b6107a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3169aa3
9b6107a
 
 
f71992d
9b6107a
 
 
1701c03
49d861a
 
7feb796
49d861a
 
f71992d
49d861a
 
 
 
 
5fd9f8e
f71992d
7feb796
5fd9f8e
 
7feb796
5fd9f8e
 
 
b456374
7feb796
 
 
 
 
9b6107a
7feb796
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edcaccf
7feb796
508d57a
7feb796
34cbce4
9b6107a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3169aa3
9b6107a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6ec92d
bfe1780
af5273e
9b6107a
 
af5273e
9b6107a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3169aa3
9b6107a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3169aa3
9b6107a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fd9f8e
9b6107a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edcaccf
5fd9f8e
9b6107a
 
7feb796
edcaccf
9b6107a
 
 
 
 
 
 
 
7feb796
9b6107a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3169aa3
 
 
 
 
 
7feb796
3169aa3
 
 
 
7feb796
 
 
3169aa3
5fd9f8e
3169aa3
5fd9f8e
 
 
f71992d
9b6107a
5fd9f8e
 
 
 
7feb796
 
5fd9f8e
edcaccf
 
 
 
 
 
 
5fd9f8e
 
9b6107a
5fd9f8e
 
 
3169aa3
5fd9f8e
edcaccf
5fd9f8e
edcaccf
5fd9f8e
edcaccf
bfe1780
cd4a730
bfe1780
 
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
# app.py
# Modern Dark Mode Streamlit Application for AI Talent Screening (FINAL - Remove Coloring, Increase Max Profiles to 7)

import streamlit as st
# Import necessary libraries
from transformers import BertTokenizer, BertForSequenceClassification, T5Tokenizer, T5ForConditionalGeneration
import torch
import numpy as np
import re
import io
import time
import pandas as pd
import PyPDF2
from docx import Document
import plotly.express as px # Import Plotly for stable charting

# Set page config with modern dark theme and wide layout
st.set_page_config(
    page_title="AI Data/Tech Talent Screening Tool", 
    page_icon="πŸš€", 
    layout="wide",
    initial_sidebar_state="expanded",
)

# --- CUSTOM MODERN DARK MODE CSS OVERHAUL (UNCHANGED) ---
st.markdown("""
    <style>
    /* 0. GLOBAL CONFIG & DARK THEME */
    :root {
        --primary-color: #42A5F5; /* Vibrant Blue (Accent) */
        --accent-gradient-start: #4F46E5; /* Deep Purple-Blue */
        --accent-gradient-end: #3B82F6; /* Brighter Blue */
        --success-color: #4CAF50; /* Green (Good Match) */
        --warning-color: #FFC107; /* Amber/Yellow (Review) */
        --danger-color: #F44336; /* Red (Irrelevant/Error) */
        --background-color: #1A1C20; /* Very Dark, Deep Background */
        --container-background: #23272F; /* Slightly Lighter Container */
        --text-color: #F8F8F8; /* Light Text */
        --secondary-text-color: #B0B0B0; /* Muted Light Gray */
    }
    
    .main {
        background-color: var(--background-color);
        color: var(--text-color);
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }
    
    .stApp {
        background-color: var(--background-color);
    }

    /* 1. HEADER & TITLES - GRADIENT AND NO BLUE STROKE */
    h1 {
        text-align: center;
        /* Applying Text Gradient to H1 */
        background: linear-gradient(90deg, var(--accent-gradient-start) 0%, var(--accent-gradient-end) 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        font-size: 2.8em;
        font-weight: 800;
        border-bottom: 3px solid rgba(66, 165, 245, 0.3);
        padding-bottom: 15px;
        margin-bottom: 30px;
    }
    h2, h3, h4 {
        color: var(--text-color);
        border-left: none; 
        padding-left: 0;
        margin-top: 30px;
        font-weight: 600;
    }
    
    /* 2. BUTTONS & HOVER EFFECTS */
    .stButton>button {
        color: var(--text-color) !important;
        border: none !important;
        background-color: var(--container-background) !important;
        border-radius: 12px; 
        transition: all 0.3s ease;
        box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
        font-weight: 600;
    }
    .stButton>button:hover {
        background-color: #404040 !important;
        box-shadow: 0 6px 15px rgba(0, 0, 0, 0.5); 
        transform: translateY(-2px);
    }
    /* Primary Button with Gradient */
    .stButton>button[kind="primary"] {
        color: white !important;
        background: linear-gradient(90deg, var(--accent-gradient-start) 0%, var(--accent-gradient-end) 100%) !important;
    }
    .stButton>button[kind="primary"]:hover {
        background: linear-gradient(90deg, #3B82F6 0%, #4F46E5 100%) !important;
    }
    
    /* 3. INPUTS, CONTAINERS, TABS & SIDEBAR */
    .stSidebar {
        background-color: #23272F; 
        border-right: 1px solid #3A3A3A;
        color: var(--text-color);
        min-width: 250px; 
    }
    
    /* Fix: Ensure text in sidebar expanders is visible */
    [data-testid="stSidebar"] p, 
    [data-testid="stSidebar"] li,
    [data-testid="stSidebar"] [data-testid="stExpander"] {
        color: var(--secondary-text-color) !important; 
    }
    
    /* Fix: Condense paragraph spacing in Quick Guide (Sidebar) */
    .stSidebar .stExpanderContent p {
        margin-block-start: 0.5em !important; 
        margin-block-end: 0.5em !important;
    }

    /* Scorecard Style (Tiles) */
    .scorecard-block {
        border: 1px solid #3A3A3A; 
        border-radius: 12px; 
        padding: 20px;
        margin: 5px 0;
        background-color: #333333;
        transition: all 0.3s;
        box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    }
    .scorecard-block:hover {
        box-shadow: 0 6px 15px rgba(0, 0, 0, 0.4);
    }
    .scorecard-value {
        font-size: 38px;
        font-weight: 800;
        color: var(--primary-color);
    }
    .scorecard-label {
        font-size: 14px;
        color: var(--secondary-text-color);
    }
    /* Color override for specific blocks */
    .block-relevant { border-left: 5px solid var(--success-color); }
    .block-uncertain { border-left: 5px solid var(--warning-color); }
    .block-irrelevant { border-left: 5px solid var(--danger-color); }
    
    </style>
""", unsafe_allow_html=True)


# --- CONFIGURATION AND CORE UTILITIES (UNCHANGED) ---

skills_list = [
    'python', 'sql', 'c++', 'java', 'tableau', 'machine learning', 'data analysis',
    'business intelligence', 'r', 'tensorflow', 'pandas', 'spark', 'scikit-learn', 'aws',
    'javascript', 'scala', 'go', 'ruby', 'pytorch', 'keras', 'deep learning', 'nlp',
    'computer vision', 'azure', 'gcp', 'docker', 'kubernetes', 'hadoop', 'kafka',
    'airflow', 'power bi', 'matplotlib', 'seaborn', 'plotly', 'ggplot', 'mysql',
    'postgresql', 'mongodb', 'redis', 'git', 'linux', 'api', 'rest',
    'rust', 'kotlin', 'typescript', 'julia', 'snowflake', 'bigquery', 'cassandra',
    'neo4j', 'hugging face', 'langchain', 'onnx', 'xgboost', 'terraform', 'ansible',
    'jenkins', 'gitlab ci', 'qlik', 'looker', 'd3 js', 'blockchain', 'quantum computing',
    'cybersecurity', 'project management', 'technical writing', 'business analysis',
    'agile methodologies', 'communication', 'team leadership',
    'databricks', 'synapse', 'delta lake', 'streamlit', 'fastapi', 'graphql', 'mlflow', 'kedro'
]

skills_pattern = re.compile(r'\b(' + '|'.join(re.escape(skill) for skill in skills_list) + r')\b', re.IGNORECASE)

# Helper functions for CV parsing
def extract_text_from_pdf(file):
    try:
        pdf_reader = PyPDF2.PdfReader(file)
        text = ""
        for page in pdf_reader.pages:
            page_text = page.extract_text()
            if page_text:
                text += page_text + "\n"
        return text.strip()
    except Exception as e:
        st.error(f"Error extracting text from PDF: {str(e)}")
        return ""

def extract_text_from_docx(file):
    try:
        doc = Document(file)
        text = ""
        for paragraph in doc.paragraphs:
            text += paragraph.text + "\n"
        return text.strip()
    except Exception as e:
        st.error(f"Error extracting text from Word document: {str(e)}")
        return ""

def extract_text_from_file(uploaded_file):
    if uploaded_file.name.endswith('.pdf'): return extract_text_from_pdf(uploaded_file)
    elif uploaded_file.name.endswith('.docx'): return extract_text_from_docx(uploaded_file)
    return ""

def normalize_text(text):
    text = text.lower()
    # Clean up text for better NLP processing
    text = re.sub(r'_|-|,\s*collaborated in agile teams|,\s*developed solutions for|,\s*led projects involving|,\s*designed applications with|,\s*built machine learning models for|,\s*implemented data pipelines for|,\s*deployed cloud-based solutions|,\s*optimized workflows for|,\s*contributed to data-driven projects', '', text)
    return text

def check_experience_mismatch(resume, job_description):
    resume_match = re.search(r'(\d+)\s*years?|senior', resume.lower())
    job_match = re.search(r'(\d+)\s*years?(?:\s+\w+)*\+|senior\+', job_description.lower())
    if resume_match and job_match:
        resume_years = resume_match.group(0)
        job_years = job_match.group(0)
        if 'senior' in resume_years: resume_num = 10
        else: resume_num = int(re.search(r'\d+', resume_years).group(0)) if re.search(r'\d+', resume_years) else 0
        if 'senior+' in job_years: job_num = 10 
        else: job_num = int(re.search(r'\d+', job_years).group(0)) if re.search(r'\d+', job_years) else 0
        if resume_num < job_num: return f"Experience mismatch: Resume has {resume_years.strip()}, job requires {job_years.strip()}"
    return None

def validate_input(text, is_resume=True):
    if not text.strip() or len(text.strip()) < 10: return "Input is too short (minimum 10 characters)."
    text_normalized = normalize_text(text)
    if is_resume and not skills_pattern.search(text_normalized): return "Please include at least one data/tech skill (e.g., python, sql, databricks)."
    if is_resume and not re.search(r'\d+\s*year(s)?|senior', text.lower()): return "Please include experience (e.g., '3 years experience' or 'senior')."
    return None

@st.cache_resource
def load_models():
    # Placeholder for model loading
    bert_model_path = 'scmlewis/bert-finetuned-isom5240'
    bert_tokenizer = BertTokenizer.from_pretrained(bert_model_path)
    bert_model = BertForSequenceClassification.from_pretrained(bert_model_path, num_labels=2)
    t5_tokenizer = T5Tokenizer.from_pretrained('t5-small')
    t5_model = T5ForConditionalGeneration.from_pretrained('t5-small')
    device = torch.device('cpu') 
    bert_model.to(device)
    t5_model.to(device)
    bert_model.eval()
    t5_model.eval()
    return bert_tokenizer, bert_model, t5_tokenizer, t5_model, device

@st.cache_data
def tokenize_inputs(resumes, job_description, _bert_tokenizer, _t5_tokenizer):
    job_description_norm = normalize_text(job_description)
    bert_inputs = [f"resume: {normalize_text(resume)} [sep] job: {job_description_norm}" for resume in resumes]
    bert_tokenized = _bert_tokenizer(bert_inputs, return_tensors='pt', padding=True, truncation=True, max_length=64)
    
    t5_inputs = []
    for resume in resumes:
        prompt = re.sub(r'\b[Cc]\+\+\b', 'c++', resume)
        prompt_normalized = normalize_text(prompt)
        t5_inputs.append(f"summarize: {prompt_normalized}")
    t5_tokenized = _t5_tokenizer(t5_inputs, return_tensors='pt', padding=True, truncation=True, max_length=64)
    
    return bert_tokenized, t5_inputs, t5_tokenized

@st.cache_data
def extract_skills(text):
    text_normalized = normalize_text(text)
    text_normalized = re.sub(r'[,_-]', ' ', text_normalized)
    found_skills = skills_pattern.findall(text_normalized)
    return set(s.lower() for s in found_skills)

@st.cache_data
def classify_and_summarize_batch(resume, job_description, _bert_tokenized, _t5_input, _t5_tokenized, _job_skills_set):
    _, bert_model, t5_tokenizer, t5_model, device = st.session_state.models
    
    try:
        # BERT Classification
        bert_tokenized = {k: v.to(device) for k, v in _bert_tokenized.items()}
        with torch.no_grad():
            outputs = bert_model(**bert_tokenized)
        logits = outputs.logits
        probabilities = torch.softmax(logits, dim=1).cpu().numpy()
        predictions = np.argmax(probabilities, axis=1)
        confidence_threshold = 0.85
        prob, pred = probabilities[0], predictions[0]

        # T5 Summarization
        t5_tokenized = {k: v.to(device) for k, v in _t5_tokenized.items()}
        with torch.no_grad():
            t5_outputs = t5_model.generate(
                t5_tokenized['input_ids'],
                attention_mask=t5_tokenized['attention_mask'],
                max_length=30,
                min_length=8,
                num_beams=2,
                no_repeat_ngram_size=3,
                length_penalty=2.0,
                early_stopping=True
            )
        summaries = [t5_tokenizer.decode(output, skip_special_tokens=True, clean_up_tokenization_spaces=True) for output in t5_outputs]
        
        # Skill and Experience Check
        resume_skills_set = extract_skills(resume)
        skill_overlap = len(_job_skills_set.intersection(resume_skills_set)) / len(_job_skills_set) if _job_skills_set else 0

        suitability = "Relevant"
        warning = "None"
        exp_warning = check_experience_mismatch(resume, job_description)

        if skill_overlap < 0.4:
            suitability = "Irrelevant"
            warning = "Low skill match (<40%) with job requirements"
        elif exp_warning:
            suitability = "Uncertain"
            warning = exp_warning
        elif prob[pred] < confidence_threshold:
            suitability = "Uncertain"
            warning = f"Lower AI confidence: {prob[pred]:.2f}"
        elif skill_overlap < 0.5:
            suitability = "Irrelevant"
            warning = "Skill overlap is present but not a strong match (<50%)"
        
        # Final Summary Formatting (HR-friendly)
        detected_skills = list(set(skills_pattern.findall(normalize_text(resume))))
        exp_match = re.search(r'\d+\s*years?|senior', resume.lower())
        
        if detected_skills and exp_match: final_summary = f"Key Skills: {', '.join(detected_skills)}. Experience: {exp_match.group(0).capitalize()}"
        elif detected_skills: final_summary = f"Key Skills: {', '.join(detected_skills)}"
        else: final_summary = f"Experience: {exp_match.group(0).capitalize() if exp_match else 'Unknown'}"
        
        # Color codes based on theme (STILL NEEDED for the scorecards/tiles and CSV)
        if suitability == "Relevant": color = "#4CAF50"
        elif suitability == "Irrelevant": color = "#F44336"
        else: color = "#FFC107"

        return {"Suitability": suitability, "Data/Tech Related Skills Summary": final_summary, "Warning": warning or "None", "Suitability_Color": color}
    except Exception as e:
        return {"Suitability": "Error", "Data/Tech Related Skills Summary": "Failed to process profile", "Warning": str(e), "Suitability_Color": "#F44336"}

@st.cache_data
def generate_skill_pie_chart(resumes):
    # Skill chart logic
    skill_counts = {}
    total_resumes = len([r for r in resumes if r.strip()])
    if total_resumes == 0: return None
    for resume in resumes:
        if resume.strip():
            resume_lower = normalize_text(resume)
            found_skills = skills_pattern.findall(resume_lower)
            for skill in found_skills:
                skill_counts[skill.lower()] = skill_counts.get(skill.lower(), 0) + 1
    if not skill_counts: return None
    
    sorted_skills = sorted(skill_counts.items(), key=lambda item: item[1], reverse=True)
    top_n = 8
    
    if len(sorted_skills) > top_n:
        top_skills = dict(sorted_skills[:top_n-1])
        other_count = sum(count for _, count in sorted_skills[top_n-1:])
        top_skills["Other Skills"] = other_count 
    else: 
        top_skills = dict(sorted_skills)
        
    chart_df = pd.DataFrame(list(top_skills.items()), columns=['Skill', 'Count'])

    # PLOTLY IMPLEMENTATION
    fig = px.pie(
        chart_df,
        values='Count',
        names='Skill',
        title='Top Candidate Skill Frequency',
        hole=0.3, # Donut chart style
        color_discrete_sequence=px.colors.qualitative.Plotly 
    )

    # Update layout for dark theme
    fig.update_layout(
        paper_bgcolor='rgba(0,0,0,0)',
        plot_bgcolor='rgba(0,0,0,0)',
        font_color='#F8F8F8', 
        title_font_color='#42A5F5',
        title_font_size=20,
        legend_title_font_color='#B0B0B0',
    )
    
    fig.update_traces(textinfo='percent+label', marker=dict(line=dict(color='#3A3A3A', width=1.5)))

    return fig

def render_sidebar():
    """Render sidebar content with professional HR language."""
    SUCCESS_COLOR = "#4CAF50"  
    WARNING_COLOR = "#FFC107"  
    DANGER_COLOR = "#F44336"   
    PRIMARY_COLOR = "#42A5F5"  

    with st.sidebar:
        st.markdown(f"""
            <h2 style='text-align: center; border-left: none; padding-left: 0; color: {PRIMARY_COLOR};'>
                TALENT SCREENING ASSISTANT
            </h2>
            <p style='text-align: center; font-size: 14px; margin-top: 0; color: #B0B0B0;'>
                Powered by Advanced NLP (BERT + T5)
            </p>
        """, unsafe_allow_html=True)
        
        with st.expander("πŸ“ Quick Guide for HR", expanded=True):
            st.markdown("""
                **1. Set Requirements (Tab 1)**:
                - Enter the **Job Description** (JD). Be clear about required skills and experience (e.g., '5 years+').
                
                **2. Upload Candidates (Tab 2)**:
                - Upload or paste up to **7 Candidate Profiles** (PDF/DOCX/Text).  <-- **UPDATED TO 7**
                - Profiles must contain key technical skills and explicit experience.
                
                **3. Run Screening**:
                - Click the **Run Candidate Screening** button.
                
                **4. Review Report (Tab 3)**:
                - View the summary scorecard and detailed table for swift assessment.
            """)
        
        with st.expander("🎯 Screening Outcomes Explained", expanded=False):
            st.markdown(f"""
                - **<span style='color: {SUCCESS_COLOR};'>Relevant</span>**: Strong match across all criteria. Proceed to interview.
                - **<span style='color: {DANGER_COLOR};'>Irrelevant</span>**: Low skill overlap or poor fit. Pass on candidate.
                - **<span style='color: {WARNING_COLOR};'>Uncertain</span>**: Flagged due to Experience Mismatch or Lower AI confidence. Requires manual review.
            """, unsafe_allow_html=True)

# --- MAIN APPLICATION LOGIC ---

def main():
    """Main function to run the Streamlit app for resume screening."""
    render_sidebar()

    # Initialize session state
    if 'resumes' not in st.session_state: st.session_state.resumes = ["Expert in python, machine learning, tableau, 4 years experience", "", ""]
    if 'input_job_description' not in st.session_state: st.session_state.input_job_description = "Data scientist requires python, machine learning, 3 years+"
    if 'results' not in st.session_state: st.session_state.results = []
    if 'valid_resumes' not in st.session_state: st.session_state.valid_resumes = []
    if 'models' not in st.session_state: st.session_state.models = None

    st.markdown("<h1>πŸš€ AI DATA/TECH TALENT SCREENING TOOL</h1>", unsafe_allow_html=True)
    
    tab_setup, tab_resumes, tab_results = st.tabs(["1. Job Requirement Setup", "2. Candidate Profile Upload", "3. Screening Report & Analytics"])

    # --- TAB 1: Setup & Job Description ---
    with tab_setup:
        st.markdown("## πŸ“‹ Define Job Requirements") 
        st.info("Please enter the **Job Description** below. This is essential for the AI to accurately match skills and experience levels.")
        
        job_description = st.text_area(
            "Job Description Text",
            value=st.session_state.input_job_description,
            height=150,
            key="job_description_tab",
            placeholder="e.g., Data engineer role requires 5 years+ experience with Python, AWS, and Databricks. Must have leadership experience."
        )
        st.session_state.input_job_description = job_description
        
        validation_error = validate_input(job_description, is_resume=False)
        if validation_error and job_description.strip():
            st.warning(f"Input Check: Job Description missing key details. {validation_error}")
            
    # --- TAB 2: Manage Resumes ---
    with tab_resumes:
        st.markdown(f"## πŸ“ Upload Candidate Profiles ({len(st.session_state.resumes)}/7)") # <-- **UPDATED TO 7**
        st.info("Upload or paste candidate text below. The AI requires **key technical skills and experience statements** to function.")

        # Manage resume inputs
        for i in range(len(st.session_state.resumes)):
            
            status_icon = "βšͺ" # Default: Pending
            validation_error = validate_input(st.session_state.resumes[i], is_resume=True)
            if not st.session_state.resumes[i].strip():
                status_icon = "πŸ“‚" # Empty/Needs Input
                is_expanded = False
            elif validation_error:
                status_icon = "⚠️" # Warning/Error
                is_expanded = True
            else:
                status_icon = "βœ…" # Valid
                is_expanded = False
                
            with st.expander(f"**{status_icon} CANDIDATE PROFILE {i+1}**", expanded=is_expanded): 
                
                uploaded_file = st.file_uploader(
                    f"Upload Profile (PDF or DOCX) for Candidate {i+1}", 
                    type=['pdf', 'docx'], 
                    key=f"file_upload_{i}"
                )
                
                if uploaded_file is not None:
                    extracted_text = extract_text_from_file(uploaded_file)
                    if extracted_text: st.session_state.resumes[i] = extracted_text
                    else: st.session_state.resumes[i] = ""
                        
                st.session_state.resumes[i] = st.text_area(
                    f"Candidate Profile Text",
                    value=st.session_state.resumes[i],
                    height=100,
                    key=f"resume_{i}_tab",
                    placeholder="e.g., Expert in Python, SQL, and 3 years experience in data science."
                )
                
                if validation_error and st.session_state.resumes[i].strip():
                    st.warning(f"Profile Check: Candidate {i+1} flagged. {validation_error}")

        st.markdown("<br>", unsafe_allow_html=True)
        col_add, col_remove, _ = st.columns([1, 1, 3])
        with col_add:
            if st.button("βž• Add Candidate Slot", use_container_width=True) and len(st.session_state.resumes) < 7: # <-- **UPDATED TO 7**
                st.session_state.resumes.append("")
                st.rerun()
        with col_remove:
            if st.button("βž– Remove Candidate Slot", use_container_width=True) and len(st.session_state.resumes) > 1:
                st.session_state.resumes.pop()
                st.rerun()
                
    # --- ACTION BUTTONS ---
    st.markdown("---")
    col_btn1, col_btn2, _ = st.columns([1, 1, 3])
    with col_btn1:
        analyze_clicked = st.button("βœ… Run Candidate Screening", type="primary", use_container_width=True)
    with col_btn2:
        reset_clicked = st.button("♻️ Reset All Inputs", use_container_width=True)
    st.markdown("---")
    
    # Handle reset and analysis logic
    if reset_clicked:
        st.session_state.resumes = ["", "", ""]
        st.session_state.input_job_description = ""
        st.session_state.results = []
        st.session_state.valid_resumes = []
        st.session_state.models = None
        st.rerun()

    if analyze_clicked:
        valid_resumes = []
        all_inputs_valid = True
        for i, resume in enumerate(st.session_state.resumes):
            validation_error = validate_input(resume, is_resume=True)
            if not validation_error and resume.strip(): valid_resumes.append(resume)
            elif validation_error and resume.strip():
                st.error(f"Screening Blocked: Candidate {i+1} failed pre-screening validation. Fix input.")
                all_inputs_valid = False
        
        job_validation_error = validate_input(job_description, is_resume=False)
        if job_validation_error and job_description.strip(): st.error(f"Screening Blocked: Job Description failed validation. Fix input."); all_inputs_valid = False

        if valid_resumes and job_description.strip() and all_inputs_valid:
            if st.session_state.models is None:
                with st.spinner("Initializing AI Model, please wait..."): st.session_state.models = load_models()
            st.session_state.results = []
            st.session_state.valid_resumes = valid_resumes
            total_steps = len(valid_resumes)
            with st.spinner("Processing Candidate Profiles..."):
                progress_bar = st.progress(0); status_text = st.empty()
                bert_tokenizer, _, t5_tokenizer, _, _ = st.session_state.models
                
                status_text.text("Status: Preparing inputs and extracting job skills...")
                bert_tokenized, t5_inputs, t5_tokenized = tokenize_inputs(valid_resumes, job_description, bert_tokenizer, t5_tokenizer)
                job_skills_set = extract_skills(job_description)
                results = []
                
                for i, resume in enumerate(valid_resumes):
                    status_text.text(f"Status: Analyzing Profile {i+1} of {total_steps}...")
                    
                    bert_tok_single = {
                        'input_ids': bert_tokenized['input_ids'][i].unsqueeze(0), 
                        'attention_mask': bert_tokenized['attention_mask'][i].unsqueeze(0)
                    }
                    t5_tok_single = {
                        'input_ids': t5_tokenized['input_ids'][i].unsqueeze(0), 
                        'attention_mask': t5_tokenized['attention_mask'][i].unsqueeze(0)
                    }
                    
                    result = classify_and_summarize_batch(
                        resume, 
                        job_description, 
                        bert_tok_single, 
                        t5_inputs[i], 
                        t5_tok_single, 
                        job_skills_set
                    )
                    result["Resume"] = f"Candidate {i+1}" 
                    results.append(result)
                    progress_bar.progress((i + 1) / total_steps)
                st.session_state.results = results
                
                status_text.empty(); progress_bar.empty()
                st.success("Screening Complete. Results are available in the 'Screening Report & Analytics' tab. πŸŽ‰")
        else:
            st.error("Screening cannot run. Ensure at least one valid candidate profile and a job description are provided.")

    # --- TAB 3: Results (The Professional Report) ---
    with tab_results:
        st.markdown("## πŸ“Š Screening Results Summary") 
        
        if st.session_state.results:
            
            # --- Scorecard Metrics (Professional Tiles) ---
            results_df = pd.DataFrame(st.session_state.results) 
            total = len(results_df)
            relevant_count = len(results_df[results_df['Suitability'] == 'Relevant'])
            review_count = len(results_df[results_df['Suitability'] == 'Uncertain'])
            irrelevant_count = len(results_df[results_df['Suitability'].isin(['Irrelevant', 'Error'])])
            
            st.markdown(f"#### Overview: {total} Candidate Profiles Processed")
            
            PRIMARY_COLOR = "#42A5F5" 
            SUCCESS_COLOR = "#4CAF50"  
            WARNING_COLOR = "#FFC107"  
            DANGER_COLOR = "#F44336"   

            col1, col2, col3, col4 = st.columns(4)
            
            with col1:
                st.markdown(f"""
                    <div class='scorecard-block'>
                        <div class='scorecard-label'>TOTAL PROFILES</div>
                        <div class='scorecard-value' style='color:{PRIMARY_COLOR};'>{total}</div>
                    </div>
                """, unsafe_allow_html=True)

            with col2:
                st.markdown(f"""
                    <div class='scorecard-block block-relevant'>
                        <div class='scorecard-label' style='color: {SUCCESS_COLOR};'>RELEVANT MATCHES</div>
                        <div class='scorecard-value' style='color: {SUCCESS_COLOR};'>{relevant_count}</div>
                    </div>
                """, unsafe_allow_html=True)

            with col3:
                st.markdown(f"""
                    <div class='scorecard-block block-uncertain'>
                        <div class='scorecard-label' style='color: {WARNING_COLOR};'>REQUIRES REVIEW</div>
                        <div class='scorecard-value' style='color: {WARNING_COLOR};'>{review_count}</div>
                    </div>
                """, unsafe_allow_html=True)
                
            with col4:
                st.markdown(f"""
                    <div class='scorecard-block block-irrelevant'>
                        <div class='scorecard-label' style='color: {DANGER_COLOR};'>IRRELEVANT / ERROR</div>
                        <div class='scorecard-value' style='color: {DANGER_COLOR};'>{irrelevant_count}</div>
                    </div>
                """, unsafe_allow_html=True)
            
            st.markdown("---")

            # --- Detailed Report Table ---
            st.markdown("### πŸ“‹ Detailed Screening Results")
            
            # **FIX**: Explicitly drop the 'Suitability_Color' column to ensure it is not displayed, 
            # and remove all cell coloring logic.
            display_df = results_df.drop(columns=['Suitability_Color'])

            # Rename columns for professional display
            display_df = display_df.rename(
                columns={
                    'Data/Tech Related Skills Summary': '**PROFILE SUMMARY**', 
                    'Warning': '**FLAGGING REASON**', 
                    'Resume': '**PROFILE ID**',
                    'Suitability': '**SUITABILITY**' 
                }
            )

            # Display the simple, unstyled DataFrame
            st.dataframe(
                display_df,
                use_container_width=True
            )
            
            # --- Download and Chart Section ---
            st.markdown("<br>", unsafe_allow_html=True)
            col_dl, col_chart_expander = st.columns([1, 3])
            
            with col_dl:
                csv_buffer = io.StringIO()
                # Drop the internal column before download
                results_df.drop(columns=['Suitability_Color']).to_csv(csv_buffer, index=False)
                    
                st.download_button(
                    "πŸ’Ύ Download Full Report (CSV)", 
                    csv_buffer.getvalue(), 
                    file_name="Talent_Screening_Report.csv", 
                    mime="text/csv", 
                    use_container_width=True
                )
            
            with col_chart_expander:
                with st.expander("πŸ“ˆ Skill Distribution Analytics (Plotly)", expanded=False):
                    if st.session_state.valid_resumes:
                        fig = generate_skill_pie_chart(st.session_state.valid_resumes)
                        if fig:
                            st.plotly_chart(fig, use_container_width=True)
                        else:
                            st.info("No recognized technical skills found in the profiles for charting.")
                    else:
                        st.info("No valid candidate profiles to analyze.")
        else:
            st.info("Please complete the setup and upload tabs, then run the screening to generate the report.")


if __name__ == "__main__":
    main()