Create spelling_grammar_checker.py
Browse files- spelling_grammar_checker.py +38 -0
spelling_grammar_checker.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import hf_hub_download
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
from ocr_extractor import process_file
|
| 4 |
+
from cv_prompt import get_spelling_grammar_prompt
|
| 5 |
+
import json
|
| 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 load_error_scoring():
|
| 14 |
+
with open('error_scoring.json', 'r') as file:
|
| 15 |
+
return json.load(file)
|
| 16 |
+
|
| 17 |
+
error_scoring = load_error_scoring()
|
| 18 |
+
|
| 19 |
+
def get_error_score(error_percentage):
|
| 20 |
+
for score_range in error_scoring['error_scores']:
|
| 21 |
+
if score_range['min'] <= error_percentage < score_range['max']:
|
| 22 |
+
return score_range['score']
|
| 23 |
+
return error_scoring['error_scores'][-1]['score'] # Return the last score if percentage is 100%
|
| 24 |
+
|
| 25 |
+
def check_spelling_and_grammar(text):
|
| 26 |
+
prompt = get_spelling_grammar_prompt(text)
|
| 27 |
+
response = llm(prompt, max_tokens=50)
|
| 28 |
+
response_text = response['choices'][0]['text'].strip()
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
error_percentage = float(response_text)
|
| 32 |
+
return get_error_score(error_percentage)
|
| 33 |
+
except ValueError:
|
| 34 |
+
return get_error_score(0)
|
| 35 |
+
|
| 36 |
+
def evaluate_cv_text(file_path, weights_file):
|
| 37 |
+
cv_text = process_file(file_path, weights_file)
|
| 38 |
+
return check_spelling_and_grammar(cv_text)
|