Spaces:
Runtime error
Runtime error
| 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 |