billyxx commited on
Commit
272e246
·
verified ·
1 Parent(s): bee3a95

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -26
app.py CHANGED
@@ -13,41 +13,57 @@ 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
-
51
  # Generate summaries
52
  for candidate in results:
53
  candidate["summary"] = summarize_resume_flan(candidate["text"], job_description)
@@ -64,6 +80,7 @@ def process_resumes(job_description, uploaded_files):
64
  return "", table_data
65
 
66
 
 
67
  def extract_text_from_docx(filepath):
68
  doc = Document(filepath)
69
  full_text = []
 
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
+ # If the uploaded_file is a file-like object (has 'read' method)
20
+ if hasattr(uploaded_file, "read"):
21
+ # Reset file pointer just in case
 
 
 
 
 
 
 
 
 
 
22
  uploaded_file.seek(0)
23
+ content = uploaded_file.read()
24
+ # Get filename attribute, fallback if not available
25
+ filename = getattr(uploaded_file, "name", "unknown")
26
+ # Save the file to disk if you want or just process in-memory
27
+ # For example, save to UPLOAD_FOLDER
28
+ filepath = os.path.join(UPLOAD_FOLDER, os.path.basename(filename))
29
+ with open(filepath, "wb") as f:
30
+ f.write(content)
31
  else:
32
+ # uploaded_file is probably a NamedString (str-like)
33
+ # Gradio provides the filename differently in this case,
34
+ # so you might have to assign a default or get from UI
35
+ content = uploaded_file
36
+ filepath = None
37
+ filename = "unknown"
38
+
39
+ # Process content depending on extension
40
+ if filename.endswith(".txt") or (filepath and filepath.endswith(".txt")):
41
+ text = content.decode("utf-8") if isinstance(content, bytes) else content
42
+ elif filename.endswith(".pdf") or (filepath and filepath.endswith(".pdf")):
43
+ # If saved to file, open from file
44
+ if filepath:
45
+ import pdfplumber
46
+ with pdfplumber.open(filepath) as pdf:
47
+ pages = [page.extract_text() for page in pdf.pages if page.extract_text()]
48
+ text = "\n".join(pages)
49
+ else:
50
+ # No file saved, cannot process PDF bytes easily here
51
+ return "Please upload PDF files via file upload.", None
52
+ elif filename.endswith(".docx") or (filepath and filepath.endswith(".docx")):
53
+ if filepath:
54
+ from docx import Document
55
+ doc = Document(filepath)
56
+ text = "\n".join([p.text for p in doc.paragraphs])
57
+ else:
58
+ return "Please upload DOCX files via file upload.", None
59
+ else:
60
+ return f"Unsupported file format: {filename}", None
61
 
62
+ resume_texts.append((filename, text))
63
 
64
+ # Now call rank_resumes etc.
65
  results = rank_resumes(job_description, resume_texts)
66
 
 
 
 
 
67
  # Generate summaries
68
  for candidate in results:
69
  candidate["summary"] = summarize_resume_flan(candidate["text"], job_description)
 
80
  return "", table_data
81
 
82
 
83
+
84
  def extract_text_from_docx(filepath):
85
  doc = Document(filepath)
86
  full_text = []