forked from Snowflake-Labs/sf-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added twelvelabs container runtime nb
- Loading branch information
1 parent
1b06af0
commit 1b92e6d
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
samples/notebooks/container_runtime/dash_container_runtime_twelvelabs_app.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Streamlit Notebook", | ||
"name": "streamlit" | ||
} | ||
}, | ||
"nbformat_minor": 5, | ||
"nbformat": 4, | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f9ab3d0d-8bca-422e-ace6-ae19aded5148", | ||
"metadata": { | ||
"name": "cell1", | ||
"collapsed": false | ||
}, | ||
"source": "# Multimodal AI app for videos using Twelve Labs\n\nIn three easy steps:\n\n1) pip install twelvelabs\n2) Create twelvelabs client to generate video embeddings\n3) Use Snowflake's VECTOR_COSINE_SIMILARITY function to find (similar) videos based on text query\n\nPrerequisites: \n> *Create [network rule](https://docs.snowflake.com/en/sql-reference/sql/create-network-rule) and [external access integration](https://docs.snowflake.com/en/sql-reference/sql/create-external-access-integration) (EAI) for installing `twelvelabs` Python packages* and [enable](https://docs.snowflake.com/en/user-guide/ui-snowsight/notebooks-external-access#enable-existing-external-access-integrations-eai) it for this notebook.\n\n> *Obtain a [Twelve Labs API key](https://api.twelvelabs.io).*\n\n\n## What is Container Runtime? \n\nIt provides a flexible environment to build and operationalize a variety of workloads, especially hashtag#ML that require Python packages from multiple sources and powerful compute — both CPUs and GPUs. With this Snowflake-native experience, you can also train models, perform hyperparameter tuning as well as batch inference.\n\nHere are some resources for you to get started:\n\n👉 [Documentation](https://docs.snowflake.com/en/user-guide/ui-snowsight/notebooks-on-spcs) includes quickstart guides\n\n👉 [Github repo](https://github.com/Snowflake-Labs/snowflake-demo-notebooks) of tutorials and example notebooks\n\n👉 [YouTube](https://www.youtube.com/playlist?list=PLavJpcg8cl1Efw8x_fBKmfA2AMwjUaeBI) demo playlist" | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"id": "3775908f-ca36-4846-8f38-5adca39217f2", | ||
"metadata": { | ||
"language": "python", | ||
"name": "Install_twelvelabs", | ||
"collapsed": false | ||
}, | ||
"source": "!pip install twelvelabs", | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"id": "2dee16f1-240c-4424-905a-e4d45569596f", | ||
"metadata": { | ||
"language": "python", | ||
"name": "Generate_video_embeddings", | ||
"collapsed": false, | ||
"codeCollapsed": false | ||
}, | ||
"outputs": [], | ||
"source": "from twelvelabs import TwelveLabs\nfrom twelvelabs.models.embed import EmbeddingsTask\nfrom snowflake.snowpark.context import get_active_session\nsession = get_active_session()\nTWELVE_LABS_API_KEY =\"tlk_2VAE3AA2XGRHKXXXXXXXXXXXX\"\n# Initialize the Twelve Labs client\ntwelvelabs_client = TwelveLabs(api_key=TWELVE_LABS_API_KEY)\ndef get_video_embeddings(url: str) -> float:\n twelvelabs_client = TwelveLabs(api_key=TWELVE_LABS_API_KEY)\n task = twelvelabs_client.embed.task.create(\n engine_name=\"Marengo-retrieval-2.6\",\n video_url=url)\n task.wait_for_done()\n task_result = twelvelabs_client.embed.task.retrieve(task.id)\n embeddings = []\n for v in task_result.video_embeddings:\n embeddings.append({\n 'embedding': v.embedding.float,\n 'start_offset_sec': v.start_offset_sec,\n 'end_offset_sec': v.end_offset_sec,\n 'embedding_scope': v.embedding_scope\n })\n return embeddings if embeddings else None\n\nvideo_url = 'https://videos.s3.us-west-1.amazonaws.com/misc/videos/demo.mp4'\nembeddings = get_video_embeddings(video_url)", | ||
"execution_count": null | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"id": "73487b72-beb7-4542-b29b-e0b3fd51a1ff", | ||
"metadata": { | ||
"language": "python", | ||
"name": "Find_text_to_video_similarity_score", | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": "def get_similar_videos(text_query):\n # Twelve Labs Embed API supports text-to-embedding \n text_query_embeddings = twelvelabs_client.embed.create(\n engine_name=\"Marengo-retrieval-2.6\",\n text=text_query,\n text_truncate=\"start\"\n ).text_embedding.float\n\n return session.sql(f\"\"\"SELECT \n round(VECTOR_COSINE_SIMILARITY({embeddings[0]['embedding']}::VECTOR(FLOAT, 1024),\n {text_query_embeddings}::VECTOR(FLOAT, 1024)),2) as similarity\"\"\") \\\n .to_pandas().iloc[0]['SIMILARITY']\n\nget_similar_videos('snowflake notebooks')", | ||
"execution_count": null | ||
} | ||
] | ||
} |