Use Gemini to summarize PDF files

This sample demonstrates how to use Gemini to generate a summary of a PDF file.

Code sample

Python

Before trying this sample, follow the Python setup instructions in the Vertex AI quickstart using client libraries. For more information, see the Vertex AI Python API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import vertexai
from vertexai.generative_models import GenerativeModel, Part

# TODO (developer): update project id
vertexai.init(project=PROJECT_ID, location="us-central1")

model = GenerativeModel("gemini-1.5-flash-002")

contents = [
    # Text prompt
    "Summarise this file",
    # Example PDF document on Transformers, a neural network architecture.
    Part.from_uri(
        "https://storage.googleapis.com/cloud-samples-data/generative-ai/pdf/1706.03762v7.pdf",
        "application/pdf",
    ),
]

response = model.generate_content(contents)
print(response.text)
# Example response:
#     'This paper introduces the Transformer, a new neural network architecture for '
#     'sequence transduction, which uses an attention mechanism to learn global '
#     'dependencies between input and output sequences. The Transformer ...

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.