import os OR_API_KEY = os.getenv("OR_API_KEY") # HELICON_API_KEY = os.getenv("HELICON_API_KEY") # SUPABASE_USER = os.environ['SUPABASE_USER'] # SUPABASE_PASSWORD = os.environ['SUPABASE_PASSWORD'] import fitz # PyMuPDF for PDFs import docx # python-docx for Word documents import requests from bs4 import BeautifulSoup import json import re import sys from pydantic import HttpUrl import json from openai import OpenAI SysPromptOpt = """ **You are a professional resume optimization expert. You have been optimizing resumes for over 20 years, helping thousands of job seekers secure their dream jobs across various industries. Your expertise lies in aligning resumes perfectly with job descriptions to highlight relevant skills, experience, and accomplishments.** **Objective:** Optimize the user's resume by tailoring it to a specific job description. Ensure that the final resume showcases the most relevant skills, experience, and achievements in a way that aligns perfectly with the requirements and preferences outlined in the job description. The goal is to significantly increase the user's chances of landing an interview for this position. **Steps:** 1. **Analyze the Job Description:** - Carefully read the provided job description. - Identify and list the key skills, qualifications, and experiences the employer is seeking. - Highlight any specific keywords, phrases, or technical skills that are emphasized. 2. **Review the User's Resume:** - Examine the user's current resume thoroughly. - Identify and list the skills, experiences, and accomplishments that are most relevant to the job description. - Note any gaps or areas that could be better aligned with the job description. 3. **Optimize the Resume:** - Rewrite sections of the resume to better match the job description, incorporating relevant keywords and phrases. - Emphasize the user’s most pertinent skills and experiences in a way that demonstrates their suitability for the role. - Ensure the resume is formatted professionally, with clear and concise language, and free of any errors. 4. **Finalize and Review:** - Review the optimized resume for coherence and alignment with the job description. - Make any necessary adjustments to improve clarity and impact. - Ensure the final resume is compelling and effectively tailored to the job description. ONLY OUTPUT THE FOLLOWING IN THE FOLLOWING FORMAT: ..... ...... """ or_client = OpenAI(api_key=OR_API_KEY, base_url = "https://openrouter.ai/api/v1") def together_response(messages,model="openai/gpt-4o-mini",SysPrompt=SysPromptOpt): response = or_client.chat.completions.create( model=model, messages=messages, max_tokens=4096, ) response_message = response.choices[0].message.content return response_message import tiktoken # Used to limit tokens encoding = tiktoken.encoding_for_model("gpt-4o") def limit_tokens(input_string, token_limit=7500): """ Limit tokens sent to the model """ return encoding.decode(encoding.encode(input_string)[:token_limit]) # Function to optimize the resume using OpenAI def optimize_resume_api(resume, job_desc): prompt = f"Optimize the following Resume:\n ## RESUME:{limit_tokens(resume, token_limit=4000)}\n\n for the:## JOB DESCRIPTION:{limit_tokens(job_desc,token_limit=4000)}\n\n" messages = [{"role": "assistant", "content": SysPromptOpt}, {"role": "user", "content": prompt}] response = together_response(messages, model = "openai/gpt-4o-mini") return response # Function to extract text from a Word document def extract_text_from_word(docx_file): doc = docx.Document(docx_file) return "\n".join([para.text for para in doc.paragraphs]) # Function to extract text from a PDF file def extract_text_from_pdf(pdf_file): doc = fitz.open(stream=pdf_file.read(), filetype="pdf") text = "" for page in doc: text += page.get_text() return text # Function to read text from a plain text file def read_text_file(text_file): return text_file.read().decode("utf-8") # Function to scrape a website and extract content def scrape_website(url): response = requests.post( 'https://pvanand-web-scraping.hf.space/scrape', headers={ 'accept': 'application/json', 'Content-Type': 'application/json' }, json={'url': url} ) return response.json() # Function to extract main content from HTML def extract_main_content(html): if html: soup = BeautifulSoup(html, 'lxml') plain_text = soup.get_text(separator=" ", strip=True) return clean_paragraph(plain_text) return "" # Function to extract JSON from script tags in HTML def extract_json_from_script(html): if html: soup = BeautifulSoup(html, 'lxml') script_tags = soup.find_all('script') json_objects = [] for tag in script_tags: if tag.string and '{' in tag.string and '}' in tag.string: script_content = tag.string.strip() json_like_content = re.search(r'\{.*\}', script_content, re.DOTALL) if json_like_content: json_text = json_like_content.group(0) try: data = json.loads(json_text) json_objects.append(data) except json.JSONDecodeError: continue # Skip this script if JSON parsing fails if json_objects: return json.dumps(json_objects, indent=4) # Pretty-print the JSON data return "No suitable JSON script tag found." return "No HTML content." # Function to fetch and parse job description from a link def fetch_job_description(link: HttpUrl): # Convert the Url object to a string link_str = str(link) response = scrape_website(link_str)["content"] job_desc = extract_main_content(response) if len(job_desc) < 100: job_desc = extract_main_content(extract_json_from_script(response)) return job_desc # Function to read file content based on file type def read_file(file): if not file or not file.filename: raise ValueError("No file uploaded or filename is empty") if file.filename.endswith(".pdf"): return extract_text_from_pdf(file.file) elif file.filename.endswith(".docx"): return extract_text_from_word(file.file) elif file.filename.endswith(".txt"): return read_text_file(file.file) else: raise ValueError(f"Unsupported file type: {file.filename}") def clean_paragraph(paragraph, n=4): """ Clean a paragraph of text by removing words that occur more than n times. """ # Split the paragraph into words words = re.findall(r'\b\w+\b', paragraph) # Create a dictionary to count the occurrences of each word word_count = {} for word in words: word_count[word] = word_count.get(word, 0) + 1 # Filter out words that occur more than n times cleaned_words = [word for word in words if word_count[word] <= n] # Join the cleaned words back into a paragraph cleaned_paragraph = ' '.join(cleaned_words) return cleaned_paragraph