from transformers import AutoTokenizer from optimum.intel.openvino import OVModelForCausalLM import gradio as gr from transformers import pipeline import json # Load OpenVINO GPT-J model model_id = "OpenVINO/gpt-j-6b-int4-ov" tokenizer = AutoTokenizer.from_pretrained(model_id) model = OVModelForCausalLM.from_pretrained(model_id) # Summarization pipeline summarizer = pipeline("summarization") def summarize_text(text, max_length=100): summary = summarizer(text, max_length=max_length, min_length=25, do_sample=False) return summary[0]["summary_text"] def match_cv_to_jobs(cv_text, job_descriptions): debug_info = "Debug Info:\n" results = [] # Summarize the CV text summarized_cv = summarize_text(cv_text, max_length=400) debug_info += f"Summarized CV Text: {summarized_cv}\n" descriptions = job_descriptions.strip().split("\n") for description in descriptions: summarized_description = summarize_text(description, max_length=100) debug_info += f"\nSummarized Job Description: {summarized_description}\n" # Create a prompt to compare the summarized CV with each job description prompt = ( f"Compare the following job description with this resume. Job Description: {summarized_description}. " f"Resume: {summarized_cv}. Provide a match score and a brief analysis." ) debug_info += f"\nGenerated Prompt: {prompt}\n" # Generate response from the model inputs = tokenizer(prompt, return_tensors="pt") try: outputs = model.generate(**inputs, max_length=200) response_content = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0] debug_info += f"Model Response: {response_content}\n" try: response_data = json.loads(response_content) results.append(response_data) except json.JSONDecodeError: results.append({ "Job Description": description, "Analysis": response_content }) except Exception as e: debug_info += f"Error: {str(e)}\n" results.append({"Job Description": description, "Error": str(e)}) return results, debug_info # Gradio interface with gr.Blocks() as demo: gr.Markdown("# CV and Job Description Matcher with Summarization and Debugging") # Input fields for CV and job descriptions cv_text = gr.Textbox(label="CV Text", placeholder="Enter the CV text here", lines=10) job_descriptions = gr.Textbox(label="Job Descriptions (one per line)", placeholder="Enter each job description on a new line", lines=5) # Button and output area match_button = gr.Button("Match CV to Job Descriptions") output = gr.JSON(label="Match Results") debug_output = gr.Textbox(label="Debug Info", lines=10) # Add a debug box to display debug info # Set button click to run the function match_button.click(fn=match_cv_to_jobs, inputs=[cv_text, job_descriptions], outputs=[output, debug_output]) demo.launch()