Skip to content

Commit a483159

Browse files
committed
fix: add support for jupyterlite / pyodide and async
threading is not supported in wasm, in this case workflow must be async
1 parent 29f87c8 commit a483159

4 files changed

Lines changed: 432 additions & 13 deletions

File tree

examples/human_in_the_loop.ipynb

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "d451d919",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from enum import Enum\n",
11+
"import json\n",
12+
"from typing import Any, Optional\n",
13+
"from oold.model import LinkedBaseModel\n",
14+
"from pydantic import Field\n",
15+
"from awl.hitl import entry_point, hitl\n",
16+
"\n",
17+
"class MachineParams(LinkedBaseModel):\n",
18+
" \"\"\"Parameters transferred to the machine\"\"\"\n",
19+
" model_config = {\n",
20+
" \"json_schema_extra\": {\n",
21+
" \"required\": [\"param1\"],\n",
22+
" },\n",
23+
" }\n",
24+
" param1: int = Field(50, ge=0, le=100)\n",
25+
"\n",
26+
"class Quality(str, Enum):\n",
27+
" good = \"Good\"\n",
28+
" bad = \"Bad\"\n",
29+
" worse = \"Worse\"\n",
30+
"\n",
31+
"class ProcessDocumentation(LinkedBaseModel):\n",
32+
" \"\"\"Visual result inspection\"\"\"\n",
33+
" quality: Quality\n",
34+
" \"\"\"Good is defined as...\"\"\"\n",
35+
"\n",
36+
"class YesNoEnum(Enum):\n",
37+
" yes = True\n",
38+
" no = False\n",
39+
"\n",
40+
"class YesNo(LinkedBaseModel):\n",
41+
" answer: bool # YesNoEnum\n",
42+
"\n",
43+
"@hitl\n",
44+
"def set_machine_params(params: MachineParams):\n",
45+
" # transfer params to machine\n",
46+
" return params\n",
47+
"\n",
48+
"@hitl\n",
49+
"def document_result(params: ProcessDocumentation):\n",
50+
" # validate documentation\n",
51+
" return params\n",
52+
"\n",
53+
"@hitl\n",
54+
"def document_more(params: YesNo) -> YesNo:\n",
55+
" return params\n",
56+
"\n",
57+
"def archive_data(params: Any):\n",
58+
" # store documentation in database\n",
59+
" pass\n",
60+
"\n",
61+
"@entry_point(gui=True, jupyter=True)\n",
62+
"def workflow():\n",
63+
" print(\"Enter\")\n",
64+
" machine_params = set_machine_params() # prompts user\n",
65+
" do_document_more = True\n",
66+
" while(do_document_more):\n",
67+
" result_evaluation = document_result() # prompts user\n",
68+
" do_document_more = (document_more()).answer\n",
69+
" \n",
70+
" print(\"Machine parameters: \", machine_params)\n",
71+
" print(\"Result evaluation: \", result_evaluation)\n",
72+
" archive_data(machine_params) # runs automatically\n",
73+
" archive_data(result_evaluation) # runs automatically\n",
74+
" \n",
75+
"workflow()\n",
76+
"print(\"Done\")"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"id": "ec9290e1-65d2-4263-ac7c-751fbae77508",
83+
"metadata": {},
84+
"outputs": [],
85+
"source": []
86+
}
87+
],
88+
"metadata": {
89+
"kernelspec": {
90+
"display_name": "Python 3 (ipykernel)",
91+
"language": "python",
92+
"name": "python3"
93+
},
94+
"language_info": {
95+
"codemirror_mode": {
96+
"name": "ipython",
97+
"version": 3
98+
},
99+
"file_extension": ".py",
100+
"mimetype": "text/x-python",
101+
"name": "python",
102+
"nbconvert_exporter": "python",
103+
"pygments_lexer": "ipython3",
104+
"version": "3.11.6"
105+
}
106+
},
107+
"nbformat": 4,
108+
"nbformat_minor": 5
109+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "1705973e",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from enum import Enum\n",
11+
"import json\n",
12+
"from typing import Any, Optional\n",
13+
"from oold.model import LinkedBaseModel\n",
14+
"from pydantic import Field\n",
15+
"from awl.hitl import entry_point, hitl\n",
16+
"\n",
17+
"class MachineParams(LinkedBaseModel):\n",
18+
" \"\"\"Parameters transferred to the machine\"\"\"\n",
19+
" model_config = {\n",
20+
" \"json_schema_extra\": {\n",
21+
" \"required\": [\"param1\"],\n",
22+
" },\n",
23+
" }\n",
24+
" param1: int = Field(50, ge=0, le=100)\n",
25+
"\n",
26+
"class Quality(str, Enum):\n",
27+
" good = \"Good\"\n",
28+
" bad = \"Bad\"\n",
29+
" worse = \"Worse\"\n",
30+
"\n",
31+
"class ProcessDocumentation(LinkedBaseModel):\n",
32+
" \"\"\"Visual result inspection\"\"\"\n",
33+
" quality: Quality\n",
34+
" \"\"\"Good is defined as...\"\"\"\n",
35+
"\n",
36+
"class YesNoEnum(Enum):\n",
37+
" yes = True\n",
38+
" no = False\n",
39+
"\n",
40+
"class YesNo(LinkedBaseModel):\n",
41+
" answer: bool # YesNoEnum\n",
42+
"\n",
43+
"@hitl\n",
44+
"async def set_machine_params(params: MachineParams):\n",
45+
" # transfer params to machine\n",
46+
" return params\n",
47+
"\n",
48+
"@hitl\n",
49+
"async def document_result(params: ProcessDocumentation):\n",
50+
" # validate documentation\n",
51+
" return params\n",
52+
"\n",
53+
"@hitl\n",
54+
"async def document_more(params: YesNo) -> YesNo:\n",
55+
" return params\n",
56+
"\n",
57+
"def archive_data(params: Any):\n",
58+
" # store documentation in database\n",
59+
" pass\n",
60+
"\n",
61+
"@entry_point(gui=True, jupyter=True)\n",
62+
"async def workflow():\n",
63+
" print(\"Enter\")\n",
64+
" machine_params = await set_machine_params() # prompts user\n",
65+
" do_document_more = True\n",
66+
" while(do_document_more):\n",
67+
" result_evaluation = await document_result() # prompts user\n",
68+
" do_document_more = (await document_more()).answer\n",
69+
" \n",
70+
" print(\"Machine parameters: \", machine_params)\n",
71+
" print(\"Result evaluation: \", result_evaluation)\n",
72+
" archive_data(machine_params) # runs automatically\n",
73+
" archive_data(result_evaluation) # runs automatically\n",
74+
" \n",
75+
"await workflow()\n",
76+
"print(\"Done\")"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"id": "185aac14-0e5b-4ba1-857d-632912bd8fb6",
83+
"metadata": {},
84+
"outputs": [],
85+
"source": []
86+
}
87+
],
88+
"metadata": {
89+
"kernelspec": {
90+
"display_name": "Python 3 (ipykernel)",
91+
"language": "python",
92+
"name": "python3"
93+
},
94+
"language_info": {
95+
"codemirror_mode": {
96+
"name": "ipython",
97+
"version": 3
98+
},
99+
"file_extension": ".py",
100+
"mimetype": "text/x-python",
101+
"name": "python",
102+
"nbconvert_exporter": "python",
103+
"pygments_lexer": "ipython3",
104+
"version": "3.11.6"
105+
}
106+
},
107+
"nbformat": 4,
108+
"nbformat_minor": 5
109+
}

0 commit comments

Comments
 (0)