arssite commited on
Commit
1f0dd44
1 Parent(s): 2e82e71

Update ResumeOpt.py

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