File size: 2,956 Bytes
06a6e91
2cde650
a361f34
b6a49f0
06a6e91
2cde650
 
06a6e91
2cde650
 
06a6e91
 
57e4050
2cde650
 
06a6e91
2cde650
06a6e91
2cde650
a361f34
2cde650
 
 
a361f34
2cde650
 
 
 
 
 
 
 
 
 
a361f34
2cde650
 
 
 
 
 
 
 
 
 
 
 
 
a361f34
06a6e91
2cde650
 
06a6e91
2cde650
06a6e91
 
2cde650
 
 
 
 
 
 
 
 
 
06a6e91
2cde650
06a6e91
2cde650
 
 
 
 
 
06a6e91
2cde650
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06a6e91
57e4050
2cde650
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
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()