import streamlit as st from PIL import Image import json import logging import tempfile import os from cv_analyzer import detect_cv_sections_and_score 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) # Detect CV sections and score detection_result = detect_cv_sections_and_score(combined_text) # Analyze personal information personal_info = analyze_personal_info(temp_file_path) # Evaluate spelling and grammar spelling_grammar_result = evaluate_cv_text(temp_file_path) # Calculate total score total_score = ( detection_result["score_sections"] + personal_info["score_personal_information"] + spelling_grammar_result["score"] ) # Display results st.subheader("Extracted Text") st.text_area("", combined_text, height=300) st.subheader("Detected CV Sections") st.write(detection_result["present_sections"]) st.subheader("CV Section Score") st.write(detection_result["score_sections"]) 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") st.write(total_score) # 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()