GeezerVison is showing github.com — docs: use direct API for audio transcription (#2447) · google/bigframes@59cbc5d
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 +54-41Lines changed: 54 additions & 41 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 92 | 92 | }, | 93 | 93 | { | 94 | 94 | "cell_type": "code", | 95 | | - "execution_count": 3, | | 95 | + "execution_count": 9, | 96 | 96 | "metadata": { | 97 | 97 | "colab": { | 98 | 98 | "base_uri": "https://localhost:8080/" | | 1459 | 1459 | "cell_type": "markdown", | 1460 | 1460 | "metadata": {}, | 1461 | 1461 | "source": [ | 1462 | | - "### 6. Audio transcribe function" | | 1462 | + "### 6. Audio transcribe" | 1463 | 1463 | ] | 1464 | 1464 | }, | 1465 | 1465 | { | 1466 | 1466 | "cell_type": "code", | 1467 | | - "execution_count": 21, | | 1467 | + "execution_count": 10, | 1468 | 1468 | "metadata": {}, | 1469 | | - "outputs": [ | 1470 | | - { | 1471 | | - "name": "stderr", | 1472 | | - "output_type": "stream", | 1473 | | - "text": [ | 1474 | | - "/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", | 1475 | | - "instead of using `db_dtypes` in the future when available in pandas\n", | 1476 | | - "(https://github.com/pandas-dev/pandas/issues/60958) and pyarrow.\n", | 1477 | | - " warnings.warn(msg, bigframes.exceptions.JSONDtypeWarning)\n" | 1478 | | - ] | 1479 | | - } | 1480 | | - ], | | 1469 | + "outputs": [], | 1481 | 1470 | "source": [ | 1482 | 1471 | "audio_gcs_path = \"gs://bigframes_blob_test/audio/*\"\n", | 1483 | 1472 | "df = bpd.from_glob_path(audio_gcs_path, name=\"audio\")" | 1484 | 1473 | ] | 1485 | 1474 | }, | 1486 | 1475 | { | 1487 | 1476 | "cell_type": "code", | 1488 | | - "execution_count": 22, | | 1477 | + "execution_count": 11, | 1489 | 1478 | "metadata": {}, | 1490 | 1479 | "outputs": [ | 1491 | 1480 | { | 1492 | 1481 | "name": "stderr", | 1493 | 1482 | "output_type": "stream", | 1494 | 1483 | "text": [ | 1495 | | - "/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", | 1496 | | - "instead of using `db_dtypes` in the future when available in pandas\n", | 1497 | | - "(https://github.com/pandas-dev/pandas/issues/60958) and pyarrow.\n", | 1498 | | - " warnings.warn(msg, bigframes.exceptions.JSONDtypeWarning)\n", | 1499 | | - "/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", | | 1484 | + "/usr/local/google/home/shuowei/src/python-bigquery-dataframes/bigframes/dtypes.py:987: JSONDtypeWarning: JSON columns will be represented as pandas.ArrowDtype(pyarrow.json_())\n", | 1500 | 1485 | "instead of using `db_dtypes` in the future when available in pandas\n", | 1501 | 1486 | "(https://github.com/pandas-dev/pandas/issues/60958) and pyarrow.\n", | 1502 | 1487 | " warnings.warn(msg, bigframes.exceptions.JSONDtypeWarning)\n" | 1503 | 1488 | ] | 1504 | 1489 | }, | 1505 | 1490 | { | 1506 | 1491 | "data": { | | 1492 | + "text/html": [ | | 1493 | + "<pre>0 Now, as all books, not primarily intended as p...</pre>" | | 1494 | + ], | 1507 | 1495 | "text/plain": [ | 1508 | 1496 | "0 Now, as all books, not primarily intended as p...\n", | 1509 | 1497 | "Name: transcribed_content, dtype: string" | 1510 | 1498 | ] | 1511 | 1499 | }, | 1512 | | - "execution_count": 22, | | 1500 | + "execution_count": 11, | 1513 | 1501 | "metadata": {}, | 1514 | 1502 | "output_type": "execute_result" | 1515 | 1503 | } | 1516 | 1504 | ], | 1517 | 1505 | "source": [ | 1518 | | - "transcribed_series = df['audio'].blob.audio_transcribe(model_name=\"gemini-2.0-flash-001\", verbose=False)\n", | | 1506 | + "import bigframes.bigquery as bbq\n", | | 1507 | + "import bigframes.operations as ops\n", | | 1508 | + "\n", | | 1509 | + "# The audio_transcribe function is a convenience wrapper around bigframes.bigquery.ai.generate.\n", | | 1510 | + "# Here's how to perform the same operation directly:\n", | | 1511 | + "\n", | | 1512 | + "audio_series = df['audio']\n", | | 1513 | + "prompt_text = (\n", | | 1514 | + " \"**Task:** Transcribe the provided audio. **Instructions:** - Your response \"\n", | | 1515 | + " \"must contain only the verbatim transcription of the audio. - Do not include \"\n", | | 1516 | + " \"any introductory text, summaries, or conversational filler in your response. \"\n", | | 1517 | + " \"The output should begin directly with the first word of the audio.\"\n", | | 1518 | + ")\n", | | 1519 | + "\n", | | 1520 | + "# Convert the audio series to the runtime representation required by the model.\n", | | 1521 | + "# 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", | | 1524 | + "\n", | | 1525 | + "transcribed_results = bbq.ai.generate(\n", | | 1526 | + " prompt=(prompt_text, audio_runtime),\n", | | 1527 | + " endpoint=\"gemini-2.0-flash-001\",\n", | | 1528 | + " model_params={\"generationConfig\": {\"temperature\": 0.0}},\n", | | 1529 | + ")\n", | | 1530 | + "\n", | | 1531 | + "transcribed_series = transcribed_results.struct.field(\"result\").rename(\"transcribed_content\")\n", | 1519 | 1532 | "transcribed_series" | 1520 | 1533 | ] | 1521 | 1534 | }, | 1522 | 1535 | { | 1523 | 1536 | "cell_type": "code", | 1524 | | - "execution_count": 23, | | 1537 | + "execution_count": 12, | 1525 | 1538 | "metadata": {}, | 1526 | 1539 | "outputs": [ | 1527 | | - { | 1528 | | - "name": "stderr", | 1529 | | - "output_type": "stream", | 1530 | | - "text": [ | 1531 | | - "/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", | 1532 | | - "instead of using `db_dtypes` in the future when available in pandas\n", | 1533 | | - "(https://github.com/pandas-dev/pandas/issues/60958) and pyarrow.\n", | 1534 | | - " warnings.warn(msg, bigframes.exceptions.JSONDtypeWarning)\n", | 1535 | | - "/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", | 1536 | | - "instead of using `db_dtypes` in the future when available in pandas\n", | 1537 | | - "(https://github.com/pandas-dev/pandas/issues/60958) and pyarrow.\n", | 1538 | | - " warnings.warn(msg, bigframes.exceptions.JSONDtypeWarning)\n" | 1539 | | - ] | 1540 | | - }, | 1541 | 1540 | { | 1542 | 1541 | "data": { | | 1542 | + "text/html": [ | | 1543 | + "<pre>0 {'status': '', 'content': 'Now, as all books, ...</pre>" | | 1544 | + ], | 1543 | 1545 | "text/plain": [ | 1544 | 1546 | "0 {'status': '', 'content': 'Now, as all books, ...\n", | 1545 | 1547 | "Name: transcription_results, dtype: struct<status: string, content: string>[pyarrow]" | 1546 | 1548 | ] | 1547 | 1549 | }, | 1548 | | - "execution_count": 23, | | 1550 | + "execution_count": 12, | 1549 | 1551 | "metadata": {}, | 1550 | 1552 | "output_type": "execute_result" | 1551 | 1553 | } | 1552 | 1554 | ], | 1553 | 1555 | "source": [ | 1554 | | - "transcribed_series_verbose = df['audio'].blob.audio_transcribe(model_name=\"gemini-2.0-flash-001\", verbose=True)\n", | | 1556 | + "# To get verbose results (including status), we can extract both fields from the result struct.\n", | | 1557 | + "transcribed_content_series = transcribed_results.struct.field(\"result\")\n", | | 1558 | + "transcribed_status_series = transcribed_results.struct.field(\"status\")\n", | | 1559 | + "\n", | | 1560 | + "transcribed_series_verbose = bpd.DataFrame(\n", | | 1561 | + " {\n", | | 1562 | + " \"status\": transcribed_status_series,\n", | | 1563 | + " \"content\": transcribed_content_series,\n", | | 1564 | + " }\n", | | 1565 | + ")\n", | | 1566 | + "# Package as a struct for consistent display\n", | | 1567 | + "transcribed_series_verbose = bbq.struct(transcribed_series_verbose).rename(\"transcription_results\")\n", | 1555 | 1568 | "transcribed_series_verbose" | 1556 | 1569 | ] | 1557 | 1570 | }, | | 1657 | 1670 | "name": "python", | 1658 | 1671 | "nbconvert_exporter": "python", | 1659 | 1672 | "pygments_lexer": "ipython3", | 1660 | | - "version": "3.10.18" | | 1673 | + "version": "3.13.0" | 1661 | 1674 | } | 1662 | 1675 | }, | 1663 | 1676 | "nbformat": 4, | |
|