billyxx commited on
Commit
efadb61
·
verified ·
1 Parent(s): 5d7ba34

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -25
app.py CHANGED
@@ -9,35 +9,42 @@ UPLOAD_FOLDER = "uploads"
9
  os.makedirs(UPLOAD_FOLDER, exist_ok=True)
10
 
11
 
12
- def process_resumes(job_description, uploaded_file):
13
  if not job_description.strip():
14
  return "Please provide a job description.", None
15
 
16
- # uploaded_file is the file path string, use it directly
17
- filepath = uploaded_file # no need to save again
18
-
19
- # Extract filename for display in results
20
- filename = os.path.basename(filepath)
21
-
22
- # Read file content depending on extension
23
- if filepath.endswith(".txt"):
24
- with open(filepath, "r", encoding="utf-8") as f:
25
- text = f.read()
26
- elif filepath.endswith(".pdf"):
27
- import pdfplumber
28
- with pdfplumber.open(filepath) as pdf:
29
- pages = [page.extract_text() for page in pdf.pages if page.extract_text() is not None]
30
- text = "\n".join(pages)
31
- elif filepath.endswith(".docx"):
32
- text = extract_text_from_docx(filepath)
33
- else:
34
- return "Unsupported file format.", None
35
-
36
- resume_texts = [(filename, text)]
 
 
 
 
 
 
37
 
38
  # Rank resumes
39
  results = rank_resumes(job_description, resume_texts)
40
 
 
41
  for i, candidate in enumerate(results):
42
  candidate["name"] = resume_texts[i][0]
43
 
@@ -51,12 +58,12 @@ def process_resumes(job_description, uploaded_file):
51
  candidate.get("name", "Unknown"),
52
  f"{candidate['score']:.4f}",
53
  candidate["summary"]
54
- ] for candidate in results
55
  ]
56
 
57
-
58
  return "", table_data
59
 
 
60
  def extract_text_from_docx(filepath):
61
  doc = Document(filepath)
62
  full_text = []
@@ -73,7 +80,7 @@ with gr.Blocks() as demo:
73
  with gr.Row():
74
  job_desc = gr.Textbox(label="Job Description", lines=10, placeholder="Paste job description here...")
75
 
76
- resumes = gr.File(label="Upload Resume (.txt, .pdf, .docx)", file_types=[".txt", ".pdf", ".docx"])
77
 
78
  btn = gr.Button("Rank Candidates")
79
 
 
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
15
 
16
+ if not uploaded_files:
17
+ return "Please upload at least one resume file.", None
18
+
19
+ resume_texts = []
20
+
21
+ for uploaded_file in uploaded_files:
22
+ filepath = uploaded_file.name # uploaded_file is a NamedTemporaryFile-like object
23
+
24
+ # Read file content depending on extension
25
+ if filepath.endswith(".txt"):
26
+ text = uploaded_file.read().decode("utf-8")
27
+ elif filepath.endswith(".pdf"):
28
+ import pdfplumber
29
+ uploaded_file.seek(0)
30
+ with pdfplumber.open(uploaded_file) as pdf:
31
+ pages = [page.extract_text() for page in pdf.pages if page.extract_text() is not None]
32
+ text = "\n".join(pages)
33
+ elif filepath.endswith(".docx"):
34
+ from docx import Document
35
+ uploaded_file.seek(0)
36
+ doc = Document(uploaded_file)
37
+ full_text = [para.text for para in doc.paragraphs]
38
+ text = "\n".join(full_text)
39
+ else:
40
+ return f"Unsupported file format: {filepath}", None
41
+
42
+ resume_texts.append((filepath, text))
43
 
44
  # Rank resumes
45
  results = rank_resumes(job_description, resume_texts)
46
 
47
+ # Add filename to candidate info
48
  for i, candidate in enumerate(results):
49
  candidate["name"] = resume_texts[i][0]
50
 
 
58
  candidate.get("name", "Unknown"),
59
  f"{candidate['score']:.4f}",
60
  candidate["summary"]
61
+ ] for candidate in results
62
  ]
63
 
 
64
  return "", table_data
65
 
66
+
67
  def extract_text_from_docx(filepath):
68
  doc = Document(filepath)
69
  full_text = []
 
80
  with gr.Row():
81
  job_desc = gr.Textbox(label="Job Description", lines=10, placeholder="Paste job description here...")
82
 
83
+ resumes = gr.File(label="Upload Resumes (.txt, .pdf, .docx)", file_types=[".txt", ".pdf", ".docx"], file_types_multiple=True, multiple=True)
84
 
85
  btn = gr.Button("Rank Candidates")
86