Nassira/completeness
0
1import streamlit as st2from PIL import Image3import json4import logging5import tempfile6import os7from cv_analyzer import analyze_cv8from ocr_utils import extract_text_aws, extract_text_doctr, extract_text_easyocr, extract_text_paddleocr, load_models, combine_ocr_results, detect_language9from config import weights10from spelling_grammar_checker import evaluate_cv_text11from personal_info_extractor import analyze_personal_info12 13# Configure logging14logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')15 16def main():17 st.title("CV Text Extraction and Analysis")18 uploaded_file = st.file_uploader("Choose a CV image file", type=["png", "jpg", "jpeg"])19 20 if uploaded_file is not None:21 image = Image.open(uploaded_file)22 with st.spinner('Processing CV...'):23 try:24 # Save the uploaded file temporarily25 with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as temp_file:26 temp_file.write(uploaded_file.getvalue())27 temp_file_path = temp_file.name28 29 # Extract text using AWS30 aws_results = extract_text_aws(uploaded_file.getvalue())31 32 # Detect language33 sample_text = ' '.join([item[0] for item in aws_results[:10]])34 detected_language = detect_language(sample_text)35 36 # Load OCR models37 doctr_model, easyocr_reader, paddleocr_reader = load_models(detected_language)38 39 if doctr_model is None or easyocr_reader is None or paddleocr_reader is None:40 st.error("Failed to load OCR models. Please check the logs for details.")41 return42 43 # Extract text using different OCR methods44 results = {45 "aws": aws_results,46 "doctr": extract_text_doctr(image, doctr_model),47 "easyocr": extract_text_easyocr(image, easyocr_reader),48 "paddleocr": extract_text_paddleocr(image, paddleocr_reader),49 }50 51 # Combine OCR results52 combined_text = combine_ocr_results(results, weights)53 54 # Analyze CV (including completeness check)55 cv_analysis = analyze_cv(temp_file_path)56 57 # Analyze personal information58 personal_info = analyze_personal_info(temp_file_path)59 60 # Evaluate spelling and grammar61 spelling_grammar_result = evaluate_cv_text(temp_file_path)62 63 # Display results64 st.subheader("Extracted Text")65 st.text_area("", combined_text, height=300)66 67 st.subheader("Detected CV Sections")68 st.write(cv_analysis["present_sections"])69 70 st.subheader("CV Completeness Analysis")71 completeness = cv_analysis["completeness_analysis"]72 for section in completeness["sections"]:73 st.write(f"**{section['name']}**")74 for element in section['elements']:75 status = "✅" if element['exists'] else "❌"76 st.write(f"{status} {element['name']} (Score: {element['score']})")77 st.write("---")78 79 st.subheader("Total Completeness Score")80 st.write(f"{completeness['score_of_completeness']:.2f} / 10")81 82 st.subheader("Personal Information")83 st.write(f"Email: {personal_info['email']}")84 st.write(f"Phone: {personal_info['phone']}")85 st.write(f"City: {personal_info['city']}")86 st.write(f"Country: {personal_info['country']}")87 88 st.subheader("Personal Information Score")89 st.write(personal_info["score_personal_information"])90 91 st.subheader("Spelling and Grammar")92 st.write(f"Error percentage: {spelling_grammar_result['error_percentage']:.2f}%")93 st.write(f"Score: {spelling_grammar_result['score']}")94 95 st.subheader("Total Score")96 total_score = (97 completeness['score_of_completeness'] +98 personal_info["score_personal_information"] +99 spelling_grammar_result["score"]100 )101 st.write(f"{total_score:.2f}")102 103 # Clean up the temporary file104 os.unlink(temp_file_path)105 106 except Exception as e:107 logging.error(f"Error in main processing: {str(e)}", exc_info=True)108 st.error(f"An error occurred during processing: {str(e)}")109 110if __name__ == "__main__":111 main()