Spaces:
Sleeping
Sleeping
File size: 34,537 Bytes
03287bc |
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 |
{
"cells": [
{
"cell_type": "markdown",
"id": "8ea54fcd-ef4a-42cb-ae26-cbdc6f6ffc64",
"metadata": {
"tags": []
},
"source": [
"# Duct Tape Pipeline\n",
"To explore how users may interact with interactive visualizations of counterfactuals for evolving the Interactive Model Card, we will need to first find a way to generate counterfactuals based on a given input. We want the user to be able to provide their input and direct the system to generate counterfactuals based on a part of speech that is significant to the model. The system should then provide a data frame of counterfactuals to be used in an interactive visualization. Below is an example wireframe of the experience based on previous research.\n",
"\n",
"![wireframe](Assets/VizNLC-Wireframe-example.png)\n",
"\n",
"## Goals of this notebook\n",
"* Test which libraries (Ex. [spaCy](https://spacy.io/) and [NLTK](https://www.nltk.org/)) will work\n",
"* Identify defaults to use\n",
"* Build a rudimentary script for generating counterfactuals from user input\n",
"* Ensure the counterfactuals are in a useable format for visualization"
]
},
{
"cell_type": "markdown",
"id": "736e6375-dd6d-4188-b8b1-92bded2bcd02",
"metadata": {},
"source": [
"## Loading the libraries and models"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7f581785-e642-4f74-9f67-06a63820eaf2",
"metadata": {},
"outputs": [],
"source": [
"#Import the libraries we know we'll need for the Generator.\n",
"import pandas as pd, spacy, nltk, numpy as np\n",
"from spacy import displacy\n",
"from spacy.matcher import Matcher\n",
"#!python -m spacy download en_core_web_sm\n",
"nlp = spacy.load(\"en_core_web_sm\")\n",
"lemmatizer = nlp.get_pipe(\"lemmatizer\")\n",
"\n",
"#Import the libraries to support the model, predictions, and LIME.\n",
"from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline\n",
"import lime\n",
"import torch\n",
"import torch.nn.functional as F\n",
"from lime.lime_text import LimeTextExplainer\n",
"\n",
"#Import the libraries for generating interactive visualizations.\n",
"import altair as alt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cbe2b292-e33e-4915-8e61-bba5327fb643",
"metadata": {},
"outputs": [],
"source": [
"#Defining all necessary variables and instances.\n",
"tokenizer = AutoTokenizer.from_pretrained(\"distilbert-base-uncased-finetuned-sst-2-english\")\n",
"model = AutoModelForSequenceClassification.from_pretrained(\"distilbert-base-uncased-finetuned-sst-2-english\")\n",
"class_names = ['negative', 'positive']\n",
"explainer = LimeTextExplainer(class_names=class_names)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "197c3e26-0fdf-49c6-9135-57f1fd55d3e3",
"metadata": {},
"outputs": [],
"source": [
"#Defining a Predictor required for LIME to function.\n",
"def predictor(texts):\n",
" outputs = model(**tokenizer(texts, return_tensors=\"pt\", padding=True))\n",
" probas = F.softmax(outputs.logits, dim=1).detach().numpy()\n",
" return probas"
]
},
{
"cell_type": "markdown",
"id": "e731dcbb-4fcf-41c6-9493-edef02fdb1b6",
"metadata": {},
"source": [
"## Exploring concepts to see what might work\n",
"To begin building the pipeline I started by identifying whether or not I needed to build my own matcher or if spaCy has something built in that would allow us to make it easier. Having to build our own matcher, to account for each of the possible patterns, would be exceptionally cumbersome with all of the variations we need to look out for. Instead, I found that using the built in `noun_chunks` attribute allows for a simplification to the parts of speech we most care about. \n",
"* I built a few helper functions from tutorials to explore the parts-of-speech within given sentences and the way `noun_chunks` work\n",
"* I explore dusing `displacy` as a means of visualizing sentences to call out what the pre-trained models already understand"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1f2eca3c-525c-4e29-8cc1-c87e89a3fadf",
"metadata": {},
"outputs": [],
"source": [
"#A quick test of Noun Chunks\n",
"text = \"The movie was filmed in New Zealand.\"\n",
"doc = nlp(text)\n",
"def n_chunk(doc):\n",
" for chunk in doc.noun_chunks:\n",
" print(f\"Text: {chunk.text:<12}| Root:{chunk.root.text:<12}| Root Dependency: {chunk.root.dep_:<12}| Root Head: {chunk.root.head.text:<12}\")\n",
"n_chunk(doc)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "98978c29-a39c-48e3-bdbb-b74388ded6bc",
"metadata": {},
"outputs": [],
"source": [
"#The user will need to enter text. For now, we're going to provide a series of sentences generated to have things we care about. For clarity \"upt\" means \"user provide text\".\n",
"upt1 = \"I like movies starring black actors.\"\n",
"upt2 = \"I am a black trans-woman.\"\n",
"upt3 = \"Native Americans deserve to have their land back.\"\n",
"upt4 = \"This movie was filmed in Iraq.\"\n",
"\n",
"#Here I provide a larger text with mixed messages one sentence per line.\n",
"text1 = (\n",
"\"I like movies starring black actors.\"\n",
"\"I am a black trans-woman.\"\n",
"\"Native Americans deserve to have their land back.\"\n",
"\"This movie was filmed in Iraq.\"\n",
"\"The Chinese cat and the African bat walked into a Jamaican bar.\"\n",
"\"There once was a flexible pole that met an imovable object.\"\n",
"\"A Catholic nun, a Buddhist monk, a satanic cultist, and a Wiccan walk into your garage.\")\n",
"\n",
"doc1 = nlp(upt1)\n",
"doc2 = nlp(upt2)\n",
"doc3 = nlp(upt3)\n",
"doc4 = nlp(upt4)\n",
"doct = nlp(text1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "38023eca-b224-412d-aa71-02bd694530e0",
"metadata": {},
"outputs": [],
"source": [
"#Using displacy to explore how the NLP model views sentences.\n",
"displacy.render(doc, style=\"ent\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c28edec8-dc30-4ef9-8c1e-131b0e1b1a45",
"metadata": {},
"outputs": [],
"source": [
"#Another visual for understanding how the model views sentences.\n",
"displacy.render(doc, style=\"dep\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "dd0d5f8e-ee80-48f7-be92-effa5f84c723",
"metadata": {},
"outputs": [],
"source": [
"#A simple token to print out the \n",
"def text_pos(doc):\n",
" for token in doc:\n",
" # Get the token text, part-of-speech tag and dependency label\n",
" token_text = token.text\n",
" token_pos = token.pos_\n",
" token_dep = token.dep_\n",
" token_ent = token.ent_type_\n",
" token_morph = token.morph\n",
" # This is for formatting only\n",
" print(f\"Text: {token_text:<12}| Part of Speech: {token_pos:<10}| Dependency: {token_dep:<10}| Entity: {token_ent:<10} | Morph: {token_morph}\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "5dfee095-3852-4dba-a7dc-5519e8ec6eaa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Text: Who | Part of Speech: PRON | Dependency: nsubj | Entity: | Morph: \n",
"Text: put | Part of Speech: VERB | Dependency: ROOT | Entity: | Morph: Tense=Past|VerbForm=Fin\n",
"Text: a | Part of Speech: DET | Dependency: det | Entity: | Morph: Definite=Ind|PronType=Art\n",
"Text: tiny | Part of Speech: ADJ | Dependency: amod | Entity: | Morph: Degree=Pos\n",
"Text: pickle | Part of Speech: NOUN | Dependency: dobj | Entity: | Morph: Number=Sing\n",
"Text: in | Part of Speech: ADP | Dependency: prep | Entity: | Morph: \n",
"Text: the | Part of Speech: DET | Dependency: det | Entity: | Morph: Definite=Def|PronType=Art\n",
"Text: jar | Part of Speech: NOUN | Dependency: pobj | Entity: | Morph: Number=Sing\n"
]
}
],
"source": [
"x = nlp(\"Who put a tiny pickle in the jar\")\n",
"text_pos(x)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "2485d88d-2dd4-4fa3-9d62-4dcbec4e9138",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(x[0].morph)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "013af6ac-f7d1-41d2-a601-b0f9a4870815",
"metadata": {},
"outputs": [],
"source": [
"#Instantiate a matcher and use it to test some patterns.\n",
"matcher = Matcher(nlp.vocab)\n",
"pattern = [{\"ENT_TYPE\": {\"IN\":[\"NORP\",\"GPE\"]}}]\n",
"matcher.add(\"proper_noun\", [pattern])\n",
"pattern_test = [{\"DEP\": \"amod\"},{\"DEP\":\"attr\"},{\"TEXT\":\"-\"},{\"DEP\":\"attr\",\"OP\":\"+\"}]\n",
"matcher.add(\"amod_attr\",[pattern_test])\n",
"pattern_an = [{\"DEP\": \"amod\"},{\"POS\":{\"IN\":[\"NOUN\",\"PROPN\"]}},{\"DEP\":{\"NOT_IN\":[\"attr\"]}}]\n",
"matcher.add(\"amod_noun\", [pattern_an])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6ac821d-7b56-446e-b9ca-42a5f5afd198",
"metadata": {},
"outputs": [],
"source": [
"def match_this(matcher, doc):\n",
" matches = matcher(doc)\n",
" for match_id, start, end in matches:\n",
" matched_span = doc[start:end]\n",
" print(f\"Mached {matched_span.text} by the rule {nlp.vocab.strings[match_id]}.\")\n",
" return matches"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "958e4dc8-6652-4f32-b7ae-6aa5ee287cf7",
"metadata": {},
"outputs": [],
"source": [
"match_this(matcher, doct)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5bf40fa5-b636-47f7-98b2-e872c78e7114",
"metadata": {},
"outputs": [],
"source": [
"text_pos(doc3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c5365304-5edb-428d-abf5-d579dcfbc269",
"metadata": {},
"outputs": [],
"source": [
"n_chunk(doc3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7f3d3c8-65a1-433f-a47c-adcaaa2353e2",
"metadata": {},
"outputs": [],
"source": [
"displacy.render(doct, style=\"ent\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "84df8e30-d142-4e5b-b3a9-02e3133ceba9",
"metadata": {},
"outputs": [],
"source": [
"txt = \"Savannah is a city in Georgia, in the United States\"\n",
"doc = nlp(txt)\n",
"displacy.render(doc, style=\"ent\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4a85f713-92bc-48ba-851e-de627d7e8c77",
"metadata": {},
"outputs": [],
"source": [
"displacy.render(doc2, style='dep')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "032f1134-7560-400b-824b-bc0196058b66",
"metadata": {},
"outputs": [],
"source": [
"n_chunk(doct)"
]
},
{
"cell_type": "markdown",
"id": "188044a1-4cf4-4141-a520-c5f11198aed8",
"metadata": {},
"source": [
"* The Model does not recognize `wiccan` as a NORP but it will recognize `Wiccan` as NORP\n",
"* The Model does not know what to do with `-` and makes a mess of `trans-woman` because of this"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2dc82250-e26e-49d5-a7f2-d4eeda170e4e",
"metadata": {},
"outputs": [],
"source": [
"chunks = list(doc1.noun_chunks)\n",
"print(chunks[-1][-2].pos_)"
]
},
{
"cell_type": "markdown",
"id": "c23d48c4-f5ab-4428-9244-0786e9903a8e",
"metadata": {},
"source": [
"## Building the Duct-Tape Pipeline cell-by-cell"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ed22421-4401-482e-b54a-ee70d3187037",
"metadata": {},
"outputs": [],
"source": [
"#Lists of important words\n",
"gender = [\"man\", \"woman\",\"girl\",\"boy\",\"male\",\"female\",\"husband\",\"wife\",\"girlfriend\",\"boyfriend\",\"brother\",\"sister\",\"aunt\",\"uncle\",\"grandma\",\"grandpa\",\"granny\",\"granps\",\"grandmother\",\"grandfather\",\"mama\",\"dada\",\"Ma\",\"Pa\",\"lady\",\"gentleman\"]\n",
"#consider pulling ethnicities from https://github.com/cgio/global-ethnicities"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b02a5d4-8a6b-4e5e-8f15-4f9182fe341f",
"metadata": {},
"outputs": [],
"source": [
"def select_crit(document, options=False, limelist=False):\n",
" '''This function is meant to select the critical part of a sentence. Critical, in this context means\n",
" the part of the sentence that is either: A) a PROPN from the correct entity group; B) an ADJ associated with a NOUN;\n",
" C) a NOUN that represents gender. It also checks this against what the model thinks is important if the user defines \"options\" as \"LIME\" or True.'''\n",
" chunks = list(document.noun_chunks)\n",
" pos_options = []\n",
" lime_options = []\n",
" \n",
" #Identify what the model cares about.\n",
" if options:\n",
" exp = explainer.explain_instance(document.text, predictor, num_features=20, num_samples=2000)\n",
" results = exp.as_list()[:10]\n",
" #prints the results from lime for QA.\n",
" if limelist == True:\n",
" print(results)\n",
" for feature in results:\n",
" lime_options.append(feature[0])\n",
" \n",
" #Identify what we care about \"parts of speech\"\n",
" for chunk in chunks:\n",
" #The use of chunk[-1] is due to testing that it appears to always match the root\n",
" root = chunk[-1]\n",
" #This currently matches to a list I've created. I don't know the best way to deal with this so I'm leaving it as is for the moment.\n",
" if root.text.lower() in gender:\n",
" cur_values = [token.text for token in chunk if token.pos_ in [\"NOUN\",\"ADJ\"]]\n",
" if (all(elem in lime_options for elem in cur_values) and ((options == \"LIME\") or (options == True))) or ((options != \"LIME\") and (options != True)):\n",
" pos_options.extend(cur_values)\n",
" #print(f\"From {chunk.text}, {cur_values} added to pos_options due to gender.\") #for QA\n",
" #This is currently set to pick up entities in a particular set of groups (which I recently expanded). Should it just pick up all named entities?\n",
" elif root.ent_type_ in [\"GPE\",\"NORP\",\"DATE\",\"EVENT\"]:\n",
" cur_values = []\n",
" if (len(chunk) > 1) and (chunk[-2].dep_ == \"compound\"):\n",
" #creates the compound element of the noun\n",
" compound = [x.text for x in chunk if x.dep_ == \"compound\"]\n",
" print(f\"This is the contents of {compound} and it is {all(elem in lime_options for elem in compound)} that all elements are present in {lime_options}.\") #for QA\n",
" #checks to see all elements in the compound are important to the model or use the compound if not checking importance.\n",
" if (all(elem in lime_options for elem in compound) and ((options == \"LIME\") or (options == True))) or ((options != \"LIME\") and (options != True)):\n",
" #creates a span for the entirety of the compound noun and adds it to the list.\n",
" span = -1 * (1 + len(compound))\n",
" pos_options.append(chunk[span:].text)\n",
" cur_values + [token.text for token in chunk if token.pos_ == \"ADJ\"]\n",
" else: \n",
" cur_values = [token.text for token in chunk if (token.ent_type_ in [\"GPE\",\"NORP\",\"DATE\",\"EVENT\"]) or (token.pos_ == \"ADJ\")]\n",
" if (all(elem in lime_options for elem in cur_values) and ((options == \"LIME\") or (options == True))) or ((options != \"LIME\") and (options != True)):\n",
" pos_options.extend(cur_values)\n",
" print(f\"From {chunk.text}, {cur_values} and {pos_options} added to pos_options due to entity recognition.\") #for QA\n",
" elif len(chunk) > 1:\n",
" cur_values = [token.text for token in chunk if token.pos_ in [\"NOUN\",\"ADJ\"]]\n",
" if (all(elem in lime_options for elem in cur_values) and ((options == \"LIME\") or (options == True))) or ((options != \"LIME\") and (options != True)):\n",
" pos_options.extend(cur_values)\n",
" print(f\"From {chunk.text}, {cur_values} added to pos_options due to wildcard.\") #for QA\n",
" else:\n",
" print(f\"No options added for \\'{chunk.text}\\' \")\n",
" \n",
" #Return the correct set of options based on user input, defaults to POS for simplicity.\n",
" if options == \"LIME\":\n",
" return lime_options\n",
" else:\n",
" return pos_options"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa95e9fe-36ea-4b95-ab51-6bb82f745c23",
"metadata": {},
"outputs": [],
"source": [
"#Testing a method to make sure I had the ability to match one list inside the other. Now incorporated in the above function's logic.\n",
"one = ['a','b','c']\n",
"two = ['a','c']\n",
"all(elem in one for elem in two)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d43e202e-64b9-4cea-b117-82492c9ee5f4",
"metadata": {},
"outputs": [],
"source": [
"#Test to make sure all three options work\n",
"pos4 = select_crit(doc4)\n",
"lime4 = select_crit(doc4,options=\"LIME\")\n",
"final4 = select_crit(doc4,options=True,limelist=True)\n",
"print(pos4, lime4, final4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5623015e-fdb2-44f0-b5ac-812203b639b3",
"metadata": {},
"outputs": [],
"source": [
"#This is a test to make sure compounds of any length are captured. \n",
"txt = \"I went to Papua New Guinea for Christmas Eve and New Years.\"\n",
"doc_t = nlp(txt)\n",
"select_crit(doc_t)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58be22eb-a5c3-4a01-820b-45d190fce52d",
"metadata": {},
"outputs": [],
"source": [
"#Test to make sure all three options work. A known issue is that if we combine the compounds then they will not end up in the final_options...\n",
"pos_t = select_crit(doc_t)\n",
"lime_t = select_crit(doc_t,options=\"LIME\")\n",
"final_t = select_crit(doc_t,options=True,limelist=True)\n",
"print(pos_t, lime_t, final_t)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1158de94-1472-4001-b3a1-42a488bcb20f",
"metadata": {},
"outputs": [],
"source": [
"select_crit(doc_t,options=True)"
]
},
{
"cell_type": "markdown",
"id": "05063ede-422f-4536-8408-ceb5441adbe8",
"metadata": {},
"source": [
"> Note `Papua` and `Eve` have such low impact on the model that they do not always appear... so there will always be limitations to matching."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c7c1ca9-4962-4fbe-b18b-1e20a223aff9",
"metadata": {},
"outputs": [],
"source": [
"select_crit(doc_t,options=\"LIME\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c70387a5-c431-43a5-a3b8-7533268a94e3",
"metadata": {},
"outputs": [],
"source": [
"displacy.render(doc_t, style=\"ent\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b92d276-7d67-4c1c-940b-d3b2dcc756b9",
"metadata": {},
"outputs": [],
"source": [
"#This run clearly indicates that this pipeline from spaCy does not know what to do with hyphens(\"-\") and that we need to be aware of that.\n",
"choices = select_crit(doct)\n",
"choices"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ea6b29d0-d0fa-4eb3-af9c-970759124145",
"metadata": {},
"outputs": [],
"source": [
"user_choice = choices[2]\n",
"matcher2 = Matcher(nlp.vocab)\n",
"pattern = [{\"TEXT\": user_choice}]\n",
"matcher2.add(\"user choice\", [pattern])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d32754b8-f1fa-4781-a6b0-829ad7ec2e50",
"metadata": {},
"outputs": [],
"source": [
"#consider using https://github.com/writerai/replaCy instead\n",
"match_id, start, end = match_this(matcher2,doc2)[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a0362734-020b-49ad-b566-fdc7196e705c",
"metadata": {},
"outputs": [],
"source": [
"docx = doc2.text.replace(user_choice,\"man\")\n",
"docx"
]
},
{
"cell_type": "markdown",
"id": "bf0512b6-336e-4842-9bde-34e03a1ca7c6",
"metadata": {},
"source": [
"### Testing predictions and visualization\n",
"Here I will attempt to import the model from huggingface, generate predictions for each of the sentences, and then visualize those predictions into a dot plot. If I can get this to work then I will move on to testing a full pipeline for letting the user pick which part of the sentence they wish to generate counterfactuals for."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e0bd4134-3b22-4ae8-870c-3a66c1cf8b23",
"metadata": {},
"outputs": [],
"source": [
"#Testing to see how to get predictions from the model. Ultimately, this did not work.\n",
"token = tokenizer(upt4, return_tensors=\"pt\")\n",
"labels = torch.tensor([1]).unsqueeze(0) # Batch size 1\n",
"outputs = model(**token, labels=labels)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "74c639bb-e74a-4a46-8047-3552265ae6a4",
"metadata": {},
"outputs": [],
"source": [
"#Discovering that there's a pipeline specifically to provide scores. \n",
"#I used it to get a list of lists of dictionaries that I can then manipulate to calculate the proper prediction score.\n",
"pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8e1ff15d-0fb9-475b-bd24-4548c0782343",
"metadata": {},
"outputs": [],
"source": [
"preds = pipe(upt4)\n",
"print(preds[0][0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8abb9ca-36cf-441a-9236-1f7e44331b53",
"metadata": {},
"outputs": [],
"source": [
"score_1 = preds[0][0]['score']\n",
"score_2 = (score_1 - .5) * 2\n",
"print(score_1, score_2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8726a284-99bd-47f1-9756-1c3ae603db10",
"metadata": {},
"outputs": [],
"source": [
"def eval_pred(text):\n",
" '''A basic function for evaluating the prediction from the model and turning it into a visualization friendly number.'''\n",
" preds = pipe(text)\n",
" neg_score = preds[0][0]['score']\n",
" pos_score = preds[0][1]['score']\n",
" if pos_score >= neg_score:\n",
" return pos_score\n",
" if neg_score >= pos_score:\n",
" return -1 * neg_score"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f38f5061-f30a-4c81-9465-37951c3ad9f4",
"metadata": {},
"outputs": [],
"source": [
"def eval_pred_test(text, return_all = False):\n",
" '''A basic function for evaluating the prediction from the model and turning it into a visualization friendly number.'''\n",
" preds = pipe(text)\n",
" neg_score = -1 * preds[0][0]['score']\n",
" sent_neg = preds[0][0]['label']\n",
" pos_score = preds[0][1]['score']\n",
" sent_pos = preds[0][1]['label']\n",
" prediction = 0\n",
" sentiment = ''\n",
" if pos_score > abs(neg_score):\n",
" prediction = pos_score\n",
" sentiment = sent_pos\n",
" elif abs(neg_score) > pos_score:\n",
" prediction = neg_score\n",
" sentiment = sent_neg\n",
" \n",
" if return_all:\n",
" return prediction, sentiment\n",
" else:\n",
" return prediction"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "abd5dd8c-8cff-4865-abf1-f5a744f2203b",
"metadata": {},
"outputs": [],
"source": [
"score = eval_pred(upt4)\n",
"og_data = {'Country': ['Iraq'], 'Continent': ['Asia'], 'text':[upt4], 'pred':[score]}\n",
"og_df = pd.DataFrame(og_data)\n",
"og_df"
]
},
{
"cell_type": "markdown",
"id": "8b349a87-fe83-4045-a63a-d054489bb461",
"metadata": {},
"source": [
"## Load the dummy countries I created to test generating counterfactuals\n",
"I decided to test the pipeline with a known problem space. Taking the text from Aurélien Géron's observations in twitter, I built a built a small scale test using the learnings I had to prove that we can identify a particular part of speech, use it to generate counterfactuals, and then build a visualization off it."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "46ab3332-964c-449f-8cef-a9ff7df397a4",
"metadata": {},
"outputs": [],
"source": [
"#load my test data from https://github.com/dbouquin/IS_608/blob/master/NanosatDB_munging/Countries-Continents.csv\n",
"df = pd.read_csv(\"Assets/Countries/countries.csv\")\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51c75894-80af-4625-8ce8-660e500b496b",
"metadata": {},
"outputs": [],
"source": [
"#Note: we will need to build the function that lets the user choose from the options available. For now I have hard coded it as \"selection\", from \"user_options\".\n",
"user_options = select_crit(doc4)\n",
"print(user_options)\n",
"selection = user_options[1]\n",
"selection"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3d6419f1-bf7d-44bc-afb8-ac26ef9002df",
"metadata": {},
"outputs": [],
"source": [
"#Create a function that generates the counterfactuals within a data frame.\n",
"def gen_cf_country(df,document,selection):\n",
" df['text'] = df.Country.apply(lambda x: document.text.replace(selection,x))\n",
" df['prediction'] = df.text.apply(eval_pred_test)\n",
" #added this because I think it will make the end results better if we ensure the seed is in the data we generate counterfactuals from.\n",
" df['seed'] = df.Country.apply(lambda x: 'seed' if x == selection else 'alternative')\n",
" return df\n",
"\n",
"df = gen_cf_country(df,doc4,selection)\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aec241a6-48c3-48c6-9e7f-d22612eaedff",
"metadata": {},
"outputs": [],
"source": [
"#Display Counterfactuals and Original in a layered chart. I couldn't get this to provide a legend.\n",
"og = alt.Chart(og_df).encode(\n",
" x='Continent:N',\n",
" y='pred:Q'\n",
").mark_square(color='green', size = 200, opacity=.5)\n",
"\n",
"cf = alt.Chart(df).encode(\n",
" x='Continent:N', # specify nominal data\n",
" y='prediction:Q', # specify quantitative data\n",
").mark_circle(color='blue', size=50, opacity =.25)\n",
"\n",
"alt_plot = alt.LayerChart(layer=[cf,og], width = 300)\n",
"alt_plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecb9dd41-2fab-49bd-bae5-30300ce39e41",
"metadata": {},
"outputs": [],
"source": [
"single_nearest = alt.selection_single(on='mouseover', nearest=True)\n",
"full = alt.Chart(df).encode(\n",
" alt.X('Continent:N'), # specify nominal data\n",
" alt.Y('prediction:Q'), # specify quantitative data\n",
" color=alt.Color('seed:N', legend=alt.Legend(title=\"Seed or Alternative\")),\n",
" size=alt.Size('seed:N', alt.scale(domain=[50,100])),\n",
" tooltip=('Country','prediction')\n",
").mark_circle(opacity=.5).properties(width=300).add_selection(single_nearest)\n",
"\n",
"full"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "56bc30d7-03a5-43ff-9dfe-878197628305",
"metadata": {},
"outputs": [],
"source": [
"df2 = df.nlargest(5, 'prediction')\n",
"df3 = df.nsmallest(5, 'prediction')\n",
"frames = [df2,df3]\n",
"results = pd.concat(frames)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1610bb48-c9b9-4bee-bcb5-999886acb9e3",
"metadata": {},
"outputs": [],
"source": [
"bar = alt.Chart(results).encode( \n",
" alt.X('prediction:Q'), \n",
" alt.Y('Country:N', sort=\"-x\"),\n",
" color=alt.Color('seed:N', legend=alt.Legend(title=\"Seed or Alternative\")),\n",
" size='seed:N',\n",
" tooltip=('Country','prediction')\n",
").mark_circle().properties(width=300).add_selection(single_nearest)\n",
"\n",
"bar"
]
},
{
"cell_type": "markdown",
"id": "84c40b74-95be-4c19-bd57-74e6004b950c",
"metadata": {},
"source": [
"#### QA"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7d15c7d8-9fdb-4c5b-84fa-599839cbceac",
"metadata": {},
"outputs": [],
"source": [
"qa_txt = \"They serve halal food in Iraq and Egypt.\"\n",
"qa_doc = nlp(qa_txt)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6956ddf-9287-419a-bb08-a3618f77700a",
"metadata": {},
"outputs": [],
"source": [
"displacy.render(qa_doc, style=\"dep\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88768d68-fe44-49ab-ac12-d41e6716b3b3",
"metadata": {},
"outputs": [],
"source": [
"select_crit(qa_doc)"
]
},
{
"cell_type": "markdown",
"id": "7bbc6c2e-df5d-4076-8532-8648fd818be4",
"metadata": {},
"source": [
"# NLC-Gen\n",
"### A Natural Language Counterfactual Generator for Exploring Bias in Sentiment Analysis Algorithms\n",
"\n",
"##### Overview\n",
"This project is an extension of [Interactive Model Cards](https://github.com/amcrisan/interactive-model-cards). It focuses on providing a person more ways to explore the bias of a model through the generation of alternatives (technically [counterfactuals](https://plato.stanford.edu/entries/counterfactuals/#WhatCoun)). We believe the use of alternatives people can better understand the limitations of a model and develop productive skepticism around its usage and trustworthiness.\n",
"\n",
"##### Set up\n",
"\n",
"Download the files from Github then perform the commands below in \n",
"```sh\n",
"cd NLC-Gen\n",
"pipenv install\n",
"pipenv shell\n",
"python -m spacy download en_core_web_lg\n",
"streamlit run NLC-app.py\n",
"```\n",
"\n",
"##### Known Limitations\n",
"* Words not in the spaCy vocab for `en_core_web_lg` won't have vectors and so won't have the ability to create similarity scores.\n",
"* WordNet provides many limitations due to its age and lack of funding for ongoing maintenance. It provides access to a large variety of the English language but certain words simply do not exist.\n",
"* There are currently only 2 lists (Countries and Professions). We would like to find community curated lists for: Race, Sexual Orientation and Gender Identity (SOGI), Religion, age, and protected status.\n",
"\n",
"\n",
"##### Key Dependencies and Packages\n",
"\n",
"1. [Hugging Face Transformers](https://huggingface.co/) - the model we've designed this iteration for is hosted on hugging face. It is: [distilbert-base-uncased-finetuned-sst-2-english](https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english).\n",
"2. [Streamlit](https://streamlit.io) - This is the library we're using to build the prototype app because it is easy to stand up and quick to fix.\n",
"3. [spaCy](https://spacy.io) - This is the main NLP Library we're using and it runs most of the text manipulation we're doing as part of the project.\n",
"4. [NLTK + WordNet](https://www.nltk.org/howto/wordnet.html) - This is the initial lexical database we're using because it is accessible directly through Python and it is free. We will be considering a move to [ConceptNet](https://conceptnet.io/) for future iterations based on better lateral movement across edges.\n",
"5. [Lime](https://github.com/marcotcr/lime) - We chose Lime over Shap because Lime has more of the functionality we need. Shap appears to provide greater performance but is not as easily suited to our original designs.\n",
"6. [Altair](https://altair-viz.github.io/user_guide/encoding.html) - We're using Altair because it's well integrated into Streamlit.\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa224bed-3630-4485-8dbc-670aaf5e6b0a",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|