Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,163 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
import numpy as np
|
| 5 |
-
import pandas as pd
|
| 6 |
-
from sklearn.
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# -------------------------
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import glob
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
| 8 |
+
from PyPDF2 import PdfReader
|
| 9 |
+
import docx
|
| 10 |
+
import re
|
| 11 |
+
from collections import Counter
|
| 12 |
+
|
| 13 |
+
# -------------------------
|
| 14 |
+
# CONFIG
|
| 15 |
+
# -------------------------
|
| 16 |
+
DATASET_FOLDER = "resumes" # Folder with 288 resumes
|
| 17 |
+
TOP_K = 3 # Top 3 recommendations
|
| 18 |
+
|
| 19 |
+
# -------------------------
|
| 20 |
+
# HELPER FUNCTIONS
|
| 21 |
+
# -------------------------
|
| 22 |
+
|
| 23 |
+
def extract_text_from_pdf(file):
|
| 24 |
+
text = ""
|
| 25 |
+
try:
|
| 26 |
+
reader = PdfReader(file)
|
| 27 |
+
for page in reader.pages:
|
| 28 |
+
page_text = page.extract_text()
|
| 29 |
+
if page_text:
|
| 30 |
+
text += page_text + " "
|
| 31 |
+
except:
|
| 32 |
+
pass
|
| 33 |
+
return text
|
| 34 |
+
|
| 35 |
+
def extract_text_from_docx(file):
|
| 36 |
+
text = ""
|
| 37 |
+
try:
|
| 38 |
+
doc = docx.Document(file)
|
| 39 |
+
text = " ".join([para.text for para in doc.paragraphs])
|
| 40 |
+
except:
|
| 41 |
+
pass
|
| 42 |
+
return text
|
| 43 |
+
|
| 44 |
+
def extract_text(file):
|
| 45 |
+
ext = file.name.split('.')[-1].lower() if hasattr(file, "name") else "txt"
|
| 46 |
+
if ext == "pdf":
|
| 47 |
+
return extract_text_from_pdf(file)
|
| 48 |
+
elif ext == "docx":
|
| 49 |
+
return extract_text_from_docx(file)
|
| 50 |
+
elif ext == "txt":
|
| 51 |
+
try:
|
| 52 |
+
file.seek(0)
|
| 53 |
+
return file.read().decode("utf-8")
|
| 54 |
+
except:
|
| 55 |
+
return ""
|
| 56 |
+
else:
|
| 57 |
+
return ""
|
| 58 |
+
|
| 59 |
+
def load_resume_dataset(folder_path):
|
| 60 |
+
resumes = []
|
| 61 |
+
names = []
|
| 62 |
+
paths = glob.glob(os.path.join(folder_path, "*"))
|
| 63 |
+
for path in paths:
|
| 64 |
+
text = ""
|
| 65 |
+
ext = path.split('.')[-1].lower()
|
| 66 |
+
try:
|
| 67 |
+
if ext == "pdf":
|
| 68 |
+
text = extract_text_from_pdf(path)
|
| 69 |
+
elif ext == "docx":
|
| 70 |
+
text = extract_text_from_docx(path)
|
| 71 |
+
elif ext == "txt":
|
| 72 |
+
with open(path, 'r', encoding='utf-8', errors='ignore') as f:
|
| 73 |
+
text = f.read()
|
| 74 |
+
except:
|
| 75 |
+
continue
|
| 76 |
+
if text.strip():
|
| 77 |
+
resumes.append(text)
|
| 78 |
+
names.append(os.path.basename(path))
|
| 79 |
+
return names, resumes
|
| 80 |
+
|
| 81 |
+
# -------------------------
|
| 82 |
+
# DYNAMIC JOB ROLE EXTRACTION
|
| 83 |
+
# -------------------------
|
| 84 |
+
|
| 85 |
+
def infer_job_from_text(text):
|
| 86 |
+
"""
|
| 87 |
+
Extract probable job/role from resume text.
|
| 88 |
+
Uses heuristics: first lines, capitalized phrases, or frequent nouns.
|
| 89 |
+
"""
|
| 90 |
+
lines = text.split("\n")
|
| 91 |
+
# Try first 5 lines for capitalized phrases
|
| 92 |
+
candidate_lines = lines[:5]
|
| 93 |
+
pattern = re.compile(r'\b[A-Z][a-zA-Z &/-]{2,}\b')
|
| 94 |
+
roles = []
|
| 95 |
+
for line in candidate_lines:
|
| 96 |
+
matches = pattern.findall(line)
|
| 97 |
+
roles.extend(matches)
|
| 98 |
+
# Fallback: top 1 frequent capitalized word
|
| 99 |
+
if roles:
|
| 100 |
+
most_common = Counter(roles).most_common(1)
|
| 101 |
+
return most_common[0][0]
|
| 102 |
+
else:
|
| 103 |
+
# fallback: "Other"
|
| 104 |
+
return "Other"
|
| 105 |
+
|
| 106 |
+
# -------------------------
|
| 107 |
+
# LOAD MODEL & DATASET
|
| 108 |
+
# -------------------------
|
| 109 |
+
|
| 110 |
+
st_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 111 |
+
resume_names, resume_texts = load_resume_dataset(DATASET_FOLDER)
|
| 112 |
+
resume_embeddings = st_model.encode(resume_texts, convert_to_numpy=True)
|
| 113 |
+
|
| 114 |
+
# Build dynamic job roles dict
|
| 115 |
+
resume_roles = {name: infer_job_from_text(text) for name, text in zip(resume_names, resume_texts)}
|
| 116 |
+
|
| 117 |
+
# -------------------------
|
| 118 |
+
# MATCH FUNCTION
|
| 119 |
+
# -------------------------
|
| 120 |
+
|
| 121 |
+
def match_resume(file):
|
| 122 |
+
input_text = extract_text(file)
|
| 123 |
+
if not input_text.strip():
|
| 124 |
+
return pd.DataFrame([{"Error": "Could not extract text from this resume."}])
|
| 125 |
+
|
| 126 |
+
input_emb = st_model.encode([input_text], convert_to_numpy=True)
|
| 127 |
+
sims = cosine_similarity(input_emb, resume_embeddings)[0]
|
| 128 |
+
top_indices = sims.argsort()[-TOP_K:][::-1]
|
| 129 |
+
|
| 130 |
+
results = []
|
| 131 |
+
for idx in top_indices:
|
| 132 |
+
matched_resume_name = resume_names[idx]
|
| 133 |
+
similarity_score = sims[idx]
|
| 134 |
+
recommended_job = resume_roles[matched_resume_name]
|
| 135 |
+
results.append({
|
| 136 |
+
"Matched Resume": matched_resume_name,
|
| 137 |
+
"Recommended Job": recommended_job,
|
| 138 |
+
"Confidence Score": f"{similarity_score*100:.2f}%"
|
| 139 |
+
})
|
| 140 |
+
return pd.DataFrame(results)
|
| 141 |
+
|
| 142 |
+
# -------------------------
|
| 143 |
+
# GRADIO UI
|
| 144 |
+
# -------------------------
|
| 145 |
+
css = """
|
| 146 |
+
body {background-color: #f7f9fc;}
|
| 147 |
+
h1 {color: #333; text-align: center;}
|
| 148 |
+
.gr-button {background-color: #4CAF50; color: white;}
|
| 149 |
+
"""
|
| 150 |
+
|
| 151 |
+
title = "<h1>AI Resume Analyzer & Job Matcher</h1>"
|
| 152 |
+
|
| 153 |
+
iface = gr.Interface(
|
| 154 |
+
fn=match_resume,
|
| 155 |
+
inputs=gr.File(label="Upload Your Resume (PDF, DOCX, TXT)"),
|
| 156 |
+
outputs=gr.Dataframe(label="Top Job Matches"),
|
| 157 |
+
title="AI Resume Analyzer & Job Matcher",
|
| 158 |
+
description="Upload a resume to get top 3 job recommendations with confidence scores.",
|
| 159 |
+
css=css,
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
if __name__ == "__main__":
|
| 163 |
+
iface.launch()
|