mirror of
https://github.com/wassname/prob_jsonformer.git
synced 2026-09-09 11:29:57 +08:00
support cuda #12
This commit is contained in:
+75
-74
@@ -9,7 +9,7 @@
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/home/ubuntu/jsonllm/.venv/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
||||
"/home/ubuntu/jsonformer/.venv/lib/python3.8/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
||||
" from .autonotebook import tqdm as notebook_tqdm\n"
|
||||
]
|
||||
},
|
||||
@@ -26,12 +26,68 @@
|
||||
"from transformers import AutoModelForCausalLM, AutoTokenizer\n",
|
||||
"\n",
|
||||
"print(\"Loading model and tokenizer...\")\n",
|
||||
"model_name = \"databricks/dolly-v2-12b\"\n",
|
||||
"model = AutoModelForCausalLM.from_pretrained(model_name, use_cache=True)\n",
|
||||
"model_name = \"databricks/dolly-v2-3b\"\n",
|
||||
"model = AutoModelForCausalLM.from_pretrained(model_name, use_cache=True, device_map=\"auto\")\n",
|
||||
"tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True, use_cache=True)\n",
|
||||
"print(\"Loaded model and tokenizer\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Generating...\n",
|
||||
"{\n",
|
||||
" temperature: \u001b[32m\"22.0\"\u001b[0m,\n",
|
||||
" humidity: \u001b[32m\"60\"\u001b[0m,\n",
|
||||
" wind_speed: {\n",
|
||||
" value: \u001b[32m\"5\"\u001b[0m,\n",
|
||||
" unit: \u001b[32m\"mph\"\u001b[0m\n",
|
||||
" }\n",
|
||||
"}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from jsonformer.format import highlight_values\n",
|
||||
"from jsonformer.main import Jsonformer\n",
|
||||
"\n",
|
||||
"weather_schema = {\n",
|
||||
" \"type\": \"object\",\n",
|
||||
" \"properties\": {\n",
|
||||
" \"temperature\": {\"type\": \"string\"},\n",
|
||||
" \"humidity\": {\n",
|
||||
" \"type\": \"string\",\n",
|
||||
" },\n",
|
||||
" \"wind_speed\": {\n",
|
||||
" \"type\": \"object\",\n",
|
||||
" \"properties\": {\n",
|
||||
" \"value\": {\"type\": \"string\"},\n",
|
||||
" \"unit\": {\"type\": \"string\"},\n",
|
||||
" },\n",
|
||||
" },\n",
|
||||
" },\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"builder = Jsonformer(\n",
|
||||
" model=model,\n",
|
||||
" tokenizer=tokenizer,\n",
|
||||
" json_schema=weather_schema,\n",
|
||||
" prompt=\"generate the weather\",\n",
|
||||
" device=\"cuda\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"Generating...\")\n",
|
||||
"output = builder()\n",
|
||||
"\n",
|
||||
"highlight_values(output)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
@@ -43,75 +99,11 @@
|
||||
"text": [
|
||||
"Generating...\n",
|
||||
"{\n",
|
||||
" temperature: \u001b[32m2.2225\u001b[0m,\n",
|
||||
" humidity: \u001b[32m1.0\u001b[0m,\n",
|
||||
" wind_speed: {\n",
|
||||
" value: \u001b[32m0.0\u001b[0m,\n",
|
||||
" unit: \u001b[32m\"value\"\u001b[0m\n",
|
||||
" }\n",
|
||||
"}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from jsonformer.format import highlight_values\n",
|
||||
"from jsonformer.main import Jsonformer\n",
|
||||
"\n",
|
||||
"weather_schema = {\n",
|
||||
" \"type\": \"object\",\n",
|
||||
" \"properties\": {\n",
|
||||
" \"temperature\": {\"type\": \"number\"},\n",
|
||||
" \"humidity\": {\n",
|
||||
" \"type\": \"number\",\n",
|
||||
" },\n",
|
||||
" \"wind_speed\": {\n",
|
||||
" \"type\": \"object\",\n",
|
||||
" \"properties\": {\n",
|
||||
" \"value\": {\"type\": \"number\"},\n",
|
||||
" \"unit\": {\"type\": \"string\"},\n",
|
||||
" },\n",
|
||||
" },\n",
|
||||
" },\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"builder = Jsonformer(\n",
|
||||
" model=model,\n",
|
||||
" tokenizer=tokenizer,\n",
|
||||
" json_schema=weather_schema,\n",
|
||||
" prompt=\"generate the weather\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"Generating...\")\n",
|
||||
"output = builder()\n",
|
||||
"\n",
|
||||
"highlight_values(output)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Generating...\n",
|
||||
"{\n",
|
||||
" make: \u001b[32m\"Ford\"\u001b[0m,\n",
|
||||
" model: \u001b[32m\"Mustang\"\u001b[0m,\n",
|
||||
" year: \u001b[32m10.0\u001b[0m,\n",
|
||||
" make: \u001b[32m\"audi\"\u001b[0m,\n",
|
||||
" model: \u001b[32m\"model a4\"\u001b[0m,\n",
|
||||
" year: \u001b[32m1.0\u001b[0m,\n",
|
||||
" colors: [\n",
|
||||
" \u001b[32m\"red\"\u001b[0m,\n",
|
||||
" \u001b[32m\"white\"\u001b[0m,\n",
|
||||
" \u001b[32m\"blue\"\u001b[0m,\n",
|
||||
" \u001b[32m\"black\"\u001b[0m,\n",
|
||||
" \u001b[32m\"yellow\"\u001b[0m,\n",
|
||||
" \u001b[32m\"orange\"\u001b[0m,\n",
|
||||
" \u001b[32m\"green\"\u001b[0m,\n",
|
||||
" \u001b[32m\"pink\"\u001b[0m,\n",
|
||||
" \u001b[32m\"purple\"\u001b[0m,\n",
|
||||
" \u001b[32m\"violet\"\u001b[0m\n",
|
||||
" \u001b[32m\"blue\"\u001b[0m\n",
|
||||
" ]\n",
|
||||
"}\n"
|
||||
]
|
||||
@@ -123,11 +115,12 @@
|
||||
" \"properties\": {\n",
|
||||
" \"make\": {\"type\": \"string\"},\n",
|
||||
" \"model\": {\"type\": \"string\"},\n",
|
||||
" \"year\": {\"type\": \"number\"},\n",
|
||||
" \"year\": {\"type\": \"string\"},\n",
|
||||
" \"colors\": {\n",
|
||||
" \"type\": \"array\",\n",
|
||||
" \"items\": {\"type\": \"string\"},\n",
|
||||
" }\n",
|
||||
" },\n",
|
||||
" \"shouldUseTurnSignal\": {\"type\": \"boolean\"},\n",
|
||||
" },\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
@@ -136,6 +129,7 @@
|
||||
" tokenizer=tokenizer,\n",
|
||||
" json_schema=car,\n",
|
||||
" prompt=\"generate an example car\",\n",
|
||||
" device=\"cuda\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"Generating...\")\n",
|
||||
@@ -143,6 +137,13 @@
|
||||
"\n",
|
||||
"highlight_values(output)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
@@ -161,7 +162,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.11"
|
||||
"version": "3.8.10"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user