|
41 | 41 | "## 0. Setup" |
42 | 42 | ] |
43 | 43 | }, |
| 44 | + { |
| 45 | + "cell_type": "markdown", |
| 46 | + "id": "39a9ad0d", |
| 47 | + "metadata": {}, |
| 48 | + "source": [ |
| 49 | + "From Azure AI Foundry, after clicking on \"Use this model\" and going through standard Foundry setup you will need the provided Endpoint, Key and Deployment Name.\n", |
| 50 | + "\n", |
| 51 | + "The Endpoint is used as the url in the REST request, the Key is used for authentication in the header, and the Deployment Name is used in the body of the request." |
| 52 | + ] |
| 53 | + }, |
44 | 54 | { |
45 | 55 | "cell_type": "code", |
46 | 56 | "execution_count": null, |
|
50 | 60 | "source": [ |
51 | 61 | "AZURE_MISTRAL_DOCUMENT_AI_ENDPOINT = \"\"\n", |
52 | 62 | "AZURE_MISTRAL_DOCUMENT_AI_KEY = \"\"\n", |
| 63 | + "AZURE_AI_DEPLOYMENT_NAME = \"\"\n", |
53 | 64 | "REQUEST_HEADERS = {\n", |
54 | 65 | " \"Content-Type\": \"application/json\",\n", |
55 | 66 | " \"Authorization\": f\"Bearer {AZURE_MISTRAL_DOCUMENT_AI_KEY}\",\n", |
|
71 | 82 | "metadata": {}, |
72 | 83 | "outputs": [], |
73 | 84 | "source": [ |
74 | | - "!wget https://raw.githubusercontent.com/mistralai/cookbook/refs/heads/main/mistral/ocr/mistral7b.pdf" |
| 85 | + "!wget -P samples https://raw.githubusercontent.com/mistralai/cookbook/refs/heads/main/mistral/ocr/mistral7b.pdf" |
75 | 86 | ] |
76 | 87 | }, |
77 | 88 | { |
|
89 | 100 | "metadata": {}, |
90 | 101 | "outputs": [], |
91 | 102 | "source": [ |
92 | | - "def encode_image(image_path: str) -> str:\n", |
| 103 | + "def encode_file(file_path: str) -> str:\n", |
93 | 104 | " try:\n", |
94 | | - " with open(image_path, \"rb\") as image_file:\n", |
| 105 | + " with open(file_path, \"rb\") as image_file:\n", |
95 | 106 | " return base64.b64encode(image_file.read()).decode(\"utf-8\")\n", |
96 | 107 | " except FileNotFoundError:\n", |
97 | | - " print(f\"Error: The file {image_path} was not found.\")\n", |
| 108 | + " print(f\"Error: The file {file_path} was not found.\")\n", |
98 | 109 | " return None\n", |
99 | 110 | "\n", |
100 | 111 | "\n", |
|
139 | 150 | "metadata": {}, |
140 | 151 | "outputs": [], |
141 | 152 | "source": [ |
142 | | - "encodedDocument = encode_image(\"mistral7b.pdf\")" |
| 153 | + "encodedDocument = encode_file(\"samples/mistral7b.pdf\")" |
143 | 154 | ] |
144 | 155 | }, |
145 | 156 | { |
|
158 | 169 | "outputs": [], |
159 | 170 | "source": [ |
160 | 171 | "documentPayload = {\n", |
161 | | - " \"model\": \"mistral-document-ai-2505\",\n", |
| 172 | + " \"model\": f\"{AZURE_AI_DEPLOYMENT_NAME}\",\n", |
162 | 173 | " \"document\": {\n", |
163 | 174 | " \"type\": \"document_url\",\n", |
164 | 175 | " \"document_url\": f\"data:application/pdf;base64,{encodedDocument}\",\n", |
|
258 | 269 | "outputs": [], |
259 | 270 | "source": [ |
260 | 271 | "documentPayloadandImages = {\n", |
261 | | - " \"model\": \"mistral-document-ai-2505\",\n", |
| 272 | + " \"model\": f\"{AZURE_AI_DEPLOYMENT_NAME}\",\n", |
262 | 273 | " \"document\": {\n", |
263 | 274 | " \"type\": \"document_url\",\n", |
264 | 275 | " \"document_url\": f\"data:application/pdf;base64,{encodedDocument}\",\n", |
|
322 | 333 | "metadata": {}, |
323 | 334 | "outputs": [], |
324 | 335 | "source": [ |
325 | | - "ms8kDocument = encode_image(\"0000950170-25-100226.pdf\")" |
| 336 | + "ms8kDocument = encode_file(\"samples/0000950170-25-100226.pdf\")" |
326 | 337 | ] |
327 | 338 | }, |
328 | 339 | { |
|
333 | 344 | "outputs": [], |
334 | 345 | "source": [ |
335 | 346 | "msRequestPayload = {\n", |
336 | | - " \"model\": \"mistral-document-ai-2505\",\n", |
| 347 | + " \"model\": f\"{AZURE_AI_DEPLOYMENT_NAME}\",\n", |
337 | 348 | " \"document\": {\n", |
338 | 349 | " \"type\": \"document_url\",\n", |
339 | 350 | " \"document_url\": f\"data:application/pdf;base64,{ms8kDocument}\",\n", |
|
381 | 392 | "As observed, the extracted text in the table is accurate and true to the original tabular representation." |
382 | 393 | ] |
383 | 394 | }, |
| 395 | + { |
| 396 | + "cell_type": "markdown", |
| 397 | + "id": "876805a1", |
| 398 | + "metadata": {}, |
| 399 | + "source": [ |
| 400 | + "# 5. Microsoft Word Documents" |
| 401 | + ] |
| 402 | + }, |
| 403 | + { |
| 404 | + "cell_type": "code", |
| 405 | + "execution_count": null, |
| 406 | + "id": "5a679e09", |
| 407 | + "metadata": {}, |
| 408 | + "outputs": [], |
| 409 | + "source": [ |
| 410 | + "wordDocument = encode_file(\"samples/TranscriptFY25q4.docx\")" |
| 411 | + ] |
| 412 | + }, |
| 413 | + { |
| 414 | + "cell_type": "code", |
| 415 | + "execution_count": null, |
| 416 | + "id": "b3b2b474", |
| 417 | + "metadata": {}, |
| 418 | + "outputs": [], |
| 419 | + "source": [ |
| 420 | + "wordPayload = {\n", |
| 421 | + " \"model\": f\"{AZURE_AI_DEPLOYMENT_NAME}\",\n", |
| 422 | + " \"document\": {\n", |
| 423 | + " \"type\": \"document_url\",\n", |
| 424 | + " \"document_url\": f\"data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,{wordDocument}\",\n", |
| 425 | + " },\n", |
| 426 | + "}" |
| 427 | + ] |
| 428 | + }, |
| 429 | + { |
| 430 | + "cell_type": "code", |
| 431 | + "execution_count": null, |
| 432 | + "id": "2961fb92", |
| 433 | + "metadata": {}, |
| 434 | + "outputs": [], |
| 435 | + "source": [ |
| 436 | + "wordResponse = requests.post(\n", |
| 437 | + " url=AZURE_MISTRAL_DOCUMENT_AI_ENDPOINT,\n", |
| 438 | + " json=wordPayload,\n", |
| 439 | + " headers=REQUEST_HEADERS,\n", |
| 440 | + ")" |
| 441 | + ] |
| 442 | + }, |
| 443 | + { |
| 444 | + "cell_type": "code", |
| 445 | + "execution_count": null, |
| 446 | + "id": "0d1938c5", |
| 447 | + "metadata": {}, |
| 448 | + "outputs": [], |
| 449 | + "source": [ |
| 450 | + "# limiting to 1000 characters for easier reading\n", |
| 451 | + "display(Markdown(simple_combined_markdown(wordResponse.json()[\"pages\"][0])[:1000]))" |
| 452 | + ] |
| 453 | + }, |
| 454 | + { |
| 455 | + "cell_type": "markdown", |
| 456 | + "id": "31701ebe", |
| 457 | + "metadata": {}, |
| 458 | + "source": [ |
| 459 | + "# 6. Microsoft Powerpoint Presentations" |
| 460 | + ] |
| 461 | + }, |
| 462 | + { |
| 463 | + "cell_type": "code", |
| 464 | + "execution_count": null, |
| 465 | + "id": "c2ae57bd", |
| 466 | + "metadata": {}, |
| 467 | + "outputs": [], |
| 468 | + "source": [ |
| 469 | + "!wget -P samples https://github.com/mistralai/cookbook/raw/52ed72962a4d2be2c922919d7c25016b71b900df/data/sample.pptx" |
| 470 | + ] |
| 471 | + }, |
| 472 | + { |
| 473 | + "cell_type": "code", |
| 474 | + "execution_count": null, |
| 475 | + "id": "3c34a751", |
| 476 | + "metadata": {}, |
| 477 | + "outputs": [], |
| 478 | + "source": [ |
| 479 | + "powerpointDocument = encode_file(\"samples/sample.pptx\")" |
| 480 | + ] |
| 481 | + }, |
| 482 | + { |
| 483 | + "cell_type": "code", |
| 484 | + "execution_count": null, |
| 485 | + "id": "bfc3b09b", |
| 486 | + "metadata": {}, |
| 487 | + "outputs": [], |
| 488 | + "source": [ |
| 489 | + "pptxPayload = {\n", |
| 490 | + " \"model\": f\"{AZURE_AI_DEPLOYMENT_NAME}\",\n", |
| 491 | + " \"document\": {\n", |
| 492 | + " \"type\": \"document_url\",\n", |
| 493 | + " \"document_url\": f\"data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,{powerpointDocument}\",\n", |
| 494 | + " },\n", |
| 495 | + " \"include_image_base64\": \"false\",\n", |
| 496 | + " \"image_limit\": 0,\n", |
| 497 | + "}" |
| 498 | + ] |
| 499 | + }, |
| 500 | + { |
| 501 | + "cell_type": "code", |
| 502 | + "execution_count": null, |
| 503 | + "id": "b058975d", |
| 504 | + "metadata": {}, |
| 505 | + "outputs": [], |
| 506 | + "source": [ |
| 507 | + "pptxResponse = requests.post(\n", |
| 508 | + " url=AZURE_MISTRAL_DOCUMENT_AI_ENDPOINT,\n", |
| 509 | + " json=pptxPayload,\n", |
| 510 | + " headers=REQUEST_HEADERS,\n", |
| 511 | + ")" |
| 512 | + ] |
| 513 | + }, |
| 514 | + { |
| 515 | + "cell_type": "code", |
| 516 | + "execution_count": null, |
| 517 | + "id": "0f5addc6", |
| 518 | + "metadata": {}, |
| 519 | + "outputs": [], |
| 520 | + "source": [ |
| 521 | + "print(json.dumps(pptxResponse.json(), indent=4))" |
| 522 | + ] |
| 523 | + }, |
384 | 524 | { |
385 | 525 | "cell_type": "markdown", |
386 | 526 | "id": "188408f8", |
387 | 527 | "metadata": {}, |
388 | 528 | "source": [ |
389 | | - "# 4. Wrap-up" |
| 529 | + "# 7. Wrap-up" |
390 | 530 | ] |
391 | 531 | }, |
392 | 532 | { |
|
0 commit comments