Luba Labs just released it's latest incredible advancement, an API to call on AI video generation tool. Here are some quick steps that you can get up and running very quickly and make your life a whole lot easier.
Here’s a frame-to-frame I generated with the API.
this code will do all 3 options:
Prerequisites
Before we dive in, make sure you have the following:
Python 3.7 or higher installed on your system
A LumaAI account and API key
An ImgBB account and API key
Step 1: Set Up Your Environment
First, let's set up our Python environment and install the necessary packages.
Create a new directory for your project:
mkdir lumaai-video-generator
cd lumaai-video-generator
(Optional) Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows, use: venv\Scripts\activate
Install the required packages:
pip install lumaai requests
Step 2: Obtain API Keys
You'll need two API keys for this project:
LumaAI API Key:
Go to https://lumalabs.ai/dream-machine/api/keys
Create a new API key if you haven't already
ImgBB API Key:
Go to https://api.imgbb.com/
Sign up for a free account and get your API key
Step 3: Set Up Environment Variables
To keep your API keys secure, we'll use environment variables:
For LumaAI, set the LUMAAI_API_KEY environment variable:
On Unix-based systems (Linux/macOS):
export LUMAAI_API_KEY=your_lumaai_api_key_here
On Windows:
set LUMAAI_API_KEY=your_lumaai_api_key_here
For ImgBB, we'll include the API key directly in our script for simplicity. In a production environment, you'd want to use an environment variable for this as well.
Step 4: Create the Python Script
Now, create a new file called luma.py in your project directory. This script will handle text-to-video, image-to-video, and frame-to-frame video generation using LumaAI.
import os
from lumaai import LumaAI
import time
import requests
from datetime import datetime, timedelta
import base64
IMGBB_API_KEY = "[your bbimg api here]"
def list_image_files():
return [f for f in os.listdir('.') if f.lower().endswith(('.jpg', '.jpeg', '.png'))]
def choose_image(prompt):
image_files = list_image_files()
print("Available images:")
for i, file in enumerate(image_files):
print(f"{i + 1}. {file}")
return image_files[int(input(prompt)) - 1]
def upload_image(image_path):
with open(image_path, "rb") as file:
url = "https://api.imgbb.com/1/upload"
payload = {
"key": IMGBB_API_KEY,
"image": base64.b64encode(file.read()),
}
res = requests.post(url, payload)
if res.ok:
return res.json()['data']['url']
else:
print(f"Failed to upload image: {res.text}")
return None
def create_generation(client, generation_params):
try:
generation = client.generations.create(**generation_params)
print("Generation started. Waiting for completion...")
print(f"Generation ID: {generation.id}")
timeout = datetime.now() + timedelta(minutes=5)
while datetime.now() < timeout:
status = client.generations.get(id=generation.id)
if status.state == "completed":
video_url = status.assets.video
print("Generation completed successfully!")
return video_url
elif status.state == "failed":
print(f"Generation failed. Reason: {status.failure_reason}")
return None
time.sleep(5)
else:
print("Generation timed out after 5 minutes. You can check its status later using the generation ID.")
print(f"Generation ID: {generation.id}")
return None
except Exception as e:
print(f"An error occurred: {str(e)}")
return None
def download_video(video_url):
if video_url:
response = requests.get(video_url, stream=True)
output_file = "luma_generated_video.mp4"
with open(output_file, 'wb') as file:
file.write(response.content)
print(f"Video downloaded and saved as {output_file}")
else:
print("Video URL not available. Generation may not have completed successfully.")
def main():
client = LumaAI()
print("Choose generation type:")
print("1. Text to Video")
print("2. Image to Video")
print("3. Frame to Frame Video")
choice = input("Enter your choice (1-3): ")
if choice == '1':
prompt = input("Enter your prompt for text-to-video generation: ")
aspect_ratio = input("Enter aspect ratio (e.g., 16:9, 4:3, 1:1): ")
params = {
"prompt": prompt,
"aspect_ratio": aspect_ratio
}
elif choice == '2':
image = choose_image("Choose an image for image-to-video generation: ")
image_url = upload_image(image)
if not image_url:
print("Failed to upload image. Exiting.")
return
prompt = input("Enter your prompt for image-to-video generation: ")
params = {
"prompt": prompt,
"keyframes": {
"frame0": {
"type": "image",
"url": image_url
}
}
}
elif choice == '3':
start_image = choose_image("Choose the start image: ")
end_image = choose_image("Choose the end image: ")
start_image_url = upload_image(start_image)
end_image_url = upload_image(end_image)
if not start_image_url or not end_image_url:
print("Failed to upload one or both images. Exiting.")
return
prompt = input("Enter your prompt for frame-to-frame video generation: ")
params = {
"prompt": prompt,
"keyframes": {
"frame0": {
"type": "image",
"url": start_image_url
},
"frame1": {
"type": "image",
"url": end_image_url
}
}
}
else:
print("Invalid choice. Exiting.")
return
video_url = create_generation(client, params)
download_video(video_url)
if __name__ == "__main__":
main()
Step 5: Run the Script
With everything set up, you're ready to generate videos:
Make sure you have some image files (JPG or PNG) in the same directory as your script.
Run the script:
python luma.py
Follow the prompts to choose your generation type and provide the necessary inputs.
Wait for the generation to complete. The script will automatically download the generated video when it's ready.
Conclusion
You now have a powerful tool for generating AI videos using LumaAI API! This script allows you to create videos from text prompts, single images, or pairs of images. Feel free to experiment with different prompts and images to see what amazing videos you can create.
Remember to keep an eye on your API usage, as both LumaAI and ImgBB have limits on their free tiers. Happy video generating!
Can anyone explain why LumaLabs can't read images from a personal server?