{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "8faa322a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !tar -cf southern_samples.tar.gz southern_samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ff2d9531",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import json\n",
    "# with open(\"/mnt/data-ssd-1/data/southern-comfort/output/artifacts/web-sample-data/sample_manifest_web.json\") as f:\n",
    "#     web_data = json.load(f)\n",
    "# for e in web_data[\"sample\"]:\n",
    "#     with open(f\"southern_samples/{e['uid']}_text.txt\", \"w\") as f:\n",
    "#         f.write(e[\"text\"])\n",
    "#     with open(f\"southern_samples/{e['uid']}_text_normalized.txt\", \"w\") as f:\n",
    "#         f.write(e[\"text_normalized\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "c59ef8c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from labelbox.data.annotation_types import (\n",
    "    Label, ImageData, MaskData, LabelList, TextData, VideoData,\n",
    "    ObjectAnnotation, ClassificationAnnotation, Polygon, Rectangle, Line, Mask,\n",
    "    Point, Checklist, Radio, Text, TextEntity, ClassificationAnswer)\n",
    "from labelbox import (LabelingFrontend, Client, OntologyBuilder, Tool,\n",
    "                      Classification, Option)\n",
    "from shapely.ops import transform\n",
    "from shapely.affinity import affine_transform\n",
    "import requests\n",
    "import IPython\n",
    "from PIL import Image\n",
    "from io import BytesIO\n",
    "import numpy as np\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "403e132c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add your api key\n",
    "API_KEY = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjbDF0bmE2OGE0aDQzMHpiaWZkMDI1ZDd1Iiwib3JnYW5pemF0aW9uSWQiOiJjbDJ5dnFxcnIxOHR1MDdiOGF0Z2hmd3gwIiwiYXBpS2V5SWQiOiJjbDNtNHgxc2IwZTdwMDdldWFsbjA3MngxIiwic2VjcmV0IjoiMmIyZjY5NDgwYTBmNjM4MjUzM2ZkNzc3M2Q4YjhlYmIiLCJpYXQiOjE2NTM1MTYzMDksImV4cCI6MjI4NDY2ODMwOX0.Wmt5SdtQOoOwCCCgNetCeJSagZdv6l_Jj0X9fL2ndCo\"\n",
    "client = Client(api_key=API_KEY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96d6d364",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "td = TextData(text=\"some text\",\n",
    "              uid=\"ckrey6o07000008l50uk2gcr3\",\n",
    "              external_id=\"my_text_data_row\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e067358",
   "metadata": {},
   "outputs": [],
   "source": [
    "entity_annotation = ObjectAnnotation(value=TextEntity(start=10, end=12),\n",
    "                                     name=\"person\")\n",
    "\n",
    "entity_annotation = ObjectAnnotation(\n",
    "    value=TextEntity(start=10, end=12),\n",
    "    feature_schema_id=\"ckrgddyli000108mk0c0t9qya\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47707a0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "text_annotation = ClassificationAnnotation(\n",
    "    name=\"my text class\", value=Text(answer=\"some text answer\"))\n",
    "\n",
    "text_annotation = ClassificationAnnotation(\n",
    "    feature_schema_id=\"my text class\", value=Text(answer=\"some text answer\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6428cc3a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8c75788",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "da5bd4cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = client.create_dataset(name=\"my_test_dataset\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "386ee010",
   "metadata": {},
   "outputs": [],
   "source": [
    "# When uploading for MAL or MEA we need an ontology.\n",
    "# So let's create one\n",
    "\n",
    "# These names have to match our object names exactly!!\n",
    "ontology_builder = OntologyBuilder(\n",
    "   classifications=[\n",
    "       Classification(\n",
    "           Classification.Type.TEXT,\n",
    "           instructions=\"transcribe Audio\",\n",
    "       )\n",
    "   ]\n",
    ")\n",
    "\n",
    "editor = next(client.get_labeling_frontends(where=LabelingFrontend.name == \"Editor\"))\n",
    "project = client.get_project(\"cl3kma0ex1o5707b3845i3d9f\")\n",
    "\n",
    "project.setup(editor, ontology_builder.asdict())\n",
    "project.datasets.connect(dataset)\n",
    "\n",
    "ontology = OntologyBuilder.from_project(project)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1dfea82",
   "metadata": {},
   "outputs": [],
   "source": [
    "cl3m4fuju27wu07v6138yf029"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "3d542766",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValidationError",
     "evalue": "2 validation errors for ObjectAnnotation\nvalue\n  value is not a valid dict (type=type_error.dict)\nvalue\n  value is not a valid dict (type=type_error.dict)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValidationError\u001b[0m                           Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_981942/3703131329.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      6\u001b[0m     ),\n\u001b[1;32m      7\u001b[0m     annotations=[\n\u001b[0;32m----> 8\u001b[0;31m         ObjectAnnotation(\n\u001b[0m\u001b[1;32m      9\u001b[0m             \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"deer_nose\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     10\u001b[0m             \u001b[0mvalue\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"blabla\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/venvs/ml/lib/python3.8/site-packages/pydantic/main.cpython-38-x86_64-linux-gnu.so\u001b[0m in \u001b[0;36mpydantic.main.BaseModel.__init__\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mValidationError\u001b[0m: 2 validation errors for ObjectAnnotation\nvalue\n  value is not a valid dict (type=type_error.dict)\nvalue\n  value is not a valid dict (type=type_error.dict)"
     ]
    }
   ],
   "source": [
    "label = Label(\n",
    "    data=AudioData(\n",
    "        text=\"some text\",\n",
    "        uid=\"ckrey6o07000008l50uk2gcr3\",\n",
    "        external_id=\"my_text_data_row\"\n",
    "    ),\n",
    "    annotations=[\n",
    "        ObjectAnnotation(\n",
    "            name=\"deer_nose\",\n",
    "            value=\"blabla\",\n",
    "            classifications=[\n",
    "                ClassificationAnnotation(\n",
    "                    name=\"my text class\", \n",
    "                    value=Text(answer=\"some text answer\")\n",
    "                )\n",
    "            ]\n",
    "        ),\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3658e6ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "label.assign_feature_schema_ids(ontology)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e1811e3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a9d0c91",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ccbdac6c",
   "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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
