Video AI is one of the most technically interesting frontiers of AI. Sure, you can use LLMs to chat with a chat interface, but what about with a human-like interface? Actually, you can. As a developer, you can now create videos of talking heads, or even replicate yourself, with just a simple API call.
Tavus’ APIs enable all developers to start making AI videos within minutes. In this tutorial, we’re gonna see how. You can find the notebook here.
Prerequisites
There are two main prerequisites - one, getting a Tavus API key, two, having a clean virtual environment of Python 3.10 or later. You can get an API key to get started for free
Once you’ve logged in, you can create an API key in the top right hand corner. Make sure you save this API key somewhere safe because you’ll only see it once.
Setting Up
Alright, let’s set up our notebook. The first thing we need to do is install two libraries, `requests` to make API calls, and `python-dotenv` to manage our environment variables (or in this case, our single API key). You can do this in one line of code via `pip install requests python-dotenv`.
Once we have our libraries installed, we need to set up our API calls in our notebook. I’ve split this into two sections for you here. First, we have a section where we load our environment variables via `load_dotenv` and set the `TAVUS_API_KEY`, which we will use later, via the operating system.
from dotenv import load_dotenv
import os
load_dotenv()
TAVUS_API_KEY = os.environ["TAVUS_API_KEY"]
With our API key ready, the only other piece we need to start making our API calls is the URL. In the next section, we import the `requests` library, which lets us make HTTP requests, and also set our URL. Tavus has both an AI video URL and a replica URL, in this example, we’re creating an AI video, so we’ll use the videos URL.
import requests
url = "https://tavusapi.com/v2/videos"
Calling the Video AI API
The next step is to actually call the API. Our API call contains a JSON payload and some headers. First, let’s define our payload. We need to pass the replica_id
and the script
. In this example, we will also name our video “Video AI Intro”.
You can find a list of the replica IDs here. You can create your own replicas or use a stock replica like I did in this video. Then, you need to define a script. The script tells the API what we want the digital replica to say. For this example, I’m going to have a meta video that talks about AI and what Tavus is doing in the space of video AI.
We only need two headers. The API key, under x-api-key
and the content type. For the API key, we simply pass the API key we created earlier. For the content type, we tell the API that we’re going to pass in some JSON data.
Now, we make a call to the API using the requests library. We send a POST request to the URL we defined above with the JSON payload and headers we defined just now.
payload = {
"replica_id": "r445a7952e",
"script": "AI has now advanced to the point we can generate AI videos with just text, and Tavus is leading the charge. Tavus provides developer APIs that let you create talking avatars with just text. In fact, this video was generated with AI.",
"video_name": "Video AI Intro"
}
headers = {
"x-api-key": TAVUS_API_KEY,
"Content-Type": "application/json"
}
response = requests.request("POST", url, json=payload, headers=headers)
print(response.text)
Let’s see what we get in our response. The API returns a response with five parameters: the status of the video, the video ID, a hosted URL where we can see the video online, when it was created, and what the name of the video is.
Getting Your AI Video Back
Tavus offers a couple of different ways to get your AI generated videos back. Let’s use the list function to get a list of all of the videos we’ve created back. All we need for this is to send a GET request to the API endpoint we defined earlier with the API key in the headers.
headers = {"x-api-key": TAVUS_API_KEY}
response = requests.request("GET", url, headers=headers)
print(response.text)
I’ve created a few different videos already so I have quite a long list of videos to look at, but your first go around, you should only get one video back. You can match the video ID, name, and replica ID. Once you see the “status” response as “ready”, you can click the hosted URL to see your video online.
Summary of Text to Video for AI Replicas
In this tutorial, we learned how to create video AI replicas using text. We used two libraries in this tutorial: requests
and python-dotenv
. We technically only need requests
so we can send HTTP requests, but we use python-dotenv
to manage our environment variables here.
Once we import our libraries, we simply craft an API call with a JSON payload and headers to make a POST request. The JSON payload tells the video AI API the ID of the replica that we’d like to use, the script that the replica should say, and the name of the video. The headers contain the API key and information on the type of data we’re passing (JSON).
Once we call the POST endpoint, the AI video is being created. We can make a GET request with the API key and get a list of the videos associated with that key. When we make the post request, the video will go into “queued” mode, once it’s done, the status should move to “ready”, and we can see our AI generated video online. Sign up for an API key and get started today.