Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -12,48 +12,48 @@ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
12 |
def process_resumes(job_description, uploaded_files):
|
13 |
if not job_description.strip():
|
14 |
return "Please provide a job description.", None
|
15 |
-
if not uploaded_files:
|
16 |
-
return "Please upload at least one resume file.", None
|
17 |
|
18 |
resume_texts = []
|
19 |
|
20 |
for uploaded_file in uploaded_files:
|
21 |
filename = getattr(uploaded_file, "name", None)
|
|
|
22 |
if filename is None:
|
23 |
return "One of the uploaded files is missing a filename. Please upload files, not text.", None
|
24 |
|
25 |
-
# Reset file pointer
|
26 |
-
if hasattr(uploaded_file, "seek"):
|
27 |
-
uploaded_file.seek(0)
|
28 |
-
|
29 |
-
# Process based on extension
|
30 |
ext = filename.lower().split(".")[-1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
if ext == "txt":
|
32 |
-
|
33 |
-
if hasattr(uploaded_file, "read"):
|
34 |
-
content = uploaded_file.read()
|
35 |
-
# bytes? decode
|
36 |
-
text = content.decode("utf-8") if isinstance(content, bytes) else content
|
37 |
-
else:
|
38 |
-
return f"Unexpected content for {filename}", None
|
39 |
|
40 |
elif ext == "pdf":
|
41 |
-
# Save temporarily to disk to use pdfplumber (which needs a file path)
|
42 |
temp_path = os.path.join(UPLOAD_FOLDER, filename)
|
43 |
with open(temp_path, "wb") as f:
|
44 |
-
|
45 |
-
|
|
|
|
|
46 |
import pdfplumber
|
47 |
with pdfplumber.open(temp_path) as pdf:
|
48 |
pages = [page.extract_text() for page in pdf.pages if page.extract_text()]
|
49 |
text = "\n".join(pages)
|
50 |
|
51 |
elif ext == "docx":
|
52 |
-
# Save temporarily to disk for python-docx
|
53 |
temp_path = os.path.join(UPLOAD_FOLDER, filename)
|
54 |
with open(temp_path, "wb") as f:
|
55 |
-
|
56 |
-
|
|
|
|
|
57 |
from docx import Document
|
58 |
doc = Document(temp_path)
|
59 |
text = "\n".join([p.text for p in doc.paragraphs])
|
@@ -63,9 +63,10 @@ def process_resumes(job_description, uploaded_files):
|
|
63 |
|
64 |
resume_texts.append((filename, text))
|
65 |
|
66 |
-
# Rank resumes
|
67 |
results = rank_resumes(job_description, resume_texts)
|
68 |
|
|
|
69 |
for candidate in results:
|
70 |
candidate["summary"] = summarize_resume_flan(candidate["text"], job_description)
|
71 |
|
@@ -80,8 +81,6 @@ def process_resumes(job_description, uploaded_files):
|
|
80 |
|
81 |
return "", table_data
|
82 |
|
83 |
-
|
84 |
-
|
85 |
def extract_text_from_docx(filepath):
|
86 |
doc = Document(filepath)
|
87 |
full_text = []
|
|
|
12 |
def process_resumes(job_description, uploaded_files):
|
13 |
if not job_description.strip():
|
14 |
return "Please provide a job description.", None
|
|
|
|
|
15 |
|
16 |
resume_texts = []
|
17 |
|
18 |
for uploaded_file in uploaded_files:
|
19 |
filename = getattr(uploaded_file, "name", None)
|
20 |
+
|
21 |
if filename is None:
|
22 |
return "One of the uploaded files is missing a filename. Please upload files, not text.", None
|
23 |
|
|
|
|
|
|
|
|
|
|
|
24 |
ext = filename.lower().split(".")[-1]
|
25 |
+
|
26 |
+
# Read file content or bytes
|
27 |
+
if hasattr(uploaded_file, "read"):
|
28 |
+
content = uploaded_file.read()
|
29 |
+
elif isinstance(uploaded_file, str):
|
30 |
+
content = uploaded_file
|
31 |
+
else:
|
32 |
+
return f"Unsupported upload type for file: {filename}", None
|
33 |
+
|
34 |
+
# Process by file type
|
35 |
if ext == "txt":
|
36 |
+
text = content.decode("utf-8") if isinstance(content, bytes) else content
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
elif ext == "pdf":
|
|
|
39 |
temp_path = os.path.join(UPLOAD_FOLDER, filename)
|
40 |
with open(temp_path, "wb") as f:
|
41 |
+
if isinstance(content, bytes):
|
42 |
+
f.write(content)
|
43 |
+
else:
|
44 |
+
f.write(content.encode("utf-8"))
|
45 |
import pdfplumber
|
46 |
with pdfplumber.open(temp_path) as pdf:
|
47 |
pages = [page.extract_text() for page in pdf.pages if page.extract_text()]
|
48 |
text = "\n".join(pages)
|
49 |
|
50 |
elif ext == "docx":
|
|
|
51 |
temp_path = os.path.join(UPLOAD_FOLDER, filename)
|
52 |
with open(temp_path, "wb") as f:
|
53 |
+
if isinstance(content, bytes):
|
54 |
+
f.write(content)
|
55 |
+
else:
|
56 |
+
f.write(content.encode("utf-8"))
|
57 |
from docx import Document
|
58 |
doc = Document(temp_path)
|
59 |
text = "\n".join([p.text for p in doc.paragraphs])
|
|
|
63 |
|
64 |
resume_texts.append((filename, text))
|
65 |
|
66 |
+
# Rank resumes
|
67 |
results = rank_resumes(job_description, resume_texts)
|
68 |
|
69 |
+
# Generate summaries
|
70 |
for candidate in results:
|
71 |
candidate["summary"] = summarize_resume_flan(candidate["text"], job_description)
|
72 |
|
|
|
81 |
|
82 |
return "", table_data
|
83 |
|
|
|
|
|
84 |
def extract_text_from_docx(filepath):
|
85 |
doc = Document(filepath)
|
86 |
full_text = []
|