import aiohttp import asyncio import nest_asyncio import streamlit as st import time from PyPDF2 import PdfReader from pdfminer.high_level import extract_text from docx import Document import os from openai import OpenAI OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") prompt = """ You will receive a user's resume and a corresponding job description. Your task is to analyze the resume to identify any gaps between the user's skills, experience, and qualifications and the requirements specified in the job description. Based on this analysis, create a 10-15 day interview preparation plan tailored to the job's specific requirements. The plan should focus on strengthening the areas where the user already meets the requirements, while also addressing and highlighting any skill gaps that need attention. Before creating the Interview Preparation Guide, check if there is a large in the requirements listed in the job description and what is listed in the resume in terms of years of experience, and necessary skills. You must assess if it feasible for the user to be able to apply and prepare for an interview for the given job. If not then simply tell the user that there are significant gaps between their skills and qualification and what the job requires Important Note: - Speak to the user - "Your Resume..." - Do not include the gaps or analysis in the output. Only include the Interview Preparation Guide """ def extract_text_from_pdf(file): try: # Reset file pointer to the start of the file file.seek(0) text = extract_text(file) return text except Exception as e: print(f"An error occurred: {e}") return None def extract_text_from_file(file): file_extension = os.path.splitext(file.name)[1] if file_extension == '.pdf': return extract_text_from_pdf(file) elif file_extension == '.docx': doc = Document(file) return "\n".join([paragraph.text for paragraph in doc.paragraphs]) elif file_extension == '.txt': return file.read().decode('utf-8') else: return "Unsupported file type" def call_openai_api(job_description, resume_text): client = OpenAI(api_key =OPENAI_API_KEY) completion = client.chat.completions.create( model="gpt-4o", temperature=0.1, messages=[ {"role": "system", "content": prompt}, {"role": "user", "content": f"Here is the resume text: {resume_text} \n Here is the job description: {job_description}"} ] ) return completion.choices[0].message.content # async def generate_completion(prompt, user_prompt, api_key): # url = "https://api.openai.com/v1/chat/completions" # headers = { # "Content-Type": "application/json", # "Authorization": f"Bearer {api_key}" # } # # payload = { # "model": "gpt-4o", # "messages": [ # {"role": "system", "content": prompt}, # {"role": "user", # "content": user_prompt} # ], # "temperature": 0.1, # # "max_tokens": 385 # } # # async with aiohttp.ClientSession() as session: # async with session.post(url, headers=headers, json=payload) as response: # if response.status != 200: # print(f"Error: {response.status}") # return None # response_json = await response.json() # return response_json def call_api(resume_text, job_description): task = call_openai_api(job_description, resume_text) return task # async def main(resume_text, job_description): # api_key = "sk-learning-bot-staging-account-SmwsiBUPWrJdslovYypiT3BlbkFJ70FRptLQcReZElmMFv6P" # # default_prompt = construct_default_prompt(resume_text, job_description) # objective_prompt = construct_objective_prompt(resume_text) # # task1 = generate_completion(prompt, default_prompt, api_key) # responses_async = await asyncio.gather(task1) # # return responses_async st.set_page_config(page_title="Resume Analyzer", layout="centered") st.title("Resume Analyzer") job_description_source = st.radio("Job Description Source", ("Upload File", "Paste Text")) if job_description_source == "Upload File": job_description_file = st.file_uploader("Upload Job Description", type=['pdf', 'docx', 'txt']) job_description = extract_text_from_file(job_description_file) if job_description_file else None else: job_description = st.text_area("Paste Job Description Here") resume_file = st.file_uploader("Upload Resume", type=['pdf', 'docx', 'txt']) if job_description: st.subheader("Job Description Preview") st.text_area("Job Description", job_description, height=200) resume_text = None if resume_file: resume_text = extract_text_from_file(resume_file) st.subheader("Resume Preview") st.text_area("Resume", resume_text, height=200) # def construct_default_prompt(resume_text, job_description): # return f"Here is the resume text: {resume_text} \n Here is the job description: {job_description}" # # def construct_objective_prompt(resume_text): # return f"Here is the resume text: {resume_text}" if st.button("Analyze Resume"): if job_description and resume_file: start_time = time.time() responses_async = call_api(resume_text, job_description) end_time = time.time() total_time = end_time - start_time st.subheader("Analysis Result") st.text_area("Result", responses_async, height=400) # st.write(f"Time taken for OpenAI response: {response_time:.2f} seconds") st.write(f"Total time taken for execution: {total_time:.2f} seconds") else: st.error("Please provide both the job description and resume.")