|
90 | 90 | "id": "5ad877ea09dd" |
91 | 91 | }, |
92 | 92 | "source": [ |
93 | | - "| | |\n", |
94 | | - "|-|-|\n", |
95 | | - "|Author(s) | [Holt Skinner](https://github.com/holtskinner) |" |
| 93 | + "| Author |\n", |
| 94 | + "| --- |\n", |
| 95 | + "| [Holt Skinner](https://github.com/holtskinner) |" |
96 | 96 | ] |
97 | 97 | }, |
98 | 98 | { |
|
139 | 139 | "%pip install --upgrade -q google-genai PyPDF2" |
140 | 140 | ] |
141 | 141 | }, |
142 | | - { |
143 | | - "cell_type": "markdown", |
144 | | - "metadata": { |
145 | | - "id": "R5Xep4W9lq-Z" |
146 | | - }, |
147 | | - "source": [ |
148 | | - "### Restart current runtime\n", |
149 | | - "\n", |
150 | | - "To use the newly installed packages in this Jupyter runtime, you must restart the runtime. You can do this by running the cell below, which will restart the current kernel." |
151 | | - ] |
152 | | - }, |
153 | | - { |
154 | | - "cell_type": "code", |
155 | | - "execution_count": null, |
156 | | - "metadata": { |
157 | | - "id": "XRvKdaPDTznN" |
158 | | - }, |
159 | | - "outputs": [], |
160 | | - "source": [ |
161 | | - "# Restart kernel after installs so that your environment can access the new packages\n", |
162 | | - "import IPython\n", |
163 | | - "\n", |
164 | | - "app = IPython.Application.instance()\n", |
165 | | - "app.kernel.do_shutdown(True)" |
166 | | - ] |
167 | | - }, |
168 | | - { |
169 | | - "cell_type": "markdown", |
170 | | - "metadata": { |
171 | | - "id": "SbmM4z7FOBpM" |
172 | | - }, |
173 | | - "source": [ |
174 | | - "<div class=\"alert alert-block alert-warning\">\n", |
175 | | - "<b>⚠️ The kernel is going to restart. Please wait until it is finished before continuing to the next step. ⚠️</b>\n", |
176 | | - "</div>\n" |
177 | | - ] |
178 | | - }, |
179 | 142 | { |
180 | 143 | "cell_type": "markdown", |
181 | 144 | "metadata": { |
|
235 | 198 | "if not PROJECT_ID or PROJECT_ID == \"[your-project-id]\":\n", |
236 | 199 | " PROJECT_ID = str(os.environ.get(\"GOOGLE_CLOUD_PROJECT\"))\n", |
237 | 200 | "\n", |
238 | | - "LOCATION = os.environ.get(\"GOOGLE_CLOUD_REGION\", \"us-central1\")\n", |
| 201 | + "LOCATION = os.environ.get(\"GOOGLE_CLOUD_REGION\", \"global\")\n", |
239 | 202 | "\n", |
240 | 203 | "client = genai.Client(vertexai=True, project=PROJECT_ID, location=LOCATION)" |
241 | 204 | ] |
|
263 | 226 | "import PyPDF2\n", |
264 | 227 | "from google.genai.types import (\n", |
265 | 228 | " GenerateContentConfig,\n", |
| 229 | + " GoogleSearch,\n", |
266 | 230 | " HarmBlockThreshold,\n", |
267 | 231 | " HarmCategory,\n", |
268 | 232 | " Part,\n", |
269 | 233 | " SafetySetting,\n", |
| 234 | + " Tool,\n", |
270 | 235 | ")" |
271 | 236 | ] |
272 | 237 | }, |
|
278 | 243 | "source": [ |
279 | 244 | "### Load the Gemini model\n", |
280 | 245 | "\n", |
281 | | - "Gemini is a multimodal model that supports multimodal prompts. You can include text, image(s), PDFs, audio, and video in your prompt requests and get text or code responses." |
| 246 | + "Gemini is a multimodal model that supports multimodal prompts. You can include text, image(s), PDFs, audio, and video in your prompt requests." |
282 | 247 | ] |
283 | 248 | }, |
284 | 249 | { |
|
301 | 266 | " )\n", |
302 | 267 | " ],\n", |
303 | 268 | " system_instruction=\"You are an expert in musicology and music history.\",\n", |
| 269 | + " tools=[Tool(google_search=GoogleSearch())],\n", |
304 | 270 | ")" |
305 | 271 | ] |
306 | 272 | }, |
|
334 | 300 | "Title\n", |
335 | 301 | "Composer with lifetime\n", |
336 | 302 | "Tempo Marking\n", |
337 | | - "Composition Year\n", |
338 | 303 | "A description of the piece\n", |
339 | 304 | "\"\"\"\n", |
340 | 305 | "\n", |
|
430 | 395 | "outputs": [], |
431 | 396 | "source": [ |
432 | 397 | "sheet_music_pdf_uri = \"gs://github-repo/use-cases/sheet-music/SebbenCrudele.pdf\"\n", |
| 398 | + "output_file_name = \"SebbenCrudele.pdf\"\n", |
433 | 399 | "\n", |
434 | 400 | "sheet_music_extraction_prompt = \"\"\"The following document is a piece of sheet music. Your task is to output structured metadata about the piece of music in the document. Correct any mistakes that are in the document and fill in missing information when not present in the document.\n", |
435 | 401 | "\n", |
|
449 | 415 | " mime_type=\"application/pdf\",\n", |
450 | 416 | ")\n", |
451 | 417 | "\n", |
452 | | - "config = GenerateContentConfig(temperature=1, response_mime_type=\"application/json\")\n", |
| 418 | + "config.response_mime_type = \"application/json\"\n", |
453 | 419 | "\n", |
454 | 420 | "# Send to Gemini\n", |
455 | 421 | "response = client.models.generate_content(\n", |
|
464 | 430 | "new_metadata = json.loads(response.text)" |
465 | 431 | ] |
466 | 432 | }, |
467 | | - { |
468 | | - "cell_type": "code", |
469 | | - "execution_count": null, |
470 | | - "metadata": { |
471 | | - "id": "dc7aedf8e698" |
472 | | - }, |
473 | | - "outputs": [], |
474 | | - "source": [ |
475 | | - "new_metadata = response.parsed\n", |
476 | | - "\n", |
477 | | - "print(new_metadata)" |
478 | | - ] |
479 | | - }, |
480 | 433 | { |
481 | 434 | "cell_type": "markdown", |
482 | 435 | "metadata": { |
|
494 | 447 | }, |
495 | 448 | "outputs": [], |
496 | 449 | "source": [ |
497 | | - "! gcloud storage cp {sheet_music_pdf_uri} ." |
| 450 | + "! gcloud storage cp {sheet_music_pdf_uri} {output_file_name}" |
498 | 451 | ] |
499 | 452 | }, |
500 | 453 | { |
|
513 | 466 | " new_metadata (dict): Dictionary containing the new metadata fields and values.\n", |
514 | 467 | " Example: {'/Author': 'John Doe', '/Title': 'My Report'}\n", |
515 | 468 | " \"\"\"\n", |
516 | | - "\n", |
517 | 469 | " with open(file_path, \"rb\") as pdf_file:\n", |
518 | 470 | " pdf_reader = PyPDF2.PdfReader(pdf_file)\n", |
519 | 471 | " pdf_writer = PyPDF2.PdfWriter()\n", |
520 | 472 | "\n", |
521 | | - " for page_num in range(len(pdf_reader.pages)):\n", |
522 | | - " page = pdf_reader.pages[page_num]\n", |
523 | | - " pdf_writer.add_page(page)\n", |
524 | | - "\n", |
| 473 | + " pdf_writer.clone_reader_document_root(pdf_reader)\n", |
525 | 474 | " pdf_writer.add_metadata(new_metadata)\n", |
526 | 475 | "\n", |
527 | 476 | " with open(file_path, \"wb\") as out_file:\n", |
528 | 477 | " pdf_writer.write(out_file)\n", |
529 | 478 | "\n", |
530 | 479 | "\n", |
531 | | - "edit_pdf_metadata(\"SebbenCrudele.pdf\", new_metadata)" |
| 480 | + "edit_pdf_metadata(output_file_name, new_metadata)" |
| 481 | + ] |
| 482 | + }, |
| 483 | + { |
| 484 | + "cell_type": "code", |
| 485 | + "execution_count": null, |
| 486 | + "metadata": { |
| 487 | + "id": "jtbQo-df1TTu" |
| 488 | + }, |
| 489 | + "outputs": [], |
| 490 | + "source": [ |
| 491 | + "pdf_reader = PyPDF2.PdfReader(output_file_name)\n", |
| 492 | + "print(pdf_reader.metadata)" |
532 | 493 | ] |
533 | 494 | } |
534 | 495 | ], |
|
0 commit comments