File size: 1,661 Bytes
51e25cb
 
d2b7a80
 
51e25cb
 
 
d2b7a80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51e25cb
 
d2b7a80
 
 
 
 
51e25cb
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging

from langchain_core.messages import HumanMessage, SystemMessage

from .llm import get_hf_token, invoke_chat

logger = logging.getLogger(__name__)

SYSTEM_PROMPT = """You are an ATS resume analyst.

You MUST base your feedback ONLY on:
- The provided ATS scores
- The detected gaps

DO NOT invent missing skills.
DO NOT give generic advice.
DO NOT mention motivation, confidence, or mindset.

Write a concise analysis with exactly 3 sections:
1. Score Explanation
2. Weak Areas
3. Actionable Improvements

Keep it under 500 words."""


def generate_resume_feedback(scores: dict, gaps: dict) -> str:
    user_prompt = f"""ATS Scores:
Semantic: {scores['semantic_score']}
Keyword: {scores['keyword_score']}
Final: {scores['final_ats_score']}

Detected Gaps:
Missing Keywords: {gaps['missing_keywords']}
Skill Overlap: {gaps['skill_overlap_percentage']}%

Provide the 3-section analysis now."""

    try:
        get_hf_token()
        return invoke_chat(
            [
                SystemMessage(content=SYSTEM_PROMPT),
                HumanMessage(content=user_prompt),
            ]
        )
    except ValueError as exc:
        logger.error("HF_TOKEN missing: %s", exc)
        return (
            "AI feedback unavailable: HF_TOKEN is not configured. "
            "Add your Hugging Face token under Space Settings → Repository secrets."
        )
    except Exception as exc:
        logger.exception("Feedback generation failed")
        return (
            "AI feedback could not be generated. "
            "Check that HF_TOKEN has Inference access and the Space logs for details. "
            f"({type(exc).__name__})"
        )