import streamlit as st from PIL import Image import json import logging import tempfile import os from cv_analyzer import analyze_cv from ocr_utils import extract_text_aws, extract_text_doctr, extract_text_easyocr, extract_text_paddleocr, load_models, combine_ocr_results, detect_language from config import weights from spelling_grammar_checker import evaluate_cv_text from personal_info_extractor import analyze_personal_info # Configure logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') def main(): st.title("CV Text Extraction and Analysis") uploaded_file = st.file_uploader("Choose a CV image file", type=["png", "jpg", "jpeg"]) if uploaded_file is not None: image = Image.open(uploaded_file) with st.spinner('Processing CV...'): try: # Save the uploaded file temporarily with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as temp_file: temp_file.write(uploaded_file.getvalue()) temp_file_path = temp_file.name # Extract text using AWS aws_results = extract_text_aws(uploaded_file.getvalue()) # Detect language sample_text = ' '.join([item[0] for item in aws_results[:10]]) detected_language = detect_language(sample_text) # Load OCR models doctr_model, easyocr_reader, paddleocr_reader = load_models(detected_language) if doctr_model is None or easyocr_reader is None or paddleocr_reader is None: st.error("Failed to load OCR models. Please check the logs for details.") return # Extract text using different OCR methods results = { "aws": aws_results, "doctr": extract_text_doctr(image, doctr_model), "easyocr": extract_text_easyocr(image, easyocr_reader), "paddleocr": extract_text_paddleocr(image, paddleocr_reader), } # Combine OCR results combined_text = combine_ocr_results(results, weights) # Analyze CV (including completeness check) cv_analysis = analyze_cv(temp_file_path) # Analyze personal information personal_info = analyze_personal_info(temp_file_path) # Evaluate spelling and grammar spelling_grammar_result = evaluate_cv_text(temp_file_path) # Display results st.subheader("Extracted Text") st.text_area("", combined_text, height=300) st.subheader("Detected CV Sections") st.write(cv_analysis["present_sections"]) st.subheader("CV Completeness Analysis") completeness = cv_analysis["completeness_analysis"] for section in completeness["sections"]: st.write(f"**{section['name']}**") for element in section['elements']: status = "✅" if element['exists'] else "❌" st.write(f"{status} {element['name']} (Score: {element['score']})") st.write("---") st.subheader("Total Completeness Score") st.write(f"{completeness['score_of_completeness']:.2f} / 10") st.subheader("Personal Information") st.write(f"Email: {personal_info['email']}") st.write(f"Phone: {personal_info['phone']}") st.write(f"City: {personal_info['city']}") st.write(f"Country: {personal_info['country']}") st.subheader("Personal Information Score") st.write(personal_info["score_personal_information"]) st.subheader("Spelling and Grammar") st.write(f"Error percentage: {spelling_grammar_result['error_percentage']:.2f}%") st.write(f"Score: {spelling_grammar_result['score']}") st.subheader("Total Score") total_score = ( completeness['score_of_completeness'] + personal_info["score_personal_information"] + spelling_grammar_result["score"] ) st.write(f"{total_score:.2f}") # Clean up the temporary file os.unlink(temp_file_path) except Exception as e: logging.error(f"Error in main processing: {str(e)}", exc_info=True) st.error(f"An error occurred during processing: {str(e)}") if __name__ == "__main__": main()