{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "kvi6cAEHY5sh" }, "source": [ "# Installation" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "ZsJUxc0aRsAf", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "98042512-9122-45b6-cee2-829d8e447d74" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.3/6.3 MB\u001b[0m \u001b[31m17.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m190.3/190.3 KB\u001b[0m \u001b[31m16.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m7.6/7.6 MB\u001b[0m \u001b[31m20.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Building wheel for openai-whisper (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m14.2/14.2 MB\u001b[0m \u001b[31m99.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m140.7/140.7 KB\u001b[0m \u001b[31m5.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m107.0/107.0 KB\u001b[0m \u001b[31m13.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m56.2/56.2 KB\u001b[0m \u001b[31m7.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.5/50.5 KB\u001b[0m \u001b[31m6.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m56.9/56.9 KB\u001b[0m \u001b[31m8.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m71.5/71.5 KB\u001b[0m \u001b[31m10.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m45.7/45.7 KB\u001b[0m \u001b[31m6.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m84.5/84.5 KB\u001b[0m \u001b[31m11.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.1/2.1 MB\u001b[0m \u001b[31m85.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m66.4/66.4 KB\u001b[0m \u001b[31m10.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m69.6/69.6 KB\u001b[0m \u001b[31m9.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 KB\u001b[0m \u001b[31m8.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m80.6/80.6 KB\u001b[0m \u001b[31m11.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Building wheel for ffmpy (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m70.1/70.1 KB\u001b[0m \u001b[31m3.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.8/62.8 KB\u001b[0m \u001b[31m6.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h" ] } ], "source": [ "!pip install -q git+https://github.com/openai/whisper.git\n", "!pip install -q gradio\n", "!pip install -q openai\n", "!pip install -q gTTS" ] }, { "cell_type": "markdown", "metadata": { "id": "usUSep_lY7q8" }, "source": [ "# Imports" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "Kr5faKybKi4p" }, "outputs": [], "source": [ "import whisper\n", "import gradio as gr \n", "import time\n", "import warnings\n", "import json\n", "import openai\n", "import os\n", "from gtts import gTTS" ] }, { "cell_type": "markdown", "metadata": { "id": "EyW6weauY9gf" }, "source": [ "# Defining Variables" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "u_6_s2iHboR4" }, "outputs": [], "source": [ "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "2zj7g1C381VJ" }, "outputs": [], "source": [ "with open('GPT_SECRET_KEY.json') as f:\n", " data = json.load(f)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "mihgqxDv81VJ" }, "outputs": [], "source": [ "openai.api_key = data[\"API_KEY\"]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "cMNnv3oHaDt8", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "5d11fe12-c0e8-4373-ccdf-2a08271bcef1" }, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "100%|████████████████████████████████████████| 139M/139M [00:00<00:00, 180MiB/s]\n" ] } ], "source": [ "model = whisper.load_model(\"base\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "e3bjy1E3R-MN", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "0ce4bb4d-cefd-4706-c4fc-d5815a4b9664" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "device(type='cuda', index=0)" ] }, "metadata": {}, "execution_count": 7 } ], "source": [ "model.device" ] }, { "cell_type": "code", "source": [ "!ffmpeg -f lavfi -i anullsrc=r=44100:cl=mono -t 10 -q:a 9 -acodec libmp3lame Temp.mp3" ], "metadata": { "id": "zlhNLyS3B22D", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "5798ffb9-769f-4793-e961-7b697c5e59ec" }, "execution_count": 8, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "ffmpeg version 4.2.7-0ubuntu0.1 Copyright (c) 2000-2022 the FFmpeg developers\n", " built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)\n", " configuration: --prefix=/usr --extra-version=0ubuntu0.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-nvenc --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared\n", " libavutil 56. 31.100 / 56. 31.100\n", " libavcodec 58. 54.100 / 58. 54.100\n", " libavformat 58. 29.100 / 58. 29.100\n", " libavdevice 58. 8.100 / 58. 8.100\n", " libavfilter 7. 57.100 / 7. 57.100\n", " libavresample 4. 0. 0 / 4. 0. 0\n", " libswscale 5. 5.100 / 5. 5.100\n", " libswresample 3. 5.100 / 3. 5.100\n", " libpostproc 55. 5.100 / 55. 5.100\n", "Input #0, lavfi, from 'anullsrc=r=44100:cl=mono':\n", " Duration: N/A, start: 0.000000, bitrate: 352 kb/s\n", " Stream #0:0: Audio: pcm_u8, 44100 Hz, mono, u8, 352 kb/s\n", "Stream mapping:\n", " Stream #0:0 -> #0:0 (pcm_u8 (native) -> mp3 (libmp3lame))\n", "Press [q] to stop, [?] for help\n", "Output #0, mp3, to 'Temp.mp3':\n", " Metadata:\n", " TSSE : Lavf58.29.100\n", " Stream #0:0: Audio: mp3 (libmp3lame), 44100 Hz, mono, s16p\n", " Metadata:\n", " encoder : Lavc58.54.100 libmp3lame\n", "size= 39kB time=00:00:10.00 bitrate= 32.1kbits/s speed= 236x \n", "video:0kB audio:39kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.568409%\n" ] } ] }, { "cell_type": "markdown", "source": [ "# ChatGPT_API_Function" ], "metadata": { "id": "_gNUvwL2BzD4" } }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "4QXJeLiy81VL" }, "outputs": [], "source": [ "def chatgpt_api(input_text):\n", " messages = [\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}]\n", " \n", " if input_text:\n", " messages.append(\n", " {\"role\": \"user\", \"content\": input_text},\n", " )\n", " chat_completion = openai.ChatCompletion.create(\n", " model=\"gpt-3.5-turbo\", messages=messages\n", " )\n", " \n", " reply = chat_completion.choices[0].message.content\n", " return reply" ] }, { "cell_type": "markdown", "metadata": { "id": "14CPZJMxY_Ks" }, "source": [ "# Transcribe Function" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "id": "JtTvvQQPcOZZ" }, "outputs": [], "source": [ "def transcribe(audio):\n", "\n", " language = 'en'\n", "\n", " audio = whisper.load_audio(audio)\n", " audio = whisper.pad_or_trim(audio)\n", "\n", " mel = whisper.log_mel_spectrogram(audio).to(model.device)\n", "\n", " _, probs = model.detect_language(mel)\n", "\n", " options = whisper.DecodingOptions()\n", " result = whisper.decode(model, mel, options)\n", " result_text = result.text\n", " \n", " out_result = chatgpt_api(result_text)\n", " \n", " audioobj = gTTS(text = out_result, \n", " lang = language, \n", " slow = False)\n", " \n", " audioobj.save(\"Temp.mp3\")\n", "\n", " return [result_text, out_result, \"Temp.mp3\"]" ] }, { "cell_type": "markdown", "metadata": { "id": "aJaFmE9aZB_8" }, "source": [ "# Gradio Interface" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "id": "deSAVvfJcWBo", "scrolled": false, "colab": { "base_uri": "https://localhost:8080/", "height": 616 }, "outputId": "a8a51e43-5dce-4c91-f56f-51541e849fc1" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n", "Note: opening Chrome Inspector may crash demo inside Colab notebooks.\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "(async (port, path, width, height, cache, element) => {\n", " if (!google.colab.kernel.accessAllowed && !cache) {\n", " return;\n", " }\n", " element.appendChild(document.createTextNode(''));\n", " const url = await google.colab.kernel.proxyPort(port, {cache});\n", "\n", " const external_link = document.createElement('div');\n", " external_link.innerHTML = `\n", "
\n", " Running on \n", " https://localhost:${port}${path}\n", " \n", "
\n", " `;\n", " element.appendChild(external_link);\n", "\n", " const iframe = document.createElement('iframe');\n", " iframe.src = new URL(path, url).toString();\n", " iframe.height = height;\n", " iframe.allow = \"autoplay; camera; microphone; clipboard-read; clipboard-write;\"\n", " iframe.width = width;\n", " iframe.style.border = 0;\n", " element.appendChild(iframe);\n", " })(7860, \"/\", \"100%\", 500, false, window.element)" ] }, "metadata": {} }, { "output_type": "execute_result", "data": { "text/plain": [] }, "metadata": {}, "execution_count": 11 } ], "source": [ "output_1 = gr.Textbox(label=\"Speech to Text\")\n", "output_2 = gr.Textbox(label=\"ChatGPT Output\")\n", "output_3 = gr.Audio(\"Temp.mp3\")\n", "\n", "gr.Interface(\n", " title = 'OpenAI Whisper and ChatGPT ASR Gradio Web UI', \n", " fn=transcribe, \n", " inputs=[\n", " gr.inputs.Audio(source=\"microphone\", type=\"filepath\")\n", " ],\n", "\n", " outputs=[\n", " output_1, output_2, output_3\n", " ],\n", " live=True).launch()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "y2Zid2MKdPxK" }, "outputs": [], "source": [] } ], "metadata": { "accelerator": "GPU", "colab": { "provenance": [] }, "gpuClass": "standard", "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.15" } }, "nbformat": 4, "nbformat_minor": 0 }