Spaces:
Sleeping
Sleeping
Download app.py from MerlinValanarasu/ARC: direct link, hf CLI and curl.
- Browser
- Download file 21.5 kB
-
https://huggingface.co/spaces/MerlinValanarasu/ARC/resolve/main/app.py
- Command line
-
hf download hf://spaces/MerlinValanarasu/ARC/app.py
-
curl -L -o app.py https://huggingface.co/spaces/MerlinValanarasu/ARC/resolve/main/app.py
21.5 kB
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "id": "2a42ee92-b6a0-4a96-af66-3ac795fe6b6b", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stderr", | |
| "output_type": "stream", | |
| "text": [ | |
| "[nltk_data] Downloading package stopwords to\n", | |
| "[nltk_data] C:\\Users\\valan\\AppData\\Roaming\\nltk_data...\n", | |
| "[nltk_data] Package stopwords is already up-to-date!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# For Hugging Face Spaces with Streamlit\n", | |
| "import os\n", | |
| "os.system(\"python -m spacy download en_core_web_sm\")\n" | |
| "import streamlit as st\n", | |
| "import pdfplumber\n", | |
| "import docx2txt\n", | |
| "import spacy\n", | |
| "import re\n", | |
| "from sklearn.feature_extraction.text import TfidfVectorizer\n", | |
| "from sklearn.metrics.pairwise import cosine_similarity\n", | |
| "import pandas as pd\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "import nltk\n", | |
| "from nltk.corpus import stopwords\n", | |
| "from collections import Counter\n", | |
| "import base64\n", | |
| "\n", | |
| "# Download NLTK data\n", | |
| "@st.cache_resource\n", | |
| "def download_nltk_data():\n", | |
| " nltk.download('stopwords')\n", | |
| " return True\n", | |
| "\n", | |
| "# Load spaCy model\n", | |
| "@st.cache_resource\n", | |
| "def load_spacy():\n", | |
| " try:\n", | |
| " return spacy.load(\"en_core_web_sm\")\n", | |
| " except OSError:\n", | |
| " import os\n", | |
| " os.system(\"python -m spacy download en_core_web_sm\")\n", | |
| " return spacy.load(\"en_core_web_sm\")\n", | |
| "\n", | |
| "# Initialize\n", | |
| "download_nltk_data()\n", | |
| "nlp = load_spacy()\n", | |
| "\n", | |
| "# Resume parsing functions\n", | |
| "def extract_text_from_pdf(file):\n", | |
| " with pdfplumber.open(file) as pdf:\n", | |
| " text = \" \".join([page.extract_text() or \"\" for page in pdf.pages])\n", | |
| " return text\n", | |
| "\n", | |
| "def extract_text_from_docx(file):\n", | |
| " text = docx2txt.process(file)\n", | |
| " return text\n", | |
| "\n", | |
| "def extract_text(file):\n", | |
| " file_name = file.name.lower()\n", | |
| " if file_name.endswith('.pdf'):\n", | |
| " return extract_text_from_pdf(file)\n", | |
| " elif file_name.endswith('.docx'):\n", | |
| " return extract_text_from_docx(file)\n", | |
| " else:\n", | |
| " return \"Unsupported file format. Please upload a PDF or DOCX file.\"\n", | |
| "\n", | |
| "# ATS Scoring functions\n", | |
| "def preprocess_text(text):\n", | |
| " text = text.lower()\n", | |
| " text = re.sub(r'[^\\w\\s]', ' ', text)\n", | |
| " text = re.sub(r'\\d+', ' ', text)\n", | |
| " text = re.sub(r'\\s+', ' ', text).strip()\n", | |
| " return text\n", | |
| "\n", | |
| "def calculate_similarity_score(resume_text, job_desc_text):\n", | |
| " resume_processed = preprocess_text(resume_text)\n", | |
| " job_processed = preprocess_text(job_desc_text)\n", | |
| " \n", | |
| " vectorizer = TfidfVectorizer(stop_words='english')\n", | |
| " try:\n", | |
| " tfidf_matrix = vectorizer.fit_transform([resume_processed, job_processed])\n", | |
| " similarity = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])[0][0]\n", | |
| " return similarity * 100\n", | |
| " except:\n", | |
| " return 0\n", | |
| "\n", | |
| "def extract_skills_from_text(text):\n", | |
| " doc = nlp(text.lower())\n", | |
| " potential_skills = [token.text for token in doc if token.pos_ in [\"NOUN\", \"PROPN\"]]\n", | |
| " \n", | |
| " stop_words = set(stopwords.words('english'))\n", | |
| " skills = [skill for skill in potential_skills \n", | |
| " if skill not in stop_words and len(skill) > 2]\n", | |
| " \n", | |
| " skill_freq = Counter(skills)\n", | |
| " return skill_freq\n", | |
| "\n", | |
| "def get_missing_keywords(resume_text, job_desc_text, threshold=2):\n", | |
| " job_skills = extract_skills_from_text(job_desc_text)\n", | |
| " resume_skills = extract_skills_from_text(resume_text)\n", | |
| " \n", | |
| " missing_skills = {skill: count for skill, count in job_skills.items() \n", | |
| " if count >= threshold and skill not in resume_skills}\n", | |
| " \n", | |
| " sorted_missing = sorted(missing_skills.items(), key=lambda x: x[1], reverse=True)\n", | |
| " return sorted_missing\n", | |
| "\n", | |
| "def check_ats_formatting(resume_text):\n", | |
| " formatting_issues = []\n", | |
| " \n", | |
| " important_sections = ['experience', 'education', 'skills', 'projects']\n", | |
| " found_sections = []\n", | |
| " \n", | |
| " for section in important_sections:\n", | |
| " pattern = re.compile(r'\\b' + section + r'\\b', re.IGNORECASE)\n", | |
| " if pattern.search(resume_text):\n", | |
| " found_sections.append(section)\n", | |
| " \n", | |
| " missing_sections = set(important_sections) - set(found_sections)\n", | |
| " if missing_sections:\n", | |
| " formatting_issues.append(f\"Missing key sections: {', '.join(missing_sections)}\")\n", | |
| " \n", | |
| " if 'โข' not in resume_text and '*' not in resume_text and '-' not in resume_text:\n", | |
| " formatting_issues.append(\"No bullet points detected - consider using them for listing skills and responsibilities\")\n", | |
| " \n", | |
| " if '|' in resume_text or '\\t' in resume_text:\n", | |
| " formatting_issues.append(\"Possible tables detected - avoid tables as they may not parse correctly\")\n", | |
| " \n", | |
| " contact_patterns = [\n", | |
| " r'\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b', # Email\n", | |
| " r'\\b\\d{3}[-.]?\\d{3}[-.]?\\d{4}\\b', # Phone number\n", | |
| " r'linkedin\\.com' # LinkedIn\n", | |
| " ]\n", | |
| " \n", | |
| " missing_contact = []\n", | |
| " for i, pattern in enumerate(['email', 'phone', 'linkedin']):\n", | |
| " if not re.search(contact_patterns[i], resume_text, re.IGNORECASE):\n", | |
| " missing_contact.append(pattern)\n", | |
| " \n", | |
| " if missing_contact:\n", | |
| " formatting_issues.append(f\"Missing contact information: {', '.join(missing_contact)}\")\n", | |
| " \n", | |
| " paragraphs = resume_text.split('\\n\\n')\n", | |
| " long_paragraphs = [p for p in paragraphs if len(p.split()) > 40]\n", | |
| " if long_paragraphs:\n", | |
| " formatting_issues.append(f\"Found {len(long_paragraphs)} long paragraphs - consider breaking them into bullet points\")\n", | |
| " \n", | |
| " return formatting_issues if formatting_issues else [\"No major formatting issues detected!\"]\n", | |
| "\n", | |
| "# Resume advice function\n", | |
| "def get_resume_advice(query, resume_score=None, missing_keywords=None, formatting_issues=None):\n", | |
| " query_lower = query.lower()\n", | |
| " \n", | |
| " context = \"\"\n", | |
| " if resume_score is not None:\n", | |
| " context += f\"Based on your ATS score of {resume_score:.1f}%: \"\n", | |
| " \n", | |
| " if any(word in query_lower for word in ['ats', 'score', 'improve score']):\n", | |
| " return context + \"To improve your ATS score, tailor your resume to match the job description by incorporating relevant keywords naturally. Use a clean, simple format and standard section headings like 'Experience', 'Education', and 'Skills'.\"\n", | |
| " \n", | |
| " elif any(word in query_lower for word in ['keyword', 'keywords', 'missing']):\n", | |
| " keyword_info = \"\"\n", | |
| " if missing_keywords and len(missing_keywords) > 0:\n", | |
| " top_keywords = [k for k, _ in missing_keywords[:5]]\n", | |
| " keyword_info = f\"Consider adding these missing keywords: {', '.join(top_keywords)}. \"\n", | |
| " \n", | |
| " return context + keyword_info + \"Add missing keywords from the job description to your resume, but do it naturally. Include them in your skills section and work experience bullets. Don't just list them - show how you've used these skills in previous roles.\"\n", | |
| " \n", | |
| " elif any(word in query_lower for word in ['format', 'formatting', 'layout']):\n", | |
| " format_info = \"\"\n", | |
| " if formatting_issues and len(formatting_issues) > 0:\n", | |
| " format_info = f\"Your specific formatting issues: {formatting_issues[0] if len(formatting_issues) == 1 else ', '.join(formatting_issues[:2]) + '...'}. \"\n", | |
| " \n", | |
| " return context + format_info + \"For ATS-friendly formatting: 1) Use standard section headings, 2) Avoid tables, images, and complex layouts, 3) Use bullet points for work experience, 4) Include your contact information at the top, 5) Save as a .docx or simple PDF file.\"\n", | |
| " \n", | |
| " elif any(word in query_lower for word in ['bullet', 'bullets', 'point']):\n", | |
| " return context + \"Effective bullet points follow the STAR method: Situation, Task, Action, Result. Start with strong action verbs, include specific metrics where possible, and keep each bullet to 1-2 lines. Make sure they highlight achievements, not just responsibilities.\"\n", | |
| " \n", | |
| " elif any(word in query_lower for word in ['experience', 'work history', 'job']):\n", | |
| " return context + \"When describing work experience, focus on achievements rather than duties. Use metrics and numbers to quantify your impact. Tailor each bullet to highlight skills relevant to the job you're applying for. Use past tense for previous jobs and present tense for current roles.\"\n", | |
| " \n", | |
| " elif any(word in query_lower for word in ['education', 'degree']):\n", | |
| " return context + \"In your education section, list your most recent degree first. Include your degree, major, university name, location, and graduation year. If you're a recent graduate, you can also include relevant coursework, GPA (if above 3.5), and academic achievements.\"\n", | |
| " \n", | |
| " elif any(word in query_lower for word in ['skill', 'skills']):\n", | |
| " return context + \"For your skills section, categorize them (Technical, Software, Soft Skills, etc.) and prioritize those mentioned in the job description. Be specific with technical skills - instead of 'Programming', list specific languages like 'Python' or 'Java'.\"\n", | |
| " \n", | |
| " elif any(word in query_lower for word in ['interview', 'prepare']):\n", | |
| " return context + \"To prepare for interviews: 1) Research the company thoroughly, 2) Practice answering common questions using the STAR method, 3) Prepare examples that showcase the skills in the job description, 4) Have questions ready to ask the interviewer, 5) Review your resume and be ready to elaborate on any point.\"\n", | |
| " \n", | |
| " else:\n", | |
| " return context + \"I'm here to help with your resume and job application questions. For best results, ask specific questions about improving your ATS score, formatting, keywords, or different resume sections. I can also help with interview preparation and career advice.\"\n", | |
| "\n", | |
| "# Create a download link for the results\n", | |
| "def get_table_download_link(df):\n", | |
| " csv = df.to_csv(index=False)\n", | |
| " b64 = base64.b64encode(csv.encode()).decode()\n", | |
| " href = f'<a href=\"data:file/csv;base64,{b64}\" download=\"resume_analysis.csv\">Download results as CSV</a>'\n", | |
| " return href\n", | |
| "\n", | |
| "# Streamlit UI\n", | |
| "st.set_page_config(page_title=\"AI Resume Checker\", page_icon=\"๐\", layout=\"wide\")\n", | |
| "\n", | |
| "st.title(\"๐ AI Resume Checker\")\n", | |
| "st.markdown(\"### Optimize your resume for Applicant Tracking Systems (ATS)\")\n", | |
| "\n", | |
| "# Sidebar with instructions\n", | |
| "with st.sidebar:\n", | |
| " st.header(\"How it works\")\n", | |
| " st.write(\"1. Upload your resume (PDF or DOCX)\")\n", | |
| " st.write(\"2. Enter the job description\")\n", | |
| " st.write(\"3. Get instant ATS compatibility analysis\")\n", | |
| " st.write(\"4. Ask questions to improve your resume\")\n", | |
| " \n", | |
| " st.markdown(\"---\")\n", | |
| " st.markdown(\"### About\")\n", | |
| " st.write(\"This tool helps you optimize your resume for ATS systems by analyzing keyword matching, formatting, and providing personalized advice.\")\n", | |
| " st.write(\"Your data is processed locally and not stored.\")\n", | |
| "\n", | |
| "# Create tabs\n", | |
| "tab1, tab2, tab3 = st.tabs([\"Upload & Analyze\", \"Results\", \"Resume Assistant\"])\n", | |
| "\n", | |
| "# Global variables to store state\n", | |
| "if 'resume_text' not in st.session_state:\n", | |
| " st.session_state.resume_text = None\n", | |
| "if 'job_desc' not in st.session_state:\n", | |
| " st.session_state.job_desc = None\n", | |
| "if 'ats_score' not in st.session_state:\n", | |
| " st.session_state.ats_score = None\n", | |
| "if 'missing_keywords' not in st.session_state:\n", | |
| " st.session_state.missing_keywords = None\n", | |
| "if 'formatting_issues' not in st.session_state:\n", | |
| " st.session_state.formatting_issues = None\n", | |
| "if 'analysis_complete' not in st.session_state:\n", | |
| " st.session_state.analysis_complete = False\n", | |
| "\n", | |
| "# Tab 1: Upload & Analyze\n", | |
| "with tab1:\n", | |
| " col1, col2 = st.columns(2)\n", | |
| " \n", | |
| " with col1:\n", | |
| " st.subheader(\"Upload Resume\")\n", | |
| " uploaded_resume = st.file_uploader(\"Choose a resume file\", type=[\"pdf\", \"docx\"])\n", | |
| " \n", | |
| " if uploaded_resume is not None:\n", | |
| " st.session_state.resume_text = extract_text(uploaded_resume)\n", | |
| " if st.session_state.resume_text == \"Unsupported file format. Please upload a PDF or DOCX file.\":\n", | |
| " st.error(st.session_state.resume_text)\n", | |
| " else:\n", | |
| " st.success(f\"Resume loaded successfully! ({len(st.session_state.resume_text)} characters)\")\n", | |
| " \n", | |
| " with col2:\n", | |
| " st.subheader(\"Job Description\")\n", | |
| " st.session_state.job_desc = st.text_area(\"Paste the job description here\", height=300)\n", | |
| " \n", | |
| " if st.button(\"Analyze Resume\") and st.session_state.resume_text and st.session_state.job_desc:\n", | |
| " with st.spinner(\"Analyzing your resume...\"):\n", | |
| " # Calculate ATS score\n", | |
| " st.session_state.ats_score = calculate_similarity_score(st.session_state.resume_text, st.session_state.job_desc)\n", | |
| " \n", | |
| " # Get missing keywords\n", | |
| " st.session_state.missing_keywords = get_missing_keywords(st.session_state.resume_text, st.session_state.job_desc)\n", | |
| " \n", | |
| " # Check formatting\n", | |
| " st.session_state.formatting_issues = check_ats_formatting(st.session_state.resume_text)\n", | |
| " \n", | |
| " st.session_state.analysis_complete = True\n", | |
| " st.success(\"Analysis complete! Check the Results tab.\")\n", | |
| " st.balloons()\n", | |
| "\n", | |
| "# Tab 2: Results\n", | |
| "with tab2:\n", | |
| " if st.session_state.analysis_complete:\n", | |
| " st.subheader(\"ATS Compatibility Analysis\")\n", | |
| " \n", | |
| " # ATS Score\n", | |
| " col1, col2 = st.columns([1, 2])\n", | |
| " with col1:\n", | |
| " st.metric(\"ATS Score\", f\"{st.session_state.ats_score:.1f}%\")\n", | |
| " if st.session_state.ats_score >= 70:\n", | |
| " st.success(\"Great match! Your resume is well-aligned with this job.\")\n", | |
| " elif st.session_state.ats_score >= 50:\n", | |
| " st.warning(\"Moderate match. Some improvements could help.\")\n", | |
| " else:\n", | |
| " st.error(\"Low match. Significant tailoring recommended.\")\n", | |
| " \n", | |
| " with col2:\n", | |
| " # Create a gauge chart\n", | |
| " fig, ax = plt.subplots(figsize=(4, 0.8))\n", | |
| " ax.barh(0, 100, color='#e0e0e0', height=0.5)\n", | |
| " ax.barh(0, st.session_state.ats_score, color='#2e6ca4' if st.session_state.ats_score >= 70 else '#f9a825' if st.session_state.ats_score >= 50 else '#d32f2f', height=0.5)\n", | |
| " ax.set_xlim(0, 100)\n", | |
| " ax.set_yticks([])\n", | |
| " ax.set_xticks([0, 25, 50, 75, 100])\n", | |
| " ax.spines['top'].set_visible(False)\n", | |
| " ax.spines['right'].set_visible(False)\n", | |
| " ax.spines['left'].set_visible(False)\n", | |
| " plt.tight_layout()\n", | |
| " st.pyplot(fig)\n", | |
| " \n", | |
| " # Formatting Check\n", | |
| " st.subheader(\"Formatting Check\")\n", | |
| " for issue in st.session_state.formatting_issues:\n", | |
| " if \"No major\" in issue:\n", | |
| " st.success(issue)\n", | |
| " else:\n", | |
| " st.warning(issue)\n", | |
| " \n", | |
| " # Missing Keywords\n", | |
| " st.subheader(\"Missing Keywords\")\n", | |
| " if st.session_state.missing_keywords:\n", | |
| " # Create dataframe for keywords\n", | |
| " keywords_df = pd.DataFrame(st.session_state.missing_keywords[:15], columns=[\"Keyword\", \"Frequency\"])\n", | |
| " \n", | |
| " col1, col2 = st.columns([3, 2])\n", | |
| " with col1:\n", | |
| " # Plot missing keywords\n", | |
| " fig, ax = plt.subplots(figsize=(10, min(8, 0.5*min(15, len(st.session_state.missing_keywords)))))\n", | |
| " keywords = keywords_df[\"Keyword\"].tolist()\n", | |
| " counts = keywords_df[\"Frequency\"].tolist()\n", | |
| " ax.barh(keywords, counts, color='#2e6ca4')\n", | |
| " ax.set_xlabel('Frequency in Job Description')\n", | |
| " ax.set_title('Top Missing Keywords')\n", | |
| " plt.tight_layout()\n", | |
| " st.pyplot(fig)\n", | |
| " \n", | |
| " with col2:\n", | |
| " st.dataframe(keywords_df)\n", | |
| " st.markdown(get_table_download_link(keywords_df), unsafe_allow_html=True)\n", | |
| " else:\n", | |
| " st.success(\"Great job! Your resume already contains key terms from the job description.\")\n", | |
| " \n", | |
| " # Improvement suggestions\n", | |
| " st.subheader(\"Improvement Suggestions\")\n", | |
| " if st.session_state.ats_score < 50:\n", | |
| " st.error(\"Your resume needs significant improvements to pass ATS systems. Focus on adding more relevant keywords and improving formatting.\")\n", | |
| " elif st.session_state.ats_score < 70:\n", | |
| " st.warning(\"Your resume may pass some ATS systems but could be improved. Try to incorporate more of the missing keywords.\")\n", | |
| " else:\n", | |
| " st.success(\"Your resume is well-optimized for ATS systems! Make minor adjustments to incorporate any missing keywords.\")\n", | |
| " else:\n", | |
| " st.info(\"Please upload your resume and job description, then click 'Analyze Resume' to see results here.\")\n", | |
| "\n", | |
| "# Tab 3: Resume Assistant\n", | |
| "with tab3:\n", | |
| " st.subheader(\"Ask the Resume AI Assistant\")\n", | |
| " st.write(\"Ask questions about improving your resume, job applications, or interview preparation.\")\n", | |
| " \n", | |
| " user_question = st.text_input(\"Your question:\", placeholder=\"How can I improve my ATS score?\")\n", | |
| " \n", | |
| " if user_question:\n", | |
| " response = get_resume_advice(\n", | |
| " user_question, \n", | |
| " st.session_state.ats_score, \n", | |
| " st.session_state.missing_keywords, \n", | |
| " st.session_state.formatting_issues\n", | |
| " )\n", | |
| " \n", | |
| " st.markdown(f\"### Answer\")\n", | |
| " st.markdown(response)\n", | |
| " \n", | |
| " # Sample questions\n", | |
| " st.markdown(\"### Sample questions\")\n", | |
| " questions = [\n", | |
| " \"How can I improve my ATS score?\",\n", | |
| " \"What keywords am I missing?\",\n", | |
| " \"How should I format my resume?\",\n", | |
| " \"How do I write good bullet points?\",\n", | |
| " \"How should I describe my work experience?\"\n", | |
| " ]\n", | |
| " \n", | |
| " cols = st.columns(3)\n", | |
| " for i, question in enumerate(questions):\n", | |
| " if cols[i % 3].button(question):\n", | |
| " response = get_resume_advice(\n", | |
| " question, \n", | |
| " st.session_state.ats_score, \n", | |
| " st.session_state.missing_keywords, \n", | |
| " st.session_state.formatting_issues\n", | |
| " )\n", | |
| " st.markdown(f\"### Answer\")\n", | |
| " st.markdown(response)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "id": "05412911-2886-46ac-bea6-675c43ac5ca5", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3 (ipykernel)", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.12.4" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } | |