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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"# !pip install sentence-transformers==2.0.0"
|
10 |
+
]
|
11 |
+
},
|
12 |
+
{
|
13 |
+
"cell_type": "code",
|
14 |
+
"execution_count": 2,
|
15 |
+
"metadata": {},
|
16 |
+
"outputs": [
|
17 |
+
{
|
18 |
+
"name": "stderr",
|
19 |
+
"output_type": "stream",
|
20 |
+
"text": [
|
21 |
+
"c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\torch\\onnx\\_internal\\_beartype.py:30: UserWarning: module 'beartype.roar' has no attribute 'BeartypeDecorHintPep585DeprecationWarning'\n",
|
22 |
+
" warnings.warn(f\"{e}\")\n"
|
23 |
+
]
|
24 |
+
}
|
25 |
+
],
|
26 |
+
"source": [
|
27 |
+
"import pandas as pd\n",
|
28 |
+
"from tqdm import tqdm\n",
|
29 |
+
"from sentence_transformers import SentenceTransformer\n",
|
30 |
+
"\n",
|
31 |
+
"model = SentenceTransformer('all-mpnet-base-v2') #all-MiniLM-L6-v2 #all-mpnet-base-v2"
|
32 |
+
]
|
33 |
+
},
|
34 |
+
{
|
35 |
+
"cell_type": "code",
|
36 |
+
"execution_count": null,
|
37 |
+
"metadata": {},
|
38 |
+
"outputs": [],
|
39 |
+
"source": [
|
40 |
+
"df = pd.read_parquet('df_encoded.parquet')\n",
|
41 |
+
"df"
|
42 |
+
]
|
43 |
+
},
|
44 |
+
{
|
45 |
+
"cell_type": "code",
|
46 |
+
"execution_count": 17,
|
47 |
+
"metadata": {},
|
48 |
+
"outputs": [],
|
49 |
+
"source": [
|
50 |
+
"from sklearn.neighbors import NearestNeighbors\n",
|
51 |
+
"import numpy as np\n",
|
52 |
+
"import pandas as pd\n",
|
53 |
+
"\n",
|
54 |
+
"from sentence_transformers import SentenceTransformer\n",
|
55 |
+
"\n",
|
56 |
+
"model = SentenceTransformer('all-mpnet-base-v2') #all-MiniLM-L6-v2 #all-mpnet-base-v2\n",
|
57 |
+
"\n",
|
58 |
+
"#prepare model\n",
|
59 |
+
"# nbrs = NearestNeighbors(n_neighbors=8, algorithm='ball_tree').fit(df['text_vector_'].values.tolist())"
|
60 |
+
]
|
61 |
+
},
|
62 |
+
{
|
63 |
+
"cell_type": "code",
|
64 |
+
"execution_count": 19,
|
65 |
+
"metadata": {},
|
66 |
+
"outputs": [
|
67 |
+
{
|
68 |
+
"data": {
|
69 |
+
"text/html": [
|
70 |
+
"<div>\n",
|
71 |
+
"<style scoped>\n",
|
72 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
73 |
+
" vertical-align: middle;\n",
|
74 |
+
" }\n",
|
75 |
+
"\n",
|
76 |
+
" .dataframe tbody tr th {\n",
|
77 |
+
" vertical-align: top;\n",
|
78 |
+
" }\n",
|
79 |
+
"\n",
|
80 |
+
" .dataframe thead th {\n",
|
81 |
+
" text-align: right;\n",
|
82 |
+
" }\n",
|
83 |
+
"</style>\n",
|
84 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
85 |
+
" <thead>\n",
|
86 |
+
" <tr style=\"text-align: right;\">\n",
|
87 |
+
" <th></th>\n",
|
88 |
+
" <th>shortName</th>\n",
|
89 |
+
" <th>location</th>\n",
|
90 |
+
" <th>title</th>\n",
|
91 |
+
" <th>hourlyRate</th>\n",
|
92 |
+
" <th>avgFeedbackScore</th>\n",
|
93 |
+
" <th>description</th>\n",
|
94 |
+
" </tr>\n",
|
95 |
+
" </thead>\n",
|
96 |
+
" <tbody>\n",
|
97 |
+
" <tr>\n",
|
98 |
+
" <th>3761</th>\n",
|
99 |
+
" <td>Steven F.</td>\n",
|
100 |
+
" <td>New York</td>\n",
|
101 |
+
" <td>Database Manger / Graphics / Social Media</td>\n",
|
102 |
+
" <td>35.0</td>\n",
|
103 |
+
" <td>4.939631</td>\n",
|
104 |
+
" <td>A highly skilled problem solver with over 10 ...</td>\n",
|
105 |
+
" </tr>\n",
|
106 |
+
" <tr>\n",
|
107 |
+
" <th>835</th>\n",
|
108 |
+
" <td>Jacquelyn N.</td>\n",
|
109 |
+
" <td>New York</td>\n",
|
110 |
+
" <td>Admin support specialist</td>\n",
|
111 |
+
" <td>12.0</td>\n",
|
112 |
+
" <td>4.920992</td>\n",
|
113 |
+
" <td>I am a Kansas City based Virtual Assistant. I ...</td>\n",
|
114 |
+
" </tr>\n",
|
115 |
+
" <tr>\n",
|
116 |
+
" <th>2787</th>\n",
|
117 |
+
" <td>Mark H.</td>\n",
|
118 |
+
" <td>New York</td>\n",
|
119 |
+
" <td>WordPress Specialist - Development, Administra...</td>\n",
|
120 |
+
" <td>60.0</td>\n",
|
121 |
+
" <td>4.751762</td>\n",
|
122 |
+
" <td>Top Rated Plus | Specialize in WordPress | Inv...</td>\n",
|
123 |
+
" </tr>\n",
|
124 |
+
" <tr>\n",
|
125 |
+
" <th>3402</th>\n",
|
126 |
+
" <td>Carleton C.</td>\n",
|
127 |
+
" <td>New York</td>\n",
|
128 |
+
" <td>Expert freelancer with skills in Divi theme, C...</td>\n",
|
129 |
+
" <td>25.0</td>\n",
|
130 |
+
" <td>4.692159</td>\n",
|
131 |
+
" <td>For over 30 years, I have developed a wide ran...</td>\n",
|
132 |
+
" </tr>\n",
|
133 |
+
" <tr>\n",
|
134 |
+
" <th>1156</th>\n",
|
135 |
+
" <td>Andee F.</td>\n",
|
136 |
+
" <td>New York</td>\n",
|
137 |
+
" <td>Experienced Freelancer</td>\n",
|
138 |
+
" <td>15.0</td>\n",
|
139 |
+
" <td>4.645855</td>\n",
|
140 |
+
" <td>I have 8+ years of successfully providing admi...</td>\n",
|
141 |
+
" </tr>\n",
|
142 |
+
" <tr>\n",
|
143 |
+
" <th>1556</th>\n",
|
144 |
+
" <td>Laura O.</td>\n",
|
145 |
+
" <td>New York</td>\n",
|
146 |
+
" <td>Admin Expert with experience in Microsoft Suit...</td>\n",
|
147 |
+
" <td>30.0</td>\n",
|
148 |
+
" <td>4.620818</td>\n",
|
149 |
+
" <td>I have been passionate about my personal budge...</td>\n",
|
150 |
+
" </tr>\n",
|
151 |
+
" <tr>\n",
|
152 |
+
" <th>1002</th>\n",
|
153 |
+
" <td>Nicole H.</td>\n",
|
154 |
+
" <td>New York</td>\n",
|
155 |
+
" <td>Experienced admin support and customer support...</td>\n",
|
156 |
+
" <td>30.0</td>\n",
|
157 |
+
" <td>4.129972</td>\n",
|
158 |
+
" <td>I'm an experienced jack of all trades. I have...</td>\n",
|
159 |
+
" </tr>\n",
|
160 |
+
" <tr>\n",
|
161 |
+
" <th>1626</th>\n",
|
162 |
+
" <td>Drew L.</td>\n",
|
163 |
+
" <td>New York</td>\n",
|
164 |
+
" <td>Front End Web Developer</td>\n",
|
165 |
+
" <td>50.0</td>\n",
|
166 |
+
" <td>0.000000</td>\n",
|
167 |
+
" <td>Worked for agency with big name clients doing ...</td>\n",
|
168 |
+
" </tr>\n",
|
169 |
+
" </tbody>\n",
|
170 |
+
"</table>\n",
|
171 |
+
"</div>"
|
172 |
+
],
|
173 |
+
"text/plain": [
|
174 |
+
" shortName location \\\n",
|
175 |
+
"3761 Steven F. New York \n",
|
176 |
+
"835 Jacquelyn N. New York \n",
|
177 |
+
"2787 Mark H. New York \n",
|
178 |
+
"3402 Carleton C. New York \n",
|
179 |
+
"1156 Andee F. New York \n",
|
180 |
+
"1556 Laura O. New York \n",
|
181 |
+
"1002 Nicole H. New York \n",
|
182 |
+
"1626 Drew L. New York \n",
|
183 |
+
"\n",
|
184 |
+
" title hourlyRate \\\n",
|
185 |
+
"3761 Database Manger / Graphics / Social Media 35.0 \n",
|
186 |
+
"835 Admin support specialist 12.0 \n",
|
187 |
+
"2787 WordPress Specialist - Development, Administra... 60.0 \n",
|
188 |
+
"3402 Expert freelancer with skills in Divi theme, C... 25.0 \n",
|
189 |
+
"1156 Experienced Freelancer 15.0 \n",
|
190 |
+
"1556 Admin Expert with experience in Microsoft Suit... 30.0 \n",
|
191 |
+
"1002 Experienced admin support and customer support... 30.0 \n",
|
192 |
+
"1626 Front End Web Developer 50.0 \n",
|
193 |
+
"\n",
|
194 |
+
" avgFeedbackScore description \n",
|
195 |
+
"3761 4.939631 A highly skilled problem solver with over 10 ... \n",
|
196 |
+
"835 4.920992 I am a Kansas City based Virtual Assistant. I ... \n",
|
197 |
+
"2787 4.751762 Top Rated Plus | Specialize in WordPress | Inv... \n",
|
198 |
+
"3402 4.692159 For over 30 years, I have developed a wide ran... \n",
|
199 |
+
"1156 4.645855 I have 8+ years of successfully providing admi... \n",
|
200 |
+
"1556 4.620818 I have been passionate about my personal budge... \n",
|
201 |
+
"1002 4.129972 I'm an experienced jack of all trades. I have... \n",
|
202 |
+
"1626 0.000000 Worked for agency with big name clients doing ... "
|
203 |
+
]
|
204 |
+
},
|
205 |
+
"execution_count": 19,
|
206 |
+
"metadata": {},
|
207 |
+
"output_type": "execute_result"
|
208 |
+
}
|
209 |
+
],
|
210 |
+
"source": [
|
211 |
+
"def filter_df(df, column_name, filter_type, filter_value):\n",
|
212 |
+
" if filter_type == '==':\n",
|
213 |
+
" df_filtered = df[df[column_name]==filter_value]\n",
|
214 |
+
" elif filter_type == '<=':\n",
|
215 |
+
" df_filtered = df[df[column_name]<=filter_value]\n",
|
216 |
+
" return df_filtered\n",
|
217 |
+
"\n",
|
218 |
+
"def search(df, query):\n",
|
219 |
+
" product = model.encode(query).tolist()\n",
|
220 |
+
" # product = df.iloc[0]['text_vector_'] #use one of the products as sample\n",
|
221 |
+
"\n",
|
222 |
+
" nbrs = NearestNeighbors(n_neighbors=8, algorithm='ball_tree').fit(df['text_vector_'].values.tolist())\n",
|
223 |
+
" distances, indices = nbrs.kneighbors([product]) #input the vector of the reference object\n",
|
224 |
+
"\n",
|
225 |
+
" #print out the description of every recommended product\n",
|
226 |
+
" df_search = df.iloc[list(indices)[0]].drop(['skills', 'text_vector_'], axis=1).sort_values('avgFeedbackScore', ascending=False)\n",
|
227 |
+
"\n",
|
228 |
+
" return df_search[['shortName', 'location', 'title', 'hourlyRate', 'avgFeedbackScore', 'description']]\n",
|
229 |
+
"\n",
|
230 |
+
"# search('I want to hire a person who does both backend and')\n",
|
231 |
+
"\n",
|
232 |
+
"df_location = filter_df(df, 'location', '==', 'New York')\n",
|
233 |
+
"df_price = filter_df(df_location, 'hourlyRate', '<=', 80)\n",
|
234 |
+
"search(df_price, 'I want to hire a person who does both backend and')"
|
235 |
+
]
|
236 |
+
},
|
237 |
+
{
|
238 |
+
"cell_type": "code",
|
239 |
+
"execution_count": null,
|
240 |
+
"metadata": {},
|
241 |
+
"outputs": [],
|
242 |
+
"source": [
|
243 |
+
"import gradio as gr\n",
|
244 |
+
"import os\n",
|
245 |
+
"\n",
|
246 |
+
"#the first module becomes text1, the second module file1\n",
|
247 |
+
"def greet(price, location, query):\n",
|
248 |
+
" # df1 = \n",
|
249 |
+
" df_location = filter_df(df, 'location', '==', location)\n",
|
250 |
+
" df_price = filter_df(df_location, 'hourlyRate', '<=', price)\n",
|
251 |
+
" df_search = search(df_price, query)\n",
|
252 |
+
" return df_search"
|
253 |
+
]
|
254 |
+
},
|
255 |
+
{
|
256 |
+
"cell_type": "code",
|
257 |
+
"execution_count": 28,
|
258 |
+
"metadata": {},
|
259 |
+
"outputs": [
|
260 |
+
{
|
261 |
+
"name": "stderr",
|
262 |
+
"output_type": "stream",
|
263 |
+
"text": [
|
264 |
+
"c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\gradio\\deprecation.py:43: UserWarning: You have unused kwarg parameters in Slider, please remove them: {'step_size': 5}\n",
|
265 |
+
" warnings.warn(\n",
|
266 |
+
"c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\gradio\\deprecation.py:43: UserWarning: You have unused kwarg parameters in Radio, please remove them: {'multiselect': False}\n",
|
267 |
+
" warnings.warn(\n"
|
268 |
+
]
|
269 |
+
},
|
270 |
+
{
|
271 |
+
"name": "stdout",
|
272 |
+
"output_type": "stream",
|
273 |
+
"text": [
|
274 |
+
"Running on local URL: http://127.0.0.1:7906\n",
|
275 |
+
"\n",
|
276 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
277 |
+
]
|
278 |
+
},
|
279 |
+
{
|
280 |
+
"data": {
|
281 |
+
"text/html": [
|
282 |
+
"<div><iframe src=\"http://127.0.0.1:7906/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
283 |
+
],
|
284 |
+
"text/plain": [
|
285 |
+
"<IPython.core.display.HTML object>"
|
286 |
+
]
|
287 |
+
},
|
288 |
+
"metadata": {},
|
289 |
+
"output_type": "display_data"
|
290 |
+
},
|
291 |
+
{
|
292 |
+
"data": {
|
293 |
+
"text/plain": []
|
294 |
+
},
|
295 |
+
"execution_count": 28,
|
296 |
+
"metadata": {},
|
297 |
+
"output_type": "execute_result"
|
298 |
+
}
|
299 |
+
],
|
300 |
+
"source": [
|
301 |
+
"with gr.Blocks(theme=gr.themes.Soft(primary_hue='amber', secondary_hue='gray', neutral_hue='amber')) as demo:\n",
|
302 |
+
" gr.Markdown(\n",
|
303 |
+
" \"\"\"\n",
|
304 |
+
" # Freelancer Upwork Search\n",
|
305 |
+
" \"\"\"\n",
|
306 |
+
" )\n",
|
307 |
+
" input1 = gr.Slider(20, 120, value=90, step_size=5, label=\"Max Hourly Rate\")\n",
|
308 |
+
" input2 = gr.Radio(['New York', 'Chicago', 'Washington'], multiselect=False, label='State', value='New York')\n",
|
309 |
+
" input3 = gr.Textbox(label='Query', value='I want to develop a mobile app')\n",
|
310 |
+
"\n",
|
311 |
+
" btn = gr.Button(value=\"Search for Product\")\n",
|
312 |
+
" output = gr.Dataframe()\n",
|
313 |
+
" # btn.click(greet, inputs='text', outputs=['dataframe'])\n",
|
314 |
+
" btn.click(greet, [input1, input2, input3], [output])\n",
|
315 |
+
"demo.launch(share=False)"
|
316 |
+
]
|
317 |
+
},
|
318 |
+
{
|
319 |
+
"cell_type": "code",
|
320 |
+
"execution_count": null,
|
321 |
+
"metadata": {},
|
322 |
+
"outputs": [],
|
323 |
+
"source": [
|
324 |
+
"# iface = gr.Interface(\n",
|
325 |
+
"# fn=greet,\n",
|
326 |
+
"# inputs=[\n",
|
327 |
+
"# gr.Slider(20, 120, value=20, step_size=5, label=\"Max Hourly Rate\"),\n",
|
328 |
+
"# gr.Radio(\n",
|
329 |
+
"# ['New York', 'Chicago', 'Washington'], multiselect=False, label='State', value='New York'\n",
|
330 |
+
"# ),\n",
|
331 |
+
"# gr.Textbox(label='Input', value='I want to develop a mobile app')\n",
|
332 |
+
"# ],\n",
|
333 |
+
"# outputs=[\"dataframe\"])\n",
|
334 |
+
"# iface.launch(share=False)"
|
335 |
+
]
|
336 |
+
}
|
337 |
+
],
|
338 |
+
"metadata": {
|
339 |
+
"kernelspec": {
|
340 |
+
"display_name": "Python 3.9.0 64-bit",
|
341 |
+
"language": "python",
|
342 |
+
"name": "python3"
|
343 |
+
},
|
344 |
+
"language_info": {
|
345 |
+
"codemirror_mode": {
|
346 |
+
"name": "ipython",
|
347 |
+
"version": 3
|
348 |
+
},
|
349 |
+
"file_extension": ".py",
|
350 |
+
"mimetype": "text/x-python",
|
351 |
+
"name": "python",
|
352 |
+
"nbconvert_exporter": "python",
|
353 |
+
"pygments_lexer": "ipython3",
|
354 |
+
"version": "3.9.13"
|
355 |
+
},
|
356 |
+
"orig_nbformat": 4,
|
357 |
+
"vscode": {
|
358 |
+
"interpreter": {
|
359 |
+
"hash": "fdf377d643bc1cb065454f0ad2ceac75d834452ecf289e7ba92c6b3f59a7cee1"
|
360 |
+
}
|
361 |
+
}
|
362 |
+
},
|
363 |
+
"nbformat": 4,
|
364 |
+
"nbformat_minor": 2
|
365 |
+
}
|