Update ResumeOpt.py
Browse files- ResumeOpt.py +93 -89
ResumeOpt.py
CHANGED
@@ -1,89 +1,93 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
-
import spacy
|
4 |
-
from docx import Document
|
5 |
-
import tempfile
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
""
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import spacy
|
4 |
+
from docx import Document
|
5 |
+
import tempfile
|
6 |
+
if not os.path.exists("en_core_web_sm-3.0.0"):
|
7 |
+
import subprocess
|
8 |
+
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
9 |
+
|
10 |
+
#nlp = spacy.load('en_core_web_sm')
|
11 |
+
# Load models quietly
|
12 |
+
nlp = spacy.load('en_core_web_sm')
|
13 |
+
fill_mask = pipeline("fill-mask", model="bert-base-uncased", device=0) # Use GPU if available
|
14 |
+
|
15 |
+
# Cell 3: Define functions
|
16 |
+
def extract_text_from_docx(file_path):
|
17 |
+
try:
|
18 |
+
doc = Document(file_path)
|
19 |
+
return '\n'.join([para.text for para in doc.paragraphs])
|
20 |
+
except:
|
21 |
+
return ""
|
22 |
+
|
23 |
+
def save_updated_resume(text):
|
24 |
+
try:
|
25 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.docx')
|
26 |
+
doc = Document()
|
27 |
+
for line in text.split('\n'):
|
28 |
+
doc.add_paragraph(line)
|
29 |
+
doc.save(temp_file.name)
|
30 |
+
return temp_file.name
|
31 |
+
except:
|
32 |
+
return None
|
33 |
+
|
34 |
+
def bert_suggest_replacements(text):
|
35 |
+
suggestions = []
|
36 |
+
improved_text = text
|
37 |
+
doc = nlp(text)
|
38 |
+
|
39 |
+
for token in doc:
|
40 |
+
if token.is_stop or not token.is_alpha or len(token.text) <= 3:
|
41 |
+
continue
|
42 |
+
|
43 |
+
masked_sentence = improved_text.replace(token.text, "[MASK]", 1)
|
44 |
+
try:
|
45 |
+
predictions = fill_mask(masked_sentence)
|
46 |
+
best_suggestion = predictions[0]['token_str']
|
47 |
+
|
48 |
+
if best_suggestion != token.text.lower():
|
49 |
+
improved_text = improved_text.replace(token.text, best_suggestion, 1)
|
50 |
+
suggestions.append(f"• Replace '{token.text}' with '{best_suggestion}'")
|
51 |
+
except:
|
52 |
+
continue
|
53 |
+
|
54 |
+
return improved_text, suggestions
|
55 |
+
|
56 |
+
def process_resume(file):
|
57 |
+
if file is None:
|
58 |
+
return "Please upload a resume file.", "No suggestions yet.", None
|
59 |
+
|
60 |
+
try:
|
61 |
+
resume_text = extract_text_from_docx(file.name)
|
62 |
+
if not resume_text:
|
63 |
+
return "Could not read the resume. Please ensure it's a valid .docx file.", "No suggestions available.", None
|
64 |
+
|
65 |
+
updated_text, suggestions = bert_suggest_replacements(resume_text)
|
66 |
+
output_file_path = save_updated_resume(updated_text)
|
67 |
+
|
68 |
+
suggestions_text = "Suggested Improvements:\n" + '\n'.join(suggestions) if suggestions else "No improvements suggested. Your resume looks good!"
|
69 |
+
|
70 |
+
return updated_text, suggestions_text, output_file_path
|
71 |
+
except:
|
72 |
+
return "An error occurred. Please try again with a different file.", "No suggestions available.", None
|
73 |
+
|
74 |
+
# Cell 4: Create and launch Gradio interface
|
75 |
+
iface = gr.Interface(
|
76 |
+
fn=process_resume,
|
77 |
+
inputs=gr.File(label="Upload Your Resume (.docx only)", file_types=[".docx"]),
|
78 |
+
outputs=[
|
79 |
+
gr.Textbox(label="Optimized Resume Text", lines=10),
|
80 |
+
gr.Textbox(label="ATS Improvement Suggestions", lines=10),
|
81 |
+
gr.File(label="Download Optimized Resume")
|
82 |
+
],
|
83 |
+
title="ATS Resume Optimizer",
|
84 |
+
description="Upload your .docx resume for ATS optimization. We'll suggest improvements and provide an updated version.",
|
85 |
+
theme="default",
|
86 |
+
css="""
|
87 |
+
.gradio-container {max-width: 800px; margin: auto;}
|
88 |
+
.output-markdown {white-space: pre-wrap;}
|
89 |
+
"""
|
90 |
+
)
|
91 |
+
|
92 |
+
iface.launch(share=True)
|
93 |
+
|