Spaces:
Sleeping
Sleeping
File size: 5,095 Bytes
06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea 06a6e91 be66eea |
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
import gradio as gr
import LumaAI
import requests
from PIL import Image
from io import BytesIO
import os
# Initialize LumaAI client
def create_client(api_key):
return LumaAI.LumaAI(auth_token=api_key) # Assuming LumaAI is a class within the LumaAI module
# Basic video generation
def generate_video(api_key, prompt, aspect_ratio, loop):
client = create_client(api_key)
try:
generation = client.generations.create(
prompt=prompt,
aspect_ratio=aspect_ratio,
loop=loop
)
return generation.id, "Generation started..."
except Exception as e:
return None, f"Error: {str(e)}"
# Polling generation status
def poll_generation(api_key, generation_id):
if not generation_id:
return "No generation in progress", None
client = create_client(api_key)
try:
generation = client.generations.get(id=generation_id)
status = generation.status
thumbnail_url = generation.assets.thumbnail
# Fetch the thumbnail image
thumbnail_response = requests.get(thumbnail_url)
thumbnail = thumbnail_response.content if thumbnail_response.status_code == 200 else None
return status, thumbnail
except Exception as e:
return f"Error: {str(e)}", None
# Download the generated video
def download_video(api_key, generation_id):
if not generation_id:
return None
client = create_client(api_key)
try:
generation = client.generations.get(id=generation_id)
video_url = generation.assets.video
response = requests.get(video_url, stream=True)
if response.status_code != 200:
return None
file_name = f"generated_video_{generation_id}.mp4"
with open(file_name, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
file.write(chunk)
return file_name
except Exception as e:
return None
with gr.Blocks() as demo:
gr.Markdown("# Luma AI Video Generation Demo")
gr.Markdown("Generate videos using Luma AI based on text prompts.")
api_key = gr.Textbox(
label="Enter your Luma AI API Key",
type="password",
placeholder="sk-..."
)
with gr.Row():
with gr.Column(scale=2):
prompt = gr.Textbox(
label="Prompt",
placeholder="Describe your video...",
lines=2
)
aspect_ratio = gr.Dropdown(
choices=["16:9", "9:16", "1:1", "4:3", "3:4"],
label="Aspect Ratio",
value="16:9"
)
loop = gr.Checkbox(
label="Loop",
value=False
)
generate_btn = gr.Button("Generate Video")
with gr.Column(scale=3):
status = gr.Textbox(
label="Status",
interactive=False # Replaces readonly=True
)
thumbnail = gr.Image(
label="Thumbnail",
interactive=False
)
video = gr.Video(
label="Generated Video",
interactive=False
)
generation_id = gr.State()
# Generate Video Action
def on_generate(api_key, prompt, aspect_ratio, loop):
if not api_key:
return None, "Please provide a valid API key."
if not prompt:
return None, "Prompt cannot be empty."
gen_id, message = generate_video(api_key, prompt, aspect_ratio, loop)
return gen_id, message
generate_btn.click(
on_generate,
inputs=[api_key, prompt, aspect_ratio, loop],
outputs=[generation_id, status]
)
# Check Status and Display Results
def on_poll(api_key, gen_id):
if not api_key:
return "Please provide a valid API key.", None, None
if not gen_id:
return "No generation in progress.", None, None
status_text, thumb_data = poll_generation(api_key, gen_id)
if status_text.lower() == "completed":
video_path = download_video(api_key, gen_id)
if video_path:
video_output = video_path
else:
video_output = None
else:
video_output = None
# Convert thumbnail bytes to image format
if thumb_data:
thumb_image = Image.open(BytesIO(thumb_data))
else:
thumb_image = None
return status_text, thumb_image, video_output
poll_btn = gr.Button("Check Status")
poll_btn.click(
on_poll,
inputs=[api_key, generation_id],
outputs=[status, thumbnail, video]
)
gr.Markdown("""
---
### Documentation & Resources
- [Luma AI Python SDK](https://github.com/lumalabs/lumaai-python)
- [Get API Key](https://lumalabs.ai/dream-machine/api/keys)
""")
demo.launch()
|