Siggmoid's picture
Fix AI feedback: use HF Inference chat API via LangChain
51e25cb
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__})"
)