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
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from lumaai import AsyncLumaAI
|
| 4 |
+
import asyncio
|
| 5 |
+
import aiohttp
|
| 6 |
+
|
| 7 |
+
async def generate_video(api_key, prompt, loop=False, aspect_ratio="16:9", progress=gr.Progress()):
|
| 8 |
+
client = AsyncLumaAI(auth_token=api_key)
|
| 9 |
+
|
| 10 |
+
progress(0, desc="Initiating video generation...")
|
| 11 |
+
generation = await client.generations.create(
|
| 12 |
+
prompt=prompt,
|
| 13 |
+
loop=loop,
|
| 14 |
+
aspect_ratio=aspect_ratio
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
progress(0.1, desc="Video generation started. Waiting for completion...")
|
| 18 |
+
|
| 19 |
+
# Poll for completion
|
| 20 |
+
start_time = asyncio.get_event_loop().time()
|
| 21 |
+
while True:
|
| 22 |
+
status = await client.generations.get(id=generation.id)
|
| 23 |
+
if status.state == "completed":
|
| 24 |
+
break
|
| 25 |
+
elif status.state == "failed":
|
| 26 |
+
raise Exception("Video generation failed")
|
| 27 |
+
|
| 28 |
+
# Update progress based on time elapsed (assuming 60 seconds total)
|
| 29 |
+
elapsed_time = asyncio.get_event_loop().time() - start_time
|
| 30 |
+
progress_value = min(0.1 + (elapsed_time / 60) * 0.8, 0.9)
|
| 31 |
+
progress(progress_value, desc="Generating video...")
|
| 32 |
+
|
| 33 |
+
await asyncio.sleep(5)
|
| 34 |
+
|
| 35 |
+
progress(0.9, desc="Downloading generated video...")
|
| 36 |
+
|
| 37 |
+
# Download the video
|
| 38 |
+
video_url = status.assets.video
|
| 39 |
+
async with aiohttp.ClientSession() as session:
|
| 40 |
+
async with session.get(video_url) as resp:
|
| 41 |
+
if resp.status == 200:
|
| 42 |
+
file_name = f"luma_ai_generated_{generation.id}.mp4"
|
| 43 |
+
with open(file_name, 'wb') as fd:
|
| 44 |
+
while True:
|
| 45 |
+
chunk = await resp.content.read(1024)
|
| 46 |
+
if not chunk:
|
| 47 |
+
break
|
| 48 |
+
fd.write(chunk)
|
| 49 |
+
|
| 50 |
+
progress(1.0, desc="Video generation complete!")
|
| 51 |
+
return file_name
|
| 52 |
+
|
| 53 |
+
async def text_to_video(api_key, prompt, loop, aspect_ratio, progress=gr.Progress()):
|
| 54 |
+
if not api_key:
|
| 55 |
+
raise gr.Error("Please enter your Luma AI API key.")
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
video_path = await generate_video(api_key, prompt, loop, aspect_ratio, progress)
|
| 59 |
+
return video_path, ""
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return None, f"An error occurred: {str(e)}"
|
| 62 |
+
|
| 63 |
+
async def image_to_video(api_key, prompt, image_url, loop, aspect_ratio, progress=gr.Progress()):
|
| 64 |
+
if not api_key:
|
| 65 |
+
raise gr.Error("Please enter your Luma AI API key.")
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
client = AsyncLumaAI(auth_token=api_key)
|
| 69 |
+
|
| 70 |
+
progress(0, desc="Initiating video generation from image...")
|
| 71 |
+
generation = await client.generations.create(
|
| 72 |
+
prompt=prompt,
|
| 73 |
+
loop=loop,
|
| 74 |
+
aspect_ratio=aspect_ratio,
|
| 75 |
+
keyframes={
|
| 76 |
+
"frame0": {
|
| 77 |
+
"type": "image",
|
| 78 |
+
"url": image_url
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
progress(0.1, desc="Video generation started. Waiting for completion...")
|
| 84 |
+
|
| 85 |
+
# Poll for completion
|
| 86 |
+
start_time = asyncio.get_event_loop().time()
|
| 87 |
+
while True:
|
| 88 |
+
status = await client.generations.get(id=generation.id)
|
| 89 |
+
if status.state == "completed":
|
| 90 |
+
break
|
| 91 |
+
elif status.state == "failed":
|
| 92 |
+
raise Exception("Video generation failed")
|
| 93 |
+
|
| 94 |
+
# Update progress based on time elapsed (assuming 60 seconds total)
|
| 95 |
+
elapsed_time = asyncio.get_event_loop().time() - start_time
|
| 96 |
+
progress_value = min(0.1 + (elapsed_time / 60) * 0.8, 0.9)
|
| 97 |
+
progress(progress_value, desc="Generating video...")
|
| 98 |
+
|
| 99 |
+
await asyncio.sleep(5)
|
| 100 |
+
|
| 101 |
+
progress(0.9, desc="Downloading generated video...")
|
| 102 |
+
|
| 103 |
+
# Download the video
|
| 104 |
+
video_url = status.assets.video
|
| 105 |
+
async with aiohttp.ClientSession() as session:
|
| 106 |
+
async with session.get(video_url) as resp:
|
| 107 |
+
if resp.status == 200:
|
| 108 |
+
file_name = f"luma_ai_generated_{generation.id}.mp4"
|
| 109 |
+
with open(file_name, 'wb') as fd:
|
| 110 |
+
while True:
|
| 111 |
+
chunk = await resp.content.read(1024)
|
| 112 |
+
if not chunk:
|
| 113 |
+
break
|
| 114 |
+
fd.write(chunk)
|
| 115 |
+
|
| 116 |
+
progress(1.0, desc="Video generation complete!")
|
| 117 |
+
return file_name, ""
|
| 118 |
+
except Exception as e:
|
| 119 |
+
return None, f"An error occurred: {str(e)}"
|
| 120 |
+
|
| 121 |
+
with gr.Blocks() as demo:
|
| 122 |
+
gr.Markdown("# Luma AI Text-to-Video Demo")
|
| 123 |
+
|
| 124 |
+
api_key = gr.Textbox(label="Luma AI API Key", type="password")
|
| 125 |
+
|
| 126 |
+
with gr.Tab("Text to Video"):
|
| 127 |
+
prompt = gr.Textbox(label="Prompt")
|
| 128 |
+
generate_btn = gr.Button("Generate Video")
|
| 129 |
+
video_output = gr.Video(label="Generated Video")
|
| 130 |
+
error_output = gr.Textbox(label="Error Messages", visible=True)
|
| 131 |
+
|
| 132 |
+
with gr.Accordion("Advanced Options", open=False):
|
| 133 |
+
loop = gr.Checkbox(label="Loop", value=False)
|
| 134 |
+
aspect_ratio = gr.Dropdown(label="Aspect Ratio", choices=["16:9", "1:1", "9:16", "4:3", "3:4"], value="16:9")
|
| 135 |
+
|
| 136 |
+
generate_btn.click(
|
| 137 |
+
text_to_video,
|
| 138 |
+
inputs=[api_key, prompt, loop, aspect_ratio],
|
| 139 |
+
outputs=[video_output, error_output]
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
with gr.Tab("Image to Video"):
|
| 143 |
+
img_prompt = gr.Textbox(label="Prompt")
|
| 144 |
+
img_url = gr.Textbox(label="Image URL")
|
| 145 |
+
img_generate_btn = gr.Button("Generate Video from Image")
|
| 146 |
+
img_video_output = gr.Video(label="Generated Video")
|
| 147 |
+
img_error_output = gr.Textbox(label="Error Messages", visible=True)
|
| 148 |
+
|
| 149 |
+
with gr.Accordion("Advanced Options", open=False):
|
| 150 |
+
img_loop = gr.Checkbox(label="Loop", value=False)
|
| 151 |
+
img_aspect_ratio = gr.Dropdown(label="Aspect Ratio", choices=["16:9", "1:1", "9:16", "4:3", "3:4"], value="16:9")
|
| 152 |
+
|
| 153 |
+
img_generate_btn.click(
|
| 154 |
+
image_to_video,
|
| 155 |
+
inputs=[api_key, img_prompt, img_url, img_loop, img_aspect_ratio],
|
| 156 |
+
outputs=[img_video_output, img_error_output]
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
demo.queue().launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
lumaai
|