Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import BertForQuestionAnswering, BertTokenizer
|
3 |
+
import torch
|
4 |
+
from io import BytesIO
|
5 |
+
import PyPDF2
|
6 |
+
import pandas as pd
|
7 |
+
import spacy
|
8 |
+
from spacy.matcher import Matcher
|
9 |
+
|
10 |
+
# Load Spacy Model
|
11 |
+
nlp = spacy.load("en_core_web_sm")
|
12 |
+
|
13 |
+
# Extract Text from PDF
|
14 |
+
def extract_text_from_pdf(uploaded_file):
|
15 |
+
pdf_reader = PyPDF2.PdfReader(BytesIO(uploaded_file.read()))
|
16 |
+
resume_text = ''
|
17 |
+
for page in pdf_reader.pages:
|
18 |
+
resume_text += page.extract_text()
|
19 |
+
return resume_text
|
20 |
+
|
21 |
+
# Load BERT Model for QA
|
22 |
+
model = BertForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
|
23 |
+
tokenizer = BertTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
|
24 |
+
|
25 |
+
# Generate Answer from QA Model
|
26 |
+
def answer_question(question, context, model, tokenizer):
|
27 |
+
inputs = tokenizer.encode_plus(
|
28 |
+
question,
|
29 |
+
context,
|
30 |
+
add_special_tokens=True,
|
31 |
+
return_tensors="pt",
|
32 |
+
truncation="only_second",
|
33 |
+
max_length=512,
|
34 |
+
)
|
35 |
+
outputs = model(**inputs, return_dict=True)
|
36 |
+
answer_start_scores = outputs.start_logits
|
37 |
+
answer_end_scores = outputs.end_logits
|
38 |
+
answer_start = torch.argmax(answer_start_scores)
|
39 |
+
answer_end = torch.argmax(answer_end_scores) + 1
|
40 |
+
input_ids = inputs["input_ids"].tolist()[0]
|
41 |
+
answer = tokenizer.convert_tokens_to_string(
|
42 |
+
tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end])
|
43 |
+
)
|
44 |
+
return answer
|
45 |
+
|
46 |
+
# Extract Keywords for Resume Improvement
|
47 |
+
def extract_keywords_for_sections(text):
|
48 |
+
doc = nlp(text)
|
49 |
+
skills_keywords = set()
|
50 |
+
project_keywords = set()
|
51 |
+
|
52 |
+
# Define patterns for skills and project ideas
|
53 |
+
skill_patterns = [[{"POS": "NOUN", "OP": "+"}], [{"POS": "PROPN", "OP": "+"}]]
|
54 |
+
project_patterns = [[{"POS": "VERB"}, {"POS": "NOUN", "OP": "+"}], [{"POS": "VERB"}, {"POS": "PROPN", "OP": "+"}]]
|
55 |
+
|
56 |
+
matcher = Matcher(nlp.vocab)
|
57 |
+
matcher.add("SKILLS", skill_patterns)
|
58 |
+
matcher.add("PROJECTS", project_patterns)
|
59 |
+
|
60 |
+
for match_id, start, end in matcher(doc):
|
61 |
+
span = doc[start:end]
|
62 |
+
if nlp.vocab.strings[match_id] == "SKILLS":
|
63 |
+
skills_keywords.add(span.text)
|
64 |
+
elif nlp.vocab.strings[match_id] == "PROJECTS":
|
65 |
+
project_keywords.add(span.text)
|
66 |
+
|
67 |
+
return skills_keywords, project_keywords
|
68 |
+
|
69 |
+
# Suggest Resume Improvements
|
70 |
+
def suggest_resume_improvements(resume_text, job_description):
|
71 |
+
skills_keywords, project_keywords = extract_keywords_for_sections(job_description)
|
72 |
+
missing_skills = [kw for kw in skills_keywords if kw.lower() not in resume_text.lower()]
|
73 |
+
potential_projects = [f"Consider a project involving '{keyword}'." for keyword in project_keywords]
|
74 |
+
|
75 |
+
skill_suggestions = [f"Consider highlighting your experience or skills related to '{keyword}'." for keyword in missing_skills[:5]]
|
76 |
+
project_suggestions = potential_projects[:5]
|
77 |
+
|
78 |
+
return skill_suggestions, project_suggestions
|
79 |
+
|
80 |
+
# Analyze Matches between Resume and Job Description
|
81 |
+
def analyze_matches(resume_text, job_description):
|
82 |
+
resume_keywords = set(extract_keywords_for_sections(resume_text)[0])
|
83 |
+
job_desc_keywords = set(extract_keywords_for_sections(job_description)[0])
|
84 |
+
|
85 |
+
matches = resume_keywords & job_desc_keywords
|
86 |
+
if matches:
|
87 |
+
commentary = f"Your resume matches the following keywords from the job description: {', '.join(matches)}"
|
88 |
+
else:
|
89 |
+
commentary = "There are no direct keyword matches between your resume and the job description."
|
90 |
+
|
91 |
+
return commentary
|
92 |
+
|
93 |
+
# Initialize session state to store the log of QA pairs and satisfaction responses
|
94 |
+
if 'qa_log' not in st.session_state:
|
95 |
+
st.session_state.qa_log = []
|
96 |
+
|
97 |
+
# Streamlit App Interface
|
98 |
+
st.title('Resume Enhancement and Analysis App')
|
99 |
+
|
100 |
+
# Resume PDF upload
|
101 |
+
uploaded_file = st.file_uploader("Upload your resume (PDF format):", type='pdf')
|
102 |
+
resume_text = ''
|
103 |
+
if uploaded_file is not None:
|
104 |
+
resume_text = extract_text_from_pdf(uploaded_file)
|
105 |
+
st.write("Resume Text:")
|
106 |
+
st.write(resume_text)
|
107 |
+
|
108 |
+
# Question-Answer Functionality
|
109 |
+
user_question = st.text_input("Ask a question based on your resume:")
|
110 |
+
if user_question:
|
111 |
+
answer = answer_question(user_question, resume_text, model, tokenizer)
|
112 |
+
st.write("Answer:")
|
113 |
+
st.write(answer)
|
114 |
+
|
115 |
+
# Log the interaction
|
116 |
+
st.session_state.qa_log.append({
|
117 |
+
'Question': user_question,
|
118 |
+
'Answer': answer,
|
119 |
+
'Satisfaction': 'Pending'
|
120 |
+
})
|
121 |
+
|
122 |
+
# Job Description Input for Resume Improvement
|
123 |
+
job_description = st.text_area("Input the job description here for resume improvement suggestions:")
|
124 |
+
if job_description:
|
125 |
+
skill_suggestions, project_suggestions = suggest_resume_improvements(resume_text, job_description)
|
126 |
+
|
127 |
+
st.write('Technical Skill Improvement Suggestions:')
|
128 |
+
for suggestion in skill_suggestions:
|
129 |
+
st.write(suggestion)
|
130 |
+
|
131 |
+
st.write('Notable Project Ideas:')
|
132 |
+
for suggestion in project_suggestions:
|
133 |
+
st.write(suggestion)
|
134 |
+
|
135 |
+
# Analyze Matches and Provide Commentary
|
136 |
+
match_commentary = analyze_matches(resume_text, job_description)
|
137 |
+
st.write("Match Commentary:")
|
138 |
+
st.write(match_commentary)
|
139 |
+
|
140 |
+
# User Feedback and Interaction Log
|
141 |
+
if st.session_state.qa_log:
|
142 |
+
st.write("Interaction Log:")
|
143 |
+
for i, interaction in enumerate(st.session_state.qa_log):
|
144 |
+
if interaction['Satisfaction'] == 'Pending':
|
145 |
+
satisfaction = st.radio(f'Are you satisfied with the answer to: "{interaction["Question"]}"?', ('Yes', 'No'), key=f'satisfaction_{i}')
|
146 |
+
st.session_state.qa_log[i]['Satisfaction'] = satisfaction
|
147 |
+
|
148 |
+
log_df = pd.DataFrame(st.session_state.qa_log)
|
149 |
+
st.dataframe(log_df)
|