File size: 11,369 Bytes
5fdb69e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1c6700cb-a0b0-4ac2-8fd5-363729284173",
   "metadata": {},
   "source": [
    "# AI-Powered Resume Analyzer for Job Postings"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2fa4891-b283-44de-aa63-f017eb9b140d",
   "metadata": {},
   "source": [
    "This tool is designed to analyze resumes against specific job postings, offering valuable insights such as:\n",
    "\n",
    "- Identification of skill gaps\n",
    "- Keyword matching between the CV and the job description\n",
    "- Tailored recommendations for CV improvement\n",
    "- An alignment score reflecting how well the CV fits the job\n",
    "- Personalized feedback \n",
    "- Job market trend insights\n",
    "\n",
    "An example of the tool's output can be found [here](https://tvarol.github.io/sideProjects/AILLMAgents/output.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a6a34ea-191f-4c54-9793-a3eb63faab23",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Imports\n",
    "import os\n",
    "import io\n",
    "import time\n",
    "import requests\n",
    "import PyPDF2\n",
    "from dotenv import load_dotenv\n",
    "from IPython.display import Markdown, display\n",
    "from openai import OpenAI\n",
    "from ipywidgets import Textarea, FileUpload, Button, VBox, HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04bbe1d3-bacc-400c-aed2-db44699e38f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load environment variables\n",
    "load_dotenv(override=True)\n",
    "api_key = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "# Check the key\n",
    "if not api_key:\n",
    "    print(\"No API key was found!!!\")\n",
    "else:\n",
    "    print(\"API key found and looks good so far!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27bfcee1-58e6-4ff2-9f12-9dc5c1aa5b5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "openai = OpenAI()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c82e79f2-3139-4520-ac01-a728c11cb8b9",
   "metadata": {},
   "source": [
    "## Using a Frontier Model GPT-4o Mini for This Project\n",
    "\n",
    "### Types of Prompts\n",
    "\n",
    "Models like GPT4o have been trained to receive instructions in a particular way.\n",
    "\n",
    "They expect to receive:\n",
    "\n",
    "**A system prompt** that tells them what task they are performing and what tone they should use\n",
    "\n",
    "**A user prompt** -- the conversation starter that they should reply to"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0da158ad-c3a8-4cef-806f-be0f90852996",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define our system prompt \n",
    "system_prompt = \"\"\"You are a powerful AI model designed to assist with resume analysis. Your task is to analyze a resume against a given job posting and provide feedback on how well the resume aligns with the job requirements. Your response should include the following: \n",
    "1) Skill gap identification: Compare the skills listed in the resume with those required in the job posting, highlighting areas where the resume may be lacking or overemphasized.\n",
    "2) Keyword matching between a CV and a job posting: Match keywords from the job description with the resume, determining how well they align. Provide specific suggestions for missing keywords to add to the CV.\n",
    "3) Recommendations for CV improvement: Provide actionable suggestions on how to enhance the resume, such as adding missing skills or rephrasing experience to match job requirements.\n",
    "4) Alignment score: Display a score that represents the degree of alignment between the resume and the job posting.\n",
    "5) Personalized feedback: Offer tailored advice based on the job posting, guiding the user on how to optimize their CV for the best chances of success.\n",
    "6) Job market trend insights, provide broader market trends and insights, such as in-demand skills and salary ranges.\n",
    "Provide responses that are concise, clear, and to the point. Respond in markdown.\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebdb34b0-85bd-4e36-933a-20c3c42e833b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# The job posting and the CV are required to define the user prompt\n",
    "# The user will input the job posting as text in a box here\n",
    "# The user will upload the CV in PDF format, from which the text will be extracted\n",
    "\n",
    "# You might need to install PyPDF2 via pip if it's not already installed\n",
    "# !pip install PyPDF2\n",
    "\n",
    "# Create widgets - to create a box for the job posting text\n",
    "job_posting_area = Textarea(\n",
    "    placeholder='Paste the job posting text here...',\n",
    "    description='Job Posting:',\n",
    "    disabled=False,\n",
    "    layout={'width': '800px', 'height': '300px'}\n",
    ")\n",
    "\n",
    "# Define file upload for CV\n",
    "cv_upload = FileUpload(\n",
    "    accept='.pdf',  # Only accept PDF files\n",
    "    multiple=False,  # Only allow single file selection\n",
    "    description='Upload CV (PDF)'\n",
    ")\n",
    "\n",
    "status = HTML(value=\"<b>Status:</b> Waiting for inputs...\")\n",
    "\n",
    "# Create Submit Buttons\n",
    "submit_cv_button = Button(description='Submit CV', button_style='success')\n",
    "submit_job_posting_button = Button(description='Submit Job Posting', button_style='success')\n",
    "\n",
    "# Initialize variables to store the data\n",
    "# This dictionary will hold the text for both the job posting and the CV\n",
    "# It will be used to define the user_prompt\n",
    "for_user_prompt = {\n",
    "    'job_posting': '',\n",
    "    'cv_text': ''\n",
    "}\n",
    "\n",
    "# Functions\n",
    "def submit_cv_action(change):\n",
    "\n",
    "    if not for_user_prompt['cv_text']:\n",
    "        status.value = \"<b>Status:</b> Please upload a CV before submitting.\"\n",
    "        \n",
    "    if cv_upload.value:\n",
    "        # Get the uploaded file\n",
    "        uploaded_file = cv_upload.value[0]\n",
    "        content = io.BytesIO(uploaded_file['content'])\n",
    "        \n",
    "        try:\n",
    "            pdf_reader = PyPDF2.PdfReader(content) \n",
    "            cv_text = \"\"\n",
    "            for page in pdf_reader.pages: \n",
    "                cv_text += page.extract_text() \n",
    "            \n",
    "            # Store CV text in for_user_prompt\n",
    "            for_user_prompt['cv_text'] = cv_text\n",
    "            status.value = \"<b>Status:</b> CV uploaded and processed successfully!\"\n",
    "        except Exception as e:\n",
    "            status.value = f\"<b>Status:</b> Error processing PDF: {str(e)}\"\n",
    "\n",
    "        time.sleep(0.5) # Short pause between upload and submit messages to display both\n",
    "        \n",
    "        if for_user_prompt['cv_text']:\n",
    "            #print(\"CV Submitted:\")\n",
    "            #print(for_user_prompt['cv_text'])\n",
    "            status.value = \"<b>Status:</b> CV submitted successfully!\"\n",
    "            \n",
    "def submit_job_posting_action(b):\n",
    "    for_user_prompt['job_posting'] = job_posting_area.value\n",
    "    if for_user_prompt['job_posting']:\n",
    "        #print(\"Job Posting Submitted:\")\n",
    "        #print(for_user_prompt['job_posting'])\n",
    "        status.value = \"<b>Status:</b> Job posting submitted successfully!\"\n",
    "    else:\n",
    "        status.value = \"<b>Status:</b> Please enter a job posting before submitting.\"\n",
    "\n",
    "# Attach actions to buttons\n",
    "submit_cv_button.on_click(submit_cv_action)\n",
    "submit_job_posting_button.on_click(submit_job_posting_action)\n",
    "\n",
    "# Layout\n",
    "job_posting_box = VBox([job_posting_area, submit_job_posting_button])\n",
    "cv_buttons = VBox([submit_cv_button])\n",
    "\n",
    "# Display all widgets\n",
    "display(VBox([\n",
    "    HTML(value=\"<h3>Input Job Posting and CV</h3>\"),\n",
    "    job_posting_box, \n",
    "    cv_upload,\n",
    "    cv_buttons,\n",
    "    status\n",
    "]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "364e42a6-0910-4c7c-8c3c-2ca7d2891cb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Now define user_prompt using for_user_prompt dictionary\n",
    "# Clearly label each input to differentiate the job posting and CV\n",
    "# The model can parse and analyze each section based on these labels\n",
    "user_prompt = f\"\"\"\n",
    "Job Posting: \n",
    "{for_user_prompt['job_posting']}\n",
    "\n",
    "CV: \n",
    "{for_user_prompt['cv_text']}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b51dda0-9a0c-48f4-8ec8-dae32c29da24",
   "metadata": {},
   "source": [
    "## Messages\n",
    "\n",
    "The API from OpenAI expects to receive messages in a particular structure.\n",
    "Many of the other APIs share this structure:\n",
    "\n",
    "```\n",
    "[\n",
    "    {\"role\": \"system\", \"content\": \"system message goes here\"},\n",
    "    {\"role\": \"user\", \"content\": \"user message goes here\"}\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3262c0b9-d3de-4e4f-b535-a25c0aed5783",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define messages with system_prompt and user_prompt\n",
    "def messages_for(system_prompt_input, user_prompt_input):\n",
    "    return [\n",
    "        {\"role\": \"system\", \"content\": system_prompt_input},\n",
    "        {\"role\": \"user\", \"content\": user_prompt_input}\n",
    "    ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2409ac13-0b39-4227-b4d4-b4c0ff009fd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# And now: call the OpenAI API. \n",
    "response = openai.chat.completions.create(\n",
    "    model = \"gpt-4o-mini\",\n",
    "    messages = messages_for(system_prompt, user_prompt)\n",
    ")\n",
    "\n",
    "# Response is provided in Markdown and displayed accordingly\n",
    "display(Markdown(response.choices[0].message.content))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86ab71cf-bd7e-45f7-9536-0486f349bfbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "## If you would like to save the response content as a Markdown file, uncomment the following lines\n",
    "#with open('yourfile.md', 'w') as file:\n",
    "#    file.write(response.choices[0].message.content)\n",
    "\n",
    "## You can then run the line below to create output.html which you can open on your browser\n",
    "#!pandoc yourfile.md -o output.html"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}