{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "gpuType": "T4" }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "accelerator": "GPU" }, "cells": [ { "cell_type": "markdown", "source": [ "# Whisper v3 is here!\n", "\n", "Whisper v3 is a new model open sourced by OpenAI. The model can do multilingual transcriptions and is quite impressive. For example, you can change from English to Spanish or Chinese in the middle of a sentence and it will work well!\n", "\n", "The model can be run in a free Google Colab instance and is integrated into `transformers` already, so switching can be a very smooth process if you already use the previous versions." ], "metadata": { "id": "OXaUqiE-eyXM" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "WFQeUT9EcIcK" }, "outputs": [], "source": [ "%%capture\n", "!pip install git+https://github.com/huggingface/transformers gradio" ] }, { "cell_type": "markdown", "source": [ "Let's use the high level `pipeline` from the `transformers` library to load the model." ], "metadata": { "id": "sZONes21fHTA" } }, { "cell_type": "code", "source": [ "import torch\n", "from transformers import pipeline\n", "\n", "pipe = pipeline(\"automatic-speech-recognition\",\n", " \"openai/whisper-large-v3\",\n", " torch_dtype=torch.float16,\n", " device=\"cuda:0\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "DvBdwMdPcr-Y", "outputId": "47f32218-fd85-49ea-d880-d31577bcf9b8" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n", "Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n" ] } ] }, { "cell_type": "code", "source": [ "pipe(\"https://cdn-media.huggingface.co/speech_samples/sample1.flac\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "GZFkIyhjc0Nc", "outputId": "f1463431-3e08-4438-815f-b71e5e7a1503" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'text': \" going along slushy country roads and speaking to damp audiences in draughty schoolrooms day after day for a fortnight he'll have to put in an appearance at some place of worship on sunday morning and he can come to us immediately afterwards\"}" ] }, "metadata": {}, "execution_count": 2 } ] }, { "cell_type": "markdown", "source": [ "Let's now build a quick Gradio demo where we can play with the model directly using our microphone! You can run this code in a Google Colab instance (or locally!) or just head to the Space to play directly with it online." ], "metadata": { "id": "pt3YtM_PfTQY" } }, { "cell_type": "code", "source": [ "import gradio as gr\n", "\n", "def transcribe(inputs):\n", " if inputs is None:\n", " raise gr.Error(\"No audio file submitted! Please record an audio before submitting your request.\")\n", "\n", " text = pipe(inputs, generate_kwargs={\"task\": \"transcribe\"}, return_timestamps=True)[\"text\"]\n", " return text\n", "\n", "demo = gr.Interface(\n", " fn=transcribe,\n", " inputs=[\n", " gr.Audio(sources=[\"microphone\", \"upload\"], type=\"filepath\"),\n", " ],\n", " outputs=\"text\",\n", " title=\"Whisper Large V3: Transcribe Audio\",\n", " description=(\n", " \"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the\"\n", " \" checkpoint [openai/whisper-large-v3](https://huggingface.co/openai/whisper-large-v3) and 🤗 Transformers to transcribe audio files\"\n", " \" of arbitrary length.\"\n", " ),\n", " allow_flagging=\"never\",\n", ")\n", "\n", "demo.launch()\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 648 }, "id": "K0b2UZLVdIze", "outputId": "bcff00e0-4fc8-4883-9ba4-480f5a6665f0" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Setting queue=True in a Colab notebook requires sharing enabled. Setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n", "\n", "Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n", "Running on public URL: https://037dbdb04542aa1a29.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "text/html": [ "
" ] }, "metadata": {} }, { "output_type": "execute_result", "data": { "text/plain": [] }, "metadata": {}, "execution_count": 4 } ] } ] }