import streamlit as st import json import os from openai import OpenAI import PyPDF2 import io def load_evaluation_criteria(file_path): with open(file_path, 'r') as file: return json.load(file) def extract_text_from_pdf(pdf_file): pdf_reader = PyPDF2.PdfReader(pdf_file) text = "" for page in pdf_reader.pages: text += page.extract_text() return text def create_prompt(evaluation_criteria, cv_content): prompt = f""" Evaluate the following CV content based on these criteria: {json.dumps(evaluation_criteria, indent=2)} CV Content: {cv_content} For each section (Education, Work Experience, Summary and Objective), identify all subsections and check if each required element is present. Respond with 'yes' if the element is present, and 'no' if it is not. Format your response exactly as follows: Education: Subsection 1: [Element 1]: yes/no [Element 2]: yes/no ... Subsection 2: [Element 1]: yes/no [Element 2]: yes/no ... Work Experience: Subsection 1: [Element 1]: yes/no [Element 2]: yes/no ... Subsection 2: [Element 1]: yes/no [Element 2]: yes/no ... Summary and Objective: Subsection 1: [Element 1]: yes/no [Element 2]: yes/no ... Do not include any additional text, explanations, or feedback. """ return prompt def analyze_cv(cv_content, evaluation_criteria, api_key): prompt = create_prompt(evaluation_criteria, cv_content) client = OpenAI(api_key=api_key) response = client.chat.completions.create( messages=[ { "role": "user", "content": prompt, } ], model="gpt-3.5-turbo", ) return response.choices[0].message.content def element_exists(subsection, element): return subsection.get(element, 'no').lower() == 'yes' def evaluate_cv_section(section_content, section_structure): total_score = 0 total_possible = 0 for subsection in section_content: subsection_score = 0 for element in section_structure["elements"]: if element_exists(subsection, element): subsection_score += section_structure["score_per_element"] total_score += subsection_score total_possible += len(section_structure["elements"]) * section_structure["score_per_element"] percentage_score = (total_score / total_possible) * 100 if total_possible > 0 else 0 return { "score": total_score, "total_possible": total_possible, "percentage": percentage_score } def evaluate_cv(cv_content, evaluation_structure): results = {} for section in evaluation_structure["main_sections"]: results[section] = evaluate_cv_section(cv_content[section], evaluation_structure[section]) return results def main(): st.title("CV Content Quality Evaluator") criteria_file_path = 'cv_evaluation_criteria.json' evaluation_criteria = load_evaluation_criteria(criteria_file_path) # Use the API key from environment variable api_key = os.environ.get("OPENAI_API_KEY") if not api_key: st.error("OpenAI API key not found. Please set the OPENAI_API_KEY environment variable.") return st.subheader("Upload CV") uploaded_file = st.file_uploader("Choose a PDF file", type="pdf") if uploaded_file is not None: cv_text = extract_text_from_pdf(uploaded_file) st.text_area("Extracted CV Content:", cv_text, height=300) if st.button("Evaluate CV"): with st.spinner("Evaluating CV..."): ai_result = analyze_cv(cv_text, evaluation_criteria, api_key) try: # Parse AI result into a format suitable for evaluate_cv parsed_cv_content = {} current_section = None current_subsection = None for line in ai_result.strip().split('\n'): if ':' not in line: if line in evaluation_criteria["content_quality"]["main_sections"]: current_section = line parsed_cv_content[current_section] = [] elif line.startswith("Subsection"): current_subsection = {} parsed_cv_content[current_section].append(current_subsection) else: element, presence = line.split(':') current_subsection[element.strip()] = presence.strip() evaluation_results = evaluate_cv(parsed_cv_content, evaluation_criteria["content_quality"]) st.subheader("Evaluation Result") total_score = 0 total_possible = 0 for section, result in evaluation_results.items(): st.write(f"{section}: {result['score']}/{result['total_possible']} ({result['percentage']:.2f}%)") total_score += result['score'] total_possible += result['total_possible'] overall_percentage = (total_score / total_possible) * 100 if total_possible > 0 else 0 st.write(f"Overall Score: {total_score}/{total_possible} ({overall_percentage:.2f}%)") except Exception as e: st.error(f"Error processing the evaluation result: {str(e)}. Please try again.") if __name__ == "__main__": main()