File size: 5,193 Bytes
f1bf1b2 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"\n",
"with open('../data/task3.json', 'r') as f:\n",
" data = json.load(f)\n",
"\n",
"test_texts = data['drafts']\n",
"test_labels = data['labels']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# If use Together API\n",
"from together import Together\n",
"\n",
"your_model_name = 'xxxxxxxxxxxxxxxxxxxxxxxx'\n",
"your_api_key = 'xxxxxxxxxxxxxxxxxxxxxxxx'\n",
"client = Together(api_key=your_api_key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from tqdm import tqdm\n",
"import pandas as pd\n",
"import random\n",
"\n",
"def classify_texts(texts):\n",
" results = []\n",
" for text in tqdm(texts):\n",
" user_prompt = f\"\"\"\n",
" The provided document is a United Nations Security Council's draft resolution. Predict whether the draft resolution will be adopted or not. Answer with 'yes' (1) or 'no' (0) without any explanation.\n",
"\n",
" Text: \"{text}\"\n",
" Answer:\n",
" \"\"\"\n",
" response = client.chat.completions.create(\n",
" model=your_model_name,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": user_prompt}\n",
" ],\n",
" max_tokens=5,\n",
" temperature=0.0\n",
" )\n",
" result = response.choices[0].message.content.strip().lower()\n",
" \n",
" if result.startswith(\"yes\") or result == \"1\":\n",
" results.append(1)\n",
" elif result.startswith(\"no\") or result == \"0\":\n",
" results.append(0)\n",
" else:\n",
" results.append(random.choice([0, 1])) \n",
" return results\n",
"\n",
"\n",
"pred = classify_texts(test_texts)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# calculate metrics\n",
"from sklearn.metrics import accuracy_score, balanced_accuracy_score, precision_recall_fscore_support\n",
"from sklearn.metrics import roc_auc_score, average_precision_score, matthews_corrcoef\n",
"from imblearn.metrics import geometric_mean_score\n",
"\n",
"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, classification_report\n",
"from sklearn.metrics import roc_auc_score, balanced_accuracy_score, precision_recall_curve, auc\n",
"\n",
"def calculate_metrics(pred, labels):\n",
" # swap 0 and 1\n",
" pred = [1 - x for x in pred]\n",
" labels = [1 - x for x in labels]\n",
" acc = accuracy_score(labels, pred)\n",
" try:\n",
" roc_auc = roc_auc_score(labels, pred)\n",
" except ValueError:\n",
" roc_auc = 0\n",
" balanced_acc = balanced_accuracy_score(labels, pred)\n",
" prec, rec, f1, _ = precision_recall_fscore_support(labels, pred, average='binary')\n",
" # pr_auc = average_precision_score(labels, pred)\n",
" precision, recall, _ = precision_recall_curve(labels, pred)\n",
" pr_auc = auc(recall, precision)\n",
" mcc = matthews_corrcoef(labels, pred)\n",
" g_mean = geometric_mean_score(labels, pred)\n",
" tn, fp, fn, tp = confusion_matrix(labels, pred).ravel()\n",
" specificity = tn / (tn + fp)\n",
"\n",
" print(f'Accuracy: {acc}')\n",
" print(f'AUC: {roc_auc}')\n",
" print(f'Balanced Accuracy: {balanced_acc}')\n",
" print(f'Precision: {prec}')\n",
" print(f'Recall: {rec}')\n",
" print(f'F1: {f1}')\n",
" print(f'PR AUC: {pr_auc}')\n",
" print(f'MCC: {mcc}')\n",
" print(f'G-Mean: {g_mean}')\n",
" print(f'Specificity: {specificity}')\n",
"\n",
" print('Accuracy AUC Balanced_Acc Precision Recall F1 PR_AUC MCC G-Mean Specificity')\n",
" print(f'{acc:.4f} {roc_auc:.4f} {balanced_acc:.4f} {prec:.4f} {rec:.4f} {f1:.4f} {pr_auc:.4f} {mcc:.4f} {g_mean:.4f} {specificity:.4f}')\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"calculate_metrics(pred, test_labels)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "llm",
"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.9.19"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|