Update app.py
Browse files
app.py
CHANGED
|
@@ -6,86 +6,27 @@ import logging
|
|
| 6 |
from typing import List, Dict, Tuple, Any
|
| 7 |
import time
|
| 8 |
import asyncio
|
|
|
|
| 9 |
from llm_functions import extract_personal_info, check_spelling_grammar, identify_sections, assess_section_quality
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def calculate_spelling_grammar_score(error_percentage: float) -> int:
|
| 30 |
-
for score_range in ERROR_SCORING:
|
| 31 |
-
if score_range['min'] <= error_percentage < score_range['max']:
|
| 32 |
-
return score_range['score']
|
| 33 |
-
return ERROR_SCORING[-1]['score']
|
| 34 |
-
|
| 35 |
-
async def check_completeness(cv_content: str) -> Tuple[List[str], int, int]:
|
| 36 |
-
present_sections = json.loads(await identify_sections(cv_content, [section["name"] for section in CV_SECTIONS]))
|
| 37 |
-
score = sum(section["weight"] for section in CV_SECTIONS if section["name"] in present_sections)
|
| 38 |
-
max_score = sum(section["weight"] for section in CV_SECTIONS)
|
| 39 |
-
return present_sections, score, max_score
|
| 40 |
-
|
| 41 |
-
async def check_content_quality(cv_content: str, present_sections: List[str]) -> Tuple[Dict[str, Any], int, int]:
|
| 42 |
-
tasks = [assess_section_quality(cv_content, section) for section in present_sections]
|
| 43 |
-
quality_results = await asyncio.gather(*tasks)
|
| 44 |
-
|
| 45 |
-
result = {}
|
| 46 |
-
total_quality = 0
|
| 47 |
-
for section, quality_result in zip(present_sections, quality_results):
|
| 48 |
-
parsed_result = json.loads(quality_result)
|
| 49 |
-
result[section] = parsed_result
|
| 50 |
-
total_quality += parsed_result["quality"]
|
| 51 |
-
max_score = len(present_sections) * 5
|
| 52 |
-
return result, total_quality, max_score
|
| 53 |
-
|
| 54 |
-
@st.cache_data
|
| 55 |
-
def analyze_cv(cv_content: str) -> Dict[str, Any]:
|
| 56 |
-
async def async_analyze():
|
| 57 |
-
personal_info, spelling_grammar, completeness = await asyncio.gather(
|
| 58 |
-
extract_personal_info(cv_content),
|
| 59 |
-
check_spelling_grammar(cv_content),
|
| 60 |
-
check_completeness(cv_content)
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
personal_info = json.loads(personal_info)
|
| 64 |
-
personal_info_score = sum(1 for value in personal_info.values() if value is not None and value != "null")
|
| 65 |
-
personal_info_max_score = 4
|
| 66 |
-
|
| 67 |
-
spelling_grammar = float(spelling_grammar)
|
| 68 |
-
spelling_grammar_score = calculate_spelling_grammar_score(spelling_grammar)
|
| 69 |
-
spelling_grammar_max_score = 5
|
| 70 |
-
|
| 71 |
-
present_sections, completeness_score, completeness_max_score = completeness
|
| 72 |
-
content_quality, content_quality_score, content_quality_max_score = await check_content_quality(cv_content, present_sections)
|
| 73 |
-
|
| 74 |
-
total_score = personal_info_score + spelling_grammar_score + completeness_score + content_quality_score
|
| 75 |
-
max_total_score = personal_info_max_score + spelling_grammar_max_score + completeness_max_score + content_quality_max_score
|
| 76 |
-
|
| 77 |
-
return {
|
| 78 |
-
"personalInfo": personal_info,
|
| 79 |
-
"personalInfoScore": {"score": personal_info_score, "maxScore": personal_info_max_score},
|
| 80 |
-
"spellingGrammar": {"errorPercentage": spelling_grammar, "score": spelling_grammar_score, "maxScore": spelling_grammar_max_score},
|
| 81 |
-
"completeness": {"presentSections": present_sections, "score": completeness_score, "maxScore": completeness_max_score},
|
| 82 |
-
"contentQuality": content_quality,
|
| 83 |
-
"contentQualityScore": {"score": content_quality_score, "maxScore": content_quality_max_score},
|
| 84 |
-
"totalScore": total_score,
|
| 85 |
-
"maxTotalScore": max_total_score
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
return asyncio.run(async_analyze())
|
| 89 |
|
| 90 |
# Streamlit app
|
| 91 |
def main():
|
|
@@ -94,41 +35,44 @@ def main():
|
|
| 94 |
uploaded_file = st.file_uploader("Choose a CV file", type=["txt", "pdf", "docx"])
|
| 95 |
|
| 96 |
if uploaded_file is not None:
|
| 97 |
-
cv_content =
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
|
| 106 |
-
|
| 107 |
|
| 108 |
-
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
| 134 |
main()
|
|
|
|
| 6 |
from typing import List, Dict, Tuple, Any
|
| 7 |
import time
|
| 8 |
import asyncio
|
| 9 |
+
import chardet
|
| 10 |
from llm_functions import extract_personal_info, check_spelling_grammar, identify_sections, assess_section_quality
|
| 11 |
|
| 12 |
+
# ... (previous code remains the same)
|
| 13 |
+
|
| 14 |
+
def read_file_content(uploaded_file):
|
| 15 |
+
bytes_data = uploaded_file.read()
|
| 16 |
+
try:
|
| 17 |
+
# Try UTF-8 first
|
| 18 |
+
return bytes_data.decode('utf-8')
|
| 19 |
+
except UnicodeDecodeError:
|
| 20 |
+
# If UTF-8 fails, detect the encoding
|
| 21 |
+
detected = chardet.detect(bytes_data)
|
| 22 |
+
if detected['encoding']:
|
| 23 |
+
try:
|
| 24 |
+
return bytes_data.decode(detected['encoding'])
|
| 25 |
+
except UnicodeDecodeError:
|
| 26 |
+
st.error(f"Unable to decode the file with detected encoding: {detected['encoding']}")
|
| 27 |
+
else:
|
| 28 |
+
st.error("Unable to detect file encoding. Please ensure the file is text-based and try again.")
|
| 29 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Streamlit app
|
| 32 |
def main():
|
|
|
|
| 35 |
uploaded_file = st.file_uploader("Choose a CV file", type=["txt", "pdf", "docx"])
|
| 36 |
|
| 37 |
if uploaded_file is not None:
|
| 38 |
+
cv_content = read_file_content(uploaded_file)
|
| 39 |
+
if cv_content:
|
| 40 |
+
st.text_area("CV Content", cv_content, height=200)
|
| 41 |
|
| 42 |
+
if st.button("Analyze CV"):
|
| 43 |
+
with st.spinner("Analyzing CV..."):
|
| 44 |
+
start_time = time.time()
|
| 45 |
+
result = analyze_cv(cv_content)
|
| 46 |
+
end_time = time.time()
|
| 47 |
|
| 48 |
+
st.success(f"Analysis completed in {end_time - start_time:.2f} seconds")
|
| 49 |
|
| 50 |
+
st.subheader("Analysis Results")
|
| 51 |
|
| 52 |
+
st.write("Personal Information:")
|
| 53 |
+
st.json(result["personalInfo"])
|
| 54 |
+
st.write(f"Score: {result['personalInfoScore']['score']}/{result['personalInfoScore']['maxScore']}")
|
| 55 |
|
| 56 |
+
st.write("Spelling and Grammar:")
|
| 57 |
+
st.write(f"Error Percentage: {result['spellingGrammar']['errorPercentage']:.2f}%")
|
| 58 |
+
st.write(f"Score: {result['spellingGrammar']['score']}/{result['spellingGrammar']['maxScore']}")
|
| 59 |
|
| 60 |
+
st.write("Completeness:")
|
| 61 |
+
st.write(f"Present Sections: {', '.join(result['completeness']['presentSections'])}")
|
| 62 |
+
st.write(f"Score: {result['completeness']['score']}/{result['completeness']['maxScore']}")
|
| 63 |
|
| 64 |
+
st.write("Content Quality:")
|
| 65 |
+
for section, quality in result["contentQuality"].items():
|
| 66 |
+
st.write(f"{section}: Quality {quality['quality']}/5 - {quality['feedback']}")
|
| 67 |
+
st.write(f"Score: {result['contentQualityScore']['score']}/{result['contentQualityScore']['maxScore']}")
|
| 68 |
|
| 69 |
+
st.subheader("Total Score")
|
| 70 |
+
st.write(f"{result['totalScore']}/{result['maxTotalScore']}")
|
| 71 |
|
| 72 |
+
st.subheader("Detailed Analysis")
|
| 73 |
+
st.json(result)
|
| 74 |
+
else:
|
| 75 |
+
st.error("Failed to read the file. Please try uploading a different file.")
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
main()
|