File size: 2,121 Bytes
ce83166
 
 
 
 
 
 
 
 
 
 
 
ff65733
 
ce83166
 
ff65733
ce83166
 
ff65733
 
 
 
 
 
 
 
 
ce83166
ff65733
 
 
 
ce83166
ff65733
 
ce83166
ff65733
 
 
 
 
 
 
 
 
ce83166
 
 
 
 
ff65733
ce83166
 
 
 
ff65733
 
ce83166
 
ff65733
 
 
 
 
 
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
import re
from transformers import pipeline, set_seed
import torch

# Initialize text generation pipeline
generator = pipeline('text-generation', 
                     model='gpt2-medium',
                     device=0 if torch.cuda.is_available() else -1)

def preprocess_text(text):
    """Clean and chunk long text"""
    text = re.sub(r'\s+', ' ', text)
    if len(text.split()) > 600:
        return ' '.join(text.split()[:600]) + '... [truncated]'
    return text

def generate_cover_letter(job_desc, resume, tone="Professional"):
    """Generate personalized cover letter using NLP"""
    set_seed(42)
    
    # Tone mapping
    tone_adjectives = {
        "Professional": "professional, polished, and business-appropriate",
        "Enthusiastic": "enthusiastic, energetic, and passionate",
        "Formal": "formal, respectful, and traditional",
        "Friendly": "friendly, approachable, and conversational"
    }
    
    prompt = f"""
    Create a {tone_adjectives.get(tone, 'professional')} cover letter using these details:
    
    Job Requirements:
    {preprocess_text(job_desc)}
    
    Candidate Qualifications:
    {preprocess_text(resume)}
    
    Instructions:
    1. Match 3-5 key skills from qualifications to job requirements
    2. Highlight most relevant experiences
    3. Show enthusiasm for the specific company/role
    4. Keep between 200-250 words
    5. Do not include placeholders like [Company Name]
    6. Use professional business letter format
    
    Cover Letter Body:
    """
    
    # Generate text with controlled parameters
    response = generator(
        prompt,
        max_length=600,
        num_return_sequences=1,
        temperature=0.7,
        top_p=0.9,
        repetition_penalty=1.2,
        no_repeat_ngram_size=3,
        do_sample=True
    )
    
    # Extract and clean the response
    letter = response[0]['generated_text'].split("Cover Letter Body:")[-1].strip()
    letter = re.sub(r'(\n\s*)+\n+', '\n\n', letter)  # Remove excessive newlines
    letter = re.sub(r'\[.*?\]', '', letter)  # Remove any bracketed placeholders
    
    return letter