Spaces:
Runtime error
Runtime error
Update services/resume_service.py
Browse files- services/resume_service.py +35 -30
services/resume_service.py
CHANGED
|
@@ -4,7 +4,7 @@ from typing import Dict
|
|
| 4 |
from core.doc_processor import extract_text_from_bytes, extract_text_from_docx
|
| 5 |
from core.text_processor import chunk_text
|
| 6 |
from core.vector_store import create_or_load_vectorstore, similarity_search
|
| 7 |
-
from services.llm_service import get_llm,
|
| 8 |
|
| 9 |
|
| 10 |
def analyze_resume_document(
|
|
@@ -21,20 +21,20 @@ def analyze_resume_document(
|
|
| 21 |
raise ValueError(resume_text)
|
| 22 |
|
| 23 |
chunks = chunk_text(resume_text)
|
| 24 |
-
|
| 25 |
vector_store = create_or_load_vectorstore(chunks)
|
|
|
|
| 26 |
|
| 27 |
-
return
|
| 28 |
|
| 29 |
|
| 30 |
-
def
|
| 31 |
try:
|
| 32 |
retrieved_docs = similarity_search(vectorstore, job_role)
|
| 33 |
relevant_content = "\n".join([doc.page_content for doc in retrieved_docs])
|
| 34 |
|
| 35 |
-
|
| 36 |
llm = get_llm()
|
| 37 |
-
chain =
|
| 38 |
|
| 39 |
response = chain.invoke(
|
| 40 |
{
|
|
@@ -44,38 +44,43 @@ def analyze_experience_with_llm(vectorstore, resume_text, job_role):
|
|
| 44 |
}
|
| 45 |
)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
if
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
parsed_response = json.loads(
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
"
|
| 55 |
-
|
| 56 |
-
),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
-
for exp in parsed_response.get("relevant_experiences", []):
|
| 60 |
-
cleaned_exp = {
|
| 61 |
-
"position": exp.get("position", ""),
|
| 62 |
-
"company": exp.get("company", ""),
|
| 63 |
-
"duration": exp.get("duration", ""),
|
| 64 |
-
"relevant_experience": exp.get("relevant_experience", ""),
|
| 65 |
-
}
|
| 66 |
-
cleaned_response["relevant_experiences"].append(cleaned_exp)
|
| 67 |
-
|
| 68 |
-
return cleaned_response
|
| 69 |
-
|
| 70 |
except json.JSONDecodeError as e:
|
| 71 |
print(f"JSON Parse Error: {str(e)}")
|
| 72 |
return {
|
| 73 |
-
"
|
| 74 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
}
|
| 76 |
except Exception as e:
|
| 77 |
print(f"Unexpected Error: {str(e)}")
|
| 78 |
return {
|
| 79 |
-
"
|
| 80 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
}
|
|
|
|
| 4 |
from core.doc_processor import extract_text_from_bytes, extract_text_from_docx
|
| 5 |
from core.text_processor import chunk_text
|
| 6 |
from core.vector_store import create_or_load_vectorstore, similarity_search
|
| 7 |
+
from services.llm_service import get_llm, get_unified_resume_prompt
|
| 8 |
|
| 9 |
|
| 10 |
def analyze_resume_document(
|
|
|
|
| 21 |
raise ValueError(resume_text)
|
| 22 |
|
| 23 |
chunks = chunk_text(resume_text)
|
|
|
|
| 24 |
vector_store = create_or_load_vectorstore(chunks)
|
| 25 |
+
all_data = analyze_resume_with_llm(vector_store, resume_text, job_role)
|
| 26 |
|
| 27 |
+
return all_data
|
| 28 |
|
| 29 |
|
| 30 |
+
def analyze_resume_with_llm(vectorstore, resume_text: str, job_role: str) -> Dict:
|
| 31 |
try:
|
| 32 |
retrieved_docs = similarity_search(vectorstore, job_role)
|
| 33 |
relevant_content = "\n".join([doc.page_content for doc in retrieved_docs])
|
| 34 |
|
| 35 |
+
unified_prompt = get_unified_resume_prompt()
|
| 36 |
llm = get_llm()
|
| 37 |
+
chain = unified_prompt | llm
|
| 38 |
|
| 39 |
response = chain.invoke(
|
| 40 |
{
|
|
|
|
| 44 |
}
|
| 45 |
)
|
| 46 |
|
| 47 |
+
response_text = response.content.strip()
|
| 48 |
+
if response_text.startswith("`"):
|
| 49 |
+
response_text = response_text.strip("`")
|
| 50 |
+
if response_text.startswith("json"):
|
| 51 |
+
response_text = response_text[4:]
|
| 52 |
|
| 53 |
+
parsed_response = json.loads(response_text.strip())
|
| 54 |
+
|
| 55 |
+
return {
|
| 56 |
+
"candidate_name": parsed_response.get("candidate_name", ""),
|
| 57 |
+
"university_name": parsed_response.get("university_name"),
|
| 58 |
+
"location": parsed_response.get("location"),
|
| 59 |
+
"physical_address": parsed_response.get("physical_address"),
|
| 60 |
+
"role": parsed_response.get("role"),
|
| 61 |
+
"relevant_experiences": parsed_response.get("relevant_experiences", "No relevant experience found"),
|
| 62 |
+
"experience_summary": parsed_response.get("experience_summary", "No experience analysis available")
|
| 63 |
}
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
except json.JSONDecodeError as e:
|
| 66 |
print(f"JSON Parse Error: {str(e)}")
|
| 67 |
return {
|
| 68 |
+
"candidate_name": "",
|
| 69 |
+
"university_name": None,
|
| 70 |
+
"location": None,
|
| 71 |
+
"physical_address": None,
|
| 72 |
+
"role": None,
|
| 73 |
+
"relevant_experiences": "Error: Could not parse resume details from response",
|
| 74 |
+
"experience_summary": "Could not parse resume details from response"
|
| 75 |
}
|
| 76 |
except Exception as e:
|
| 77 |
print(f"Unexpected Error: {str(e)}")
|
| 78 |
return {
|
| 79 |
+
"candidate_name": "",
|
| 80 |
+
"university_name": None,
|
| 81 |
+
"location": None,
|
| 82 |
+
"physical_address": None,
|
| 83 |
+
"role": None,
|
| 84 |
+
"relevant_experiences": f"Error analyzing resume: {str(e)}",
|
| 85 |
+
"experience_summary": f"Error analyzing resume: {str(e)}"
|
| 86 |
}
|