openai-cookbook/examples/GPT_with_vision_for_video_understanding.ipynb

275 lines
3.0 MiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Processing and narrating a video with GPT's visual capabilities and the TTS API\n",
"\n",
"This notebook demonstrates how to use GPT's visual capabilities with a video. GPT-4 doesn't take videos as input directly, but we can use vision and the new 128K context window to describe the static frames of a whole video at once. We'll walk through two examples:\n",
"\n",
"1. Using GPT-4 to get a description of a video\n",
"2. Generating a voiceover for a video with GPT-4 and the TTS API\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import display, Image, Audio\n",
"\n",
"import cv2 # We're using OpenCV to read video, to install !pip install opencv-python\n",
"import base64\n",
"import time\n",
"from openai import OpenAI\n",
"import os\n",
"import requests\n",
"\n",
"client = OpenAI(api_key=os.environ.get(\"OPENAI_API_KEY\", \"<your OpenAI API key if not set as env var>\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Using GPT's visual capabilities to get a description of a video\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2023-11-10 17:32:51 +00:00
"First, we use OpenCV to extract frames from a nature [video](https://www.youtube.com/watch?v=kQ_7GtE529M) containing bisons and wolves:\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"618 frames read.\n"
]
}
],
"source": [
2023-11-06 21:05:53 +00:00
"video = cv2.VideoCapture(\"data/bison.mp4\")\n",
"\n",
"base64Frames = []\n",
"while video.isOpened():\n",
" success, frame = video.read()\n",
" if not success:\n",
" break\n",
" _, buffer = cv2.imencode(\".jpg\", frame)\n",
" base64Frames.append(base64.b64encode(buffer).decode(\"utf-8\"))\n",
"\n",
"video.release()\n",
"print(len(base64Frames), \"frames read.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Display frames to make sure we've read them in correctly:\n"
]
},
{
"cell_type":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SUEZGK2AjooooAkoPTio6KAHRoWIz61LsUcmmo7RnKHFIxODzQASAEYWmbW9Kcv3aWgBmD6GjB9DT6KAI6KKKACiiigCZVBGTR5fvSQysOoHX0qyIhIo+XH0oArFCBxzSbSeq/pVr7MPf86TyF7E0AVCgzwadTvL96PL96AGBQO1I49BUuweppmCOooAZg+hpCCOoqSggHqKAI6KfsXOaNgoAZRTih7UmxqAEpCmTnNOII6ikoAQJg5zS0UUAMb71NZRgnFSFMnOaTy/egCGnhVI6U/yz2NNP1oAjooooAKinJAJBqWmyRhlOV60AVgW6AnmjY/8AdP5VOIF4IH60/YPU0AVdj/3T+VGx/wC6fyq1sHqaNg9TQBVYYPU1Fv8A9r9atvApOB+tRSW6qcH68UkmiGyk8zK54/GoJZJicqx6+lXpowRgCoDGFGSOavTlDSxTZpQOpH1NQvvPGf1q5OOgxwaqyllJHoeKFBtXCzZBKvOGFUdRthKmSBwelX5CSOT3qnc52nkitoKwjKmsowcmMZ9xURt4lJKxgGrs6ORnnPvVSZZghKAE9s1siZFF9RhiJBHIPSq82sSEkRqfbAzVyLw7JKwMr5zzipDoNvHnLnI709EZcrMWaS7uDkZT0NN+xu5xK7deg6VvNoqDhJOSe4qSHRoWcgAt7elMLMwEsFAxhjz+tB0aMDJQYJ44rq4NGQjBXgfrUq6Iu0KVOfcVLkkUoNnIW+ho042qCMc8Vp2nhncNxiB5/hroLbQY2cDZk9qvw6PtGFBGOnFJzSKVM5y10BY5Qwt24PetRdEicDbGcjqB1rUGlTnkAZzViz06aOQMSCe1Q6yGopGbZ6L5Z3CI8f
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display_handle = display(None, display_id=True)\n",
"for img in base64Frames:\n",
" display_handle.update(Image(data=base64.b64decode(img.encode(\"utf-8\"))))\n",
" time.sleep(0.025)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2023-11-10 17:32:51 +00:00
"Once we have the video frames, we craft our prompt and send a request to GPT (Note that we don't need to send every frame for GPT to understand what's going on):\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\"🐺 Survival of the Fittest: An Epic Tale in the Snow ❄️ - Witness the intense drama of nature as a pack of wolves face off against mighty bison in a harsh winter landscape. This raw footage captures the essence of the wild where every creature fights for survival. With each frame, experience the tension, the strategy, and the sheer force exerted in this life-or-death struggle. See nature's true colors in this gripping encounter on the snowy plains. 🦬\"\n",
"\n",
"Remember to respect wildlife and nature. This video may contain scenes that some viewers might find intense or distressing, but they depict natural animal behaviors important for ecological studies and understanding the reality of life in the wilderness.\n"
]
}
],
"source": [
"PROMPT_MESSAGES = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": [\n",
" \"These are frames from a video that I want to upload. Generate a compelling description that I can upload along with the video.\",\n",
" *map(lambda x: {\"image\": x, \"resize\": 768}, base64Frames[0::50]),\n",
" ],\n",
" },\n",
"]\n",
"params = {\n",
" \"model\": \"gpt-4-vision-preview\",\n",
" \"messages\": PROMPT_MESSAGES,\n",
" \"max_tokens\": 200,\n",
"}\n",
"\n",
"result = client.chat.completions.create(**params)\n",
"print(result.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Generating a voiceover for a video with GPT-4 and the TTS API\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's create a voiceover for this video in the style of David Attenborough. Using the same video frames we prompt GPT to give us a short script:\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"In the vast, white expanse of the northern wilderness, a drama as old as time unfolds. Here, amidst the silence of the snow, the wolf pack circles, their breaths visible as they cautiously approach their formidable quarry, the bison. These wolves are practiced hunters, moving with strategic precision, yet the bison, a titan of strength, stands resolute, a force to be reckoned with.\n",
"\n",
"As tension crackles in the frozen air, the wolves close in, their eyes locked on their target. The bison, wary of every movement, prepares to defend its life. It's a perilous dance between predator and prey, where each step could be the difference between life and death.\n",
"\n",
"In an instant, the quiet of the icy landscape is shattered. The bison charges, a desperate bid for survival as the pack swarms. The wolves are relentless, each one aware that their success depends on the strength of the collective. The bison, though powerful, is outnumbered, its massive form stirring up clouds of snow as it struggles.\n",
"\n",
"It's an epic battle, a testament to the harsh realities of nature. In these moments, there is no room for error, for either side. The wolves, agile and tenacious, work in unison, their bites a chorus aiming to bring down the great beast. The bison, its every heaving breath a testament to its will to survive, fights fiercely, but the odds are not in its favor.\n",
"\n",
"With the setting sun casting long shadows over the snow, the outcome is inevitable. Nature, in all its raw beauty and brutality, does not show favor. The wolves, now victors, gather around their prize, their survival in this harsh climate secured for a moment longer. It's a poignant reminder of the circle of life that rules this pristine wilderness, a reminder that every creature plays its part in the enduring saga of the natural world.\n"
]
}
],
"source": [
"PROMPT_MESSAGES = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": [\n",
" \"These are frames of a video. Create a short voiceover script in the style of David Attenborough. Only include the narration.\",\n",
" *map(lambda x: {\"image\": x, \"resize\": 768}, base64Frames[0::60]),\n",
" ],\n",
" },\n",
"]\n",
"params = {\n",
" \"model\": \"gpt-4-vision-preview\",\n",
" \"messages\": PROMPT_MESSAGES,\n",
" \"max_tokens\": 500,\n",
"}\n",
"\n",
"result = client.chat.completions.create(**params)\n",
"print(result.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2023-11-10 17:32:51 +00:00
"Now we can pass the script to the TTS API where it will generate an mp3 of the voiceover:\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <audio controls=\"controls\" >\n",
" <source src=\"data:audio/wav;base64,//PkxABhnDnsAOawCD18XMSm4zcRDkbj7WjePjlSDdNjcKDXpzVqzSoz/081OdTrU5xN7TtA1KNTDYo0INSjUg0INAjSUylMowEMsmAQmMICGAhmYRiAWYLSFpC5BaQuQg4W3LxoB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IBIKeEiAVhcseCAVG2npaI+JzrCGB4ApnsEXMzaakzhM9ZI1uniiKqPCwLsyFIgiBNP1GdIqFRF0lzLxgnT1Qp2p9gjwt2lDKIqmmlRBEYbg77xvM/r9rdSZTZZKzqersIe1Y95RVuKxUuEunHbrJmXNKaRcXepZRMnbJHWdxF21qP440vr2Hesw5ALtMukrluI9C7GRzaZq000lywG6lplkjdBgq52BRRu7rUzasTcZkT0srYZOtqsd3HVf51n8kVW1MyWkgOhg7Knld2LU9uc7YhiXy+Dofb/luU2aWNRyC+UFqZuR2H4K9/86WmQIIKBADGezQ01nY33MLsOSTRtzg4yE4lGMRGS0TIbxhww88OeMsqEJDAzQTgxyMGOMvSIkDAegJAwRMDSMFmAazAZALQxLIN2MI6A6zAAqjLMXjEMCjJ42DFMezFwZzcJjjPw5jLoFAEFgUggxCFeCj//PkxHF8dDokrZ/oAAZGUwRXg0kJAx9A8x6OA1usI1KRI7zMEwlBswgDcwnBYyECcwKE4MH8WBAwgCowpE8wVEYw+IAzQIASWYxuKwyoCoxMF8YFQwlAlmSYy2xkIh0KQMIb2uZdMRg4MohsMwBgMdxVMJwLMDgYclhTxsPtKYN0ZfAUMu/GEJZEBocGTJi
" Your browser does not support the audio element.\n",
" </audio>\n",
" "
],
"text/plain": [
"<IPython.lib.display.Audio object>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response = requests.post(\n",
" \"https://api.openai.com/v1/audio/speech\",\n",
" headers={\n",
" \"Authorization\": f\"Bearer {os.environ['OPENAI_API_KEY']}\",\n",
" },\n",
" json={\n",
" \"model\": \"tts-1-1106\",\n",
" \"input\": result.choices[0].message.content,\n",
" \"voice\": \"onyx\",\n",
" },\n",
")\n",
"\n",
"audio = b\"\"\n",
"for chunk in response.iter_content(chunk_size=1024 * 1024):\n",
" audio += chunk\n",
"Audio(audio)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "openai",
"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.16"
}
},
"nbformat": 4,
"nbformat_minor": 2
}