Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,94 +1,116 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
import
|
4 |
import time
|
|
|
5 |
|
6 |
-
def
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
def
|
22 |
-
if not
|
23 |
-
return "
|
24 |
|
25 |
-
client = create_client(api_key)
|
26 |
try:
|
27 |
-
|
28 |
-
return
|
29 |
except Exception as e:
|
30 |
-
return f"
|
31 |
|
32 |
-
def
|
33 |
-
if not
|
34 |
-
return
|
35 |
|
36 |
-
client = create_client(api_key)
|
37 |
try:
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
response = requests.get(video_url, stream=True)
|
41 |
|
42 |
-
file_name = f"
|
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
|
49 |
|
50 |
with gr.Blocks() as demo:
|
51 |
-
gr.Markdown("# Luma AI Video
|
52 |
-
gr.Markdown("Generate videos using Luma AI based on text prompts.")
|
53 |
|
54 |
-
api_key = gr.Textbox(label="
|
55 |
|
56 |
-
with gr.
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
loop = gr.Checkbox(label="Loop")
|
61 |
-
generate_btn = gr.Button("Generate Video")
|
62 |
|
63 |
-
with gr.
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
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 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
from lumaai import LumaAI
|
4 |
import time
|
5 |
+
import requests
|
6 |
|
7 |
+
def generate_video(api_key, prompt, loop=False, aspect_ratio="16:9"):
|
8 |
+
client = LumaAI(auth_token=api_key)
|
9 |
+
|
10 |
+
generation = client.generations.create(
|
11 |
+
prompt=prompt,
|
12 |
+
loop=loop,
|
13 |
+
aspect_ratio=aspect_ratio
|
14 |
+
)
|
15 |
+
|
16 |
+
# Poll for completion
|
17 |
+
while True:
|
18 |
+
status = client.generations.get(id=generation.id)
|
19 |
+
if status.status == "completed":
|
20 |
+
break
|
21 |
+
time.sleep(5)
|
22 |
+
|
23 |
+
# Download the video
|
24 |
+
video_url = status.assets.video
|
25 |
+
response = requests.get(video_url, stream=True)
|
26 |
+
|
27 |
+
file_name = f"luma_ai_generated_{generation.id}.mp4"
|
28 |
+
with open(file_name, 'wb') as file:
|
29 |
+
file.write(response.content)
|
30 |
+
|
31 |
+
return file_name
|
32 |
|
33 |
+
def text_to_video(api_key, prompt, loop, aspect_ratio):
|
34 |
+
if not api_key:
|
35 |
+
return "Please enter your Luma AI API key."
|
36 |
|
|
|
37 |
try:
|
38 |
+
video_path = generate_video(api_key, prompt, loop, aspect_ratio)
|
39 |
+
return video_path
|
40 |
except Exception as e:
|
41 |
+
return f"An error occurred: {str(e)}"
|
42 |
|
43 |
+
def image_to_video(api_key, prompt, image_url, loop, aspect_ratio):
|
44 |
+
if not api_key:
|
45 |
+
return "Please enter your Luma AI API key."
|
46 |
|
|
|
47 |
try:
|
48 |
+
client = LumaAI(auth_token=api_key)
|
49 |
+
generation = client.generations.create(
|
50 |
+
prompt=prompt,
|
51 |
+
loop=loop,
|
52 |
+
aspect_ratio=aspect_ratio,
|
53 |
+
keyframes={
|
54 |
+
"frame0": {
|
55 |
+
"type": "image",
|
56 |
+
"url": image_url
|
57 |
+
}
|
58 |
+
}
|
59 |
+
)
|
60 |
+
|
61 |
+
# Poll for completion
|
62 |
+
while True:
|
63 |
+
status = client.generations.get(id=generation.id)
|
64 |
+
if status.status == "completed":
|
65 |
+
break
|
66 |
+
time.sleep(5)
|
67 |
+
|
68 |
+
# Download the video
|
69 |
+
video_url = status.assets.video
|
70 |
response = requests.get(video_url, stream=True)
|
71 |
|
72 |
+
file_name = f"luma_ai_generated_{generation.id}.mp4"
|
73 |
with open(file_name, 'wb') as file:
|
74 |
file.write(response.content)
|
75 |
|
76 |
return file_name
|
77 |
except Exception as e:
|
78 |
+
return f"An error occurred: {str(e)}"
|
79 |
|
80 |
with gr.Blocks() as demo:
|
81 |
+
gr.Markdown("# Luma AI Text-to-Video Demo")
|
|
|
82 |
|
83 |
+
api_key = gr.Textbox(label="Luma AI API Key", type="password")
|
84 |
|
85 |
+
with gr.Tab("Text to Video"):
|
86 |
+
prompt = gr.Textbox(label="Prompt")
|
87 |
+
generate_btn = gr.Button("Generate Video")
|
88 |
+
video_output = gr.Video(label="Generated Video")
|
|
|
|
|
89 |
|
90 |
+
with gr.Accordion("Advanced Options", open=False):
|
91 |
+
loop = gr.Checkbox(label="Loop", value=False)
|
92 |
+
aspect_ratio = gr.Dropdown(label="Aspect Ratio", choices=["16:9", "1:1", "9:16", "4:3", "3:4"], value="16:9")
|
93 |
+
|
94 |
+
generate_btn.click(
|
95 |
+
text_to_video,
|
96 |
+
inputs=[api_key, prompt, loop, aspect_ratio],
|
97 |
+
outputs=video_output
|
98 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
+
with gr.Tab("Image to Video"):
|
101 |
+
img_prompt = gr.Textbox(label="Prompt")
|
102 |
+
img_url = gr.Textbox(label="Image URL")
|
103 |
+
img_generate_btn = gr.Button("Generate Video from Image")
|
104 |
+
img_video_output = gr.Video(label="Generated Video")
|
105 |
+
|
106 |
+
with gr.Accordion("Advanced Options", open=False):
|
107 |
+
img_loop = gr.Checkbox(label="Loop", value=False)
|
108 |
+
img_aspect_ratio = gr.Dropdown(label="Aspect Ratio", choices=["16:9", "1:1", "9:16", "4:3", "3:4"], value="16:9")
|
109 |
+
|
110 |
+
img_generate_btn.click(
|
111 |
+
image_to_video,
|
112 |
+
inputs=[api_key, img_prompt, img_url, img_loop, img_aspect_ratio],
|
113 |
+
outputs=img_video_output
|
114 |
+
)
|
115 |
|
116 |
demo.launch()
|