GeezerVison is showing github.com — docs: Update multimodal notebook to use public runtime helpers (#2451) · google/bigframes@e36dd8b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: notebooks/multimodal/multimodal_dataframe.ipynb +41-37Lines changed: 41 additions & 37 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 92 | 92 | }, | 93 | 93 | { | 94 | 94 | "cell_type": "code", | 95 | | - "execution_count": 9, | | 95 | + "execution_count": 1, | 96 | 96 | "metadata": { | 97 | 97 | "colab": { | 98 | 98 | "base_uri": "https://localhost:8080/" | | 128 | 128 | "import bigframes.bigquery as bbq" | 129 | 129 | ] | 130 | 130 | }, | | 131 | + { | | 132 | + "cell_type": "code", | | 133 | + "execution_count": 2, | | 134 | + "metadata": {}, | | 135 | + "outputs": [], | | 136 | + "source": [ | | 137 | + "import bigframes.bigquery as bbq\n", | | 138 | + "\n", | | 139 | + "def get_runtime_json_str(series, mode=\"R\", with_metadata=False):\n", | | 140 | + " \"\"\"\n", | | 141 | + " Get the runtime (contains signed URL to access gcs data) and apply the\n", | | 142 | + " ToJSONSTring transformation.\n", | | 143 | + " \n", | | 144 | + " Args:\n", | | 145 | + " series: bigframes.series.Series to operate on.\n", | | 146 | + " mode: \"R\" for read, \"RW\" for read/write.\n", | | 147 | + " with_metadata: Whether to fetch and include blob metadata.\n", | | 148 | + " \"\"\"\n", | | 149 | + " # 1. Optionally fetch metadata\n", | | 150 | + " s = (\n", | | 151 | + " bbq.obj.fetch_metadata(series)\n", | | 152 | + " if with_metadata\n", | | 153 | + " else series\n", | | 154 | + " )\n", | | 155 | + " \n", | | 156 | + " # 2. Retrieve the access URL runtime object\n", | | 157 | + " runtime = bbq.obj.get_access_url(s, mode=mode)\n", | | 158 | + " \n", | | 159 | + " # 3. Convert the runtime object to a JSON string\n", | | 160 | + " return bbq.to_json_string(runtime)" | | 161 | + ] | | 162 | + }, | 131 | 163 | { | 132 | 164 | "cell_type": "markdown", | 133 | 165 | "metadata": { | | 1290 | 1322 | }, | 1291 | 1323 | { | 1292 | 1324 | "cell_type": "code", | 1293 | | - "execution_count": 17, | | 1325 | + "execution_count": 3, | 1294 | 1326 | "metadata": { | 1295 | 1327 | "id": "oDDuYtUm5Yiy" | 1296 | 1328 | }, | 1297 | | - "outputs": [ | 1298 | | - { | 1299 | | - "name": "stderr", | 1300 | | - "output_type": "stream", | 1301 | | - "text": [ | 1302 | | - "/usr/local/google/home/shuowei/src/github.com/googleapis/python-bigquery-dataframes/bigframes/dtypes.py:959: JSONDtypeWarning: JSON columns will be represented as pandas.ArrowDtype(pyarrow.json_())\n", | 1303 | | - "instead of using `db_dtypes` in the future when available in pandas\n", | 1304 | | - "(https://github.com/pandas-dev/pandas/issues/60958) and pyarrow.\n", | 1305 | | - " warnings.warn(msg, bigframes.exceptions.JSONDtypeWarning)\n" | 1306 | | - ] | 1307 | | - } | 1308 | | - ], | | 1329 | + "outputs": [], | 1309 | 1330 | "source": [ | 1310 | 1331 | "df_pdf = bpd.from_glob_path(\"gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/documents/*\", name=\"pdf\")" | 1311 | 1332 | ] | | 1464 | 1485 | }, | 1465 | 1486 | { | 1466 | 1487 | "cell_type": "code", | 1467 | | - "execution_count": 10, | | 1488 | + "execution_count": 4, | 1468 | 1489 | "metadata": {}, | 1469 | 1490 | "outputs": [], | 1470 | 1491 | "source": [ | | 1474 | 1495 | }, | 1475 | 1496 | { | 1476 | 1497 | "cell_type": "code", | 1477 | | - "execution_count": 11, | | 1498 | + "execution_count": null, | 1478 | 1499 | "metadata": {}, | 1479 | 1500 | "outputs": [ | 1480 | 1501 | { | | 1486 | 1507 | "(https://github.com/pandas-dev/pandas/issues/60958) and pyarrow.\n", | 1487 | 1508 | " warnings.warn(msg, bigframes.exceptions.JSONDtypeWarning)\n" | 1488 | 1509 | ] | 1489 | | - }, | 1490 | | - { | 1491 | | - "data": { | 1492 | | - "text/html": [ | 1493 | | - "<pre>0 Now, as all books, not primarily intended as p...</pre>" | 1494 | | - ], | 1495 | | - "text/plain": [ | 1496 | | - "0 Now, as all books, not primarily intended as p...\n", | 1497 | | - "Name: transcribed_content, dtype: string" | 1498 | | - ] | 1499 | | - }, | 1500 | | - "execution_count": 11, | 1501 | | - "metadata": {}, | 1502 | | - "output_type": "execute_result" | 1503 | 1510 | } | 1504 | 1511 | ], | 1505 | 1512 | "source": [ | 1506 | | - "import bigframes.bigquery as bbq\n", | 1507 | | - "import bigframes.operations as ops\n", | 1508 | | - "\n", | 1509 | 1513 | "# The audio_transcribe function is a convenience wrapper around bigframes.bigquery.ai.generate.\n", | 1510 | 1514 | "# Here's how to perform the same operation directly:\n", | 1511 | 1515 | "\n", | | 1519 | 1523 | "\n", | 1520 | 1524 | "# Convert the audio series to the runtime representation required by the model.\n", | 1521 | 1525 | "# This involves fetching metadata and getting a signed access URL.\n", | 1522 | | - "audio_metadata = audio_series._apply_unary_op(ops.obj_fetch_metadata_op)\n", | 1523 | | - "audio_runtime = audio_metadata._apply_unary_op(ops.ObjGetAccessUrl(mode=\"R\"))\n", | | 1526 | + "audio_metadata = bbq.obj.fetch_metadata(audio_series)\n", | | 1527 | + "audio_runtime = bbq.obj.get_access_url(audio_metadata, mode=\"R\")\n", | 1524 | 1528 | "\n", | 1525 | 1529 | "transcribed_results = bbq.ai.generate(\n", | 1526 | 1530 | " prompt=(prompt_text, audio_runtime),\n", | | 1534 | 1538 | }, | 1535 | 1539 | { | 1536 | 1540 | "cell_type": "code", | 1537 | | - "execution_count": 12, | | 1541 | + "execution_count": null, | 1538 | 1542 | "metadata": {}, | 1539 | 1543 | "outputs": [ | 1540 | 1544 | { | | 1638 | 1642 | "\n", | 1639 | 1643 | "# Generate a JSON string containing the runtime information (including signed read URLs)\n", | 1640 | 1644 | "# This allows the UDF to download the images from Google Cloud Storage\n", | 1641 | | - "access_urls = exif_image_df[\"blob_col\"].blob.get_runtime_json_str(mode=\"R\")\n", | | 1645 | + "access_urls = get_runtime_json_str(exif_image_df[\"blob_col\"], mode=\"R\")\n", | 1642 | 1646 | "\n", | 1643 | 1647 | "# Apply the BigQuery Python UDF to the runtime JSON strings\n", | 1644 | 1648 | "# We cast to string to ensure the input matches the UDF's signature\n", | |
|