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9 |
+
"name": "stdout",
|
10 |
+
"output_type": "stream",
|
11 |
+
"text": [
|
12 |
+
"Collecting sentencepiece\n",
|
13 |
+
" Using cached sentencepiece-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (7.7 kB)\n",
|
14 |
+
"Downloading sentencepiece-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)\n",
|
15 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m49.7 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
|
16 |
+
"\u001b[?25hInstalling collected packages: sentencepiece\n",
|
17 |
+
"Successfully installed sentencepiece-0.2.0\n"
|
18 |
+
]
|
19 |
+
}
|
20 |
+
],
|
21 |
+
"source": [
|
22 |
+
"!pip install sentencepiece"
|
23 |
+
]
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"cell_type": "code",
|
27 |
+
"execution_count": 57,
|
28 |
+
"metadata": {},
|
29 |
+
"outputs": [
|
30 |
+
{
|
31 |
+
"name": "stdout",
|
32 |
+
"output_type": "stream",
|
33 |
+
"text": [
|
34 |
+
"Collecting langsmith\n",
|
35 |
+
" Downloading langsmith-0.1.104-py3-none-any.whl.metadata (13 kB)\n",
|
36 |
+
"Requirement already satisfied: httpx<1,>=0.23.0 in /home/sukhera/miniconda3/lib/python3.12/site-packages (from langsmith) (0.27.0)\n",
|
37 |
+
"Requirement already satisfied: orjson<4.0.0,>=3.9.14 in /home/sukhera/miniconda3/lib/python3.12/site-packages (from langsmith) (3.10.7)\n",
|
38 |
+
"Requirement already satisfied: pydantic<3,>=1 in /home/sukhera/miniconda3/lib/python3.12/site-packages (from langsmith) (2.8.2)\n",
|
39 |
+
"Requirement already satisfied: requests<3,>=2 in /home/sukhera/miniconda3/lib/python3.12/site-packages (from langsmith) (2.32.2)\n",
|
40 |
+
"Requirement already satisfied: anyio in /home/sukhera/miniconda3/lib/python3.12/site-packages (from httpx<1,>=0.23.0->langsmith) (4.4.0)\n",
|
41 |
+
"Requirement already satisfied: certifi in /home/sukhera/miniconda3/lib/python3.12/site-packages (from httpx<1,>=0.23.0->langsmith) (2024.7.4)\n",
|
42 |
+
"Requirement already satisfied: httpcore==1.* in /home/sukhera/miniconda3/lib/python3.12/site-packages (from httpx<1,>=0.23.0->langsmith) (1.0.5)\n",
|
43 |
+
"Requirement already satisfied: idna in /home/sukhera/miniconda3/lib/python3.12/site-packages (from httpx<1,>=0.23.0->langsmith) (3.7)\n",
|
44 |
+
"Requirement already satisfied: sniffio in /home/sukhera/miniconda3/lib/python3.12/site-packages (from httpx<1,>=0.23.0->langsmith) (1.3.1)\n",
|
45 |
+
"Requirement already satisfied: h11<0.15,>=0.13 in /home/sukhera/miniconda3/lib/python3.12/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith) (0.14.0)\n",
|
46 |
+
"Requirement already satisfied: annotated-types>=0.4.0 in /home/sukhera/miniconda3/lib/python3.12/site-packages (from pydantic<3,>=1->langsmith) (0.7.0)\n",
|
47 |
+
"Requirement already satisfied: pydantic-core==2.20.1 in /home/sukhera/miniconda3/lib/python3.12/site-packages (from pydantic<3,>=1->langsmith) (2.20.1)\n",
|
48 |
+
"Requirement already satisfied: typing-extensions>=4.6.1 in /home/sukhera/miniconda3/lib/python3.12/site-packages (from pydantic<3,>=1->langsmith) (4.12.2)\n",
|
49 |
+
"Requirement already satisfied: charset-normalizer<4,>=2 in /home/sukhera/miniconda3/lib/python3.12/site-packages (from requests<3,>=2->langsmith) (2.0.4)\n",
|
50 |
+
"Requirement already satisfied: urllib3<3,>=1.21.1 in /home/sukhera/miniconda3/lib/python3.12/site-packages (from requests<3,>=2->langsmith) (2.2.2)\n",
|
51 |
+
"Downloading langsmith-0.1.104-py3-none-any.whl (149 kB)\n",
|
52 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m149.1/149.1 kB\u001b[0m \u001b[31m22.4 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
|
53 |
+
"\u001b[?25hInstalling collected packages: langsmith\n",
|
54 |
+
"Successfully installed langsmith-0.1.104\n"
|
55 |
+
]
|
56 |
+
}
|
57 |
+
],
|
58 |
+
"source": [
|
59 |
+
"!pip install -U langsmith"
|
60 |
+
]
|
61 |
+
},
|
62 |
+
{
|
63 |
+
"cell_type": "code",
|
64 |
+
"execution_count": 1,
|
65 |
+
"metadata": {},
|
66 |
+
"outputs": [
|
67 |
+
{
|
68 |
+
"name": "stderr",
|
69 |
+
"output_type": "stream",
|
70 |
+
"text": [
|
71 |
+
"/home/sukhera/miniconda3/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
72 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
73 |
+
]
|
74 |
+
}
|
75 |
+
],
|
76 |
+
"source": [
|
77 |
+
"import os\n",
|
78 |
+
"import PyPDF2\n",
|
79 |
+
"from transformers import BertTokenizer, BertModel\n",
|
80 |
+
"from transformers import LongformerModel, LongformerTokenizer\n",
|
81 |
+
"from transformers import BigBirdModel, BigBirdTokenizer\n",
|
82 |
+
"import numpy as np\n",
|
83 |
+
"from groq import Groq\n",
|
84 |
+
"import gradio as gr\n",
|
85 |
+
"from pathlib import Path\n",
|
86 |
+
"import torch\n",
|
87 |
+
"import json\n"
|
88 |
+
]
|
89 |
+
},
|
90 |
+
{
|
91 |
+
"cell_type": "code",
|
92 |
+
"execution_count": 2,
|
93 |
+
"metadata": {},
|
94 |
+
"outputs": [],
|
95 |
+
"source": [
|
96 |
+
"from langsmith import Client\n",
|
97 |
+
"\n",
|
98 |
+
"# Initialize the LangSmith Client\n",
|
99 |
+
"os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
|
100 |
+
"os.environ[\"LANGCHAIN_API_KEY\"] = \"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\"\n",
|
101 |
+
"client = Client()"
|
102 |
+
]
|
103 |
+
},
|
104 |
+
{
|
105 |
+
"cell_type": "code",
|
106 |
+
"execution_count": 3,
|
107 |
+
"metadata": {},
|
108 |
+
"outputs": [
|
109 |
+
{
|
110 |
+
"name": "stderr",
|
111 |
+
"output_type": "stream",
|
112 |
+
"text": [
|
113 |
+
"/home/sukhera/miniconda3/lib/python3.12/site-packages/transformers/tokenization_utils_base.py:1601: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be depracted in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884\n",
|
114 |
+
" warnings.warn(\n",
|
115 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
116 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
117 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
118 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
119 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
120 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
121 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
122 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
123 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
124 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
125 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
126 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
127 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
128 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
129 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
130 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
131 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
132 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
133 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
134 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
135 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
136 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
137 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
138 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
139 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
140 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
141 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
142 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
143 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
144 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
145 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
146 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
147 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
148 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
149 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
150 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
151 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
152 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
153 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
154 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
155 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
156 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
157 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
158 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
159 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
160 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
161 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
162 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
163 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
164 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n",
|
165 |
+
"A parameter name that contains `beta` will be renamed internally to `bias`. Please use a different name to suppress this warning.\n",
|
166 |
+
"A parameter name that contains `gamma` will be renamed internally to `weight`. Please use a different name to suppress this warning.\n"
|
167 |
+
]
|
168 |
+
}
|
169 |
+
],
|
170 |
+
"source": [
|
171 |
+
"# Load BERT tokenizer and model\n",
|
172 |
+
"tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')\n",
|
173 |
+
"model = BertModel.from_pretrained('bert-base-uncased')"
|
174 |
+
]
|
175 |
+
},
|
176 |
+
{
|
177 |
+
"cell_type": "code",
|
178 |
+
"execution_count": 4,
|
179 |
+
"metadata": {},
|
180 |
+
"outputs": [],
|
181 |
+
"source": [
|
182 |
+
"# Load the BigBird model and tokenizer\n",
|
183 |
+
"tokenizer = BigBirdTokenizer.from_pretrained('google/bigbird-roberta-base')\n",
|
184 |
+
"model = BigBirdModel.from_pretrained('google/bigbird-roberta-base')\n"
|
185 |
+
]
|
186 |
+
},
|
187 |
+
{
|
188 |
+
"cell_type": "code",
|
189 |
+
"execution_count": 5,
|
190 |
+
"metadata": {},
|
191 |
+
"outputs": [],
|
192 |
+
"source": [
|
193 |
+
"#longformer\n",
|
194 |
+
"# Load the Longformer model and tokenizer\n",
|
195 |
+
"tokenizer = LongformerTokenizer.from_pretrained('allenai/longformer-base-4096')\n",
|
196 |
+
"model = LongformerModel.from_pretrained('allenai/longformer-base-4096')\n"
|
197 |
+
]
|
198 |
+
},
|
199 |
+
{
|
200 |
+
"cell_type": "code",
|
201 |
+
"execution_count": 28,
|
202 |
+
"metadata": {},
|
203 |
+
"outputs": [],
|
204 |
+
"source": [
|
205 |
+
"#longFormer\n",
|
206 |
+
"\n",
|
207 |
+
"def get_longformer_embedding(text):\n",
|
208 |
+
" # Tokenize the text\n",
|
209 |
+
" inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True, max_length=4096)\n",
|
210 |
+
" \n",
|
211 |
+
" # Get the embeddings from Longformer\n",
|
212 |
+
" with torch.no_grad():\n",
|
213 |
+
" outputs = model(**inputs)\n",
|
214 |
+
" \n",
|
215 |
+
" # Use the [CLS] token's embedding as the aggregate representation\n",
|
216 |
+
" cls_embedding = outputs.last_hidden_state[:, 0, :].numpy()\n",
|
217 |
+
" \n",
|
218 |
+
" return cls_embedding"
|
219 |
+
]
|
220 |
+
},
|
221 |
+
{
|
222 |
+
"cell_type": "code",
|
223 |
+
"execution_count": 7,
|
224 |
+
"metadata": {},
|
225 |
+
"outputs": [],
|
226 |
+
"source": [
|
227 |
+
"# BIGBIRD\n",
|
228 |
+
"def get_bigbird_embedding(text):\n",
|
229 |
+
" # Tokenize the text\n",
|
230 |
+
" inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True, max_length=4096)\n",
|
231 |
+
" \n",
|
232 |
+
" # Get the embeddings from BigBird\n",
|
233 |
+
" with torch.no_grad():\n",
|
234 |
+
" outputs = model(**inputs)\n",
|
235 |
+
" \n",
|
236 |
+
" # Use the [CLS] token's embedding as the aggregate representation\n",
|
237 |
+
" cls_embedding = outputs.last_hidden_state[:, 0, :].numpy()\n",
|
238 |
+
" \n",
|
239 |
+
" return cls_embedding"
|
240 |
+
]
|
241 |
+
},
|
242 |
+
{
|
243 |
+
"cell_type": "code",
|
244 |
+
"execution_count": 8,
|
245 |
+
"metadata": {},
|
246 |
+
"outputs": [],
|
247 |
+
"source": [
|
248 |
+
"def get_bert_embedding(text):\n",
|
249 |
+
" # Tokenize the text\n",
|
250 |
+
" inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True, max_length=512)\n",
|
251 |
+
" \n",
|
252 |
+
" # Get the embeddings from BERT\n",
|
253 |
+
" with torch.no_grad():\n",
|
254 |
+
" outputs = model(**inputs)\n",
|
255 |
+
" \n",
|
256 |
+
" # Use the [CLS] token's embedding as the aggregate representation\n",
|
257 |
+
" cls_embedding = outputs.last_hidden_state[:, 0, :].numpy()\n",
|
258 |
+
" \n",
|
259 |
+
" return cls_embedding\n"
|
260 |
+
]
|
261 |
+
},
|
262 |
+
{
|
263 |
+
"cell_type": "code",
|
264 |
+
"execution_count": 9,
|
265 |
+
"metadata": {},
|
266 |
+
"outputs": [],
|
267 |
+
"source": [
|
268 |
+
"def process_folder(file):\n",
|
269 |
+
" folder_path = os.path.dirname(file.name) # Get the directory of the selected file\n",
|
270 |
+
" files = os.listdir(folder_path) # List all files in the directory\n",
|
271 |
+
" file_paths = [os.path.join(folder_path, f) for f in files] # Get full paths of all files\n",
|
272 |
+
" return f\"Files in folder: {', '.join(files)}\""
|
273 |
+
]
|
274 |
+
},
|
275 |
+
{
|
276 |
+
"cell_type": "code",
|
277 |
+
"execution_count": 10,
|
278 |
+
"metadata": {},
|
279 |
+
"outputs": [],
|
280 |
+
"source": [
|
281 |
+
"# Function to extract text from a PDF\n",
|
282 |
+
"def extract_text_from_pdf(pdf_file):\n",
|
283 |
+
" text = ''\n",
|
284 |
+
" with open(pdf_file, 'rb') as file:\n",
|
285 |
+
" reader = PyPDF2.PdfReader(file)\n",
|
286 |
+
" for page in reader.pages:\n",
|
287 |
+
" text += page.extract_text() or ''\n",
|
288 |
+
" return text\n"
|
289 |
+
]
|
290 |
+
},
|
291 |
+
{
|
292 |
+
"cell_type": "code",
|
293 |
+
"execution_count": 11,
|
294 |
+
"metadata": {},
|
295 |
+
"outputs": [],
|
296 |
+
"source": [
|
297 |
+
"cluster_emb={}"
|
298 |
+
]
|
299 |
+
},
|
300 |
+
{
|
301 |
+
"cell_type": "code",
|
302 |
+
"execution_count": 12,
|
303 |
+
"metadata": {},
|
304 |
+
"outputs": [],
|
305 |
+
"source": [
|
306 |
+
"\n",
|
307 |
+
"def calculate_cosine(embedding1, embedding2):\n",
|
308 |
+
" # Calculate the dot product and magnitudes of the embeddings\n",
|
309 |
+
" dot_product = np.dot(embedding1, embedding2)\n",
|
310 |
+
" magnitude1 = np.linalg.norm(embedding1)\n",
|
311 |
+
" magnitude2 = np.linalg.norm(embedding2)\n",
|
312 |
+
" \n",
|
313 |
+
" # Calculate cosine similarity\n",
|
314 |
+
" similarity = dot_product / (magnitude1 * magnitude2)\n",
|
315 |
+
" return similarity"
|
316 |
+
]
|
317 |
+
},
|
318 |
+
{
|
319 |
+
"cell_type": "code",
|
320 |
+
"execution_count": null,
|
321 |
+
"metadata": {},
|
322 |
+
"outputs": [],
|
323 |
+
"source": []
|
324 |
+
},
|
325 |
+
{
|
326 |
+
"cell_type": "code",
|
327 |
+
"execution_count": 24,
|
328 |
+
"metadata": {},
|
329 |
+
"outputs": [],
|
330 |
+
"source": [
|
331 |
+
"def foo(files, JD):\n",
|
332 |
+
" # Extract text and compute embeddings for job description using different models\n",
|
333 |
+
" text_jd = extract_text_from_pdf(JD) \n",
|
334 |
+
" JD_embedding_bert = get_bert_embedding(text_jd).flatten() # Flatten to match the dimension\n",
|
335 |
+
" JD_embedding_longformer = get_longformer_embedding(text_jd).flatten()\n",
|
336 |
+
" JD_embedding_bigbird = get_bigbird_embedding(text_jd).flatten()\n",
|
337 |
+
"\n",
|
338 |
+
" sim = []\n",
|
339 |
+
" \n",
|
340 |
+
" for d in files:\n",
|
341 |
+
" text = extract_text_from_pdf(d)\n",
|
342 |
+
" # Compute embeddings for the resume using different models\n",
|
343 |
+
" resume_embedding_bert = get_bert_embedding(text).flatten() # Fixed function call\n",
|
344 |
+
" resume_embedding_longformer = get_longformer_embedding(text).flatten()\n",
|
345 |
+
" resume_embedding_bigbird = get_bigbird_embedding(text).flatten()\n",
|
346 |
+
" # Calculate cosine similarity for each model\n",
|
347 |
+
" similarity_bert = calculate_cosine(resume_embedding_bert, JD_embedding_bert)\n",
|
348 |
+
" similarity_longformer = calculate_cosine(resume_embedding_longformer, JD_embedding_longformer)\n",
|
349 |
+
" similarity_bigbird = calculate_cosine(resume_embedding_bigbird, JD_embedding_bigbird)\n",
|
350 |
+
" # Append the results to the array\n",
|
351 |
+
" sim.append(f\"\\nFile: {d.name:}\\n\"\n",
|
352 |
+
" f\"Bert Similarity: {similarity_bert:.4f}\\n\"\n",
|
353 |
+
" f\"Longformer Similarity: {similarity_longformer:.4f}\\n\"\n",
|
354 |
+
" f\"BigBird Similarity: {similarity_bigbird:.4f}\\n\")\n",
|
355 |
+
" \n",
|
356 |
+
" \n",
|
357 |
+
" \n",
|
358 |
+
" return \"\\n\".join(sim) # Join the list into a single string for Gradio output\n"
|
359 |
+
]
|
360 |
+
},
|
361 |
+
{
|
362 |
+
"cell_type": "code",
|
363 |
+
"execution_count": 25,
|
364 |
+
"metadata": {},
|
365 |
+
"outputs": [
|
366 |
+
{
|
367 |
+
"name": "stderr",
|
368 |
+
"output_type": "stream",
|
369 |
+
"text": [
|
370 |
+
"/home/sukhera/miniconda3/lib/python3.12/site-packages/transformers/tokenization_utils_base.py:2888: UserWarning: `max_length` is ignored when `padding`=`True` and there is no truncation strategy. To pad to max length, use `padding='max_length'`.\n",
|
371 |
+
" warnings.warn(\n",
|
372 |
+
"/home/sukhera/miniconda3/lib/python3.12/site-packages/transformers/tokenization_utils_base.py:2888: UserWarning: `max_length` is ignored when `padding`=`True` and there is no truncation strategy. To pad to max length, use `padding='max_length'`.\n",
|
373 |
+
" warnings.warn(\n"
|
374 |
+
]
|
375 |
+
}
|
376 |
+
],
|
377 |
+
"source": [
|
378 |
+
"\n",
|
379 |
+
"with gr.Blocks() as func:\n",
|
380 |
+
" inputs = [gr.File(file_count=\"multiple\", label=\"Upload Resume Files\"), gr.File(label=\"Upload Job Description\")]\n",
|
381 |
+
" outputs = gr.Textbox(label=\"Similarity Scores\")\n",
|
382 |
+
" show = gr.Button(value=\"Calculate Similarity\")\n",
|
383 |
+
" show.click(foo, inputs, outputs)"
|
384 |
+
]
|
385 |
+
},
|
386 |
+
{
|
387 |
+
"cell_type": "code",
|
388 |
+
"execution_count": 29,
|
389 |
+
"metadata": {},
|
390 |
+
"outputs": [
|
391 |
+
{
|
392 |
+
"name": "stdout",
|
393 |
+
"output_type": "stream",
|
394 |
+
"text": [
|
395 |
+
"Rerunning server... use `close()` to stop if you need to change `launch()` parameters.\n",
|
396 |
+
"----\n",
|
397 |
+
"\n",
|
398 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
399 |
+
]
|
400 |
+
},
|
401 |
+
{
|
402 |
+
"data": {
|
403 |
+
"text/html": [
|
404 |
+
"<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
405 |
+
],
|
406 |
+
"text/plain": [
|
407 |
+
"<IPython.core.display.HTML object>"
|
408 |
+
]
|
409 |
+
},
|
410 |
+
"metadata": {},
|
411 |
+
"output_type": "display_data"
|
412 |
+
},
|
413 |
+
{
|
414 |
+
"data": {
|
415 |
+
"text/plain": []
|
416 |
+
},
|
417 |
+
"execution_count": 29,
|
418 |
+
"metadata": {},
|
419 |
+
"output_type": "execute_result"
|
420 |
+
}
|
421 |
+
],
|
422 |
+
"source": [
|
423 |
+
"func.launch()"
|
424 |
+
]
|
425 |
+
},
|
426 |
+
{
|
427 |
+
"cell_type": "code",
|
428 |
+
"execution_count": null,
|
429 |
+
"metadata": {},
|
430 |
+
"outputs": [],
|
431 |
+
"source": []
|
432 |
+
}
|
433 |
+
],
|
434 |
+
"metadata": {
|
435 |
+
"kernelspec": {
|
436 |
+
"display_name": "base",
|
437 |
+
"language": "python",
|
438 |
+
"name": "python3"
|
439 |
+
},
|
440 |
+
"language_info": {
|
441 |
+
"codemirror_mode": {
|
442 |
+
"name": "ipython",
|
443 |
+
"version": 3
|
444 |
+
},
|
445 |
+
"file_extension": ".py",
|
446 |
+
"mimetype": "text/x-python",
|
447 |
+
"name": "python",
|
448 |
+
"nbconvert_exporter": "python",
|
449 |
+
"pygments_lexer": "ipython3",
|
450 |
+
"version": "3.12.2"
|
451 |
+
}
|
452 |
+
},
|
453 |
+
"nbformat": 4,
|
454 |
+
"nbformat_minor": 2
|
455 |
+
}
|