Spaces:
Sleeping
Sleeping
File size: 3,497 Bytes
06a6e91 a361f34 57e4050 a361f34 b6a49f0 06a6e91 57e4050 a361f34 06a6e91 57e4050 a361f34 57e4050 a361f34 57e4050 06a6e91 57e4050 06a6e91 57e4050 06a6e91 a361f34 be66eea b6a49f0 a361f34 b6a49f0 a361f34 b6a49f0 a361f34 06a6e91 57e4050 06a6e91 a361f34 57e4050 06a6e91 57e4050 a361f34 57e4050 06a6e91 57e4050 06a6e91 57e4050 06a6e91 57e4050 a361f34 06a6e91 57e4050 a361f34 57e4050 a361f34 06a6e91 57e4050 06a6e91 57e4050 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
import gradio as gr
import os
from lumaai import LumaAI
import requests
import time
def generate_content(api_key, prompt, aspect_ratio, loop):
"""
Generates video content using LumaAI's API based on user inputs.
Polls for the video once the generation is completed.
Parameters:
- api_key (str): User's LumaAI API key.
- prompt (str): The prompt for video generation.
- aspect_ratio (str): Desired aspect ratio (e.g., "16:9").
- loop (bool): Whether the generation should loop.
Returns:
- str: URL to the generated video or an error message.
"""
try:
# Initialize the LumaAI client with the provided API key
client = LumaAI(auth_token=api_key)
# Create a generation request
generation = client.generations.create(
aspect_ratio=aspect_ratio,
loop=loop,
prompt=prompt,
)
# Polling to check if the generation is complete
generation_id = generation.id
video_url = None
# Poll until the video is ready (adding a timeout of 60 seconds max)
timeout = 60 # seconds
start_time = time.time()
while video_url is None and time.time() - start_time < timeout:
generation_status = client.generations.get(id=generation_id)
video_url = generation_status.assets.get('video', None)
if video_url is None:
time.sleep(5) # Wait for 5 seconds before polling again
if video_url is None:
return "The video generation timed out. Please try again later."
return video_url
except Exception as e:
return f"An error occurred: {str(e)}"
def download_video(video_url):
"""
Downloads the video from the provided URL and saves it locally.
Parameters:
- video_url (str): URL of the video to download.
Returns:
- str: File path of the downloaded video.
"""
response = requests.get(video_url, stream=True)
file_name = 'generated_video.mp4'
with open(file_name, 'wb') as file:
file.write(response.content)
return file_name
# Create the Gradio Blocks interface
with gr.Blocks() as demo:
with gr.Row():
gr.Markdown("# LumaAI Video Generator")
with gr.Row():
api_key = gr.Textbox(label="LumaAI API Key", placeholder="Enter your LumaAI API Key", type="password")
with gr.Row():
prompt = gr.Textbox(label="Prompt", placeholder="Describe what you want to generate...", lines=2)
with gr.Row():
aspect_ratio = gr.Dropdown(label="Aspect Ratio", choices=["16:9", "4:3", "1:1", "21:9"], value="16:9")
with gr.Row():
loop = gr.Checkbox(label="Loop", value=False)
with gr.Row():
generate_button = gr.Button("Generate Video")
with gr.Row():
output = gr.Video(label="Generated Video")
# Define the button click behavior
def handle_generation(api_key, prompt, aspect_ratio, loop):
video_url = generate_content(api_key, prompt, aspect_ratio, loop)
if video_url.startswith("http"):
file_name = download_video(video_url)
return file_name
else:
return video_url
# Link the button click to the function
generate_button.click(
handle_generation,
inputs=[api_key, prompt, aspect_ratio, loop],
outputs=output
)
# Launch the demo
if __name__ == "__main__":
demo.launch()
|