You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"<a target=\"_blank\" href=\"https://colab.research.google.com/github/elastic/elasticsearch-labs/blob/main/supporting-blog-content/alternative-approach-for-parsing-pdfs-in-rag/alternative-approach-for-parsing-pdfs-in-rag.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n"
22
+
],
23
+
"metadata": {
24
+
"id": "e9-GuDRKCz_1"
25
+
}
26
+
},
27
+
{
28
+
"cell_type": "markdown",
29
+
"source": [
30
+
"##Objective\n",
31
+
"This Python script extracts text and tables from a PDF file, converts the tables into a human-readable text format using Azure OpenAI, and writes the processed content to a text file. The script uses pdfplumber to extract text and table data from each page of the PDF. For tables, it sends a cleaned version (handling any missing or None values) to Azure OpenAI, which generates a natural language summary of the table. The extracted non-table text and the summarized table text are then saved to a text file for easy search and readability."
32
+
],
33
+
"metadata": {
34
+
"id": "MBdflc9G0ICc"
35
+
}
36
+
},
37
+
{
38
+
"cell_type": "code",
39
+
"source": [
40
+
"!pip install pdfplumber"
41
+
],
42
+
"metadata": {
43
+
"id": "QBwz0_VNL1p6"
44
+
},
45
+
"execution_count": null,
46
+
"outputs": []
47
+
},
48
+
{
49
+
"cell_type": "markdown",
50
+
"source": [
51
+
"This code imports necessary libraries for PDF extraction, data processing, and interacting with Azure OpenAI via API calls. It retrieves the Azure OpenAI API key and endpoint from Google Colab's userdata storage, sets up the required headers, and prepares for sending requests to the Azure OpenAI service."
52
+
],
53
+
"metadata": {
54
+
"id": "QC37eVM90few"
55
+
}
56
+
},
57
+
{
58
+
"cell_type": "code",
59
+
"execution_count": null,
60
+
"metadata": {
61
+
"id": "X3vuHZTjK6l7"
62
+
},
63
+
"outputs": [],
64
+
"source": [
65
+
"import pdfplumber\n",
66
+
"import pandas as pd\n",
67
+
"import requests\n",
68
+
"import base64\n",
69
+
"import json\n",
70
+
"from getpass import getpass\n",
71
+
"import io # To create an in-memory file-like object\n",
"API_KEY = getpass(\"Azure OpenAI API Key: \")\n",
76
+
"\n",
77
+
"\n",
78
+
"headers = {\n",
79
+
"\"Content-Type\": \"application/json\",\n",
80
+
"\"api-key\": API_KEY,\n",
81
+
"}\n",
82
+
"\n",
83
+
"\n"
84
+
]
85
+
},
86
+
{
87
+
"cell_type": "markdown",
88
+
"source": [
89
+
"This code defines two functions: extract_table_text_from_openai and parse_pdf. The extract_table_text_from_openai function sends a table's plain text to Azure OpenAI for conversion into a human-readable description by building a request payload and handling the response. The parse_pdf function processes a PDF file page by page, extracting both text and tables, and sends the extracted tables to Azure OpenAI for summarization, saving all the content (including summarized tables) to a text file."
0 commit comments