File size: 3,097 Bytes
eb6064a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict, List, Optional
from .hf_api import hf_summarize

def summarize_resume(resume_data: Dict, *, hf_model: str, hf_token: Optional[str]) -> str:
    """
    Generate a summary of the resume using a Hugging Face text summarization model
    via the Inference API.
    
    Args:
        resume_data: Parsed resume dictionary containing 'raw_text' and optional fields.
        hf_model: Model repo id, e.g. 'facebook/bart-large-cnn' or 'sshleifer/distilbart-cnn-12-6'.
        hf_token: Optional HF API token. Required for rate limits or private models.
    Returns:
        str: Generated summary text.
    """
    name = resume_data.get('name', 'The candidate')
    skills: List[str] = resume_data.get('skills', []) or []
    raw_text: str = resume_data.get('raw_text', '')

    if not raw_text or len(raw_text.strip()) == 0:
        return generate_template_summary(resume_data)

    # The Inference API has input length limits; truncate long text conservatively.
    # We'll keep about ~2000 characters to stay safe across most summarization models.
    truncated = raw_text.strip()
    if len(truncated) > 2000:
        truncated = truncated[:2000]

    prompt = truncated
    try:
        summary = hf_summarize(prompt, model=hf_model, api_token=hf_token)
    except Exception:
        # Fallback to template summary on any API failure
        return generate_template_summary(resume_data)

    # Optionally append skills
    if skills:
        summary = summary.strip() + f"\n\nKey Skills: {', '.join(skills[:10])}"
    return summary

def generate_extractive_summary(*args, **kwargs) -> str:
    """Deprecated placeholder. We now use the Hugging Face Inference API."""
    raise NotImplementedError("Use summarize_resume with hf_model and hf_token instead.")

def generate_template_summary(resume_data: Dict) -> str:
    """Generate a template-based summary when there's not enough text for extractive summarization."""
    name = resume_data.get('name', 'The candidate')
    skills = resume_data.get('skills', [])
    
    summary = f"{name} "
    
    if skills:
        summary += f"is proficient in {', '.join(skills[:5])}."
    else:
        summary += "has submitted a resume for review."
    
    return summary

# For testing
if __name__ == "__main__":
    test_resume = {
        'name': 'John Doe',
        'email': 'john.doe@example.com',
        'phone': '(123) 456-7890',
        'skills': ['Python', 'Machine Learning', 'Data Analysis'],
        'raw_text': """John Doe
        Senior Data Scientist
        
        Experienced data scientist with 5+ years of experience in machine learning and data analysis. 
        Proficient in Python, R, and SQL. Strong background in statistical modeling and data visualization.
        
        Experience:
        - Data Scientist at Tech Corp (2020-Present)
        - Junior Data Analyst at Data Systems (2018-2020)
        
        Education:
        - MS in Computer Science, University of Technology
        - BS in Mathematics, State University
        """
    }
    
    print(summarize_resume(test_resume))