Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from lumaai import Lumaai
|
3 |
+
import requests
|
4 |
+
import time
|
5 |
+
|
6 |
+
def create_client(api_key):
|
7 |
+
return Lumaai(auth_token=api_key)
|
8 |
+
|
9 |
+
def generate_video(api_key, prompt, aspect_ratio, loop):
|
10 |
+
client = create_client(api_key)
|
11 |
+
try:
|
12 |
+
generation = client.generations.create(
|
13 |
+
prompt=prompt,
|
14 |
+
aspect_ratio=aspect_ratio,
|
15 |
+
loop=loop
|
16 |
+
)
|
17 |
+
return generation.id, "Generation started..."
|
18 |
+
except Exception as e:
|
19 |
+
return None, f"Error: {str(e)}"
|
20 |
+
|
21 |
+
def poll_generation(api_key, generation_id):
|
22 |
+
if not generation_id:
|
23 |
+
return "No generation in progress", None
|
24 |
+
|
25 |
+
client = create_client(api_key)
|
26 |
+
try:
|
27 |
+
generation = client.generations.get(id=generation_id)
|
28 |
+
return generation.status, generation.assets.thumbnail
|
29 |
+
except Exception as e:
|
30 |
+
return f"Error: {str(e)}", None
|
31 |
+
|
32 |
+
def download_video(api_key, generation_id):
|
33 |
+
if not generation_id:
|
34 |
+
return None
|
35 |
+
|
36 |
+
client = create_client(api_key)
|
37 |
+
try:
|
38 |
+
generation = client.generations.get(id=generation_id)
|
39 |
+
video_url = generation.assets.video
|
40 |
+
response = requests.get(video_url, stream=True)
|
41 |
+
|
42 |
+
file_name = f"generated_video_{generation_id}.mp4"
|
43 |
+
with open(file_name, 'wb') as file:
|
44 |
+
file.write(response.content)
|
45 |
+
|
46 |
+
return file_name
|
47 |
+
except Exception as e:
|
48 |
+
return None
|
49 |
+
|
50 |
+
with gr.Blocks() as demo:
|
51 |
+
gr.Markdown("# Luma AI Video Generation Demo")
|
52 |
+
gr.Markdown("Generate videos using Luma AI based on text prompts.")
|
53 |
+
|
54 |
+
api_key = gr.Textbox(label="Enter your Luma AI API Key", type="password")
|
55 |
+
|
56 |
+
with gr.Row():
|
57 |
+
with gr.Column(scale=2):
|
58 |
+
prompt = gr.Textbox(label="Prompt")
|
59 |
+
aspect_ratio = gr.Dropdown(["16:9", "9:16", "1:1", "4:3", "3:4"], label="Aspect Ratio")
|
60 |
+
loop = gr.Checkbox(label="Loop")
|
61 |
+
generate_btn = gr.Button("Generate Video")
|
62 |
+
|
63 |
+
with gr.Column(scale=3):
|
64 |
+
status = gr.Textbox(label="Status")
|
65 |
+
thumbnail = gr.Image(label="Thumbnail")
|
66 |
+
video = gr.Video(label="Generated Video")
|
67 |
+
|
68 |
+
generation_id = gr.State()
|
69 |
+
|
70 |
+
def on_generate(api_key, prompt, aspect_ratio, loop):
|
71 |
+
gen_id, message = generate_video(api_key, prompt, aspect_ratio, loop)
|
72 |
+
return gen_id, message
|
73 |
+
|
74 |
+
generate_btn.click(
|
75 |
+
on_generate,
|
76 |
+
inputs=[api_key, prompt, aspect_ratio, loop],
|
77 |
+
outputs=[generation_id, status]
|
78 |
+
)
|
79 |
+
|
80 |
+
def on_poll(api_key, gen_id):
|
81 |
+
status, thumb = poll_generation(api_key, gen_id)
|
82 |
+
if status == "completed":
|
83 |
+
video_path = download_video(api_key, gen_id)
|
84 |
+
return status, thumb, video_path
|
85 |
+
return status, thumb, None
|
86 |
+
|
87 |
+
poll_btn = gr.Button("Check Status")
|
88 |
+
poll_btn.click(
|
89 |
+
on_poll,
|
90 |
+
inputs=[api_key, generation_id],
|
91 |
+
outputs=[status, thumbnail, video]
|
92 |
+
)
|
93 |
+
|
94 |
+
demo.launch()
|