Create cv_analyzer.py
Browse files- cv_analyzer.py +34 -0
cv_analyzer.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
+
from cv_prompt import get_cv_prompt, cv_sections_data
|
| 5 |
+
from ocr_extractor import process_file
|
| 6 |
+
|
| 7 |
+
def load_model():
|
| 8 |
+
model_path = hf_hub_download("TheBloke/Mistral-7B-Instruct-v0.2-GGUF", filename="mistral-7b-instruct-v0.2.Q4_K_M.gguf")
|
| 9 |
+
return Llama(model_path=model_path, n_ctx=32768, n_gpu_layers=2)
|
| 10 |
+
|
| 11 |
+
llm = load_model()
|
| 12 |
+
|
| 13 |
+
def detect_cv_sections_and_score(text):
|
| 14 |
+
prompt = get_cv_prompt(text)
|
| 15 |
+
response = llm(prompt, max_tokens=500)
|
| 16 |
+
response_text = response['choices'][0]['text']
|
| 17 |
+
json_start = response_text.find('{')
|
| 18 |
+
json_end = response_text.rfind('}') + 1
|
| 19 |
+
json_str = response_text[json_start:json_end]
|
| 20 |
+
try:
|
| 21 |
+
detected_sections = json.loads(json_str)
|
| 22 |
+
present_sections = detected_sections.get("present_sections", [])
|
| 23 |
+
score_sections = sum(cv_sections_data["sections"].get(section, 0) for section in present_sections)
|
| 24 |
+
result = {
|
| 25 |
+
"present_sections": present_sections,
|
| 26 |
+
"score_sections": score_sections
|
| 27 |
+
}
|
| 28 |
+
return result
|
| 29 |
+
except json.JSONDecodeError:
|
| 30 |
+
return {"present_sections": [], "score_sections": 0}
|
| 31 |
+
|
| 32 |
+
def analyze_cv(file_path):
|
| 33 |
+
text = process_file(file_path, 'ocr_weights.json')
|
| 34 |
+
return detect_cv_sections_and_score(text)
|