Beginner’s Guide to Video Generation with Allegro’s API

Thursday, October 31, 2024 by TommyA
Beginner’s Guide to Video Generation with Allegro’s API

Beginner’s Guide to Video Generation with Allegro’s API

Introduction 📹

Hello! Tommy here, and today I’m excited to introduce you to Allegro’s API for video generation by Rhymes AI. This tutorial will walk you through setting up the API, making requests, and receiving high-quality video output—all in real time using Google Colab. I’ll also provide a Colab notebook link so you can easily test the API hands-on.

Whether you’re curious about how text prompts can bring videos to life or looking to start your journey in automated video content creation, this tutorial will walk you through everything you need to get started while keeping it fun and accessible.

Let’s dive into the world of video generation! 🎬

Step 1: Setting Up Your API Token 🔐

Start by storing your Allegro API token securely in Google Colab by setting it as an environment variable.

import os

# Store your API token as an environment variable
os.environ["ALLEGRO_API_KEY"] = "your_bearer_token_here"  # Replace with your actual token

Step 2: Creating a Video Task 🎬

Next, create a video task with the following Python function. Note that Allegro’s API expects a refined prompt, the original user prompt, and several other parameters. Here’s the code to initiate the video generation task:

import requests

def generate_video(token):
    url = "https://api.rhymes.ai/v1/generateVideoSyn"
    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }
    data = {
        "refined_prompt": "Handsome man running on the streets of Tokyo",
        "num_step": 100,
        "cfg_scale": 7.5,
        "user_prompt": "Handsome man running on the streets of Tokyo",
        "rand_seed": 12345
    }

    try:
        response = requests.post(url, headers=headers, json=data)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return f"An error occurred: {str(e)}"

# Fetch your API token from the environment variable
bearer_token = os.getenv("ALLEGRO_API_KEY")
response_data = generate_video(bearer_token)
print(response_data)

Explanation of Parameters:

  • refined_prompt: A refined version of your initial prompt (must be at least 8 words).
  • num_step: Defines the number of inference steps (default is 100 for quality).
  • cfg_scale: Controls video quality (recommended default of 7.5).
  • rand_seed: Ensures consistent output with the same seed value.
  • user_prompt: Original prompt provided by the user.

Upon execution, you should see output like this:

{
  "message": "成功",
  "data": "data-value-from-creating-video-task",
  "status": 0
}

The data field provides the requestId, which you’ll use to track the status of your video task.

Using curl for Command-Line Users

For users who prefer the command line, here’s how you can create the video task using curl:

curl -X POST "https://api.rhymes.ai/v1/generateVideoSyn" \
-H "Authorization: Bearer your_allegro_api_token_here" \
-H "Content-Type: application/json" \
-d '{
    "refined_prompt": "Handsome man running on the streets of Tokyo",
    "num_step": 100,
    "cfg_scale": 7.5,
    "user_prompt": "Handsome man running on the streets of Tokyo",
    "rand_seed": 12345
}'

Replace "your_allegro_api_token_here" with your actual API token.

Step 3: Querying the Video Task Status ⏳

Wait at least two minutes before querying the task status, as the video processing may take some time. Here’s the code to check whether the video is ready:

import requests

def query_video_status(token, request_id):
    url = "https://api.rhymes.ai/v1/videoQuery"
    headers = {
        "Authorization": f"Bearer {token}",
    }
    params = {
        "requestId": request_id
    }

    try:
        response = requests.get(url, headers=headers, params=params)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return f"An error occurred: {str(e)}"

# Replace with the request ID from the previous step
request_id = "data-value-from-creating-video-task"
response_data = query_video_status(bearer_token, request_id)
print(response_data)

The output will include a link to the video in the data field if it’s ready:

{
  "message": "成功",
  "data": "link-to-generated-video",
  "status": 0
}

If the video is not yet ready, you’ll see:

{
  "message": "成功",
  "data": "",
  "status": 0
}

Using curl for Command-Line Users

To check the status of your video task via curl, use the following:

curl -X GET "https://api.rhymes.ai/v1/videoQuery?requestId=data-value-from-creating-video-task" \
-H "Authorization: Bearer your_allegro_api_token_here"

Replace your_bearer_token_here and data-value-from-creating-video-task with your actual values. If the video is ready, the data field will contain a link to the video file stored on an S3 bucket. If not, it will return an empty data field.

Using these curl commands, you can create and track video tasks directly from your terminal, making Allegro’s API accessible for command-line automation or testing.

Find the link to the Google Colab Notebook for this tutorial here.

Conclusion ✅

Congratulations! You’ve successfully created and tracked a video generation task using Allegro’s API. Through this guide, you learned to make API requests, set up parameters, and handle responses, taking you from prompt input to a ready-to-watch video output. Allegro’s API makes video generation simple, letting you create dynamic, high-quality videos from text prompts in just a few steps.

Using this workflow, you can experiment with different prompts and settings, producing video content quickly and efficiently. This setup is ideal for prototyping, social media content, or any scenario where you need short, customized video clips.

For more Advanced API documentation, check out this pdf.

As you continue to explore Allegro’s capabilities, remember that the possibilities are endless—so keep experimenting, and let your creativity shape your next big idea! 🎉

Next Steps 🔜

  1. Experiment with Different Prompts: Explore various prompts to discover Allegro's possibilities.

  2. Optimize Speed or Quality: Adjust num_step to balance speed and quality.

  3. Automate Task Status Checks: Set up a loop or scheduler to automatically query until completion.

  4. Try New Use Cases: Think of projects like generating product demos or instructional videos.

Discover tutorials with similar technologies

Upcoming AI Hackathons and Events