Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import openai | |
| import os | |
| import fitz # PyMuPDF | |
| # Set OpenAI API key from environment variable | |
| api_key = os.getenv("OPENAI_API_KEY") | |
| if not api_key: | |
| raise ValueError("OpenAI API key not found. Please set it in the environment variables.") | |
| openai.api_key = api_key | |
| def extract_text_from_pdf(pdf_file): | |
| try: | |
| # Open the PDF file | |
| document = fitz.open(pdf_file.name) | |
| text = "" | |
| # Extract text from each page | |
| for page_num in range(len(document)): | |
| page = document.load_page(page_num) | |
| text += page.get_text() | |
| return text | |
| except Exception as e: | |
| return f"Error extracting text from PDF: {e}" | |
| def extract_keywords(job_description): | |
| prompt = f"""به عنوان یک تحلیلگر حرفهای، لطفا مهمترین کلمات کلیدی را از شرح شغل زیر استخراج کنید: | |
| شرح شغل: {job_description} | |
| پاسخ را به صورت یک لیست از کلمات کلیدی ارائه دهید. | |
| """ | |
| try: | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4", | |
| messages=[ | |
| {"role": "system", "content": "You are a helpful assistant."}, | |
| {"role": "user", "content": prompt} | |
| ], | |
| temperature=0 | |
| ) | |
| keywords = response.choices[0].message['content'] | |
| return keywords | |
| except Exception as e: | |
| return f"Error during keyword extraction: {e}" | |
| def evaluate_resume(pdf_file, job_description): | |
| # Extract keywords from job description | |
| keywords = extract_keywords(job_description) | |
| if "Error" in keywords: | |
| return keywords | |
| # Extract text from PDF | |
| resume_text = extract_text_from_pdf(pdf_file) | |
| if "Error" in resume_text: | |
| return resume_text | |
| prompt = f"""به عنوان یک تحلیلگر با تجربه سیستم ردیابی متقاضی (ATS)، نقش شما شامل ارزیابی رزومه در برابر شرح شغل و کلمات کلیدی استخراج شده است. | |
| لطفاً رزومه فرد را با کلمات کلیدی زیر مطابقت دهید و درصد تطابق را بر اساس معیارهای کلیدی و همچنین تعداد کلمات کلیدی گمشده و منطبق را محاسبه کنید. | |
| کلمات کلیدی: {keywords} | |
| رزومه: {resume_text} | |
| من پاسخ را در یک رشته با ساختار زیر میخواهم: | |
| {{"تطابق شرح شغل با رزومه فرد ":"%"، "تعداد کلمات کلیدی غیر منطبق ":""، "تعداد کلمات کلیدی منطبق ":" }} | |
| """ | |
| try: | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4", | |
| messages=[ | |
| {"role": "system", "content": "You are a helpful assistant."}, | |
| {"role": "user", "content": prompt} | |
| ], | |
| temperature=0 | |
| ) | |
| return response.choices[0].message['content'] | |
| except Exception as e: | |
| return f"Error during resume evaluation: {e}" | |
| iface = gr.Interface( | |
| fn=evaluate_resume, | |
| inputs=[ | |
| gr.File(label="Upload Resume PDF"), | |
| gr.Textbox(lines=10, label="Job Description") | |
| ], | |
| outputs="text", | |
| title="Resume Evaluator" | |
| ) | |
| iface.launch() | |