akhaliq HF Staff commited on
Commit
a93774a
·
verified ·
1 Parent(s): baa251a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -89
app.py CHANGED
@@ -1,15 +1,11 @@
1
  import gradio as gr
2
- from lumaai.client import LumaAI # Adjust based on your findings
3
  import requests
4
- from PIL import Image
5
- from io import BytesIO
6
- import os
7
 
8
- # Initialize LumaAI client
9
  def create_client(api_key):
10
- return LumaAI(auth_token=api_key)
11
 
12
- # Basic video generation
13
  def generate_video(api_key, prompt, aspect_ratio, loop):
14
  client = create_client(api_key)
15
  try:
@@ -22,7 +18,6 @@ def generate_video(api_key, prompt, aspect_ratio, loop):
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
@@ -30,17 +25,10 @@ def poll_generation(api_key, generation_id):
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
@@ -51,14 +39,9 @@ def download_video(api_key, 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
  file_name = f"generated_video_{generation_id}.mp4"
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:
@@ -68,53 +51,23 @@ with gr.Blocks() as demo:
68
  gr.Markdown("# Luma AI Video Generation Demo")
69
  gr.Markdown("Generate videos using Luma AI based on text prompts.")
70
 
71
- api_key = gr.Textbox(
72
- label="Enter your Luma AI API Key",
73
- type="password",
74
- placeholder="sk-..."
75
- )
76
 
77
  with gr.Row():
78
  with gr.Column(scale=2):
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
  generation_id = gr.State()
110
 
111
- # Generate Video Action
112
  def on_generate(api_key, prompt, aspect_ratio, loop):
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
 
@@ -124,30 +77,12 @@ with gr.Blocks() as demo:
124
  outputs=[generation_id, status]
125
  )
126
 
127
- # Check Status and Display Results
128
  def on_poll(api_key, gen_id):
129
- if not api_key:
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(
@@ -155,12 +90,5 @@ with gr.Blocks() as demo:
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
  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
  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
 
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
 
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:
 
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
 
 
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
  inputs=[api_key, generation_id],
91
  outputs=[status, thumbnail, video]
92
  )
 
 
 
 
 
 
 
93
 
94
+ demo.launch()