billyxx commited on
Commit
0d3c88a
·
verified ·
1 Parent(s): 643b85a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -27
app.py CHANGED
@@ -1,13 +1,12 @@
1
  import gradio as gr
2
  import os
3
  import pdfplumber
4
- from recommender import rank_resumes, summarize_resume_flan, extract_applicant_name
5
  from docx import Document
 
6
 
7
  UPLOAD_FOLDER = "uploads"
8
  os.makedirs(UPLOAD_FOLDER, exist_ok=True)
9
 
10
-
11
  def process_resumes(job_description, uploaded_files):
12
  if not job_description.strip():
13
  return "Please provide a job description.", None
@@ -21,44 +20,54 @@ def process_resumes(job_description, uploaded_files):
21
 
22
  ext = filename.lower().split(".")[-1]
23
 
24
- # Save the uploaded file locally first
25
  file_path = os.path.join(UPLOAD_FOLDER, filename)
26
- with open(file_path, "wb") as f:
27
- content = uploaded_file.read() if hasattr(uploaded_file, "read") else uploaded_file
28
- if isinstance(content, str):
29
- content = content.encode("utf-8")
30
- f.write(content)
31
-
32
- # Read file content based on extension
33
- if ext == "txt":
34
- with open(file_path, "r", encoding="utf-8") as f:
35
- text = f.read()
36
-
37
- elif ext == "pdf":
38
- try:
 
 
 
 
 
 
 
 
 
 
 
 
39
  with pdfplumber.open(file_path) as pdf:
40
  pages = [page.extract_text() for page in pdf.pages if page.extract_text() is not None]
41
  if not pages:
42
  return f"No extractable text found in PDF: {filename}. Is it scanned or image-only?", None
43
  text = "\n".join(pages)
44
- except Exception as e:
45
- return f"Failed to process PDF {filename}: {str(e)}", None
46
 
47
- elif ext == "docx":
48
- try:
49
  doc = Document(file_path)
50
  text = "\n".join([p.text for p in doc.paragraphs])
51
- except Exception as e:
52
- return f"Failed to process DOCX {filename}: {str(e)}", None
53
 
54
- else:
55
- return f"Unsupported file format: {filename}", None
 
 
 
56
 
57
  resume_texts.append((filename, text))
58
 
59
- # Rank resumes and generate summaries
60
  results = rank_resumes(job_description, resume_texts)
61
 
 
62
  for candidate in results:
63
  candidate["summary"] = summarize_resume_flan(candidate["text"], job_description)
64
 
@@ -73,7 +82,6 @@ def process_resumes(job_description, uploaded_files):
73
 
74
  return "", table_data
75
 
76
-
77
  with gr.Blocks() as demo:
78
  gr.Markdown("## Candidate Recommendation Engine")
79
  with gr.Row():
@@ -88,6 +96,5 @@ with gr.Blocks() as demo:
88
 
89
  btn.click(process_resumes, inputs=[job_desc, resumes], outputs=[msg, output_table])
90
 
91
-
92
  if __name__ == "__main__":
93
  demo.launch()
 
1
  import gradio as gr
2
  import os
3
  import pdfplumber
 
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
 
20
 
21
  ext = filename.lower().split(".")[-1]
22
 
 
23
  file_path = os.path.join(UPLOAD_FOLDER, filename)
24
+ # Save uploaded file to disk
25
+ try:
26
+ with open(file_path, "wb") as f:
27
+ content = uploaded_file.read() if hasattr(uploaded_file, "read") else uploaded_file
28
+ if isinstance(content, str):
29
+ content = content.encode("utf-8")
30
+ f.write(content)
31
+ except Exception as e:
32
+ return f"Failed to save file {filename}: {str(e)}", None
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
+ elif ext == "docx":
 
56
  doc = Document(file_path)
57
  text = "\n".join([p.text for p in doc.paragraphs])
 
 
58
 
59
+ else:
60
+ return f"Unsupported file format: {filename}", None
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
 
83
  return "", table_data
84
 
 
85
  with gr.Blocks() as demo:
86
  gr.Markdown("## Candidate Recommendation Engine")
87
  with gr.Row():
 
96
 
97
  btn.click(process_resumes, inputs=[job_desc, resumes], outputs=[msg, output_table])
98
 
 
99
  if __name__ == "__main__":
100
  demo.launch()