dream-machine / app.py
akhaliq's picture
akhaliq HF staff
Create app.py
06a6e91 verified
raw
history blame
2.96 kB
import gradio as gr
from lumaai import Lumaai
import requests
import time
def create_client(api_key):
return Lumaai(auth_token=api_key)
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)}"
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)
return generation.status, generation.assets.thumbnail
except Exception as e:
return f"Error: {str(e)}", None
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)
file_name = f"generated_video_{generation_id}.mp4"
with open(file_name, 'wb') as file:
file.write(response.content)
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")
with gr.Row():
with gr.Column(scale=2):
prompt = gr.Textbox(label="Prompt")
aspect_ratio = gr.Dropdown(["16:9", "9:16", "1:1", "4:3", "3:4"], label="Aspect Ratio")
loop = gr.Checkbox(label="Loop")
generate_btn = gr.Button("Generate Video")
with gr.Column(scale=3):
status = gr.Textbox(label="Status")
thumbnail = gr.Image(label="Thumbnail")
video = gr.Video(label="Generated Video")
generation_id = gr.State()
def on_generate(api_key, prompt, aspect_ratio, loop):
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]
)
def on_poll(api_key, gen_id):
status, thumb = poll_generation(api_key, gen_id)
if status == "completed":
video_path = download_video(api_key, gen_id)
return status, thumb, video_path
return status, thumb, None
poll_btn = gr.Button("Check Status")
poll_btn.click(
on_poll,
inputs=[api_key, generation_id],
outputs=[status, thumbnail, video]
)
demo.launch()