akhaliq HF Staff commited on
Commit
b5810d8
·
verified ·
1 Parent(s): 3967a70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -12
app.py CHANGED
@@ -1,11 +1,14 @@
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:
@@ -18,6 +21,7 @@ def generate_video(api_key, prompt, aspect_ratio, loop):
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
@@ -29,6 +33,7 @@ def poll_generation(api_key, generation_id):
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
@@ -47,17 +52,41 @@ def download_video(api_key, generation_id):
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):
@@ -67,6 +96,7 @@ with gr.Blocks() as demo:
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
@@ -77,12 +107,16 @@ with gr.Blocks() as demo:
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(
@@ -90,5 +124,64 @@ with gr.Blocks() as demo:
90
  inputs=[api_key, generation_id],
91
  outputs=[status, thumbnail, video]
92
  )
93
-
94
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from lumaai import LumaAI
3
  import requests
4
+ import os
5
  import time
6
 
7
+ # Initialize LumaAI client
8
  def create_client(api_key):
9
+ return LumaAI(auth_token=api_key)
10
 
11
+ # Basic video generation
12
  def generate_video(api_key, prompt, aspect_ratio, loop):
13
  client = create_client(api_key)
14
  try:
 
21
  except Exception as e:
22
  return None, f"Error: {str(e)}"
23
 
24
+ # Polling generation status
25
  def poll_generation(api_key, generation_id):
26
  if not generation_id:
27
  return "No generation in progress", None
 
33
  except Exception as e:
34
  return f"Error: {str(e)}", None
35
 
36
+ # Download the generated video
37
  def download_video(api_key, generation_id):
38
  if not generation_id:
39
  return None
 
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(label="Enter your Luma AI API Key", type="password", placeholder="sk-...")
84
 
85
  with gr.Row():
86
  with gr.Column(scale=2):
87
+ prompt = gr.Textbox(label="Prompt", placeholder="Describe your video...")
88
+ aspect_ratio = gr.Dropdown(["16:9", "9:16", "1:1", "4:3", "3:4"], label="Aspect Ratio", value="16:9")
89
+ loop = gr.Checkbox(label="Loop", value=False)
90
  generate_btn = gr.Button("Generate Video")
91
 
92
  with gr.Column(scale=3):
 
96
 
97
  generation_id = gr.State()
98
 
99
+ # Basic generate button action
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
 
107
  outputs=[generation_id, status]
108
  )
109
 
110
+ # Poll status and download video
111
  def on_poll(api_key, gen_id):
112
+ status_text, thumb = poll_generation(api_key, gen_id)
113
+ if status_text.lower() == "completed":
114
  video_path = download_video(api_key, gen_id)
115
+ if video_path:
116
+ return status_text, thumb, video_path
117
+ else:
118
+ return status_text, thumb, None
119
+ return status_text, thumb, None
120
 
121
  poll_btn = gr.Button("Check Status")
122
  poll_btn.click(
 
124
  inputs=[api_key, generation_id],
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
+ default=None,
140
+ optional=True
141
+ )
142
+ advanced_generate_btn = gr.Button("Generate with Advanced Options")
143
+
144
+ with gr.Column(scale=2):
145
+ advanced_status = gr.Textbox(label="Advanced Status")
146
+ advanced_thumbnail = gr.Image(label="Advanced Thumbnail")
147
+ advanced_video = gr.Video(label="Advanced Generated Video")
148
+
149
+ advanced_gen_id = gr.State()
150
+
151
+ # Advanced generate button action
152
+ def on_generate_advanced(api_key, prompt, aspect_ratio, loop, keyframes, camera_motion):
153
+ gen_id, message = generate_video_advanced(api_key, prompt, aspect_ratio, loop, keyframes, camera_motion)
154
+ return gen_id, message
155
+
156
+ advanced_generate_btn.click(
157
+ on_generate_advanced,
158
+ inputs=[api_key, prompt, aspect_ratio, loop, keyframes, camera_motion],
159
+ outputs=[advanced_gen_id, advanced_status]
160
+ )
161
+
162
+ # Poll status and download video for advanced generation
163
+ def on_poll_advanced(api_key, gen_id):
164
+ status_text, thumb = poll_generation(api_key, gen_id)
165
+ if status_text.lower() == "completed":
166
+ video_path = download_video(api_key, gen_id)
167
+ if video_path:
168
+ return status_text, thumb, video_path
169
+ else:
170
+ return status_text, thumb, None
171
+ return status_text, thumb, None
172
+
173
+ advanced_poll_btn = gr.Button("Check Advanced Status")
174
+ advanced_poll_btn.click(
175
+ on_poll_advanced,
176
+ inputs=[api_key, advanced_gen_id],
177
+ outputs=[advanced_status, advanced_thumbnail, advanced_video]
178
+ )
179
+
180
+ gr.Markdown("""
181
+ ---
182
+ ### Documentation & Resources
183
+ - [Luma AI Python SDK](https://github.com/lumalabs/lumaai-python)
184
+ - [Get API Key](https://lumalabs.ai/dream-machine/api/keys)
185
+ """)
186
+
187
+ demo.launch()