Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,166 +1,64 @@
|
|
1 |
import gradio as gr
|
2 |
-
import LumaAI
|
3 |
-
import requests
|
4 |
-
from PIL import Image
|
5 |
-
from io import BytesIO
|
6 |
-
import os
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
try:
|
|
|
|
|
|
|
|
|
16 |
generation = client.generations.create(
|
17 |
-
prompt=prompt,
|
18 |
aspect_ratio=aspect_ratio,
|
19 |
-
loop=loop
|
|
|
20 |
)
|
21 |
-
return generation.id, "Generation started..."
|
22 |
-
except Exception as e:
|
23 |
-
return None, f"Error: {str(e)}"
|
24 |
-
|
25 |
-
# Polling generation status
|
26 |
-
def poll_generation(api_key, generation_id):
|
27 |
-
if not generation_id:
|
28 |
-
return "No generation in progress", None
|
29 |
-
|
30 |
-
client = create_client(api_key)
|
31 |
-
try:
|
32 |
-
generation = client.generations.get(id=generation_id)
|
33 |
-
status = generation.status
|
34 |
-
thumbnail_url = generation.assets.thumbnail
|
35 |
-
|
36 |
-
# Fetch the thumbnail image
|
37 |
-
thumbnail_response = requests.get(thumbnail_url)
|
38 |
-
thumbnail = thumbnail_response.content if thumbnail_response.status_code == 200 else None
|
39 |
-
return status, thumbnail
|
40 |
-
except Exception as e:
|
41 |
-
return f"Error: {str(e)}", None
|
42 |
-
|
43 |
-
# Download the generated video
|
44 |
-
def download_video(api_key, generation_id):
|
45 |
-
if not generation_id:
|
46 |
-
return None
|
47 |
-
|
48 |
-
client = create_client(api_key)
|
49 |
-
try:
|
50 |
-
generation = client.generations.get(id=generation_id)
|
51 |
-
video_url = generation.assets.video
|
52 |
-
response = requests.get(video_url, stream=True)
|
53 |
-
|
54 |
-
if response.status_code != 200:
|
55 |
-
return None
|
56 |
|
57 |
-
|
58 |
-
with open(file_name, 'wb') as file:
|
59 |
-
for chunk in response.iter_content(chunk_size=8192):
|
60 |
-
if chunk:
|
61 |
-
file.write(chunk)
|
62 |
-
|
63 |
-
return file_name
|
64 |
except Exception as e:
|
65 |
-
return
|
66 |
|
|
|
67 |
with gr.Blocks() as demo:
|
68 |
-
gr.
|
69 |
-
|
|
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
label="
|
73 |
-
type="password",
|
74 |
-
placeholder="sk-..."
|
75 |
-
)
|
76 |
|
77 |
with gr.Row():
|
78 |
-
|
79 |
-
prompt = gr.Textbox(
|
80 |
-
label="Prompt",
|
81 |
-
placeholder="Describe your video...",
|
82 |
-
lines=2
|
83 |
-
)
|
84 |
-
aspect_ratio = gr.Dropdown(
|
85 |
-
choices=["16:9", "9:16", "1:1", "4:3", "3:4"],
|
86 |
-
label="Aspect Ratio",
|
87 |
-
value="16:9"
|
88 |
-
)
|
89 |
-
loop = gr.Checkbox(
|
90 |
-
label="Loop",
|
91 |
-
value=False
|
92 |
-
)
|
93 |
-
generate_btn = gr.Button("Generate Video")
|
94 |
-
|
95 |
-
with gr.Column(scale=3):
|
96 |
-
status = gr.Textbox(
|
97 |
-
label="Status",
|
98 |
-
interactive=False # Replaces readonly=True
|
99 |
-
)
|
100 |
-
thumbnail = gr.Image(
|
101 |
-
label="Thumbnail",
|
102 |
-
interactive=False
|
103 |
-
)
|
104 |
-
video = gr.Video(
|
105 |
-
label="Generated Video",
|
106 |
-
interactive=False
|
107 |
-
)
|
108 |
|
109 |
-
|
|
|
|
|
|
|
|
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
if not api_key:
|
114 |
-
return None, "Please provide a valid API key."
|
115 |
-
if not prompt:
|
116 |
-
return None, "Prompt cannot be empty."
|
117 |
-
|
118 |
-
gen_id, message = generate_video(api_key, prompt, aspect_ratio, loop)
|
119 |
-
return gen_id, message
|
120 |
|
121 |
-
|
122 |
-
|
|
|
123 |
inputs=[api_key, prompt, aspect_ratio, loop],
|
124 |
-
outputs=
|
125 |
)
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
return "Please provide a valid API key.", None, None
|
131 |
-
if not gen_id:
|
132 |
-
return "No generation in progress.", None, None
|
133 |
-
|
134 |
-
status_text, thumb_data = poll_generation(api_key, gen_id)
|
135 |
-
if status_text.lower() == "completed":
|
136 |
-
video_path = download_video(api_key, gen_id)
|
137 |
-
if video_path:
|
138 |
-
video_output = video_path
|
139 |
-
else:
|
140 |
-
video_output = None
|
141 |
-
else:
|
142 |
-
video_output = None
|
143 |
-
|
144 |
-
# Convert thumbnail bytes to image format
|
145 |
-
if thumb_data:
|
146 |
-
thumb_image = Image.open(BytesIO(thumb_data))
|
147 |
-
else:
|
148 |
-
thumb_image = None
|
149 |
-
|
150 |
-
return status_text, thumb_image, video_output
|
151 |
-
|
152 |
-
poll_btn = gr.Button("Check Status")
|
153 |
-
poll_btn.click(
|
154 |
-
on_poll,
|
155 |
-
inputs=[api_key, generation_id],
|
156 |
-
outputs=[status, thumbnail, video]
|
157 |
-
)
|
158 |
-
|
159 |
-
gr.Markdown("""
|
160 |
-
---
|
161 |
-
### Documentation & Resources
|
162 |
-
- [Luma AI Python SDK](https://github.com/lumalabs/lumaai-python)
|
163 |
-
- [Get API Key](https://lumalabs.ai/dream-machine/api/keys)
|
164 |
-
""")
|
165 |
-
|
166 |
-
demo.launch()
|
|
|
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 content generation.
|
11 |
+
- aspect_ratio (str): Desired aspect ratio (e.g., "16:9").
|
12 |
+
- loop (bool): Whether the generation should loop.
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
- str: The ID of the generated content or an error message.
|
16 |
+
"""
|
17 |
try:
|
18 |
+
# Initialize the LumaAI client with the provided API key
|
19 |
+
client = LumaAI(auth_token=api_key)
|
20 |
+
|
21 |
+
# Create a generation request
|
22 |
generation = client.generations.create(
|
|
|
23 |
aspect_ratio=aspect_ratio,
|
24 |
+
loop=loop,
|
25 |
+
prompt=prompt,
|
26 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
return f"Generation ID: {generation.id}"
|
|
|
|
|
|
|
|
|
|
|
|
|
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 Content Generator")
|
36 |
+
|
37 |
+
with gr.Row():
|
38 |
+
api_key = gr.Textbox(label="LumaAI API Key", placeholder="Enter your LumaAI API Key", type="password")
|
39 |
|
40 |
+
with gr.Row():
|
41 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Describe what you want to generate...", lines=2)
|
|
|
|
|
|
|
42 |
|
43 |
with gr.Row():
|
44 |
+
aspect_ratio = gr.Dropdown(label="Aspect Ratio", choices=["16:9", "4:3", "1:1", "21:9"], value="16:9")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
with gr.Row():
|
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.Textbox(label="Generation Result")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
# Link the button click to the function
|
56 |
+
generate_button.click(
|
57 |
+
generate_content,
|
58 |
inputs=[api_key, prompt, aspect_ratio, loop],
|
59 |
+
outputs=output
|
60 |
)
|
61 |
+
|
62 |
+
# Launch the demo
|
63 |
+
if __name__ == "__main__":
|
64 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|