File size: 5,834 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"df = pd.read_csv('../data/task2.csv')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"\n",
"draft_ids = list(df['Original_id'])\n",
"\n",
"# input data\n",
"countries = list(df['Country'])\n",
"drafts = []\n",
"votes = list(df['Voting'])\n",
"\n",
"path = '../data/task2'\n",
"for i in draft_ids:\n",
" folder_path = os.path.join(path, str(i))\n",
" files = os.listdir(folder_path)\n",
" json_file = [file for file in files if file.endswith('EN.json')][0]\n",
" with open(os.path.join(folder_path, json_file)) as f:\n",
" draft = json.load(f)\n",
" drafts.append(draft['Content'])"
]
},
{
"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": [
"import random\n",
"from tqdm import tqdm\n",
"\n",
"pred = []\n",
"invalid_responses = []\n",
"for i, (draft, country) in tqdm(enumerate(zip(drafts, countries))):\n",
" system_prompt = \"You are an experienced diplomat participating in United Nations Security Council sessions. Your task is to read draft resolutions and vote accordingly. You must strictly respond with 'Y', 'N', or 'A' without any additional explanation.\"\n",
" user_prompt = f\"\"\"The following is a United Nations Security Council draft resolution. Assume you are a diplomat from {country} and must cast your vote.\n",
"\n",
" Please read the draft and vote using one of the following options:\n",
" - 'Y' for Yes (in favor)\n",
" - 'N' for No (against)\n",
" - 'A' for Abstain\n",
"\n",
" Draft Resolution: {draft}\n",
" Answer:\n",
" \"\"\"\n",
" response = client.chat.completions.create(\n",
" model=your_model_name,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt}\n",
" ],\n",
" max_tokens=1, \n",
" temperature=0.0\n",
" )\n",
" result = response.choices[0].message.content.strip()\n",
" valid_votes = ['Y', 'N', 'A']\n",
" if result not in valid_votes:\n",
" print(f\"Invalid response: {result}\")\n",
" result = random.choice(valid_votes)\n",
" invalid_responses.append(i)\n",
" pred.append(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"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 sklearn.preprocessing import LabelEncoder, label_binarize\n",
"from imblearn.metrics import geometric_mean_score\n",
"import numpy as np\n",
"\n",
"def calculate_metrics(pred, labels):\n",
" label_encoder = LabelEncoder()\n",
" all_classes = list(set(labels) | set(pred)) \n",
" label_encoder.fit(all_classes)\n",
"\n",
" labels = label_encoder.transform(labels) \n",
" pred = label_encoder.transform(pred) \n",
"\n",
" acc = accuracy_score(labels, pred)\n",
" \n",
" num_classes = len(label_encoder.classes_)\n",
" true_labels_bin = label_binarize(labels, classes=list(range(num_classes)))\n",
" pred_bin = label_binarize(pred, classes=list(range(num_classes))) \n",
"\n",
" auc = roc_auc_score(true_labels_bin, pred_bin, multi_class='ovr', average='macro')\n",
" pr_auc = average_precision_score(true_labels_bin, pred_bin, average='macro')\n",
"\n",
" balanced_acc = balanced_accuracy_score(labels, pred)\n",
" prec, rec, f1, _ = precision_recall_fscore_support(labels, pred, average='macro')\n",
"\n",
" mcc = matthews_corrcoef(labels, pred)\n",
" g_mean = geometric_mean_score(labels, pred, average='macro')\n",
"\n",
" print(f'Accuracy: {acc}')\n",
" print(f'AUC: {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",
"\n",
" print('Accuracy AUC Balanced_Acc Precision Recall F1 PR_AUC MCC G-Mean')\n",
" print(f'{acc:.4f} {auc:.4f} {balanced_acc:.4f} {prec:.4f} {rec:.4f} {f1:.4f} {pr_auc:.4f} {mcc:.4f} {g_mean:.4f}')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"calculate_metrics(pred, votes)"
]
}
],
"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
}
|