Spaces:
Sleeping
Sleeping
Update spelling_grammar_checker.py
Browse files- spelling_grammar_checker.py +15 -26
spelling_grammar_checker.py
CHANGED
@@ -1,19 +1,23 @@
|
|
1 |
import json
|
2 |
-
import os
|
3 |
from openai_utils import get_ai_response
|
4 |
from ocr_extractor import process_file
|
5 |
from cv_prompt import get_spelling_grammar_prompt
|
6 |
|
|
|
|
|
|
|
|
|
7 |
def check_spelling_and_grammar(text):
|
8 |
prompt = get_spelling_grammar_prompt(text)
|
9 |
messages = [
|
10 |
{"role": "user", "content": prompt}
|
11 |
]
|
|
|
12 |
response = get_ai_response(messages)
|
13 |
|
14 |
if not response:
|
15 |
print("Unexpected response from OpenAI API")
|
16 |
-
return 100 # Assume 100% errors if we can't get a response
|
17 |
|
18 |
try:
|
19 |
result = json.loads(response)
|
@@ -21,31 +25,16 @@ def check_spelling_and_grammar(text):
|
|
21 |
return min(max(float(error_percentage), 0), 100) # Ensure the percentage is between 0 and 100
|
22 |
except (json.JSONDecodeError, ValueError):
|
23 |
print(f"Unable to parse error percentage from API response: {response}")
|
24 |
-
return 100 # Assume 100% errors if we can't parse the response
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
def evaluate_cv_text(file_path, weights_file):
|
27 |
cv_text = process_file(file_path, weights_file)
|
28 |
error_percentage = check_spelling_and_grammar(cv_text)
|
29 |
-
|
30 |
-
|
31 |
-
if not os.path.exists(error_scoring_file):
|
32 |
-
print(f"Error: {error_scoring_file} not found. Using default scoring.")
|
33 |
-
return -10 # Default score if file not found
|
34 |
-
|
35 |
-
try:
|
36 |
-
with open(error_scoring_file, 'r') as f:
|
37 |
-
error_scoring = json.load(f)
|
38 |
-
except json.JSONDecodeError as e:
|
39 |
-
print(f"Error parsing {error_scoring_file}: {str(e)}. Using default scoring.")
|
40 |
-
return -10 # Default score if JSON is invalid
|
41 |
-
|
42 |
-
score = None
|
43 |
-
for error_score in error_scoring.get('error_scores', []):
|
44 |
-
if error_score['min'] <= error_percentage <= error_score['max']:
|
45 |
-
score = error_score['score']
|
46 |
-
break
|
47 |
-
|
48 |
-
if score is None:
|
49 |
-
score = -10 # Default score if no matching range is found
|
50 |
-
|
51 |
-
return score
|
|
|
1 |
import json
|
|
|
2 |
from openai_utils import get_ai_response
|
3 |
from ocr_extractor import process_file
|
4 |
from cv_prompt import get_spelling_grammar_prompt
|
5 |
|
6 |
+
# Load the error scoring data
|
7 |
+
with open('error_scoring.json', 'r') as f:
|
8 |
+
error_scoring = json.load(f)
|
9 |
+
|
10 |
def check_spelling_and_grammar(text):
|
11 |
prompt = get_spelling_grammar_prompt(text)
|
12 |
messages = [
|
13 |
{"role": "user", "content": prompt}
|
14 |
]
|
15 |
+
|
16 |
response = get_ai_response(messages)
|
17 |
|
18 |
if not response:
|
19 |
print("Unexpected response from OpenAI API")
|
20 |
+
return 100, -10 # Assume 100% errors if we can't get a response
|
21 |
|
22 |
try:
|
23 |
result = json.loads(response)
|
|
|
25 |
return min(max(float(error_percentage), 0), 100) # Ensure the percentage is between 0 and 100
|
26 |
except (json.JSONDecodeError, ValueError):
|
27 |
print(f"Unable to parse error percentage from API response: {response}")
|
28 |
+
return 100, -10 # Assume 100% errors if we can't parse the response
|
29 |
+
|
30 |
+
def get_error_score(error_percentage):
|
31 |
+
for error_score in error_scoring['error_scores']:
|
32 |
+
if error_score['min'] <= error_percentage <= error_score['max']:
|
33 |
+
return error_score['score']
|
34 |
+
return -10 # Default score if no matching range is found
|
35 |
|
36 |
def evaluate_cv_text(file_path, weights_file):
|
37 |
cv_text = process_file(file_path, weights_file)
|
38 |
error_percentage = check_spelling_and_grammar(cv_text)
|
39 |
+
error_score = get_error_score(error_percentage)
|
40 |
+
return error_percentage, error_score
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|