diff --git a/examples/transcription.ipynb b/examples/transcription.ipynb new file mode 100644 index 0000000..0a1b2a5 --- /dev/null +++ b/examples/transcription.ipynb @@ -0,0 +1,450 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Transcription Task\n", + "\n", + "This is an example Python notebook for running transcription tasks using [Camb.ai](https://camb.ai)'s API.\n", + "\n", + "# Index\n", + "\n", + "- [Optional: Loading API Key from .env file](#optional-loading-api-key-from-env-file)\n", + "- [Transcribe Method](#transcribe-method)\n", + " - [Optional: Transcribing a Public video (i.e. YouTube)](#optional-transcribing-a-public-video-ie-youtube)\n", + "- [Individual Methods](#individual-methods)\n", + " - [Create Transcription Task](#create-transcription-task)\n", + " - [Get Transcription Status](#get-transcription-status)\n", + " - [Get Transcription Result](#get-dubbed-result)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# !pip install cambai" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To get started, make sure you import both the `CambAI` class from the `cambai` module.\n", + "\n", + "Ensure that you have the Camb AI API Key from https://studio.camb.ai" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "from cambai import CambAI" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "client = CambAI(\n", + " api_key=os.environ.get(\"CAMB_API_KEY\") # Get your API key from https://studio.camb.ai\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Optional: Loading API Key from .env file\n", + "\n", + "While you can provide an `api_key` as an argument, we recommend using [`python-dotenv`](https://pypi.org/project/python-dotenv/) to add `CAMB_API_KEY=\"Your API Key\"` to your `.env` file to prevent your API Key from being stored in source control." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# !pip install python-dotenv" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from dotenv import load_dotenv\n", + "\n", + "load_dotenv()\n", + "\n", + "client = CambAI(\n", + " api_key=os.environ.get(\"CAMB_API_KEY\") # Get your API key from https://studio.camb.ai\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "----\n", + "\n", + "To get the numbers for the list of all available languages, you can use the `get_languages()` method.\n", + "\n", + "You can also save the list of languages to a file by setting the `write_to_file` argument to `True`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "client.get_languages(\"source\", write_to_file=True)\n", + "# client.get_languages(\"target\", write_to_file=True)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---\n", + "---\n", + "\n", + "# Transcribe Method\n", + "\n", + "You can call the `transcribe()` method to perform the entire transcribe process.
\n", + "From calling the API, to getting the transcription result." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "transcription = client.transcribe(\n", + " \"path/to/audio/file\",\n", + " language=1, # Get the language code from the file generated in the previous step\n", + " save_to_file=True, # Optionally, save the transcription to a file\n", + " debug=True, # Optionally, you can set debug to True to see the print statements\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**NOTE:** The transcription process can take a while to complete, so depending on how long the video/audio is, you may have to set the `polling_interval` attribute to a higher value.\n", + "\n", + "---" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Optional: Transcribing a Public video (i.e. YouTube)\n", + "\n", + "To transcribe a public video from YouTube or any other website, use [`yt-dlp`](https://pypi.org/project/yt-dlp/) to download it first" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# !pip install yt-dlp" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[youtube] Extracting URL: https://youtu.be/rlK-BySadHk\n", + "[youtube] rlK-BySadHk: Downloading webpage\n", + "[youtube] rlK-BySadHk: Downloading ios player API JSON\n", + "[youtube] rlK-BySadHk: Downloading m3u8 information\n", + "[info] rlK-BySadHk: Downloading 1 format(s): 18\n", + "[download] Destination: video.mp4\n", + "[download] 100% of 3.11MiB in 00:00:02 at 1.35MiB/s \n" + ] + }, + { + "data": { + "text/html": [ + "
Starting Transcription\n",
+       "
\n" + ], + "text/plain": [ + "Starting Transcription\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
Transcription Task Started: {'task_id': 'e9a58959-3e81-4bf1-94cb-9067297d60ec'}\n",
+       "
\n" + ], + "text/plain": [ + "Transcription Task Started: \u001b[1m{\u001b[0m\u001b[32m'task_id'\u001b[0m: \u001b[32m'e9a58959-3e81-4bf1-94cb-9067297d60ec'\u001b[0m\u001b[1m}\u001b[0m\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
Task Status: PENDING, Run ID: None\n",
+       "
\n" + ], + "text/plain": [ + "Task Status: PENDING, Run ID: \u001b[3;35mNone\u001b[0m\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
Sleeping for 60 seconds\n",
+       "
\n" + ], + "text/plain": [ + "Sleeping for \u001b[1;36m60\u001b[0m seconds\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Waiting 60 seconds before checking status again: 100%|██████████| 60/60 [01:00<00:00, 1.00s/s]\n" + ] + }, + { + "data": { + "text/html": [ + "
Task Status: SUCCESS, Run ID: 28173\n",
+       "
\n" + ], + "text/plain": [ + "Task Status: SUCCESS, Run ID: \u001b[1;36m28173\u001b[0m\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "from yt_dlp import YoutubeDL\n", + "\n", + "URLS = [\"https://youtu.be/rlK-BySadHk\"]\n", + "\n", + "ydl_opts = {\n", + " \"outtmpl\": \"video.%(ext)s\",\n", + " \"format\": \"mp4\",\n", + " \"overwrite\": True,\n", + "}\n", + "\n", + "with YoutubeDL(ydl_opts) as ydl:\n", + " error_code = ydl.download(URLS)\n", + "\n", + "transcription = client.transcribe(\n", + " audio_file=\"video.mp4\",\n", + " language=1, # Get the language code from the file generated in the previous step\n", + " save_to_file=True, # Optionally, save the transcription to a file\n", + " debug=True, # Optionally, you can set debug to True to see the print statements\n", + " polling_interval=60\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "transcription" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---\n", + "---\n", + "\n", + "# Individual Methods\n", + "\n", + "You can also access individual methods to have more control over the transcription task.\n", + "\n", + "- [Create Transcription](#create-transcription-task)\n", + "- [Get Transcription Status](#get-transcription-status)\n", + "- [Get Transcription Result](#get-transcription-result)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Create Transcription Task\n", + "\n", + "This method creates a transcription task and returns the Run ID which is used to check the status of the transcription task." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "task = client.create_transcription(\"path/to/audio\", language=123)\n", + "\n", + "task_id = task[\"task_id\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'e9a58959-3e81-4bf1-94cb-9067297d60ec'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "task_id" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "----\n", + "\n", + "## Get Transcription Status\n", + "\n", + "This method returns the status of the transcription. The status can be one of the following:\n", + "\n", + "- SUCCESS\n", + "- PENDING\n", + "- TIMEOUT\n", + "- ERROR\n", + "- PAYMENT_REQUIRED" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "transcription_status = client.get_task_status(\"transcription\", task_id)\n", + "# transcription_status = client.get_transcription_status(task_id)\n", + "\n", + "status = transcription_status[\"status\"]\n", + "run_id = transcription_status[\"run_id\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('SUCCESS', 28173)" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "status, run_id" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "----\n", + "\n", + "## Get Transcription Result\n", + "\n", + "You can use this method to get the transcription result.
\n", + "If you wish to save the transcription to a file, set the `write_to_file` argument to `True`." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "transcription = client.get_transcription_result(run_id, save_to_file=True)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "----\n", + "\n", + "To know more, check out Camb AI's [API Documentation](https://docs.camb.ai)." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}