Skip to content

Commit 9671e60

Browse files
[SN-150] Model Evaluation SDK Method Notebook (#1645)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent b142a6f commit 9671e60

File tree

3 files changed

+219
-1
lines changed

3 files changed

+219
-1
lines changed

examples/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@
104104
</tr>
105105
</thead>
106106
<tbody>
107+
<tr>
108+
<td>Model Chat Evaluation Project</td>
109+
<td><a href="https://github.com/Labelbox/labelbox-python/tree/develop/examples/project_configuration/model_chat_evaluation_project.ipynb" target="_blank"><img src="https://img.shields.io/badge/GitHub-100000?logo=github&logoColor=white" alt="Open In Github"></a></td>
110+
<td><a href="https://colab.research.google.com/github/Labelbox/labelbox-python/blob/develop/examples/project_configuration/model_chat_evaluation_project.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"></a></td>
111+
</tr>
107112
<tr>
108113
<td>Project Setup</td>
109114
<td><a href="https://github.com/Labelbox/labelbox-python/tree/develop/examples/project_configuration/project_setup.ipynb" target="_blank"><img src="https://img.shields.io/badge/GitHub-100000?logo=github&logoColor=white" alt="Open In Github"></a></td>
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 2,
4+
"metadata": {},
5+
"cells": [
6+
{
7+
"metadata": {},
8+
"source": [
9+
"<td>",
10+
" <a target=\"_blank\" href=\"https://labelbox.com\" ><img src=\"https://labelbox.com/blog/content/images/2021/02/logo-v4.svg\" width=256/></a>",
11+
"</td>\n"
12+
],
13+
"cell_type": "markdown"
14+
},
15+
{
16+
"metadata": {},
17+
"source": [
18+
"<td>\n",
19+
"<a href=\"https://colab.research.google.com/github/Labelbox/labelbox-python/blob/develop/examples/project_configuration/model_chat_evaluation_project.ipynb\" target=\"_blank\"><img\n",
20+
"src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
21+
"</td>\n",
22+
"\n",
23+
"<td>\n",
24+
"<a href=\"https://github.com/Labelbox/labelbox-python/tree/develop/examples/project_configuration/model_chat_evaluation_project.ipynb\" target=\"_blank\"><img\n",
25+
"src=\"https://img.shields.io/badge/GitHub-100000?logo=github&logoColor=white\" alt=\"GitHub\"></a>\n",
26+
"</td>"
27+
],
28+
"cell_type": "markdown"
29+
},
30+
{
31+
"metadata": {},
32+
"source": [
33+
"# Model Chat Evaluation Project Setup\n",
34+
"\n",
35+
"This notebook will provide an example workflow of setting up a Model Chat Evaluation (MCE) Project with the Labelbox-Python SDK.\n",
36+
"Model Chat Evaluation Projects are set up differently than other projects with its own unique method and modifications to existing methods:\n",
37+
"\n",
38+
"- `client.create_model_evaluation_project`: The main method used to create a model chat evaluation project\n",
39+
"\n",
40+
"- `client.create_ontology`: Methods used to create Labelbox ontologies for MCE project this requires an `ontology_kind` parameter set to `lb.OntologyKind.ModelEvaluation`\n",
41+
"\n",
42+
"- `client.create_ontology_from_feature_schemas`: Similar to `client.create_ontology` but from a list of `feature schema ids` designed to allow you to use existing features instead of creating new features. This also requires an `ontology_kind` set to `lb.OntologyKind.ModelEvaluation`."
43+
],
44+
"cell_type": "markdown"
45+
},
46+
{
47+
"metadata": {},
48+
"source": [
49+
"## Set Up"
50+
],
51+
"cell_type": "markdown"
52+
},
53+
{
54+
"metadata": {},
55+
"source": "%pip install -q \"labelbox[data]\"",
56+
"cell_type": "code",
57+
"outputs": [],
58+
"execution_count": null
59+
},
60+
{
61+
"metadata": {},
62+
"source": "import labelbox as lb",
63+
"cell_type": "code",
64+
"outputs": [],
65+
"execution_count": null
66+
},
67+
{
68+
"metadata": {},
69+
"source": [
70+
"## API Key and Client\n",
71+
"Provide a valid API key below in order to properly connect to the Labelbox client. Please review [Create API key guide](https://docs.labelbox.com/reference/create-api-key) for more information."
72+
],
73+
"cell_type": "markdown"
74+
},
75+
{
76+
"metadata": {},
77+
"source": "API_KEY = None\nclient = lb.Client(api_key=API_KEY)",
78+
"cell_type": "code",
79+
"outputs": [],
80+
"execution_count": null
81+
},
82+
{
83+
"metadata": {},
84+
"source": [
85+
"## Example: Create Model Chat Evaluation Project\n",
86+
"\n",
87+
"The steps to creating a Model Chat Evaluation Project through the Labelbox-Python SDK are similar to creating a regular project. However, they vary slightly, and we will showcase the different methods in this example workflow."
88+
],
89+
"cell_type": "markdown"
90+
},
91+
{
92+
"metadata": {},
93+
"source": [
94+
"### Create a Model Chat Evaluation Ontology\n",
95+
"\n",
96+
"You can create ontologies for Model Evaluation projects the same way as creating ontologies for other projects with the only requirement of passing in a `ontology_kind` parameter which needs set to `lb.OntologyKind.ModelEvaluation`. You can create ontologies with two methods: `client.create_ontology` and `client.create_ontology_from_feature_schemas`."
97+
],
98+
"cell_type": "markdown"
99+
},
100+
{
101+
"metadata": {},
102+
"source": [
103+
"#### Option A: `client.create_ontology`\n",
104+
"\n",
105+
"Typically, you create ontologies and generate the associated features at the same time. Below is an example of creating an ontology for your model chat evaluation project using supported tools and classifications. For information on supported annotation types visit our [model chat evaluation](https://docs.labelbox.com/docs/model-chat-evaluation#supported-annotation-types) guide."
106+
],
107+
"cell_type": "markdown"
108+
},
109+
{
110+
"metadata": {},
111+
"source": "ontology_builder = lb.OntologyBuilder(\n tools=[\n lb.Tool(\n tool=lb.Tool.Type.MESSAGE_SINGLE_SELECTION,\n name=\"single select feature\",\n ),\n lb.Tool(\n tool=lb.Tool.Type.MESSAGE_MULTI_SELECTION,\n name=\"multi select feature\",\n ),\n lb.Tool(tool=lb.Tool.Type.MESSAGE_RANKING, name=\"ranking feature\"),\n ],\n classifications=[\n lb.Classification(\n class_type=lb.Classification.Type.CHECKLIST,\n name=\"checklist feature\",\n options=[\n lb.Option(value=\"option 1\", label=\"option 1\"),\n lb.Option(value=\"option 2\", label=\"option 2\"),\n ],\n ),\n lb.Classification(\n class_type=lb.Classification.Type.RADIO,\n name=\"radio_question\",\n options=[\n lb.Option(value=\"first_radio_answer\"),\n lb.Option(value=\"second_radio_answer\"),\n ],\n ),\n ],\n)\n\n# Create ontology\nontology = client.create_ontology(\n \"MCE ontology\",\n ontology_builder.asdict(),\n media_type=lb.MediaType.Conversational,\n ontology_kind=lb.OntologyKind.ModelEvaluation,\n)",
112+
"cell_type": "code",
113+
"outputs": [],
114+
"execution_count": null
115+
},
116+
{
117+
"metadata": {},
118+
"source": [
119+
"### Option B: `client.create_ontology_from_feature_schemas`\n",
120+
"Ontologies can also be created with feature schema IDs. This makes your ontologies with existing features compared to generating new features. You can get these features by going to the _Schema_ tab inside Labelbox. (uncomment the below code block for this option)"
121+
],
122+
"cell_type": "markdown"
123+
},
124+
{
125+
"metadata": {},
126+
"source": "# ontology = client.create_ontology_from_feature_schemas(\n# \"MCE ontology\",\n# feature_schema_ids=[\"<list of feature schema ids\"],\n# media_type=lb.MediaType.Conversational,\n# ontology_kind=lb.OntologyKind.ModelEvaluation,\n# )",
127+
"cell_type": "code",
128+
"outputs": [],
129+
"execution_count": null
130+
},
131+
{
132+
"metadata": {},
133+
"source": [
134+
"### Set Up Model Chat Evaluation Project\n",
135+
"You do not have to create data rows with a model evaluation project; instead, they are generated for you when you create the project. The method you use to create your project is `client.create_model_evaluation_project`, which takes the same parameters as the traditional `client.create_project` but with a few specific additional parameters. \n",
136+
"\n",
137+
"__Setting up different models that you are evaluating can only be done inside the Labelbox platform__\n",
138+
"\n",
139+
"#### Parameters\n",
140+
"When using `client.create_model_evaluation_project` the following parameters are needed:\n",
141+
"\n",
142+
"- `create_model_evaluation_project` parameters:\n",
143+
"\n",
144+
" - `name`: The name of your new project.\n",
145+
"\n",
146+
" - `description`: An optional description of your project.\n",
147+
"\n",
148+
" - `media_type`: The type of assets that this project will accept. This should be set to lb.MediaType.Conversational\n",
149+
"\n",
150+
" - `dataset_name`: The name of the dataset where the generated data rows will be located. Include this parameter only if you want to create a new dataset.\n",
151+
"\n",
152+
" - `dataset_id`: An optional dataset ID of an existing Labelbox dataset. Include this parameter if you are wanting to append to an existing MCE dataset.\n",
153+
"\n",
154+
" - `data_row_count`: The number of data row assets that will be generated and used with your project.\n"
155+
],
156+
"cell_type": "markdown"
157+
},
158+
{
159+
"metadata": {},
160+
"source": "project = client.create_model_evaluation_project(\n name=\"Demo MCE Project\",\n media_type=lb.MediaType.Conversational,\n dataset_name=\"Demo MCE dataset\",\n data_row_count=100,\n)\n\n# Setup project with ontology created above\nproject.setup_editor(ontology)",
161+
"cell_type": "code",
162+
"outputs": [],
163+
"execution_count": null
164+
},
165+
{
166+
"metadata": {},
167+
"source": [
168+
"## Exporting Model Chat Evaluation Project\n",
169+
"Exporting from a Model Chat Evaluation project works the same as exporting from other projects. In this example, your export will be shown as empty unless you have created labels inside the Labelbox platform. Please review our [Model Chat Evaluation Export](https://docs.labelbox.com/reference/export-model-chat-evaluation-annotations) guide for a sample export."
170+
],
171+
"cell_type": "markdown"
172+
},
173+
{
174+
"metadata": {},
175+
"source": "# Start export from project\nexport_task = project.export()\nexport_task.wait_till_done()\n\n# Conditional if task has errors\nif export_task.has_errors():\n export_task.get_buffered_stream(stream_type=lb.StreamType.ERRORS).start(\n stream_handler=lambda error: print(error))\n\nif export_task.has_result():\n # Start export stream\n stream = export_task.get_buffered_stream()\n\n # Iterate through data rows\n for data_row in stream:\n print(data_row.json)",
176+
"cell_type": "code",
177+
"outputs": [],
178+
"execution_count": null
179+
},
180+
{
181+
"metadata": {},
182+
"source": [
183+
"## Clean Up\n",
184+
"\n",
185+
"This section serves as an optional clean-up step to delete the Labelbox assets created within this guide. You will need to uncomment the delete methods shown."
186+
],
187+
"cell_type": "markdown"
188+
},
189+
{
190+
"metadata": {},
191+
"source": "# project.delete()\n# client.delete_unused_ontology(ontology.uid)\n# dataset.delete()",
192+
"cell_type": "code",
193+
"outputs": [],
194+
"execution_count": null
195+
}
196+
]
197+
}

examples/template.ipynb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@
2727
],
2828
"cell_type": "markdown"
2929
},
30+
{
31+
"metadata": {},
32+
"source": [
33+
"# Labelbox Example SDK Template\n",
34+
"\n",
35+
"This notebook serves as a template to guide develop of Labelbox example notebooks. Review notebook [CONTRIBUTING.md](https://github.com/Labelbox/labelbox-python/blob/develop/examples/CONTRIBUTING.md) for more detailed information."
36+
],
37+
"cell_type": "markdown"
38+
},
39+
{
40+
"metadata": {},
41+
"source": [
42+
"## Set Up"
43+
],
44+
"cell_type": "markdown"
45+
},
3046
{
3147
"metadata": {},
3248
"source": "%pip install -q \"labelbox[data]\"\n# Always install \"labelbox[data]\" over labelbox to ensure dependencies are installed correctly\n# Include other installs in this block with similar format: \"%pip install -q <package name>\"",
@@ -51,7 +67,7 @@
5167
},
5268
{
5369
"metadata": {},
54-
"source": "# Add your API key\nAPI_KEY = None\n# To get your API key go to: Workspace settings -> API -> Create API Key\nclient = lb.Client(api_key=API_KEY)",
70+
"source": "API_KEY = None\nclient = lb.Client(api_key=API_KEY)",
5571
"cell_type": "code",
5672
"outputs": [],
5773
"execution_count": null

0 commit comments

Comments
 (0)