Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import os
|
|
3 |
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)
|
@@ -60,14 +61,27 @@ async def text_to_video(api_key, prompt, loop, aspect_ratio, progress=gr.Progres
|
|
60 |
except Exception as e:
|
61 |
return None, f"An error occurred: {str(e)}"
|
62 |
|
63 |
-
async def image_to_video(api_key, prompt,
|
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="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
generation = await client.generations.create(
|
72 |
prompt=prompt,
|
73 |
loop=loop,
|
@@ -80,7 +94,7 @@ async def image_to_video(api_key, prompt, image_url, loop, aspect_ratio, progres
|
|
80 |
}
|
81 |
)
|
82 |
|
83 |
-
progress(0.
|
84 |
|
85 |
# Poll for completion
|
86 |
start_time = asyncio.get_event_loop().time()
|
@@ -93,7 +107,7 @@ async def image_to_video(api_key, prompt, image_url, loop, aspect_ratio, progres
|
|
93 |
|
94 |
# Update progress based on time elapsed (assuming 60 seconds total)
|
95 |
elapsed_time = asyncio.get_event_loop().time() - start_time
|
96 |
-
progress_value = min(0.
|
97 |
progress(progress_value, desc="Generating video...")
|
98 |
|
99 |
await asyncio.sleep(5)
|
@@ -113,11 +127,20 @@ async def image_to_video(api_key, prompt, image_url, loop, aspect_ratio, progres
|
|
113 |
break
|
114 |
fd.write(chunk)
|
115 |
|
|
|
|
|
|
|
116 |
progress(1.0, desc="Video generation complete!")
|
117 |
return file_name, ""
|
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 |
|
@@ -141,7 +164,7 @@ with gr.Blocks() as demo:
|
|
141 |
|
142 |
with gr.Tab("Image to Video"):
|
143 |
img_prompt = gr.Textbox(label="Prompt")
|
144 |
-
|
145 |
img_generate_btn = gr.Button("Generate Video from Image")
|
146 |
img_video_output = gr.Video(label="Generated Video")
|
147 |
img_error_output = gr.Textbox(label="Error Messages", visible=True)
|
@@ -152,7 +175,7 @@ with gr.Blocks() as demo:
|
|
152 |
|
153 |
img_generate_btn.click(
|
154 |
image_to_video,
|
155 |
-
inputs=[api_key, img_prompt,
|
156 |
outputs=[img_video_output, img_error_output]
|
157 |
)
|
158 |
|
|
|
3 |
from lumaai import AsyncLumaAI
|
4 |
import asyncio
|
5 |
import aiohttp
|
6 |
+
import tempfile
|
7 |
|
8 |
async def generate_video(api_key, prompt, loop=False, aspect_ratio="16:9", progress=gr.Progress()):
|
9 |
client = AsyncLumaAI(auth_token=api_key)
|
|
|
61 |
except Exception as e:
|
62 |
return None, f"An error occurred: {str(e)}"
|
63 |
|
64 |
+
async def image_to_video(api_key, prompt, image, loop, aspect_ratio, progress=gr.Progress()):
|
65 |
if not api_key:
|
66 |
raise gr.Error("Please enter your Luma AI API key.")
|
67 |
|
68 |
+
if image is None:
|
69 |
+
raise gr.Error("Please upload an image.")
|
70 |
+
|
71 |
try:
|
72 |
client = AsyncLumaAI(auth_token=api_key)
|
73 |
|
74 |
+
progress(0, desc="Uploading image...")
|
75 |
+
|
76 |
+
# Create a temporary file to store the uploaded image
|
77 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
78 |
+
temp_file.write(image)
|
79 |
+
temp_file_path = temp_file.name
|
80 |
+
|
81 |
+
# Upload the image to Luma AI (you might need to implement this function)
|
82 |
+
image_url = await upload_image_to_luma(client, temp_file_path)
|
83 |
+
|
84 |
+
progress(0.1, desc="Initiating video generation from image...")
|
85 |
generation = await client.generations.create(
|
86 |
prompt=prompt,
|
87 |
loop=loop,
|
|
|
94 |
}
|
95 |
)
|
96 |
|
97 |
+
progress(0.2, desc="Video generation started. Waiting for completion...")
|
98 |
|
99 |
# Poll for completion
|
100 |
start_time = asyncio.get_event_loop().time()
|
|
|
107 |
|
108 |
# Update progress based on time elapsed (assuming 60 seconds total)
|
109 |
elapsed_time = asyncio.get_event_loop().time() - start_time
|
110 |
+
progress_value = min(0.2 + (elapsed_time / 60) * 0.7, 0.9)
|
111 |
progress(progress_value, desc="Generating video...")
|
112 |
|
113 |
await asyncio.sleep(5)
|
|
|
127 |
break
|
128 |
fd.write(chunk)
|
129 |
|
130 |
+
# Clean up the temporary file
|
131 |
+
os.unlink(temp_file_path)
|
132 |
+
|
133 |
progress(1.0, desc="Video generation complete!")
|
134 |
return file_name, ""
|
135 |
except Exception as e:
|
136 |
return None, f"An error occurred: {str(e)}"
|
137 |
|
138 |
+
# You need to implement this function based on Luma AI's API for image uploading
|
139 |
+
async def upload_image_to_luma(client, image_path):
|
140 |
+
# This is a placeholder. You need to implement the actual image upload logic
|
141 |
+
# using the Luma AI API. The function should return the URL of the uploaded image.
|
142 |
+
raise NotImplementedError("Image upload to Luma AI is not implemented yet.")
|
143 |
+
|
144 |
with gr.Blocks() as demo:
|
145 |
gr.Markdown("# Luma AI Text-to-Video Demo")
|
146 |
|
|
|
164 |
|
165 |
with gr.Tab("Image to Video"):
|
166 |
img_prompt = gr.Textbox(label="Prompt")
|
167 |
+
img_input = gr.Image(label="Upload Image", type="numpy")
|
168 |
img_generate_btn = gr.Button("Generate Video from Image")
|
169 |
img_video_output = gr.Video(label="Generated Video")
|
170 |
img_error_output = gr.Textbox(label="Error Messages", visible=True)
|
|
|
175 |
|
176 |
img_generate_btn.click(
|
177 |
image_to_video,
|
178 |
+
inputs=[api_key, img_prompt, img_input, img_loop, img_aspect_ratio],
|
179 |
outputs=[img_video_output, img_error_output]
|
180 |
)
|
181 |
|