Create model2.py
Browse files- pages/model2.py +108 -0
pages/model2.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import zipfile
|
| 3 |
+
import tempfile
|
| 4 |
+
import fitz
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 7 |
+
|
| 8 |
+
os.environ["HF_TOKEN"]=os.getenv('HF_Token')
|
| 9 |
+
os.environ["HUGGINGFACEHUB_API_KEY"]=os.getenv('HF_Token')
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
base_llm = HuggingFaceEndpoint(
|
| 13 |
+
repo_id="meta-llama/Llama-3.1-8B-Instruct",
|
| 14 |
+
provider="novita",
|
| 15 |
+
temperature=0.7,
|
| 16 |
+
max_new_tokens=150,
|
| 17 |
+
task="conversational"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
llm = ChatHuggingFace(
|
| 21 |
+
llm=base_llm,
|
| 22 |
+
repo_id="meta-llama/Llama-3.2-3B-Instruct",
|
| 23 |
+
provider="novita",
|
| 24 |
+
temperature=0.7,
|
| 25 |
+
max_new_tokens=200,
|
| 26 |
+
task="conversational"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
def extract_text(file_bytes):
|
| 30 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 31 |
+
tmp.write(file_bytes)
|
| 32 |
+
text = ""
|
| 33 |
+
doc = fitz.open(tmp.name)
|
| 34 |
+
for page in doc:
|
| 35 |
+
text += page.get_text()
|
| 36 |
+
return text
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def resume_prompt(text):
|
| 40 |
+
return (
|
| 41 |
+
"Extract the following from this resume:\n"
|
| 42 |
+
"1. Full Name\n2. Education Background\n3. Total Years of Experience\n4. Technical & Soft Skills\n"
|
| 43 |
+
"5. Key Projects and Outcomes\n\nResume Content:\n" + text
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def jd_prompt(text):
|
| 48 |
+
return (
|
| 49 |
+
"Extract this information from the job description:\n"
|
| 50 |
+
"1. Job ID\n2. Company Name\n3. Designation\n4. Required Experience\n"
|
| 51 |
+
"5. Key Skills\n6. Education\n7. Location\n\nJD Text:\n" + text
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def match_prompt(jd_text, all_resumes, top_n=3):
|
| 56 |
+
return (
|
| 57 |
+
f"You are an expert recruitment AI. Based on the following job description and candidate summaries, rank the top {top_n} suitable profiles.\n"
|
| 58 |
+
"Evaluate based on: Skills match, Experience, Education, and Project Relevance.\n"
|
| 59 |
+
f"\nJob Description:\n{jd_text}\n\nCandidates:\n{all_resumes}\n\n"
|
| 60 |
+
f"List the Top {top_n} candidates like this:\n"
|
| 61 |
+
"1. Candidate Name - Matching Reason\n2. Candidate Name - Matching Reason\n..."
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
st.set_page_config(page_title="AI Resume Screener", layout="centered")
|
| 65 |
+
st.title("π§ Smart Resume Screener + JD Extractor")
|
| 66 |
+
st.markdown("Upload a ZIP of resumes and a Job Description to discover top-fit candidates, powered by AI.")
|
| 67 |
+
|
| 68 |
+
zip_file = st.file_uploader("π Upload ZIP of Resumes (PDFs Only)", type="zip")
|
| 69 |
+
jd_file = st.file_uploader("π Upload Job Description (PDF/TXT)", type=["pdf", "txt"])
|
| 70 |
+
jd_text_manual = st.text_area("βοΈ Or Paste JD Text Directly Below")
|
| 71 |
+
top_n = st.slider("π― Number of Top Candidates", 1, 10, 3)
|
| 72 |
+
|
| 73 |
+
if st.button("π Find Best Matches"):
|
| 74 |
+
if not zip_file or not (jd_file or jd_text_manual.strip()):
|
| 75 |
+
st.warning("Please upload both a ZIP file and a JD file/text.")
|
| 76 |
+
st.stop()
|
| 77 |
+
|
| 78 |
+
jd_text = ""
|
| 79 |
+
if jd_file:
|
| 80 |
+
jd_text = extract_text(jd_file.read()) if jd_file.name.endswith(".pdf") else jd_file.read().decode("utf-8")
|
| 81 |
+
else:
|
| 82 |
+
jd_text = jd_text_manual.strip()
|
| 83 |
+
|
| 84 |
+
st.subheader("π Extracted JD Information")
|
| 85 |
+
jd_response = llm.invoke(jd_prompt(jd_text))
|
| 86 |
+
st.markdown(jd_response.content)
|
| 87 |
+
|
| 88 |
+
resume_summaries = ""
|
| 89 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 90 |
+
with zipfile.ZipFile(zip_file, "r") as archive:
|
| 91 |
+
pdfs = [f for f in archive.namelist() if f.endswith(".pdf")]
|
| 92 |
+
if not pdfs:
|
| 93 |
+
st.error("No PDFs found in the ZIP file.")
|
| 94 |
+
st.stop()
|
| 95 |
+
st.success(f"β
Found {len(pdfs)} resumes. Extracting details...")
|
| 96 |
+
|
| 97 |
+
for file in pdfs:
|
| 98 |
+
with archive.open(file) as pdf:
|
| 99 |
+
text = extract_text(pdf.read())
|
| 100 |
+
summary = llm.invoke(resume_prompt(text)).content
|
| 101 |
+
resume_summaries += f"\n\n[File: {file}]\n{summary}"
|
| 102 |
+
|
| 103 |
+
with st.spinner("π Matching candidates to job description..."):
|
| 104 |
+
final_prompt = match_prompt(jd_text, resume_summaries, top_n)
|
| 105 |
+
match_result = llm.invoke(final_prompt)
|
| 106 |
+
|
| 107 |
+
st.subheader("β
Top Matched Candidates")
|
| 108 |
+
st.markdown(match_result.content)
|