Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,21 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from lumaai import LumaAI
|
|
|
3 |
|
4 |
def generate_content(api_key, prompt, aspect_ratio, loop):
|
5 |
"""
|
6 |
-
Generates content using LumaAI's API based on user inputs.
|
|
|
7 |
|
8 |
Parameters:
|
9 |
- api_key (str): User's LumaAI API key.
|
10 |
-
- prompt (str): The prompt for
|
11 |
- aspect_ratio (str): Desired aspect ratio (e.g., "16:9").
|
12 |
- loop (bool): Whether the generation should loop.
|
13 |
|
14 |
Returns:
|
15 |
-
- str:
|
16 |
"""
|
17 |
try:
|
18 |
# Initialize the LumaAI client with the provided API key
|
@@ -24,15 +27,43 @@ def generate_content(api_key, prompt, aspect_ratio, loop):
|
|
24 |
loop=loop,
|
25 |
prompt=prompt,
|
26 |
)
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
29 |
except Exception as e:
|
30 |
return f"An error occurred: {str(e)}"
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
# Create the Gradio Blocks interface
|
33 |
with gr.Blocks() as demo:
|
34 |
with gr.Row():
|
35 |
-
gr.Markdown("# LumaAI
|
36 |
|
37 |
with gr.Row():
|
38 |
api_key = gr.Textbox(label="LumaAI API Key", placeholder="Enter your LumaAI API Key", type="password")
|
@@ -47,14 +78,23 @@ with gr.Blocks() as demo:
|
|
47 |
loop = gr.Checkbox(label="Loop", value=False)
|
48 |
|
49 |
with gr.Row():
|
50 |
-
generate_button = gr.Button("Generate")
|
51 |
|
52 |
with gr.Row():
|
53 |
-
output = gr.
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
# Link the button click to the function
|
56 |
generate_button.click(
|
57 |
-
|
58 |
inputs=[api_key, prompt, aspect_ratio, loop],
|
59 |
outputs=output
|
60 |
)
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
from lumaai import LumaAI
|
4 |
+
import requests
|
5 |
|
6 |
def generate_content(api_key, prompt, aspect_ratio, loop):
|
7 |
"""
|
8 |
+
Generates video content using LumaAI's API based on user inputs.
|
9 |
+
Polls for the video once the generation is completed.
|
10 |
|
11 |
Parameters:
|
12 |
- api_key (str): User's LumaAI API key.
|
13 |
+
- prompt (str): The prompt for video generation.
|
14 |
- aspect_ratio (str): Desired aspect ratio (e.g., "16:9").
|
15 |
- loop (bool): Whether the generation should loop.
|
16 |
|
17 |
Returns:
|
18 |
+
- str: URL to the generated video or an error message.
|
19 |
"""
|
20 |
try:
|
21 |
# Initialize the LumaAI client with the provided API key
|
|
|
27 |
loop=loop,
|
28 |
prompt=prompt,
|
29 |
)
|
30 |
+
|
31 |
+
# Polling to check if the generation is complete
|
32 |
+
generation_id = generation.id
|
33 |
+
video_url = None
|
34 |
|
35 |
+
# Poll until the video is ready
|
36 |
+
while video_url is None:
|
37 |
+
generation_status = client.generations.get(id=generation_id)
|
38 |
+
video_url = generation_status.assets.get('video', None)
|
39 |
+
|
40 |
+
return video_url
|
41 |
except Exception as e:
|
42 |
return f"An error occurred: {str(e)}"
|
43 |
|
44 |
+
|
45 |
+
def download_video(video_url):
|
46 |
+
"""
|
47 |
+
Downloads the video from the provided URL and saves it locally.
|
48 |
+
|
49 |
+
Parameters:
|
50 |
+
- video_url (str): URL of the video to download.
|
51 |
+
|
52 |
+
Returns:
|
53 |
+
- str: File path of the downloaded video.
|
54 |
+
"""
|
55 |
+
response = requests.get(video_url, stream=True)
|
56 |
+
file_name = 'generated_video.mp4'
|
57 |
+
|
58 |
+
with open(file_name, 'wb') as file:
|
59 |
+
file.write(response.content)
|
60 |
+
|
61 |
+
return file_name
|
62 |
+
|
63 |
# Create the Gradio Blocks interface
|
64 |
with gr.Blocks() as demo:
|
65 |
with gr.Row():
|
66 |
+
gr.Markdown("# LumaAI Video Generator")
|
67 |
|
68 |
with gr.Row():
|
69 |
api_key = gr.Textbox(label="LumaAI API Key", placeholder="Enter your LumaAI API Key", type="password")
|
|
|
78 |
loop = gr.Checkbox(label="Loop", value=False)
|
79 |
|
80 |
with gr.Row():
|
81 |
+
generate_button = gr.Button("Generate Video")
|
82 |
|
83 |
with gr.Row():
|
84 |
+
output = gr.Video(label="Generated Video")
|
85 |
+
|
86 |
+
# Define the button click behavior
|
87 |
+
def handle_generation(api_key, prompt, aspect_ratio, loop):
|
88 |
+
video_url = generate_content(api_key, prompt, aspect_ratio, loop)
|
89 |
+
if video_url.startswith("http"):
|
90 |
+
file_name = download_video(video_url)
|
91 |
+
return file_name
|
92 |
+
else:
|
93 |
+
return video_url
|
94 |
+
|
95 |
# Link the button click to the function
|
96 |
generate_button.click(
|
97 |
+
handle_generation,
|
98 |
inputs=[api_key, prompt, aspect_ratio, loop],
|
99 |
outputs=output
|
100 |
)
|