{ "cells": [ { "cell_type": "code", "execution_count": 17, "id": "ef8edf3a-4fe9-4f5b-8e70-bd9bd42a609b", "metadata": {}, "outputs": [], "source": [ "import json\n", "import re\n", "from termcolor import colored\n" ] }, { "cell_type": "code", "execution_count": 92, "id": "e7b1e345-08d4-49f1-b5cc-b856e6f045d5", "metadata": {}, "outputs": [], "source": [ "outs= json.load(open('Downloads/qwen3_out (2).json'))" ] }, { "cell_type": "code", "execution_count": 93, "id": "795f3269-23f2-4843-848f-cd1ec43180ba", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "{'0': 'exclaiming', '1': 'argued', '2': 'sob'}" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "outs[IDX]['candidates']" ] }, { "cell_type": "code", "execution_count": 94, "id": "14add67b-7bef-4301-b9c6-48f743d489a4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'{\"0\": \"no\", \"1\": \"no\", \"2\": \"no\"}'" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "outs[IDX]['pred']" ] }, { "cell_type": "code", "execution_count": 95, "id": "37a381c6-0846-47c9-91d4-ae7216a221ef", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\x1b[32mrr\\x1b[0m'" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "colored('rr','green')" ] }, { "cell_type": "code", "execution_count": 546, "id": "c385d973-bc75-4bab-9aea-d9113eee1fb7", "metadata": {}, "outputs": [], "source": [ "annotations += [{}] * len(out)" ] }, { "cell_type": "code", "execution_count": 343, "id": "b7ddfe82-565b-4d2c-a82b-02602dcf89ef", "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "<>:4: SyntaxWarning: invalid escape sequence '\\|'\n", "<>:6: SyntaxWarning: invalid escape sequence '\\|'\n", "<>:12: SyntaxWarning: invalid escape sequence '\\|'\n", "<>:14: SyntaxWarning: invalid escape sequence '\\|'\n", "<>:4: SyntaxWarning: invalid escape sequence '\\|'\n", "<>:6: SyntaxWarning: invalid escape sequence '\\|'\n", "<>:12: SyntaxWarning: invalid escape sequence '\\|'\n", "<>:14: SyntaxWarning: invalid escape sequence '\\|'\n", "/var/folders/_s/rl4c05mx7ldfyms14mh6s7_w0000gp/T/ipykernel_22336/4162419528.py:4: SyntaxWarning: invalid escape sequence '\\|'\n", " c = re.sub('<\\|Q\\|>([^<]+)<\\|Q\\|>', lambda x: '\\033[1m\\x1b[34m' + x.group(1) +'\\x1b[0m', context)\n", "/var/folders/_s/rl4c05mx7ldfyms14mh6s7_w0000gp/T/ipykernel_22336/4162419528.py:6: SyntaxWarning: invalid escape sequence '\\|'\n", " c = re.sub('(\\|([\\d]+)\\|([^\\|])+\\|[\\d]+\\|)', lambda x:f'\\033[1m{cs[preds[x.group(2)]]}' + x.group(1) +'\\x1b[0m', c)\n", "/var/folders/_s/rl4c05mx7ldfyms14mh6s7_w0000gp/T/ipykernel_22336/4162419528.py:12: SyntaxWarning: invalid escape sequence '\\|'\n", " c = re.sub('<\\|Q\\|>([^<]+)<\\|Q\\|>', lambda x: '\\033[1m\\x1b[34m' + x.group(1) +'\\x1b[0m', context)\n", "/var/folders/_s/rl4c05mx7ldfyms14mh6s7_w0000gp/T/ipykernel_22336/4162419528.py:14: SyntaxWarning: invalid escape sequence '\\|'\n", " c = re.sub('(\\|([\\d]+)\\|([^\\|])+\\|[\\d]+\\|)', lambda x:f'\\033[1m\\x1b[31m' + x.group(1) +'\\x1b[0m', c)\n" ] } ], "source": [ "def print_c(context, preds) : \n", " cs = {'no' : '\\x1b[31m', 'yes' : '\\x1b[32m'}\n", " \n", " c = re.sub('<\\|Q\\|>([^<]+)<\\|Q\\|>', lambda x: '\\033[1m\\x1b[34m' + x.group(1) +'\\x1b[0m', context)\n", " \n", " c = re.sub('(\\|([\\d]+)\\|([^\\|])+\\|[\\d]+\\|)', lambda x:f'\\033[1m{cs[preds[x.group(2)]]}' + x.group(1) +'\\x1b[0m', c)\n", " print(c)\n", "\n", "def print_c(context) : \n", " #cs = {'no' : '\\x1b[31m', 'yes' : '\\x1b[32m'}\n", " \n", " c = re.sub('<\\|Q\\|>([^<]+)<\\|Q\\|>', lambda x: '\\033[1m\\x1b[34m' + x.group(1) +'\\x1b[0m', context)\n", " \n", " c = re.sub('(\\|([\\d]+)\\|([^\\|])+\\|[\\d]+\\|)', lambda x:f'\\033[1m\\x1b[31m' + x.group(1) +'\\x1b[0m', c)\n", " print(c)\n", "\n", " " ] }, { "cell_type": "code", "execution_count": 978, "id": "b2f9fed1-f2d1-4082-a62d-e9a53cbfefa3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "368 \n", "\n", "\n", "All sorts of complications might ensue from this hasty proceeding. Yet Dorothy, even in that moment of trouble, realized that she must keep her brain clear for whatever might transpire. Tavia, she felt, might do something reckless — well meant, no doubt, but none the less something that might put a weapon in the hands of the man against whom they hoped to proceed for the sake of Aunt Winnie.\n", "\n", "“Just what do you mean?” \u001b[1m\u001b[31m|0|snapped|0|\u001b[0m the man again, and he seemed master of the situation, even though Dorothy thought she detected a gleam of — was it fear? in his eyes. \u001b[1m\u001b[34m“I am not in the habit of being spoken to in that manner,”\u001b[0m he went on.\n", "\n", "“I am afraid I shall have to ask you to explain yourself. It is the first time I have ever been accused of wrongdoing.”\n" ] } ], "source": [ "IDX = 368\n", "print(IDX, '\\n\\n')\n", "print_c(outs[IDX]['context'])\n", "\n", "preds = eval(outs[IDX]['pred']).copy()\n" ] }, { "cell_type": "code", "execution_count": 979, "id": "faf55c79-d96d-4fee-a046-27a0ddfe991e", "metadata": {}, "outputs": [], "source": [ "\n", "annotations[IDX]['0'] = 'yes'\n", "annotations[IDX]['1'] = 'no'\n", "annotations[IDX]['2'] = 'no'\n", "annotations[IDX]['3'] = 'no'\n", " \n", "annotations[IDX] = { k : annotations[IDX][k] for k in preds}" ] }, { "cell_type": "code", "execution_count": 981, "id": "12203b4b-ca6a-4a1b-822e-827f41f6eb61", "metadata": {}, "outputs": [], "source": [ "with open('Desktop/annotations_gmichel.json','w') as f : \n", " json.dump(annotations, f)" ] }, { "cell_type": "code", "execution_count": 963, "id": "3900b42a-75f8-4a50-ab7e-5239ff140a99", "metadata": {}, "outputs": [], "source": [ "### 200 examples took 45 minutes\n", "### 400 examples took 1h15" ] }, { "cell_type": "code", "execution_count": 1021, "id": "6d736767-a400-4432-baeb-b3013dc41f8d", "metadata": {}, "outputs": [], "source": [ "corr_y = 0\n", "corr_n = 0\n", "total_y = 0 \n", "total_n = 0 \n", "\n", "errors = []\n", "for cnt, (gt, pred) in enumerate(zip(annotations, outs)) : \n", " p = eval(pred['pred'])\n", " assert len(gt) == len(p)\n", " for k in p : \n", " if any([gt[k] not in ['no', 'yes'], p[k] not in ['no', 'yes']]): \n", " print(cnt)\n", " \n", " if gt[k] == p[k] : \n", " if gt[k] == 'yes' : \n", " corr_y += 1\n", " else : \n", " corr_n += 1\n", "\n", " else : \n", " errors.append((cnt, k, gt[k], p[k] ))\n", " if gt[k] == 'yes' : \n", " total_y +=1\n", " else : \n", " total_n +=1" ] }, { "cell_type": "code", "execution_count": 1022, "id": "ce296326-817a-48f2-87a3-12897ff46443", "metadata": {}, "outputs": [], "source": [ "tp = corr_y\n", "tn = corr_n\n", "fp = total_n - tn\n", "fn = total_y - tp" ] }, { "cell_type": "code", "execution_count": 1023, "id": "164dcbdb-ffae-45d8-9ae3-04fb374a24a5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Precision: 0.88\n", "Recall: 0.76\n" ] } ], "source": [ "print(f'Precision: {tp / (tp+fp):.2f}')\n", "print(f'Recall: {tp / (tp+fn):.2f}')" ] }, { "cell_type": "code", "execution_count": 1024, "id": "a9a3cfa3-09de-424e-b578-43d05c34241c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Acc: 0.89\n", "Acc + : 0.76\n", "Acc - : 0.95\n" ] } ], "source": [ "print(f'Acc: {(corr_n+corr_y)/(total_n + total_y):0.2f}')\n", "\n", "print(f'Acc + : {corr_y/total_y:0.2f}')\n", "print(f'Acc - : {corr_n/total_n:0.2f}')\n", "\n", "#print(f'Prec : {corr_y/(total_y+total_n):0.2f}')\n" ] }, { "cell_type": "code", "execution_count": 1025, "id": "853e4059-7a72-4a77-8dc9-3f8c8267ae64", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, '0', 'yes', 'no')" ] }, "execution_count": 1025, "metadata": {}, "output_type": "execute_result" } ], "source": [ "IDX = 0\n", "errors[IDX]" ] }, { "cell_type": "code", "execution_count": 1085, "id": "c881415b-6ca2-43ea-91b9-ac8abdc1c6ba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "idx, v_id, gt, pred\n", "(24, '0', 'yes', 'no') \n", "\n", "\n", "{'0': 'no'}\n", "\n", "\n", "\n", "Pollyanna gave her head a dubious shake.\n", "\n", "\"Well, I'm afraid maybe I don't know ALL of 'em,\" she \u001b[1m\u001b[31m|0|admitted|0|\u001b[0m. \u001b[1m\u001b[34m\"Are they all -- in books?\"\u001b[0m\n", "\n", "The boy nodded.\n" ] } ], "source": [ "IDX += 1\n", "\n", "print('idx, v_id, gt, pred')\n", "print(errors[IDX], '\\n\\n')\n", "preds = eval(outs[errors[IDX][0]]['pred']).copy()\n", "print(preds)\n", "print('\\n\\n')\n", "print_c(outs[errors[IDX][0]]['context'])\n" ] }, { "cell_type": "code", "execution_count": null, "id": "20b327ae-8b67-4590-ae63-1c849eba96bd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "5829df94-48d5-4075-9767-8a5da3a208e1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Looking in indexes: https://pypi.org/simple, https://artifacts.deez.re/repository/python-research/simple\n", "\u001b[33mWARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')': /repository/python-research/simple/transformers/\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')': /repository/python-research/simple/transformers/\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')': /repository/python-research/simple/transformers/\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')': /repository/python-research/simple/transformers/\u001b[0m\u001b[33m\n", "\u001b[0m" ] } ], "source": [ "!pip install transformers" ] }, { "cell_type": "code", "execution_count": null, "id": "b00aefa2-fbd4-4441-a2b0-d0cf5dbbf0ea", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "3d2f953f-9b18-4294-8a4c-eb16816e5d9c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "2406125b-4b81-4a01-8648-cfef91b19d4f", "metadata": {}, "source": [ "#### Example 1: case where 'pouted' then 'giggled' --> giggled = false\n", "\n", "Bunting spoke in a muffled, angry tone. He was wiping his forehead with one hand, while with the other he mechanically squeezed the little packet of tobacco, for which, as he now remembered, he had forgotten to pay.\n", "\n", "Daisy pouted. <|Q|>“Oh, father, I think you might let me have a treat on my birthday! I told him that Saturday wasn’t a very good day — at least, so I’d heard — for Madame Tussaud’s. Then he said we could go early, while the fine folk are still having their dinners.”<|Q|> She turned to her stepmother, then |0|giggled|0| happily. “He particularly said you was to come, too. The lodger has a wonderful fancy for you, Ellen; if I was father, I’d feel quite jealous!”\n", "\n", "Her last words were cut across by a tap-tap on the door.\n", "\n", "\n" ] }, { "cell_type": "markdown", "id": "96dd645d-f66e-4d74-a04e-6f6ccf48b3f6", "metadata": {}, "source": [ "#### Example 2: here snapped then went-on --> means that snapping continues so snap=yes\n", "\n", "“Just what do you mean?” |0|snapped|0| the man again, and he seemed master of the situation, even though Dorothy thought she detected a gleam of — was it fear? in his eyes. <|Q|>“I am not in the habit of being spoken to in that manner,”<|Q|> he went on." ] }, { "cell_type": "code", "execution_count": null, "id": "21403b44-94ff-4e0a-82ad-dc56c7cd5832", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "0442a33b-cadb-4517-971f-4d7ffafb3ecc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "03cbdd05-a5a8-4668-8076-100b87a43e27", "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.13.2" } }, "nbformat": 4, "nbformat_minor": 5 }