Skip to content

Commit 22a68ff

Browse files
authored
refactor: Batch prediction using GCS supports global endpoint (#2394)
1 parent e8baf65 commit 22a68ff

File tree

1 file changed

+29
-45
lines changed

1 file changed

+29
-45
lines changed

gemini/batch-prediction/intro_batch_prediction.ipynb

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -150,44 +150,6 @@
150150
"%pip install --upgrade --quiet google-genai pandas google-cloud-storage google-cloud-bigquery"
151151
]
152152
},
153-
{
154-
"cell_type": "markdown",
155-
"metadata": {
156-
"id": "R5Xep4W9lq-Z"
157-
},
158-
"source": [
159-
"### Restart runtime\n",
160-
"\n",
161-
"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 restarts the current kernel.\n",
162-
"\n",
163-
"The restart might take a minute or longer. After it's restarted, continue to the next step."
164-
]
165-
},
166-
{
167-
"cell_type": "code",
168-
"execution_count": null,
169-
"metadata": {
170-
"id": "XRvKdaPDTznN"
171-
},
172-
"outputs": [],
173-
"source": [
174-
"import IPython\n",
175-
"\n",
176-
"app = IPython.Application.instance()\n",
177-
"app.kernel.do_shutdown(True)"
178-
]
179-
},
180-
{
181-
"cell_type": "markdown",
182-
"metadata": {
183-
"id": "SbmM4z7FOBpM"
184-
},
185-
"source": [
186-
"<div class=\"alert alert-block alert-warning\">\n",
187-
"<b>⚠️ The kernel is going to restart. Wait until it's finished before continuing to the next step. ⚠️</b>\n",
188-
"</div>\n"
189-
]
190-
},
191153
{
192154
"cell_type": "markdown",
193155
"metadata": {
@@ -269,7 +231,7 @@
269231
"if not PROJECT_ID or PROJECT_ID == \"[your-project-id]\":\n",
270232
" PROJECT_ID = str(os.environ.get(\"GOOGLE_CLOUD_PROJECT\"))\n",
271233
"\n",
272-
"LOCATION = os.environ.get(\"GOOGLE_CLOUD_REGION\", \"us-central1\")"
234+
"LOCATION = os.environ.get(\"GOOGLE_CLOUD_REGION\", \"global\")"
273235
]
274236
},
275237
{
@@ -293,7 +255,7 @@
293255
"\n",
294256
"You can find a list of the Gemini models that support batch predictions in the [Multimodal models that support batch predictions](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/batch-prediction-gemini#multimodal_models_that_support_batch_predictions) page.\n",
295257
"\n",
296-
"This tutorial uses Gemini 2.0 Flash (`gemini-2.0-flash-001`) model."
258+
"This tutorial uses Gemini 2.5 Flash (`gemini-2.5-flash`) model."
297259
]
298260
},
299261
{
@@ -304,7 +266,7 @@
304266
},
305267
"outputs": [],
306268
"source": [
307-
"MODEL_ID = \"gemini-2.0-flash-001\" # @param {type:\"string\", isTemplate: true}"
269+
"MODEL_ID = \"gemini-2.5-flash\" # @param {type:\"string\", isTemplate: true}"
308270
]
309271
},
310272
{
@@ -378,12 +340,13 @@
378340
"outputs": [],
379341
"source": [
380342
"BUCKET_URI = \"[your-cloud-storage-bucket]\" # @param {type:\"string\"}\n",
343+
"GCS_LOCATION = \"us-central1\" # @param {type:\"string\"}\n",
381344
"\n",
382345
"if BUCKET_URI == \"[your-cloud-storage-bucket]\":\n",
383346
" TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
384347
" BUCKET_URI = f\"gs://{PROJECT_ID}-{TIMESTAMP}\"\n",
385348
"\n",
386-
" ! gsutil mb -l {LOCATION} -p {PROJECT_ID} {BUCKET_URI}"
349+
" ! gsutil mb -l {GCS_LOCATION} -p {PROJECT_ID} {BUCKET_URI}"
387350
]
388351
},
389352
{
@@ -477,7 +440,11 @@
477440
"outputs": [],
478441
"source": [
479442
"# Refresh the job until complete\n",
480-
"while gcs_batch_job.state == \"JOB_STATE_RUNNING\":\n",
443+
"while gcs_batch_job.state in (\n",
444+
" \"JOB_STATE_RUNNING\",\n",
445+
" \"JOB_STATE_PENDING\",\n",
446+
" \"JOB_STATE_QUEUED\",\n",
447+
"):\n",
481448
" time.sleep(5)\n",
482449
" gcs_batch_job = client.batches.get(name=gcs_batch_job.name)\n",
483450
"\n",
@@ -544,7 +511,20 @@
544511
"id": "bfb2a462a7c6"
545512
},
546513
"source": [
547-
"## BigQuery"
514+
"## BigQuery\n",
515+
"\n",
516+
"⚠️ Batch predictions using BigQuery currently does not support Global endpoints. "
517+
]
518+
},
519+
{
520+
"cell_type": "code",
521+
"execution_count": null,
522+
"metadata": {
523+
"id": "9c694724e069"
524+
},
525+
"outputs": [],
526+
"source": [
527+
"client = genai.Client(vertexai=True, project=PROJECT_ID, location=\"us-central1\")"
548528
]
549529
},
550530
{
@@ -755,7 +735,11 @@
755735
"outputs": [],
756736
"source": [
757737
"# Refresh the job until complete\n",
758-
"while bq_batch_job.state == \"JOB_STATE_RUNNING\":\n",
738+
"while bq_batch_job.state in (\n",
739+
" \"JOB_STATE_RUNNING\",\n",
740+
" \"JOB_STATE_PENDING\",\n",
741+
" \"JOB_STATE_QUEUED\",\n",
742+
"):\n",
759743
" time.sleep(5)\n",
760744
" bq_batch_job = client.batches.get(name=bq_batch_job.name)\n",
761745
"\n",

0 commit comments

Comments
 (0)