Spaces:
Configuration error
Configuration error
Download ai_summarizer.py from dkshah/mypythonapp: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/spaces/dkshah/mypythonapp/resolve/main/ai_summarizer.py
- Command line
-
hf download hf://spaces/dkshah/mypythonapp/ai_summarizer.py
-
curl -L -o ai_summarizer.py https://huggingface.co/spaces/dkshah/mypythonapp/resolve/main/ai_summarizer.py
3.1 kB
| 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)) | |