From 66313bdc752092238ae81387d924d54f60b3615a Mon Sep 17 00:00:00 2001 From: John Vajda Date: Mon, 20 May 2024 13:24:57 -0600 Subject: [PATCH 1/3] updates cookbooks for anthropic --- third_party/Deepgram/livestream_audio.ipynb | 223 ------------------- third_party/Deepgram/prerecorded_audio.ipynb | 126 ++++++++--- 2 files changed, 94 insertions(+), 255 deletions(-) delete mode 100644 third_party/Deepgram/livestream_audio.ipynb diff --git a/third_party/Deepgram/livestream_audio.ipynb b/third_party/Deepgram/livestream_audio.ipynb deleted file mode 100644 index 37413f9..0000000 --- a/third_party/Deepgram/livestream_audio.ipynb +++ /dev/null @@ -1,223 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Transcribe any audio stream with Deepgram!\n", - "\n", - "**Make a copy of this notebook into your own drive, and follow the instructions below!** 🥳🥳🥳\n", - "\n", - "\n", - "----------------------------\n", - "\n", - "# Get started:\n", - "\n", - "1) Copy this notebook (`File > Save a copy in Drive`) or download the .ipynb (`File > Download > Download as .ipynb`). \n", - "\n", - "2) Follow the instructions below!\n", - "\n", - "----------------------------\n", - "# Instructions:\n", - "Running the following cells will allow you to transcribe any audio stream you wish. The comments below point out the variables you can manipulate to modify your output.\n", - "\n", - "Also, this notebook comes with a video tutorial! Here: https://youtu.be/dq4AiiiaAsY\n", - "\n", - "And by the way, if you haven't yet signed up for Deepgram, check out this link: https://dpgr.am/streaming-notebook" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Step 1: Dependencies\n", - "\n", - "Run this cell to download all necessary dependencies.\n", - "\n", - "Note: You can run a cell by clicking the play button on the left or by clicking on the cell and pressing `shift`+`ENTER` at the same time. (Or `shift` + `return` on Mac)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "shellscript" - } - }, - "outputs": [], - "source": [ - "! pip install deepgram-sdk --upgrade" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Step 2: Live Transcription\n", - "\n", - "Run this cell to livestream audio from any source! By default, we are streaming from BBC radio: 'http://stream.live.vc.bbcmedia.co.uk/bbc_radio_fourlw_online_nonuk'\n", - "\n", - "Set the following variables as necessary:\n", - "\n", - "\n", - "* `DEEPGRAM_API_KEY` = Your personal Deepgram API key\n", - "* `URL` = The URL you wish to stream from. Again, we're streaming from BBC Radio by default, but if you wish to stream from another source just replace this URL.\n", - "* `PARAMS` = The starter code here should give you a well-punctuated English transcript. However, if you wish to change the language, use a different model, or modify the output, check out our docs to figure out the best parameter configuration for you: https://developers.deepgram.com/documentation/\n", - "* `TRANSCRIPTION_DURATION` = The number of seconds you wish to transcribe the livestream. Set this equal to `float(inf)` if you wish to stream forever. (Or at least until you wish to cut off the function manually.)\n", - "* `TRANSCRIPT_ONLY` = Set this variable to `True` if you wish only to see the words you're transcribing. Set it to `False` if you wish to see the full JSON responses (which include word-level timestamps, metadata, and confidence levels).\n", - "\n", - "To run the cell, click the play button or press `Shift`+`Enter` on your keyboard. (Or `Shift`+`return` if you're on Mac.)\n", - "\n", - "-------\n", - "\n", - "Note: if you're already a Deepgram user, and you're getting an error in this cell the most common issues are:\n", - "\n", - "1. You may need to update your installation of `deepgram-sdk`.\n", - "2. You may need to check how many credits you have left in your Deepgram account.\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import httpx\n", - "import threading\n", - "import time\n", - "import json\n", - "\n", - "from deepgram import (\n", - " DeepgramClient,\n", - " LiveTranscriptionEvents,\n", - " LiveOptions,\n", - ")\n", - "\n", - "# Your Deepgram API key\n", - "DEEPGRAM_API_KEY = '🔑🔑🔑 Your API Key here! 🔑🔑🔑'\n", - "\n", - "# URL for the realtime streaming audio you would like to transcribe\n", - "URL = 'http://stream.live.vc.bbcmedia.co.uk/bbc_world_service'\n", - "\n", - "# Duration for transcription in seconds\n", - "TRANSCRIPTION_DURATION = 30\n", - "\n", - "# Set this variable to `True` if you wish only to \n", - "# see the transcribed words, like closed captions. \n", - "# Set it to `False` if you wish to see the raw JSON responses.\n", - "TRANSCRIPT_ONLY = False\n", - "\n", - "def main():\n", - " try:\n", - " # Initialize Deepgram client\n", - " deepgram = DeepgramClient(DEEPGRAM_API_KEY)\n", - "\n", - " # Create a websocket connection to Deepgram\n", - " dg_connection = deepgram.listen.live.v(\"1\")\n", - "\n", - " # Define event handlers\n", - " def on_message(self, result, **kwargs):\n", - " if TRANSCRIPT_ONLY:\n", - " sentence = result.channel.alternatives[0].transcript\n", - " if len(sentence) > 0:\n", - " print(f\"Speaker: {sentence}\")\n", - " else:\n", - " # Extract relevant information from the LiveResultResponse object\n", - " response_dict = {\n", - " \"type\": result.type,\n", - " \"channel\": {\n", - " \"alternatives\": [\n", - " {\n", - " \"transcript\": alt.transcript\n", - " } for alt in result.channel.alternatives\n", - " ]\n", - " }\n", - " }\n", - " print(json.dumps(response_dict, indent=4))\n", - "\n", - "\n", - " def on_metadata(self, metadata, **kwargs):\n", - " print(f\"\\n\\n{metadata}\\n\\n\")\n", - "\n", - " def on_error(self, error, **kwargs):\n", - " print(f\"\\n\\n{error}\\n\\n\")\n", - "\n", - " # Register event handlers\n", - " dg_connection.on(LiveTranscriptionEvents.Transcript, on_message)\n", - " dg_connection.on(LiveTranscriptionEvents.Metadata, on_metadata)\n", - " dg_connection.on(LiveTranscriptionEvents.Error, on_error)\n", - "\n", - " # Configure Deepgram options for live transcription\n", - " options = LiveOptions(\n", - " model=\"nova-2\", \n", - " language=\"en-US\", \n", - " smart_format=True,\n", - " )\n", - "\n", - " # Start the connection\n", - " dg_connection.start(options)\n", - "\n", - " # Create a lock and a flag for thread synchronization\n", - " lock_exit = threading.Lock()\n", - " exit = False\n", - "\n", - " # Define a thread that streams the audio and sends it to Deepgram\n", - " def myThread():\n", - " start_time = time.time()\n", - " with httpx.stream(\"GET\", URL) as r:\n", - " for data in r.iter_bytes():\n", - " if time.time() - start_time >= TRANSCRIPTION_DURATION:\n", - " break\n", - " dg_connection.send(data)\n", - "\n", - " # Start the thread\n", - " myHttp = threading.Thread(target=myThread)\n", - " myHttp.start()\n", - "\n", - " # Wait for the specified duration\n", - " myHttp.join(TRANSCRIPTION_DURATION)\n", - "\n", - " # Set the exit flag to True to stop the thread\n", - " lock_exit.acquire()\n", - " exit = True\n", - " lock_exit.release()\n", - "\n", - " # Close the connection to Deepgram\n", - " dg_connection.finish()\n", - "\n", - " print(\"Finished\")\n", - "\n", - " except Exception as e:\n", - " print(f\"Could not open socket: {e}\")\n", - " return\n", - "\n", - "\n", - "if __name__ == \"__main__\":\n", - " main()\n" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "sdk", - "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.10.10" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/third_party/Deepgram/prerecorded_audio.ipynb b/third_party/Deepgram/prerecorded_audio.ipynb index de7305c..c326697 100644 --- a/third_party/Deepgram/prerecorded_audio.ipynb +++ b/third_party/Deepgram/prerecorded_audio.ipynb @@ -4,18 +4,16 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Transcribe any audio file with Deepgram!\n", + "# Transcribe an audio file with Deepgram & use Anthropic to prepare interview questions!\n", "\n", "**Make a copy of this notebook into your own drive, and follow the instructions below!** 🥳🥳🥳\n", "\n", - "\n", "----------------------------\n", "\n", "# Get started:\n", "Running the following three cells will allow you to transcribe any audio you wish. The comments below point out the variables you can manipulate to modify your output as you wish.\n", "\n", - "Before running this notebook, you'll need to have a couple audio files on-hand\n", - "that you wish to transcribe. Once you have those files in a folder, you should be able to transcribe as you please. Just specify the filepaths as outlined below!\n", + "Before running this notebook, you'll need to have a couple audio URLs to transcribe. You can use any audio files you wish.\n", "\n", "And by the way, if you haven't yet signed up for Deepgram, check out this link here: https://dpgr.am/prerecorded-notebook-signup" ] @@ -39,22 +37,17 @@ "source": [ "! pip install requests ffmpeg-python\n", "! pip install deepgram-sdk --upgrade\n", - "! pip install requests" + "! pip install requests\n", + "! pip install anthropic" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "# Step 2: Upload audio files to this Colab!\n", - "\n", - "On the left, you'll see a side-bar with a folder icon. Click that icon, and you'll see a series of folders. This is where you'll upload your audio files.\n", - "\n", - "You can upload your files directly into this directory by clicking the upload icon in the top left. The icon looks like a sheet of paper with an upwards-pointing arrow on it.\n", + "# Step 2: Audio URL files\n", "\n", - "Click the upload icon and select the audio file you wish to transcribe. It will take a few moments for the audio to appear, but once it does, move onto Step 3.\n", - "\n", - "(We have added an example audio, `preamble.wav` to this project.)" + "Find some audio files hosted on a server so you can use this notebook. OR An example file is provided by Deepgram is code below. " ] }, { @@ -150,34 +143,20 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "'''\n", - "If the cell above succeeds, you should see JSON output file(s) in the same directory\n", - "as your audio files. Note: There may be a small delay between when\n", - "the cell finishes running and when the JSON file actually appears.\n", - "This is normal. Just wait a few moments for the JSONs to appear.\n", - "It should take less than a minute, depending on the size of your file(s).\n", - "'''" + "If the cell above succeeds, you should see JSON output file(s) in the content directory. Note: There may be a small delay between when the cell finishes running and when the JSON file actually appears. This is normal. Just wait a few moments for the file(s) to appear." ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [] - }, { "cell_type": "markdown", "metadata": {}, "source": [ - "# Step 4: Check out your transcription!\n", + "# Step 4: Check out your transcription\n", "\n", - "The function below parses the output JSON and prints out the pure transcription of one of the files you just transcribed! (Make sure\n", - "the file you're trying to examine is indeed already loaded into the\n", - "folder on the left!)\n", + "The function below parses the output JSON and prints out the transcription of one of the files you just transcribed! (Make sure\n", + "the file you're trying to examine is indeed already loaded into the content directory.)\n", "\n", "**Set the `OUTPUT` variable to the name of the file you wish to see the transcription of.**\n", "\n", @@ -207,6 +186,89 @@ "\n", "print_transcript(OUTPUT)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "If the cell above succeeds you should see a plain text version of your audio transcription. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Step 5: Prepare Interview Questions using Anthropic\n", + "\n", + "Now so we can send off our transcript to Anthropic for analysis to help us prepare some interview questions. Run the cell below (`Shift`+`Enter`) to get a suggested set of interview questions provided by Anthropic based on your audio transcript above." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import anthropic\n", + "import json\n", + "\n", + "transcription_file = \"transcript.json\"\n", + "\n", + "# Function to get the transcript from the JSON file\n", + "def get_transcript(transcription_file):\n", + " with open(transcription_file, \"r\") as file:\n", + " data = json.load(file)\n", + " result = data['results']['channels'][0]['alternatives'][0]['transcript']\n", + " return result\n", + "\n", + "# Load the transcript from the JSON file\n", + "message_text = get_transcript(transcription_file)\n", + "\n", + "# Initialize the Anthropic API client\n", + "client = anthropic.Anthropic(\n", + " # Defaults to os.environ.get(\"ANTHROPIC_API_KEY\")\n", + " # Anthropic API key\n", + " api_key=\"🔑🔑🔑 Your API Key here! 🔑🔑🔑\"\n", + ")\n", + "\n", + "# Prepare the text for the API request\n", + "formatted_messages = [\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": message_text\n", + " }\n", + "]\n", + "\n", + "# Generate thoughtful, open-ended interview questions\n", + "response = client.messages.create(\n", + " model=\"claude-3-opus-20240229\",\n", + " max_tokens=1000,\n", + " temperature=0.5,\n", + " system=\"Your task is to generate a series of thoughtful, open-ended questions for an interview based on the given context. The questions should be designed to elicit insightful and detailed responses from the interviewee, allowing them to showcase their knowledge, experience, and critical thinking skills. Avoid yes/no questions or those with obvious answers. Instead, focus on questions that encourage reflection, self-assessment, and the sharing of specific examples or anecdotes.\",\n", + " messages=formatted_messages\n", + ")\n", + "\n", + "# Print the generated questions\n", + "\n", + "# Join the text of each TextBlock into a single string\n", + "content = ''.join(block.text for block in response.content)\n", + "\n", + "# Split the content by '\\n\\n'\n", + "parts = content.split('\\n\\n')\n", + "\n", + "# Print each part with an additional line break\n", + "for part in parts:\n", + " print(part)\n", + " print('\\n')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If this cell succeeded you should see a list of interview questions based on your original audio file. Now you can transcribe audio with Deepgram and use Anthropic to get a set of interview questions. " + ] } ], "metadata": { From 922d447f92c4daf32713902b4841187c154324fb Mon Sep 17 00:00:00 2001 From: John Vajda Date: Mon, 20 May 2024 13:36:18 -0600 Subject: [PATCH 2/3] updates README --- third_party/Deepgram/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/third_party/Deepgram/README.md b/third_party/Deepgram/README.md index 33bf495..8a79ed8 100644 --- a/third_party/Deepgram/README.md +++ b/third_party/Deepgram/README.md @@ -2,7 +2,6 @@ [Deepgram](https://deepgram.com/) is a foundational AI company providing the speech-to-text, text-to-speech, text-to-text and language intelligence capabilities you need to make your data readable and actionable by human or machines. -* The [Livestream Audio Notebook](./livestream_audio.ipynb) allows you to transcribe live streaming audio using Deepgram. * The [Pre-Recorded Audio Notebook](./prerecorded_audio.ipynb) allows you to transcribe pre-recorded audio using Deepgram. # More about Deepgram From 2fdfcc3a89a17db36729691fd03ba8562541f2a7 Mon Sep 17 00:00:00 2001 From: "John Vajda (JV)" Date: Mon, 20 May 2024 13:36:39 -0600 Subject: [PATCH 3/3] Update third_party/Deepgram/prerecorded_audio.ipynb Co-authored-by: Sandra Rodgers <45321563+SandraRodgers@users.noreply.github.com> --- third_party/Deepgram/prerecorded_audio.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/third_party/Deepgram/prerecorded_audio.ipynb b/third_party/Deepgram/prerecorded_audio.ipynb index c326697..745ca8c 100644 --- a/third_party/Deepgram/prerecorded_audio.ipynb +++ b/third_party/Deepgram/prerecorded_audio.ipynb @@ -201,7 +201,7 @@ "source": [ "# Step 5: Prepare Interview Questions using Anthropic\n", "\n", - "Now so we can send off our transcript to Anthropic for analysis to help us prepare some interview questions. Run the cell below (`Shift`+`Enter`) to get a suggested set of interview questions provided by Anthropic based on your audio transcript above." + "Now we can send off our transcript to Anthropic for analysis to help us prepare some interview questions. Run the cell below (`Shift`+`Enter`) to get a suggested set of interview questions provided by Anthropic based on your audio transcript above." ] }, {