seawolf2357 commited on
Commit
88d63cf
·
verified ·
1 Parent(s): a6b51c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -26
app.py CHANGED
@@ -4,15 +4,44 @@ from lumaai import AsyncLumaAI
4
  import asyncio
5
  import aiohttp
6
 
7
- async def generate_video(api_key, prompt, loop=False, aspect_ratio="16:9", progress=gr.Progress()):
8
- client = AsyncLumaAI(auth_token=api_key)
9
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  progress(0, desc="Initiating video generation...")
11
- generation = await client.generations.create(
12
- prompt=prompt,
13
- loop=loop,
14
- aspect_ratio=aspect_ratio
15
- )
16
 
17
  progress(0.1, desc="Video generation started. Waiting for completion...")
18
 
@@ -50,35 +79,48 @@ async def generate_video(api_key, prompt, loop=False, aspect_ratio="16:9", progr
50
  progress(1.0, desc="Video generation complete!")
51
  return file_name
52
 
53
- async def text_to_video(api_key, prompt, loop, aspect_ratio, progress=gr.Progress()):
 
54
  if not api_key:
55
- raise gr.Error("Please enter your Luma AI API key.")
 
 
56
 
57
  try:
58
- video_path = await generate_video(api_key, prompt, loop, aspect_ratio, progress)
 
 
 
 
 
 
 
59
  return video_path, ""
60
  except Exception as e:
61
  return None, f"An error occurred: {str(e)}"
62
 
63
- async def image_to_video(api_key, prompt, image_url, loop, aspect_ratio, progress=gr.Progress()):
 
64
  if not api_key:
65
- raise gr.Error("Please enter your Luma AI API key.")
 
 
66
 
67
  try:
68
- client = AsyncLumaAI(auth_token=api_key)
69
-
70
- progress(0, desc="Initiating video generation from image...")
71
- generation = await client.generations.create(
72
- prompt=prompt,
73
- loop=loop,
74
- aspect_ratio=aspect_ratio,
75
- keyframes={
76
  "frame0": {
77
  "type": "image",
78
  "url": image_url
79
  }
80
  }
81
- )
 
 
 
82
 
83
  progress(0.1, desc="Video generation started. Waiting for completion...")
84
 
@@ -118,11 +160,18 @@ async def image_to_video(api_key, prompt, image_url, loop, aspect_ratio, progres
118
  except Exception as e:
119
  return None, f"An error occurred: {str(e)}"
120
 
 
 
 
 
 
 
 
 
 
121
  with gr.Blocks() as demo:
122
  gr.Markdown("# Luma AI Text-to-Video Demo")
123
 
124
- api_key = gr.Textbox(label="Luma AI API Key", type="password")
125
-
126
  with gr.Tab("Text to Video"):
127
  prompt = gr.Textbox(label="Prompt")
128
  generate_btn = gr.Button("Generate Video")
@@ -132,10 +181,16 @@ with gr.Blocks() as demo:
132
  with gr.Accordion("Advanced Options", open=False):
133
  loop = gr.Checkbox(label="Loop", value=False)
134
  aspect_ratio = gr.Dropdown(label="Aspect Ratio", choices=["16:9", "1:1", "9:16", "4:3", "3:4"], value="16:9")
 
 
 
 
 
 
135
 
136
  generate_btn.click(
137
  text_to_video,
138
- inputs=[api_key, prompt, loop, aspect_ratio],
139
  outputs=[video_output, error_output]
140
  )
141
 
@@ -149,10 +204,11 @@ with gr.Blocks() as demo:
149
  with gr.Accordion("Advanced Options", open=False):
150
  img_loop = gr.Checkbox(label="Loop", value=False)
151
  img_aspect_ratio = gr.Dropdown(label="Aspect Ratio", choices=["16:9", "1:1", "9:16", "4:3", "3:4"], value="16:9")
 
152
 
153
  img_generate_btn.click(
154
  image_to_video,
155
- inputs=[api_key, img_prompt, img_url, img_loop, img_aspect_ratio],
156
  outputs=[img_video_output, img_error_output]
157
  )
158
 
 
4
  import asyncio
5
  import aiohttp
6
 
7
+ async def generate_video(client, prompt, loop=False, aspect_ratio="16:9", camera_motion=None, extend_id=None, reverse_extend_id=None, interpolate_ids=None, progress=gr.Progress()):
8
+ generation_params = {
9
+ "prompt": prompt,
10
+ "loop": loop,
11
+ "aspect_ratio": aspect_ratio
12
+ }
13
+
14
+ if camera_motion:
15
+ generation_params["prompt"] += f" {camera_motion}"
16
+
17
+ if extend_id:
18
+ generation_params["keyframes"] = {
19
+ "frame0": {
20
+ "type": "generation",
21
+ "id": extend_id
22
+ }
23
+ }
24
+ elif reverse_extend_id:
25
+ generation_params["keyframes"] = {
26
+ "frame1": {
27
+ "type": "generation",
28
+ "id": reverse_extend_id
29
+ }
30
+ }
31
+ elif interpolate_ids:
32
+ generation_params["keyframes"] = {
33
+ "frame0": {
34
+ "type": "generation",
35
+ "id": interpolate_ids[0]
36
+ },
37
+ "frame1": {
38
+ "type": "generation",
39
+ "id": interpolate_ids[1]
40
+ }
41
+ }
42
+
43
  progress(0, desc="Initiating video generation...")
44
+ generation = await client.generations.create(**generation_params)
 
 
 
 
45
 
46
  progress(0.1, desc="Video generation started. Waiting for completion...")
47
 
 
79
  progress(1.0, desc="Video generation complete!")
80
  return file_name
81
 
82
+ async def text_to_video(prompt, loop, aspect_ratio, camera_motion, extend_id, reverse_extend_id, interpolate_id1, interpolate_id2, progress=gr.Progress()):
83
+ api_key = os.getenv("LMGEN_KEY")
84
  if not api_key:
85
+ raise gr.Error("LMGEN_KEY environment variable is not set.")
86
+
87
+ client = AsyncLumaAI(auth_token=api_key)
88
 
89
  try:
90
+ interpolate_ids = None
91
+ if interpolate_id1 and interpolate_id2:
92
+ interpolate_ids = [interpolate_id1, interpolate_id2]
93
+
94
+ video_path = await generate_video(
95
+ client, prompt, loop, aspect_ratio, camera_motion,
96
+ extend_id, reverse_extend_id, interpolate_ids, progress
97
+ )
98
  return video_path, ""
99
  except Exception as e:
100
  return None, f"An error occurred: {str(e)}"
101
 
102
+ async def image_to_video(prompt, image_url, loop, aspect_ratio, camera_motion, progress=gr.Progress()):
103
+ api_key = os.getenv("LMGEN_KEY")
104
  if not api_key:
105
+ raise gr.Error("LMGEN_KEY environment variable is not set.")
106
+
107
+ client = AsyncLumaAI(auth_token=api_key)
108
 
109
  try:
110
+ generation_params = {
111
+ "prompt": prompt + (f" {camera_motion}" if camera_motion else ""),
112
+ "loop": loop,
113
+ "aspect_ratio": aspect_ratio,
114
+ "keyframes": {
 
 
 
115
  "frame0": {
116
  "type": "image",
117
  "url": image_url
118
  }
119
  }
120
+ }
121
+
122
+ progress(0, desc="Initiating video generation from image...")
123
+ generation = await client.generations.create(**generation_params)
124
 
125
  progress(0.1, desc="Video generation started. Waiting for completion...")
126
 
 
160
  except Exception as e:
161
  return None, f"An error occurred: {str(e)}"
162
 
163
+ async def get_camera_motions():
164
+ api_key = os.getenv("LMGEN_KEY")
165
+ if not api_key:
166
+ raise gr.Error("LMGEN_KEY environment variable is not set.")
167
+
168
+ client = AsyncLumaAI(auth_token=api_key)
169
+ motions = await client.generations.camera_motion.list()
170
+ return [motion.name for motion in motions]
171
+
172
  with gr.Blocks() as demo:
173
  gr.Markdown("# Luma AI Text-to-Video Demo")
174
 
 
 
175
  with gr.Tab("Text to Video"):
176
  prompt = gr.Textbox(label="Prompt")
177
  generate_btn = gr.Button("Generate Video")
 
181
  with gr.Accordion("Advanced Options", open=False):
182
  loop = gr.Checkbox(label="Loop", value=False)
183
  aspect_ratio = gr.Dropdown(label="Aspect Ratio", choices=["16:9", "1:1", "9:16", "4:3", "3:4"], value="16:9")
184
+ camera_motion = gr.Dropdown(label="Camera Motion", choices=lambda: asyncio.run(get_camera_motions()))
185
+ extend_id = gr.Textbox(label="Extend Video ID (optional)")
186
+ reverse_extend_id = gr.Textbox(label="Reverse Extend Video ID (optional)")
187
+ with gr.Row():
188
+ interpolate_id1 = gr.Textbox(label="Interpolate Video ID 1 (optional)")
189
+ interpolate_id2 = gr.Textbox(label="Interpolate Video ID 2 (optional)")
190
 
191
  generate_btn.click(
192
  text_to_video,
193
+ inputs=[prompt, loop, aspect_ratio, camera_motion, extend_id, reverse_extend_id, interpolate_id1, interpolate_id2],
194
  outputs=[video_output, error_output]
195
  )
196
 
 
204
  with gr.Accordion("Advanced Options", open=False):
205
  img_loop = gr.Checkbox(label="Loop", value=False)
206
  img_aspect_ratio = gr.Dropdown(label="Aspect Ratio", choices=["16:9", "1:1", "9:16", "4:3", "3:4"], value="16:9")
207
+ img_camera_motion = gr.Dropdown(label="Camera Motion", choices=lambda: asyncio.run(get_camera_motions()))
208
 
209
  img_generate_btn.click(
210
  image_to_video,
211
+ inputs=[img_prompt, img_url, img_loop, img_aspect_ratio, img_camera_motion],
212
  outputs=[img_video_output, img_error_output]
213
  )
214