akhaliq HF staff commited on
Commit
eacc26a
1 Parent(s): 2cde650

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -69
app.py CHANGED
@@ -1,94 +1,116 @@
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:
12
- generation = client.generations.create(
13
- prompt=prompt,
14
- aspect_ratio=aspect_ratio,
15
- loop=loop
16
- )
17
- return generation.id, "Generation started..."
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
24
 
25
- client = create_client(api_key)
26
  try:
27
- generation = client.generations.get(id=generation_id)
28
- return generation.status, generation.assets.thumbnail
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
35
 
36
- client = create_client(api_key)
37
  try:
38
- generation = client.generations.get(id=generation_id)
39
- video_url = generation.assets.video
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  response = requests.get(video_url, stream=True)
41
 
42
- file_name = f"generated_video_{generation_id}.mp4"
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 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):
64
- status = gr.Textbox(label="Status")
65
- thumbnail = gr.Image(label="Thumbnail")
66
- video = gr.Video(label="Generated Video")
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
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
- poll_btn = gr.Button("Check Status")
88
- poll_btn.click(
89
- on_poll,
90
- inputs=[api_key, generation_id],
91
- outputs=[status, thumbnail, video]
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()