Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import
|
| 4 |
-
from docx import Document
|
| 5 |
from recommender import rank_resumes, summarize_resume_flan, extract_applicant_name
|
|
|
|
|
|
|
| 6 |
|
| 7 |
UPLOAD_FOLDER = "uploads"
|
| 8 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 9 |
|
|
|
|
| 10 |
def process_resumes(job_description, uploaded_files):
|
| 11 |
if not job_description.strip():
|
| 12 |
return "Please provide a job description.", None
|
|
@@ -14,60 +16,44 @@ def process_resumes(job_description, uploaded_files):
|
|
| 14 |
resume_texts = []
|
| 15 |
|
| 16 |
for uploaded_file in uploaded_files:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
return "One of the uploaded files is missing a filename. Please upload files, not text.", None
|
| 20 |
-
|
| 21 |
ext = filename.lower().split(".")[-1]
|
| 22 |
|
|
|
|
| 23 |
file_path = os.path.join(UPLOAD_FOLDER, filename)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# Verify file saved correctly
|
| 35 |
-
if not os.path.exists(file_path):
|
| 36 |
-
return f"File was not saved correctly: {filename}", None
|
| 37 |
-
size = os.path.getsize(file_path)
|
| 38 |
-
print(f"Saved file {filename} size={size} bytes at {file_path}")
|
| 39 |
-
if size == 0:
|
| 40 |
-
return f"File {filename} saved but is empty!", None
|
| 41 |
-
|
| 42 |
-
# Extract text depending on file type
|
| 43 |
-
try:
|
| 44 |
-
if ext == "txt":
|
| 45 |
-
with open(file_path, "r", encoding="utf-8") as f:
|
| 46 |
-
text = f.read()
|
| 47 |
-
|
| 48 |
-
elif ext == "pdf":
|
| 49 |
with pdfplumber.open(file_path) as pdf:
|
| 50 |
pages = [page.extract_text() for page in pdf.pages if page.extract_text() is not None]
|
| 51 |
if not pages:
|
| 52 |
return f"No extractable text found in PDF: {filename}. Is it scanned or image-only?", None
|
| 53 |
text = "\n".join(pages)
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
|
|
|
| 56 |
doc = Document(file_path)
|
| 57 |
text = "\n".join([p.text for p in doc.paragraphs])
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
except Exception as e:
|
| 63 |
-
return f"Failed to process {ext.upper()} {filename}: {str(e)}", None
|
| 64 |
|
| 65 |
resume_texts.append((filename, text))
|
| 66 |
|
| 67 |
-
# Rank resumes
|
| 68 |
results = rank_resumes(job_description, resume_texts)
|
| 69 |
|
| 70 |
-
# Generate summaries
|
| 71 |
for candidate in results:
|
| 72 |
candidate["summary"] = summarize_resume_flan(candidate["text"], job_description)
|
| 73 |
|
|
@@ -82,6 +68,7 @@ def process_resumes(job_description, uploaded_files):
|
|
| 82 |
|
| 83 |
return "", table_data
|
| 84 |
|
|
|
|
| 85 |
with gr.Blocks() as demo:
|
| 86 |
gr.Markdown("## Candidate Recommendation Engine")
|
| 87 |
with gr.Row():
|
|
@@ -96,5 +83,6 @@ with gr.Blocks() as demo:
|
|
| 96 |
|
| 97 |
btn.click(process_resumes, inputs=[job_desc, resumes], outputs=[msg, output_table])
|
| 98 |
|
|
|
|
| 99 |
if __name__ == "__main__":
|
| 100 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import shutil
|
|
|
|
| 4 |
from recommender import rank_resumes, summarize_resume_flan, extract_applicant_name
|
| 5 |
+
from docx import Document
|
| 6 |
+
import pdfplumber
|
| 7 |
|
| 8 |
UPLOAD_FOLDER = "uploads"
|
| 9 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 10 |
|
| 11 |
+
|
| 12 |
def process_resumes(job_description, uploaded_files):
|
| 13 |
if not job_description.strip():
|
| 14 |
return "Please provide a job description.", None
|
|
|
|
| 16 |
resume_texts = []
|
| 17 |
|
| 18 |
for uploaded_file in uploaded_files:
|
| 19 |
+
# uploaded_file is a file path string from gr.Files
|
| 20 |
+
filename = os.path.basename(uploaded_file)
|
|
|
|
|
|
|
| 21 |
ext = filename.lower().split(".")[-1]
|
| 22 |
|
| 23 |
+
# Copy the file from Gradio temp folder to your uploads folder
|
| 24 |
file_path = os.path.join(UPLOAD_FOLDER, filename)
|
| 25 |
+
shutil.copy(uploaded_file, file_path)
|
| 26 |
+
|
| 27 |
+
# Read content based on extension
|
| 28 |
+
if ext == "txt":
|
| 29 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
| 30 |
+
text = f.read()
|
| 31 |
+
|
| 32 |
+
elif ext == "pdf":
|
| 33 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
with pdfplumber.open(file_path) as pdf:
|
| 35 |
pages = [page.extract_text() for page in pdf.pages if page.extract_text() is not None]
|
| 36 |
if not pages:
|
| 37 |
return f"No extractable text found in PDF: {filename}. Is it scanned or image-only?", None
|
| 38 |
text = "\n".join(pages)
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Failed to process PDF {filename}: {str(e)}", None
|
| 41 |
|
| 42 |
+
elif ext == "docx":
|
| 43 |
+
try:
|
| 44 |
doc = Document(file_path)
|
| 45 |
text = "\n".join([p.text for p in doc.paragraphs])
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"Failed to process DOCX {filename}: {str(e)}", None
|
| 48 |
|
| 49 |
+
else:
|
| 50 |
+
return f"Unsupported file format: {filename}", None
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
resume_texts.append((filename, text))
|
| 53 |
|
| 54 |
+
# Rank resumes and generate summaries
|
| 55 |
results = rank_resumes(job_description, resume_texts)
|
| 56 |
|
|
|
|
| 57 |
for candidate in results:
|
| 58 |
candidate["summary"] = summarize_resume_flan(candidate["text"], job_description)
|
| 59 |
|
|
|
|
| 68 |
|
| 69 |
return "", table_data
|
| 70 |
|
| 71 |
+
|
| 72 |
with gr.Blocks() as demo:
|
| 73 |
gr.Markdown("## Candidate Recommendation Engine")
|
| 74 |
with gr.Row():
|
|
|
|
| 83 |
|
| 84 |
btn.click(process_resumes, inputs=[job_desc, resumes], outputs=[msg, output_table])
|
| 85 |
|
| 86 |
+
|
| 87 |
if __name__ == "__main__":
|
| 88 |
demo.launch()
|