|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "cd4f8056-2015-4c28-8974-d9862db07e84", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "Setup" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": null, |
| 14 | + "id": "8b8e5368-6268-4da9-9e8d-5c38637da8a5", |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import os\n", |
| 19 | + "\n", |
| 20 | + "def get_api_key():\n", |
| 21 | + " api_key = os.getenv(\"UNS_API_KEY\")\n", |
| 22 | + " if api_key is None:\n", |
| 23 | + " raise ValueError(\"\"\"UNS_API_KEY environment variable not set. \n", |
| 24 | + "Set it in your current shell session with `export UNS_API_KEY=<api_key>`\"\"\")\n", |
| 25 | + " return api_key" |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "markdown", |
| 30 | + "id": "11822c83-0791-432c-b1fb-05d8e2ae25bb", |
| 31 | + "metadata": {}, |
| 32 | + "source": [ |
| 33 | + "\"Usage\" instructions from README for `unstructured-python-client` (as of 01/29/2023)" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "code", |
| 38 | + "execution_count": null, |
| 39 | + "id": "5c28a39c-ad38-47a5-8247-a2fa1488313c", |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [], |
| 42 | + "source": [ |
| 43 | + "from unstructured_client import UnstructuredClient\n", |
| 44 | + "from unstructured_client.models import shared\n", |
| 45 | + "from unstructured_client.models.errors import SDKError\n", |
| 46 | + "\n", |
| 47 | + "s = UnstructuredClient(api_key_auth=get_api_key())\n", |
| 48 | + "\n", |
| 49 | + "filename = \"sample-docs/layout-parser-paper-fast.pdf\"\n", |
| 50 | + "\n", |
| 51 | + "with open(filename, \"rb\") as f:\n", |
| 52 | + " # Note that this currently only supports a single filea\n", |
| 53 | + " files=shared.Files(\n", |
| 54 | + " content=f.read(),\n", |
| 55 | + " file_name=filename,\n", |
| 56 | + "\t)\n", |
| 57 | + "\n", |
| 58 | + "req = shared.PartitionParameters(\n", |
| 59 | + " files=files,\n", |
| 60 | + " # Other partition params\n", |
| 61 | + " strategy='ocr_only',\n", |
| 62 | + " languages=[\"eng\"],\n", |
| 63 | + ")\n", |
| 64 | + "\n", |
| 65 | + "try:\n", |
| 66 | + " resp = s.general.partition(req)\n", |
| 67 | + " print(resp.elements[0])\n", |
| 68 | + "except SDKError as e:\n", |
| 69 | + " print(e)\n", |
| 70 | + "\n", |
| 71 | + "# {\n", |
| 72 | + "# 'type': 'UncategorizedText', \n", |
| 73 | + "# 'element_id': 'fc550084fda1e008e07a0356894f5816', \n", |
| 74 | + "# 'metadata': {\n", |
| 75 | + "# 'filename': 'layout-parser-paper-fast.pdf', \n", |
| 76 | + "# 'filetype': 'application/pdf', \n", |
| 77 | + "# 'languages': ['eng'], \n", |
| 78 | + "# 'page_number': 1\n", |
| 79 | + "# }\n", |
| 80 | + "# }" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": null, |
| 86 | + "id": "f5dfdb68-ba5d-4d21-98b2-4efe04126b7a", |
| 87 | + "metadata": {}, |
| 88 | + "outputs": [], |
| 89 | + "source": [] |
| 90 | + } |
| 91 | + ], |
| 92 | + "metadata": { |
| 93 | + "kernelspec": { |
| 94 | + "display_name": "Python 3 (ipykernel)", |
| 95 | + "language": "python", |
| 96 | + "name": "python3" |
| 97 | + }, |
| 98 | + "language_info": { |
| 99 | + "codemirror_mode": { |
| 100 | + "name": "ipython", |
| 101 | + "version": 3 |
| 102 | + }, |
| 103 | + "file_extension": ".py", |
| 104 | + "mimetype": "text/x-python", |
| 105 | + "name": "python", |
| 106 | + "nbconvert_exporter": "python", |
| 107 | + "pygments_lexer": "ipython3", |
| 108 | + "version": "3.10.13" |
| 109 | + } |
| 110 | + }, |
| 111 | + "nbformat": 4, |
| 112 | + "nbformat_minor": 5 |
| 113 | +} |
0 commit comments