Spaces:
Runtime error
Runtime error
File size: 41,329 Bytes
6ecf14b |
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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Load the dataset\n",
"We will combine the Description and Patient text into a single combined text. The model will encode this combined text and it will output a single vector embedding."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"To run this notebook, you will need to install: pandas, openai, transformers, plotly, matplotlib, scikit-learn, torch (transformer dep), torchvision, and scipy."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"import pandas as pd\n",
"import tiktoken\n",
"from openai.embeddings_utils import get_embedding\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# embedding model parameters\n",
"embedding_model = \"text-embedding-ada-002\"\n",
"embedding_encoding = \"cl100k_base\" # this the encoding for text-embedding-ada-002\n",
"max_tokens = 8000 # the maximum for text-embedding-ada-002 is 8191"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# load & inspect dataset\n",
"df = pd.read_csv(\"../2-Data/dialogues.csv\", sep = '\\t')\n",
"df = df.dropna()#.head(1000)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"df.rename(columns = {'Description':'Question',\"Doctor\":\"Answer\"}, inplace = True)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Question</th>\n",
" <th>Patient</th>\n",
" <th>Answer</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Q. What does abutment of the nerve root mean?</td>\n",
" <td>Hi doctor,I am just wondering what is abutting...</td>\n",
" <td>Hi. I have gone through your query with dilige...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Q. What should I do to reduce my weight gained...</td>\n",
" <td>Hi doctor, I am a 22-year-old female who was d...</td>\n",
" <td>Hi. You have really done well with the hypothy...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Q. I have started to get lots of acne on my fa...</td>\n",
" <td>Hi doctor! I used to have clear skin but since...</td>\n",
" <td>Hi there Acne has multifactorial etiology. Onl...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Q. Why do I have uncomfortable feeling between...</td>\n",
" <td>Hello doctor,I am having an uncomfortable feel...</td>\n",
" <td>Hello. The popping and discomfort what you fel...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Q. My symptoms after intercourse threatns me e...</td>\n",
" <td>Hello doctor,Before two years had sex with a c...</td>\n",
" <td>Hello. The HIV test uses a finger prick blood ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>256911</th>\n",
" <td>Why is hair fall increasing while using Bontre...</td>\n",
" <td>I am suffering from excessive hairfall. My doc...</td>\n",
" <td>Hello Dear Thanks for writing to us, we are he...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>256912</th>\n",
" <td>Why was I asked to discontinue Androanagen whi...</td>\n",
" <td>Hi Doctor, I have been having severe hair fall...</td>\n",
" <td>hello, hair4u is combination of minoxid...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>256913</th>\n",
" <td>Can Mintop 5% Lotion be used by women for seve...</td>\n",
" <td>Hi..i hav sever hair loss problem so consulted...</td>\n",
" <td>HI I have evaluated your query thoroughly you...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>256914</th>\n",
" <td>Is Minoxin 5% lotion advisable instead of Foli...</td>\n",
" <td>Hi, i am 25 year old girl, i am having massive...</td>\n",
" <td>Hello and Welcome to ‘Ask A Doctor’ service.I ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>256915</th>\n",
" <td>Are Biotin supplements need to reduce severe h...</td>\n",
" <td>iam having hairfall for a decade.. but fews we...</td>\n",
" <td>you did'nt mention about thyroid problem ...us...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>256916 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
" Question \\\n",
"0 Q. What does abutment of the nerve root mean? \n",
"1 Q. What should I do to reduce my weight gained... \n",
"2 Q. I have started to get lots of acne on my fa... \n",
"3 Q. Why do I have uncomfortable feeling between... \n",
"4 Q. My symptoms after intercourse threatns me e... \n",
"... ... \n",
"256911 Why is hair fall increasing while using Bontre... \n",
"256912 Why was I asked to discontinue Androanagen whi... \n",
"256913 Can Mintop 5% Lotion be used by women for seve... \n",
"256914 Is Minoxin 5% lotion advisable instead of Foli... \n",
"256915 Are Biotin supplements need to reduce severe h... \n",
"\n",
" Patient \\\n",
"0 Hi doctor,I am just wondering what is abutting... \n",
"1 Hi doctor, I am a 22-year-old female who was d... \n",
"2 Hi doctor! I used to have clear skin but since... \n",
"3 Hello doctor,I am having an uncomfortable feel... \n",
"4 Hello doctor,Before two years had sex with a c... \n",
"... ... \n",
"256911 I am suffering from excessive hairfall. My doc... \n",
"256912 Hi Doctor, I have been having severe hair fall... \n",
"256913 Hi..i hav sever hair loss problem so consulted... \n",
"256914 Hi, i am 25 year old girl, i am having massive... \n",
"256915 iam having hairfall for a decade.. but fews we... \n",
"\n",
" Answer \n",
"0 Hi. I have gone through your query with dilige... \n",
"1 Hi. You have really done well with the hypothy... \n",
"2 Hi there Acne has multifactorial etiology. Onl... \n",
"3 Hello. The popping and discomfort what you fel... \n",
"4 Hello. The HIV test uses a finger prick blood ... \n",
"... ... \n",
"256911 Hello Dear Thanks for writing to us, we are he... \n",
"256912 hello, hair4u is combination of minoxid... \n",
"256913 HI I have evaluated your query thoroughly you... \n",
"256914 Hello and Welcome to ‘Ask A Doctor’ service.I ... \n",
"256915 you did'nt mention about thyroid problem ...us... \n",
"\n",
"[256916 rows x 3 columns]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Question</th>\n",
" <th>Patient</th>\n",
" <th>Answer</th>\n",
" <th>combined</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Q. What does abutment of the nerve root mean?</td>\n",
" <td>Hi doctor,I am just wondering what is abutting...</td>\n",
" <td>Hi. I have gone through your query with dilige...</td>\n",
" <td>Question: Q. What does abutment of the nerve r...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Q. What should I do to reduce my weight gained...</td>\n",
" <td>Hi doctor, I am a 22-year-old female who was d...</td>\n",
" <td>Hi. You have really done well with the hypothy...</td>\n",
" <td>Question: Q. What should I do to reduce my wei...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Question \\\n",
"0 Q. What does abutment of the nerve root mean? \n",
"1 Q. What should I do to reduce my weight gained... \n",
"\n",
" Patient \\\n",
"0 Hi doctor,I am just wondering what is abutting... \n",
"1 Hi doctor, I am a 22-year-old female who was d... \n",
"\n",
" Answer \\\n",
"0 Hi. I have gone through your query with dilige... \n",
"1 Hi. You have really done well with the hypothy... \n",
"\n",
" combined \n",
"0 Question: Q. What does abutment of the nerve r... \n",
"1 Question: Q. What should I do to reduce my wei... "
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[\"combined\"] = (\n",
" \"Question: \" + df.Question.str.strip() + \"; Patient: \" + df.Patient.str.strip()+ \"; Answer: \" + df.Answer.str.strip()\n",
")\n",
"df.head(2)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"#df[\"combined\"] = ( \"Description: \" + df.Description.str.strip() + \"; Patient: \" + df.Patient.str.strip())\n",
"#df.head(2)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"# subsample to 1k most recent reviews and remove samples that are too long\n",
"top_n = df.shape[0]\n",
"#df = df.tail(top_n * 2) # first cut to first 2k entries, assuming less than half will be filtered out"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"256916"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"encoding = tiktoken.get_encoding(embedding_encoding)\n",
"# omit reviews that are too long to embed\n",
"df[\"n_tokens\"] = df.combined.apply(lambda x: len(encoding.encode(x)))\n",
"df = df[df.n_tokens <= max_tokens].tail(top_n)\n",
"len(df)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Description</th>\n",
" <th>Patient</th>\n",
" <th>Doctor</th>\n",
" <th>combined</th>\n",
" <th>n_tokens</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Q. What does abutment of the nerve root mean?</td>\n",
" <td>Hi doctor,I am just wondering what is abutting...</td>\n",
" <td>Hi. I have gone through your query with dilige...</td>\n",
" <td>Description: Q. What does abutment of the nerv...</td>\n",
" <td>95</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Q. What should I do to reduce my weight gained...</td>\n",
" <td>Hi doctor, I am a 22-year-old female who was d...</td>\n",
" <td>Hi. You have really done well with the hypothy...</td>\n",
" <td>Description: Q. What should I do to reduce my ...</td>\n",
" <td>519</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Q. I have started to get lots of acne on my fa...</td>\n",
" <td>Hi doctor! I used to have clear skin but since...</td>\n",
" <td>Hi there Acne has multifactorial etiology. Onl...</td>\n",
" <td>Description: Q. I have started to get lots of ...</td>\n",
" <td>285</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Q. Why do I have uncomfortable feeling between...</td>\n",
" <td>Hello doctor,I am having an uncomfortable feel...</td>\n",
" <td>Hello. The popping and discomfort what you fel...</td>\n",
" <td>Description: Q. Why do I have uncomfortable fe...</td>\n",
" <td>324</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Q. My symptoms after intercourse threatns me e...</td>\n",
" <td>Hello doctor,Before two years had sex with a c...</td>\n",
" <td>Hello. The HIV test uses a finger prick blood ...</td>\n",
" <td>Description: Q. My symptoms after intercourse ...</td>\n",
" <td>442</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>256911</th>\n",
" <td>Why is hair fall increasing while using Bontre...</td>\n",
" <td>I am suffering from excessive hairfall. My doc...</td>\n",
" <td>Hello Dear Thanks for writing to us, we are he...</td>\n",
" <td>Description: Why is hair fall increasing while...</td>\n",
" <td>211</td>\n",
" </tr>\n",
" <tr>\n",
" <th>256912</th>\n",
" <td>Why was I asked to discontinue Androanagen whi...</td>\n",
" <td>Hi Doctor, I have been having severe hair fall...</td>\n",
" <td>hello, hair4u is combination of minoxid...</td>\n",
" <td>Description: Why was I asked to discontinue An...</td>\n",
" <td>154</td>\n",
" </tr>\n",
" <tr>\n",
" <th>256913</th>\n",
" <td>Can Mintop 5% Lotion be used by women for seve...</td>\n",
" <td>Hi..i hav sever hair loss problem so consulted...</td>\n",
" <td>HI I have evaluated your query thoroughly you...</td>\n",
" <td>Description: Can Mintop 5% Lotion be used by w...</td>\n",
" <td>191</td>\n",
" </tr>\n",
" <tr>\n",
" <th>256914</th>\n",
" <td>Is Minoxin 5% lotion advisable instead of Foli...</td>\n",
" <td>Hi, i am 25 year old girl, i am having massive...</td>\n",
" <td>Hello and Welcome to ‘Ask A Doctor’ service.I ...</td>\n",
" <td>Description: Is Minoxin 5% lotion advisable in...</td>\n",
" <td>232</td>\n",
" </tr>\n",
" <tr>\n",
" <th>256915</th>\n",
" <td>Are Biotin supplements need to reduce severe h...</td>\n",
" <td>iam having hairfall for a decade.. but fews we...</td>\n",
" <td>you did'nt mention about thyroid problem ...us...</td>\n",
" <td>Description: Are Biotin supplements need to re...</td>\n",
" <td>213</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>256916 rows × 5 columns</p>\n",
"</div>"
],
"text/plain": [
" Description \\\n",
"0 Q. What does abutment of the nerve root mean? \n",
"1 Q. What should I do to reduce my weight gained... \n",
"2 Q. I have started to get lots of acne on my fa... \n",
"3 Q. Why do I have uncomfortable feeling between... \n",
"4 Q. My symptoms after intercourse threatns me e... \n",
"... ... \n",
"256911 Why is hair fall increasing while using Bontre... \n",
"256912 Why was I asked to discontinue Androanagen whi... \n",
"256913 Can Mintop 5% Lotion be used by women for seve... \n",
"256914 Is Minoxin 5% lotion advisable instead of Foli... \n",
"256915 Are Biotin supplements need to reduce severe h... \n",
"\n",
" Patient \\\n",
"0 Hi doctor,I am just wondering what is abutting... \n",
"1 Hi doctor, I am a 22-year-old female who was d... \n",
"2 Hi doctor! I used to have clear skin but since... \n",
"3 Hello doctor,I am having an uncomfortable feel... \n",
"4 Hello doctor,Before two years had sex with a c... \n",
"... ... \n",
"256911 I am suffering from excessive hairfall. My doc... \n",
"256912 Hi Doctor, I have been having severe hair fall... \n",
"256913 Hi..i hav sever hair loss problem so consulted... \n",
"256914 Hi, i am 25 year old girl, i am having massive... \n",
"256915 iam having hairfall for a decade.. but fews we... \n",
"\n",
" Doctor \\\n",
"0 Hi. I have gone through your query with dilige... \n",
"1 Hi. You have really done well with the hypothy... \n",
"2 Hi there Acne has multifactorial etiology. Onl... \n",
"3 Hello. The popping and discomfort what you fel... \n",
"4 Hello. The HIV test uses a finger prick blood ... \n",
"... ... \n",
"256911 Hello Dear Thanks for writing to us, we are he... \n",
"256912 hello, hair4u is combination of minoxid... \n",
"256913 HI I have evaluated your query thoroughly you... \n",
"256914 Hello and Welcome to ‘Ask A Doctor’ service.I ... \n",
"256915 you did'nt mention about thyroid problem ...us... \n",
"\n",
" combined n_tokens \n",
"0 Description: Q. What does abutment of the nerv... 95 \n",
"1 Description: Q. What should I do to reduce my ... 519 \n",
"2 Description: Q. I have started to get lots of ... 285 \n",
"3 Description: Q. Why do I have uncomfortable fe... 324 \n",
"4 Description: Q. My symptoms after intercourse ... 442 \n",
"... ... ... \n",
"256911 Description: Why is hair fall increasing while... 211 \n",
"256912 Description: Why was I asked to discontinue An... 154 \n",
"256913 Description: Can Mintop 5% Lotion be used by w... 191 \n",
"256914 Description: Is Minoxin 5% lotion advisable in... 232 \n",
"256915 Description: Are Biotin supplements need to re... 213 \n",
"\n",
"[256916 rows x 5 columns]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are different ways to convert text into a vector or into embeddings.\n",
"\n",
"Unfortunately, most good methods to get embeddings in Python are not free.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Get embeddings using SentenceTransformers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us use SentenceTransformers, a Python framework for state-of-the-art sentence, text, and image embeddings. The initial work is described in our paper Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First we verify that Torch is CUDA capable"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import torch\n",
"torch.cuda.is_available()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We define our list of sentences. You can use a larger list (it is best to use a list of sentences for easier processing of each sentence)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can install Sentence BERT using:\n",
"`!pip install sentence-transformers`\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Step 1: We will then load the pre-trained BERT model. There are many other pre-trained models available."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"from sentence_transformers import SentenceTransformer\n",
"sbert_model = SentenceTransformer('bert-base-nli-mean-tokens')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We proceed to test the embeding creation"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"from sentence_transformers import SentenceTransformer\n",
"model = SentenceTransformer('paraphrase-MiniLM-L6-v2')\n",
"#Sentences we want to encode. Example:\n",
"sentence = ['This framework generates embeddings for each input sentence']\n",
"#Sentences are encoded by calling model.encode()\n",
"embedding = model.encode(sentence)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['This framework generates embeddings for each input sentence']"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sentence"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"def get_embeddings(x,transformer='paraphrase-MiniLM-L6-v2'):\n",
" model = SentenceTransformer(transformer)\n",
" #Sentences we want to encode\n",
" sentence =x\n",
" #Sentences are encoded by calling model.encode()\n",
" embedding = model.encode(sentence)\n",
" return embedding"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"# This may take a few minutes\n",
"embedding_mod='paraphrase-MiniLM-L6-v2'\n",
"#df[\"embedding\"] = df.combined.apply(lambda x: get_embeddings(x, transformer=embedding_mod))"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"df=df.head(1000)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"#embedding_doctor\n",
"# This may take a few minutes\n",
"df[\"embedding\"] = df.Answer.apply(lambda x: get_embeddings(x, transformer=embedding_mod))"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Question</th>\n",
" <th>Patient</th>\n",
" <th>Answer</th>\n",
" <th>combined</th>\n",
" <th>n_tokens</th>\n",
" <th>embedding</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Q. What does abutment of the nerve root mean?</td>\n",
" <td>Hi doctor,I am just wondering what is abutting...</td>\n",
" <td>Hi. I have gone through your query with dilige...</td>\n",
" <td>Question: Q. What does abutment of the nerve r...</td>\n",
" <td>95</td>\n",
" <td>[-0.109211065, -0.17469415, 0.18996556, 0.0599...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Q. What should I do to reduce my weight gained...</td>\n",
" <td>Hi doctor, I am a 22-year-old female who was d...</td>\n",
" <td>Hi. You have really done well with the hypothy...</td>\n",
" <td>Question: Q. What should I do to reduce my wei...</td>\n",
" <td>519</td>\n",
" <td>[-0.014065318, 0.0440334, 0.26095688, 0.086799...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Q. I have started to get lots of acne on my fa...</td>\n",
" <td>Hi doctor! I used to have clear skin but since...</td>\n",
" <td>Hi there Acne has multifactorial etiology. Onl...</td>\n",
" <td>Question: Q. I have started to get lots of acn...</td>\n",
" <td>285</td>\n",
" <td>[-0.39175138, -0.025890486, -0.024644196, -0.0...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Q. Why do I have uncomfortable feeling between...</td>\n",
" <td>Hello doctor,I am having an uncomfortable feel...</td>\n",
" <td>Hello. The popping and discomfort what you fel...</td>\n",
" <td>Question: Q. Why do I have uncomfortable feeli...</td>\n",
" <td>324</td>\n",
" <td>[-0.29406005, -0.31878802, 0.27588362, 0.09649...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Q. My symptoms after intercourse threatns me e...</td>\n",
" <td>Hello doctor,Before two years had sex with a c...</td>\n",
" <td>Hello. The HIV test uses a finger prick blood ...</td>\n",
" <td>Question: Q. My symptoms after intercourse thr...</td>\n",
" <td>442</td>\n",
" <td>[-0.36187398, 0.18491694, -0.3090741, -0.30197...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>995</th>\n",
" <td>Q. My lax les is 38 cm with inflamed gastric f...</td>\n",
" <td>Hello doctor, My lax les is 38 cm with inflame...</td>\n",
" <td>Hello. Gastritis is an inflammation of stomach...</td>\n",
" <td>Question: Q. My lax les is 38 cm with inflamed...</td>\n",
" <td>214</td>\n",
" <td>[-0.1555396, -0.44157797, -0.15364785, 0.25760...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>996</th>\n",
" <td>Q. I am suffering from mood swings. Kindly adv...</td>\n",
" <td>Hello doctor,I want to get some information re...</td>\n",
" <td>Hello. Let me answer your questions via some b...</td>\n",
" <td>Question: Q. I am suffering from mood swings. ...</td>\n",
" <td>491</td>\n",
" <td>[-0.2296337, 0.119730674, 0.37153018, 0.062901...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>997</th>\n",
" <td>Q. I am having swollen lymph node in my neck. ...</td>\n",
" <td>Hello doctor, I went to the chiropractor and g...</td>\n",
" <td>Hello. I do not think that because of chiropra...</td>\n",
" <td>Question: Q. I am having swollen lymph node in...</td>\n",
" <td>395</td>\n",
" <td>[-0.10149522, -0.33532476, 0.40812746, -0.2713...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>998</th>\n",
" <td>Q. How good is Albenza for a raccoon roundworm...</td>\n",
" <td>Hello doctor,I am concerned about a possible r...</td>\n",
" <td>Hello. Albendazole 400 mg single star dose is ...</td>\n",
" <td>Question: Q. How good is Albenza for a raccoon...</td>\n",
" <td>240</td>\n",
" <td>[-0.06408733, 0.17669381, 0.09132431, -0.09456...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>999</th>\n",
" <td>Q. Will Kalarchikai cure multiple ovarian cyst...</td>\n",
" <td>Hello doctor, I have multiple small cysts in b...</td>\n",
" <td>Hello. I just read your query. See Kalarachi K...</td>\n",
" <td>Question: Q. Will Kalarchikai cure multiple ov...</td>\n",
" <td>309</td>\n",
" <td>[0.03657364, 0.24297515, 0.09555141, 0.0270566...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1000 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" Question \\\n",
"0 Q. What does abutment of the nerve root mean? \n",
"1 Q. What should I do to reduce my weight gained... \n",
"2 Q. I have started to get lots of acne on my fa... \n",
"3 Q. Why do I have uncomfortable feeling between... \n",
"4 Q. My symptoms after intercourse threatns me e... \n",
".. ... \n",
"995 Q. My lax les is 38 cm with inflamed gastric f... \n",
"996 Q. I am suffering from mood swings. Kindly adv... \n",
"997 Q. I am having swollen lymph node in my neck. ... \n",
"998 Q. How good is Albenza for a raccoon roundworm... \n",
"999 Q. Will Kalarchikai cure multiple ovarian cyst... \n",
"\n",
" Patient \\\n",
"0 Hi doctor,I am just wondering what is abutting... \n",
"1 Hi doctor, I am a 22-year-old female who was d... \n",
"2 Hi doctor! I used to have clear skin but since... \n",
"3 Hello doctor,I am having an uncomfortable feel... \n",
"4 Hello doctor,Before two years had sex with a c... \n",
".. ... \n",
"995 Hello doctor, My lax les is 38 cm with inflame... \n",
"996 Hello doctor,I want to get some information re... \n",
"997 Hello doctor, I went to the chiropractor and g... \n",
"998 Hello doctor,I am concerned about a possible r... \n",
"999 Hello doctor, I have multiple small cysts in b... \n",
"\n",
" Answer \\\n",
"0 Hi. I have gone through your query with dilige... \n",
"1 Hi. You have really done well with the hypothy... \n",
"2 Hi there Acne has multifactorial etiology. Onl... \n",
"3 Hello. The popping and discomfort what you fel... \n",
"4 Hello. The HIV test uses a finger prick blood ... \n",
".. ... \n",
"995 Hello. Gastritis is an inflammation of stomach... \n",
"996 Hello. Let me answer your questions via some b... \n",
"997 Hello. I do not think that because of chiropra... \n",
"998 Hello. Albendazole 400 mg single star dose is ... \n",
"999 Hello. I just read your query. See Kalarachi K... \n",
"\n",
" combined n_tokens \\\n",
"0 Question: Q. What does abutment of the nerve r... 95 \n",
"1 Question: Q. What should I do to reduce my wei... 519 \n",
"2 Question: Q. I have started to get lots of acn... 285 \n",
"3 Question: Q. Why do I have uncomfortable feeli... 324 \n",
"4 Question: Q. My symptoms after intercourse thr... 442 \n",
".. ... ... \n",
"995 Question: Q. My lax les is 38 cm with inflamed... 214 \n",
"996 Question: Q. I am suffering from mood swings. ... 491 \n",
"997 Question: Q. I am having swollen lymph node in... 395 \n",
"998 Question: Q. How good is Albenza for a raccoon... 240 \n",
"999 Question: Q. Will Kalarchikai cure multiple ov... 309 \n",
"\n",
" embedding \n",
"0 [-0.109211065, -0.17469415, 0.18996556, 0.0599... \n",
"1 [-0.014065318, 0.0440334, 0.26095688, 0.086799... \n",
"2 [-0.39175138, -0.025890486, -0.024644196, -0.0... \n",
"3 [-0.29406005, -0.31878802, 0.27588362, 0.09649... \n",
"4 [-0.36187398, 0.18491694, -0.3090741, -0.30197... \n",
".. ... \n",
"995 [-0.1555396, -0.44157797, -0.15364785, 0.25760... \n",
"996 [-0.2296337, 0.119730674, 0.37153018, 0.062901... \n",
"997 [-0.10149522, -0.33532476, 0.40812746, -0.2713... \n",
"998 [-0.06408733, 0.17669381, 0.09132431, -0.09456... \n",
"999 [0.03657364, 0.24297515, 0.09555141, 0.0270566... \n",
"\n",
"[1000 rows x 6 columns]"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"from ast import literal_eval\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"df[\"embedding\"] = df.embedding.apply(np.array) # convert string to numpy array"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"#df[\"embedding_doctor\"] = df.embedding_doctor.apply(np.array) # convert string to numpy array"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"df.to_pickle(\"../2-Data/dialogues_embededd.pkl\")"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"#df.to_csv(\"../2-Data/dialogues_embededd.csv\", sep = '\\t', encoding='utf-8', index=False)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Get embeddings using OpenAI (optional)\n",
"If we have a subscription in OpenAI, you can follow the following steps.\n",
"Is optional, we are going to use the previous method."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"# Python program to read\n",
"# json file\n",
"import json\n",
"# Opening JSON file\n",
"f = open('./credentials/api.json')\n",
"# returns JSON object as\n",
"# a dictionary\n",
"data = json.load(f)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"# Ensure you have your API key set in your environment per the README: https://github.com/openai/openai-python#usage\n",
"import openai\n",
"openai.api_key = data['OPENAI_API_KEY']\n",
"# Closing file\n",
"f.close()"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"# This may take a few minutes\n",
"df[\"embedding\"] = df.combined.apply(lambda x: get_embedding(x, engine=embedding_model))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.to_csv(\"../2-Data/dialogues_embededd_openai.csv\", sep='\\t', encoding='utf-8', index=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Additional Notes (not neeeded)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"# list of text documents\n",
"text = [\"I am doga.\",\n",
" \"I am a dog\"]\n",
"# create the transform\n",
"vectorizer = TfidfVectorizer()\n",
"# tokenize and build vocab\n",
"vectorizer.fit(text)\n",
"# summarize\n",
"print(vectorizer.vocabulary_)\n",
"print(vectorizer.idf_)\n",
"# encode document\n",
"vector = vectorizer.transform([text[0]])\n",
"# summarize encoded vector\n",
"print(vector.shape)\n",
"print(vector.toarray())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.feature_extraction.text import HashingVectorizer\n",
"# list of text documents\n",
"text = [\"I am doc.\", \"I am dog\"]\n",
"# create the transform\n",
"vectorizer = HashingVectorizer(n_features=20)\n",
"# encode document\n",
"vector = vectorizer.transform(text)\n",
"# summarize encoded vector\n",
"print(vector.shape)\n",
"print(vector.toarray())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python3 (GPT)",
"language": "python",
"name": "gpt"
},
"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.10.11"
},
"vscode": {
"interpreter": {
"hash": "365536dcbde60510dc9073d6b991cd35db2d9bac356a11f5b64279a5e6708b97"
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|