Skip to content

Commit

Permalink
feat: simplified code in image-to-text cookbook (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
adubovik committed Apr 8, 2024
1 parent c10dada commit 43d43bb
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions dial-cookbook/examples/how_to_call_image_to_text_applications.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
"def read_image_base64(image_path: str) -> str:\n",
" with open(image_path, \"rb\") as image_file:\n",
" image_base64 = base64.b64encode(image_file.read()).decode()\n",
" return image_base64\n"
" return image_base64"
]
},
{
Expand Down Expand Up @@ -208,20 +208,15 @@
}
],
"source": [
"from io import BytesIO\n",
"import json\n",
"\n",
"image_base64 = read_image_base64(\"./data/images/square.png\")\n",
"image_bytes = base64.b64decode(image_base64)\n",
"image_file = BytesIO(image_bytes)\n",
"files = {'file': ('square.png', image_file, 'image/png')}\n",
"\n",
"metadata = requests.put(\n",
" f\"{dial_url}/v1/files/{bucket}/images/square.png\",\n",
" headers={\"Api-Key\": \"dial_api_key\"},\n",
" files=files,\n",
").json()\n",
"print(f\"Metadata: {json.dumps(metadata, indent=2)}\")"
"with open(\"./data/images/square.png\", \"rb\") as file:\n",
" metadata = requests.put(\n",
" f\"{dial_url}/v1/files/{bucket}/images/square.png\",\n",
" headers={\"Api-Key\": \"dial_api_key\"},\n",
" files={'file': ('square.png', file, 'image/png')},\n",
" ).json()\n",
" print(f\"Metadata: {json.dumps(metadata, indent=2)}\")"
]
},
{
Expand Down Expand Up @@ -313,6 +308,8 @@
}
],
"source": [
"image_base64 = read_image_base64(\"./data/images/square.png\")\n",
"\n",
"os.environ[\"IMAGE_BASE64\"] = image_base64\n",
"\n",
"display_base64_image(image_base64)\n",
Expand Down

0 comments on commit 43d43bb

Please sign in to comment.