Spaces:
Sleeping
Sleeping
File size: 4,848 Bytes
998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac 0014ea1 998e8ac |
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 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Text task notebook template\n",
"## Loading the necessary libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fastapi import APIRouter\n",
"from datetime import datetime\n",
"from datasets import load_dataset\n",
"from sklearn.metrics import accuracy_score\n",
"import random\n",
"\n",
"import sys\n",
"sys.path.append('../tasks')\n",
"\n",
"from utils.evaluation import AudioEvaluationRequest\n",
"from utils.emissions import tracker, clean_emissions_data, get_space_info\n",
"\n",
"\n",
"# Define the label mapping\n",
"LABEL_MAPPING = {\n",
" \"chainsaw\": 0,\n",
" \"environment\": 1\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from huggingface_hub import login\n",
"login()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Loading the datasets and splitting them"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"request = AudioEvaluationRequest()\n",
"\n",
"# Load and prepare the dataset\n",
"dataset = load_dataset(request.dataset_name)\n",
"\n",
"# Split dataset\n",
"train_test = dataset[\"train\"].train_test_split(test_size=request.test_size, seed=request.test_seed)\n",
"test_dataset = train_test[\"test\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_test.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Random Baseline"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Start tracking emissions\n",
"tracker.start()\n",
"tracker.start_task(\"inference\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"#--------------------------------------------------------------------------------------------\n",
"# YOUR MODEL INFERENCE CODE HERE\n",
"# Update the code below to replace the random baseline by your model inference within the inference pass where the energy consumption and emissions are tracked.\n",
"#-------------------------------------------------------------------------------------------- \n",
"\n",
"# Make random predictions (placeholder for actual model inference)\n",
"true_labels = test_dataset[\"label\"]\n",
"predictions = [random.randint(0, 1) for _ in range(len(true_labels))]\n",
"\n",
"predictions\n",
"\n",
"#--------------------------------------------------------------------------------------------\n",
"# YOUR MODEL INFERENCE STOPS HERE\n",
"#-------------------------------------------------------------------------------------------- "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Stop tracking emissions\n",
"emissions_data = tracker.stop_task()\n",
"emissions_data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Calculate accuracy\n",
"accuracy = accuracy_score(true_labels, predictions)\n",
"accuracy"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Prepare results dictionary\n",
"results = {\n",
" \"submission_timestamp\": datetime.now().isoformat(),\n",
" \"accuracy\": float(accuracy),\n",
" \"energy_consumed_wh\": emissions_data.energy_consumed * 1000,\n",
" \"emissions_gco2eq\": emissions_data.emissions * 1000,\n",
" \"emissions_data\": clean_emissions_data(emissions_data),\n",
" \"dataset_config\": {\n",
" \"dataset_name\": request.dataset_name,\n",
" \"test_size\": request.test_size,\n",
" \"test_seed\": request.test_seed\n",
" }\n",
"}\n",
"\n",
"results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|