Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
-
from lumaai
|
3 |
import requests
|
4 |
import os
|
5 |
-
import time
|
6 |
|
7 |
# Initialize LumaAI client
|
8 |
def create_client(api_key):
|
@@ -29,7 +28,11 @@ def poll_generation(api_key, generation_id):
|
|
29 |
client = create_client(api_key)
|
30 |
try:
|
31 |
generation = client.generations.get(id=generation_id)
|
32 |
-
|
|
|
|
|
|
|
|
|
33 |
except Exception as e:
|
34 |
return f"Error: {str(e)}", None
|
35 |
|
@@ -44,60 +47,70 @@ def download_video(api_key, generation_id):
|
|
44 |
video_url = generation.assets.video
|
45 |
response = requests.get(video_url, stream=True)
|
46 |
|
|
|
|
|
|
|
47 |
file_name = f"generated_video_{generation_id}.mp4"
|
48 |
with open(file_name, 'wb') as file:
|
49 |
-
|
|
|
|
|
50 |
|
51 |
return file_name
|
52 |
except Exception as e:
|
53 |
return None
|
54 |
|
55 |
-
# Advanced video generation with keyframes and camera motions
|
56 |
-
def generate_video_advanced(api_key, prompt, aspect_ratio, loop, keyframes, camera_motion):
|
57 |
-
client = create_client(api_key)
|
58 |
-
try:
|
59 |
-
generation_params = {
|
60 |
-
"prompt": prompt,
|
61 |
-
"aspect_ratio": aspect_ratio,
|
62 |
-
"loop": loop
|
63 |
-
}
|
64 |
-
|
65 |
-
# Parse keyframes from JSON string
|
66 |
-
if keyframes:
|
67 |
-
import json
|
68 |
-
generation_params["keyframes"] = json.loads(keyframes)
|
69 |
-
|
70 |
-
# Add camera motion if provided
|
71 |
-
if camera_motion:
|
72 |
-
generation_params["camera_motion"] = camera_motion
|
73 |
-
|
74 |
-
generation = client.generations.create(**generation_params)
|
75 |
-
return generation.id, "Advanced generation started..."
|
76 |
-
except Exception as e:
|
77 |
-
return None, f"Error: {str(e)}"
|
78 |
-
|
79 |
with gr.Blocks() as demo:
|
80 |
gr.Markdown("# Luma AI Video Generation Demo")
|
81 |
gr.Markdown("Generate videos using Luma AI based on text prompts.")
|
82 |
|
83 |
-
api_key = gr.Textbox(
|
|
|
|
|
|
|
|
|
84 |
|
85 |
with gr.Row():
|
86 |
with gr.Column(scale=2):
|
87 |
-
prompt = gr.Textbox(
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
generate_btn = gr.Button("Generate Video")
|
91 |
|
92 |
with gr.Column(scale=3):
|
93 |
-
status = gr.Textbox(
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
generation_id = gr.State()
|
98 |
|
99 |
-
#
|
100 |
def on_generate(api_key, prompt, aspect_ratio, loop):
|
|
|
|
|
|
|
|
|
|
|
101 |
gen_id, message = generate_video(api_key, prompt, aspect_ratio, loop)
|
102 |
return gen_id, message
|
103 |
|
@@ -107,16 +120,32 @@ with gr.Blocks() as demo:
|
|
107 |
outputs=[generation_id, status]
|
108 |
)
|
109 |
|
110 |
-
#
|
111 |
def on_poll(api_key, gen_id):
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
113 |
if status_text.lower() == "completed":
|
114 |
video_path = download_video(api_key, gen_id)
|
115 |
if video_path:
|
116 |
-
|
117 |
else:
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
poll_btn = gr.Button("Check Status")
|
122 |
poll_btn.click(
|
@@ -125,59 +154,6 @@ with gr.Blocks() as demo:
|
|
125 |
outputs=[status, thumbnail, video]
|
126 |
)
|
127 |
|
128 |
-
# Accordion for Advanced Features
|
129 |
-
with gr.Accordion("Advanced Features", open=False):
|
130 |
-
with gr.Row():
|
131 |
-
with gr.Column(scale=1):
|
132 |
-
keyframes = gr.Textbox(
|
133 |
-
label="Keyframes (JSON)",
|
134 |
-
placeholder='{"frame0": {"type": "image", "url": "https://example.com/image1.jpg"}, "frame1": {"type": "image", "url": "https://example.com/image2.jpg"}}'
|
135 |
-
)
|
136 |
-
camera_motion = gr.Dropdown(
|
137 |
-
["pan", "tilt", "zoom", "orbit"],
|
138 |
-
label="Camera Motion",
|
139 |
-
choices=["pan", "tilt", "zoom", "orbit"], # Ensure choices are correctly passed
|
140 |
-
value=None,
|
141 |
-
allow_none=True
|
142 |
-
)
|
143 |
-
advanced_generate_btn = gr.Button("Generate with Advanced Options")
|
144 |
-
|
145 |
-
with gr.Column(scale=2):
|
146 |
-
advanced_status = gr.Textbox(label="Advanced Status")
|
147 |
-
advanced_thumbnail = gr.Image(label="Advanced Thumbnail")
|
148 |
-
advanced_video = gr.Video(label="Advanced Generated Video")
|
149 |
-
|
150 |
-
advanced_gen_id = gr.State()
|
151 |
-
|
152 |
-
# Advanced generate button action
|
153 |
-
def on_generate_advanced(api_key, prompt, aspect_ratio, loop, keyframes, camera_motion):
|
154 |
-
gen_id, message = generate_video_advanced(api_key, prompt, aspect_ratio, loop, keyframes, camera_motion)
|
155 |
-
return gen_id, message
|
156 |
-
|
157 |
-
advanced_generate_btn.click(
|
158 |
-
on_generate_advanced,
|
159 |
-
inputs=[api_key, prompt, aspect_ratio, loop, keyframes, camera_motion],
|
160 |
-
outputs=[advanced_gen_id, advanced_status]
|
161 |
-
)
|
162 |
-
|
163 |
-
# Poll status and download video for advanced generation
|
164 |
-
def on_poll_advanced(api_key, gen_id):
|
165 |
-
status_text, thumb = poll_generation(api_key, gen_id)
|
166 |
-
if status_text.lower() == "completed":
|
167 |
-
video_path = download_video(api_key, gen_id)
|
168 |
-
if video_path:
|
169 |
-
return status_text, thumb, video_path
|
170 |
-
else:
|
171 |
-
return status_text, thumb, None
|
172 |
-
return status_text, thumb, None
|
173 |
-
|
174 |
-
advanced_poll_btn = gr.Button("Check Advanced Status")
|
175 |
-
advanced_poll_btn.click(
|
176 |
-
on_poll_advanced,
|
177 |
-
inputs=[api_key, advanced_gen_id],
|
178 |
-
outputs=[advanced_status, advanced_thumbnail, advanced_video]
|
179 |
-
)
|
180 |
-
|
181 |
gr.Markdown("""
|
182 |
---
|
183 |
### Documentation & Resources
|
|
|
1 |
import gradio as gr
|
2 |
+
from lumaai import LumaAI
|
3 |
import requests
|
4 |
import os
|
|
|
5 |
|
6 |
# Initialize LumaAI client
|
7 |
def create_client(api_key):
|
|
|
28 |
client = create_client(api_key)
|
29 |
try:
|
30 |
generation = client.generations.get(id=generation_id)
|
31 |
+
status = generation.status
|
32 |
+
thumbnail_url = generation.assets.thumbnail
|
33 |
+
thumbnail_response = requests.get(thumbnail_url)
|
34 |
+
thumbnail = thumbnail_response.content if thumbnail_response.status_code == 200 else None
|
35 |
+
return status, thumbnail
|
36 |
except Exception as e:
|
37 |
return f"Error: {str(e)}", None
|
38 |
|
|
|
47 |
video_url = generation.assets.video
|
48 |
response = requests.get(video_url, stream=True)
|
49 |
|
50 |
+
if response.status_code != 200:
|
51 |
+
return None
|
52 |
+
|
53 |
file_name = f"generated_video_{generation_id}.mp4"
|
54 |
with open(file_name, 'wb') as file:
|
55 |
+
for chunk in response.iter_content(chunk_size=8192):
|
56 |
+
if chunk:
|
57 |
+
file.write(chunk)
|
58 |
|
59 |
return file_name
|
60 |
except Exception as e:
|
61 |
return None
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
with gr.Blocks() as demo:
|
64 |
gr.Markdown("# Luma AI Video Generation Demo")
|
65 |
gr.Markdown("Generate videos using Luma AI based on text prompts.")
|
66 |
|
67 |
+
api_key = gr.Textbox(
|
68 |
+
label="Enter your Luma AI API Key",
|
69 |
+
type="password",
|
70 |
+
placeholder="sk-..."
|
71 |
+
)
|
72 |
|
73 |
with gr.Row():
|
74 |
with gr.Column(scale=2):
|
75 |
+
prompt = gr.Textbox(
|
76 |
+
label="Prompt",
|
77 |
+
placeholder="Describe your video...",
|
78 |
+
lines=2
|
79 |
+
)
|
80 |
+
aspect_ratio = gr.Dropdown(
|
81 |
+
choices=["16:9", "9:16", "1:1", "4:3", "3:4"],
|
82 |
+
label="Aspect Ratio",
|
83 |
+
value="16:9"
|
84 |
+
)
|
85 |
+
loop = gr.Checkbox(
|
86 |
+
label="Loop",
|
87 |
+
value=False
|
88 |
+
)
|
89 |
generate_btn = gr.Button("Generate Video")
|
90 |
|
91 |
with gr.Column(scale=3):
|
92 |
+
status = gr.Textbox(
|
93 |
+
label="Status",
|
94 |
+
readonly=True
|
95 |
+
)
|
96 |
+
thumbnail = gr.Image(
|
97 |
+
label="Thumbnail",
|
98 |
+
interactive=False
|
99 |
+
)
|
100 |
+
video = gr.Video(
|
101 |
+
label="Generated Video",
|
102 |
+
interactive=False
|
103 |
+
)
|
104 |
|
105 |
generation_id = gr.State()
|
106 |
|
107 |
+
# Generate Video Action
|
108 |
def on_generate(api_key, prompt, aspect_ratio, loop):
|
109 |
+
if not api_key:
|
110 |
+
return None, "Please provide a valid API key."
|
111 |
+
if not prompt:
|
112 |
+
return None, "Prompt cannot be empty."
|
113 |
+
|
114 |
gen_id, message = generate_video(api_key, prompt, aspect_ratio, loop)
|
115 |
return gen_id, message
|
116 |
|
|
|
120 |
outputs=[generation_id, status]
|
121 |
)
|
122 |
|
123 |
+
# Check Status and Display Results
|
124 |
def on_poll(api_key, gen_id):
|
125 |
+
if not api_key:
|
126 |
+
return "Please provide a valid API key.", None, None
|
127 |
+
if not gen_id:
|
128 |
+
return "No generation in progress.", None, None
|
129 |
+
|
130 |
+
status_text, thumb_data = poll_generation(api_key, gen_id)
|
131 |
if status_text.lower() == "completed":
|
132 |
video_path = download_video(api_key, gen_id)
|
133 |
if video_path:
|
134 |
+
video_output = video_path
|
135 |
else:
|
136 |
+
video_output = None
|
137 |
+
else:
|
138 |
+
video_output = None
|
139 |
+
|
140 |
+
# Convert thumbnail bytes to image format
|
141 |
+
if thumb_data:
|
142 |
+
from PIL import Image
|
143 |
+
from io import BytesIO
|
144 |
+
thumb_image = Image.open(BytesIO(thumb_data))
|
145 |
+
else:
|
146 |
+
thumb_image = None
|
147 |
+
|
148 |
+
return status_text, thumb_image, video_output
|
149 |
|
150 |
poll_btn = gr.Button("Check Status")
|
151 |
poll_btn.click(
|
|
|
154 |
outputs=[status, thumbnail, video]
|
155 |
)
|
156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
gr.Markdown("""
|
158 |
---
|
159 |
### Documentation & Resources
|