YouTube Video Summary with Gemini Pro Vision

Gemini Vision Pro is an advanced generative AI model developed by Google AI. It has the remarkable ability to process visual information.

In this post, let us see how we can utilize Gemini Vision Pro to summarize YouTube videos.

Get the deps:

pip install google-cloud-aiplatform
pip install pytube

Download and save to Google Storage:

The GenerativeModel expects the video to be on Google Storage for ML to operate on the file.

from pytube import YouTube
from google.cloud import storage

def  download_youtube_video(youtube_url, output_path):
  yt = YouTube(youtube_url)
  video_stream = yt.streams.filter(file_extension='mp4', res='720p').first()
  video_stream.download(output_path)
  return  f"{output_path}/{yt.title}.mp4"


def  upload_video_to_gcs(video_path, bucket_name, destination_blob_name):
  client = storage.Client()
  bucket = client.get_bucket(bucket_name)
  blob = bucket.blob(destination_blob_name)
  blob.upload_from_filename(video_path)
  print(f"Video uploaded to GCS: gs://{bucket_name}/{destination_blob_name}")
youtube_url = "https://www.youtube.com/watch?v=bSHp7WVpPgc"
output_path = "."
bucket_name = "my-bucket"
destination_blob_name = "videos/video.mp4"
  
# Download YouTube video
downloaded_video_path = download_youtube_video(youtube_url, output_path)

# Upload video to Google Cloud Storage
upload_video_to_gcs(downloaded_video_path, bucket_name, destination_blob_name)

Summarize with VertexAI:

from vertexai.preview import generative_models
from vertexai.preview.generative_models import GenerativeModel

gemini_pro_vision_model = GenerativeModel("gemini-pro-vision")
response = gemini_pro_vision_model.generate_content([
	"Summarize the video",
	generative_models.Part.from_uri("gs://my-bucket/video.mp4", mime_type="video/mp4"),
], stream=True)

for chunk in response :
	print(chunk.text)

This would output something like:

In this video, Hemant HM discusses the importance of open source in India. 

He highlights how open source has helped to create a vibrant developer community in the country, and how it is helping to drive innovation. 

He also discusses some of the challenges that the open source community faces in India, and how these can be overcome.

Provided that you have properly established the necessary configurations for Vertex in your Google Cloud project, you should encounter no difficulties when utilizing this code.