utkarsh134's picture
Update services/resume_service.py
0e67ca9 verified
Raw History Blame Contribute Delete
3.26 kB
import json
from typing import Dict
from core.doc_processor import extract_text_from_bytes, extract_text_from_docx
from core.text_processor import chunk_text
from core.vector_store import create_or_load_vectorstore, similarity_search
from services.llm_service import get_llm, get_unified_resume_prompt
def analyze_resume_document(
file_bytes: bytes, job_role: str, file_extension: str
) -> Dict:
if file_extension.lower() == ".pdf":
resume_text = extract_text_from_bytes(file_bytes)
elif file_extension.lower() == ".docx":
resume_text = extract_text_from_docx(file_bytes)
else:
raise ValueError(f"Unsupported file format: {file_extension}")
if resume_text.startswith("Error"):
raise ValueError(resume_text)
chunks = chunk_text(resume_text)
vector_store = create_or_load_vectorstore(chunks)
all_data = analyze_resume_with_llm(vector_store, resume_text, job_role)
return all_data
def analyze_resume_with_llm(vectorstore, resume_text: str, job_role: str) -> Dict:
try:
retrieved_docs = similarity_search(vectorstore, job_role)
relevant_content = "\n".join([doc.page_content for doc in retrieved_docs])
unified_prompt = get_unified_resume_prompt()
llm = get_llm()
chain = unified_prompt | llm
response = chain.invoke(
{
"resume_text": resume_text,
"relevant_content": relevant_content,
"job_role": job_role,
}
)
response_text = response.content.strip()
if response_text.startswith("`"):
response_text = response_text.strip("`")
if response_text.startswith("json"):
response_text = response_text[4:]
parsed_response = json.loads(response_text.strip())
return {
"candidate_name": parsed_response.get("candidate_name", ""),
"university_name": parsed_response.get("university_name"),
"location": parsed_response.get("location"),
"physical_address": parsed_response.get("physical_address"),
"role": parsed_response.get("role"),
"relevant_experiences": parsed_response.get("relevant_experiences", "No relevant experience found"),
"experience_summary": parsed_response.get("experience_summary", "No experience analysis available")
}
except json.JSONDecodeError as e:
print(f"JSON Parse Error: {str(e)}")
return {
"candidate_name": "",
"university_name": None,
"location": None,
"physical_address": None,
"role": None,
"relevant_experiences": "Error: Could not parse resume details from response",
"experience_summary": "Could not parse resume details from response"
}
except Exception as e:
print(f"Unexpected Error: {str(e)}")
return {
"candidate_name": "",
"university_name": None,
"location": None,
"physical_address": None,
"role": None,
"relevant_experiences": f"Error analyzing resume: {str(e)}",
"experience_summary": f"Error analyzing resume: {str(e)}"
}