Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,112 +1,94 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from lumaai import LumaAI
|
4 |
import requests
|
5 |
import time
|
6 |
|
7 |
-
def
|
8 |
-
|
9 |
-
Generates video content using LumaAI's API based on user inputs.
|
10 |
-
Polls for the video once the generation is completed.
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
- prompt (str): The prompt for video generation.
|
15 |
-
- aspect_ratio (str): Desired aspect ratio (e.g., "16:9").
|
16 |
-
- loop (bool): Whether the generation should loop.
|
17 |
-
|
18 |
-
Returns:
|
19 |
-
- str: URL to the generated video or an error message.
|
20 |
-
"""
|
21 |
try:
|
22 |
-
# Initialize the LumaAI client with the provided API key
|
23 |
-
client = LumaAI(auth_token=api_key)
|
24 |
-
|
25 |
-
# Create a generation request
|
26 |
generation = client.generations.create(
|
27 |
-
aspect_ratio=aspect_ratio,
|
28 |
-
loop=loop,
|
29 |
prompt=prompt,
|
|
|
|
|
30 |
)
|
31 |
-
|
32 |
-
# Polling to check if the generation is complete
|
33 |
-
generation_id = generation.id
|
34 |
-
video_url = None
|
35 |
-
|
36 |
-
# Poll until the video is ready (adding a timeout of 60 seconds max)
|
37 |
-
timeout = 60 # seconds
|
38 |
-
start_time = time.time()
|
39 |
-
|
40 |
-
while video_url is None and time.time() - start_time < timeout:
|
41 |
-
generation_status = client.generations.get(id=generation_id)
|
42 |
-
video_url = generation_status.assets.get('video', None)
|
43 |
-
if video_url is None:
|
44 |
-
time.sleep(5) # Wait for 5 seconds before polling again
|
45 |
-
|
46 |
-
if video_url is None:
|
47 |
-
return "The video generation timed out. Please try again later."
|
48 |
-
return video_url
|
49 |
except Exception as e:
|
50 |
-
return f"
|
51 |
-
|
52 |
|
53 |
-
def
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
Parameters:
|
58 |
-
- video_url (str): URL of the video to download.
|
59 |
-
|
60 |
-
Returns:
|
61 |
-
- str: File path of the downloaded video.
|
62 |
-
"""
|
63 |
-
response = requests.get(video_url, stream=True)
|
64 |
-
file_name = 'generated_video.mp4'
|
65 |
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
# Create the Gradio Blocks interface
|
72 |
with gr.Blocks() as demo:
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
with gr.Row():
|
77 |
-
api_key = gr.Textbox(label="LumaAI API Key", placeholder="Enter your LumaAI API Key", type="password")
|
78 |
|
79 |
-
|
80 |
-
prompt = gr.Textbox(label="Prompt", placeholder="Describe what you want to generate...", lines=2)
|
81 |
|
82 |
with gr.Row():
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
-
|
86 |
-
loop = gr.Checkbox(label="Loop", value=False)
|
87 |
-
|
88 |
-
with gr.Row():
|
89 |
-
generate_button = gr.Button("Generate Video")
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
if video_url.startswith("http"):
|
98 |
-
file_name = download_video(video_url)
|
99 |
-
return file_name
|
100 |
-
else:
|
101 |
-
return video_url
|
102 |
-
|
103 |
-
# Link the button click to the function
|
104 |
-
generate_button.click(
|
105 |
-
handle_generation,
|
106 |
inputs=[api_key, prompt, aspect_ratio, loop],
|
107 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
)
|
109 |
|
110 |
-
|
111 |
-
if __name__ == "__main__":
|
112 |
-
demo.launch()
|
|
|
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()
|
|
|
|