akhaliq HF Staff commited on
Commit
6123d43
·
verified ·
1 Parent(s): eacc26a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -26
app.py CHANGED
@@ -1,52 +1,68 @@
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,
@@ -58,21 +74,34 @@ def image_to_video(api_key, prompt, image_url, loop, aspect_ratio):
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)}"
@@ -113,4 +142,4 @@ with gr.Blocks() as demo:
113
  outputs=img_video_output
114
  )
115
 
116
- demo.launch()
 
1
  import gradio as gr
2
  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)
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.2, desc="Video generation started. Waiting for completion...")
18
+
19
  # Poll for completion
20
  while True:
21
+ status = await client.generations.get(id=generation.id)
22
  if status.status == "completed":
23
  break
24
+ elif status.status == "failed":
25
+ raise Exception("Video generation failed")
26
+ await asyncio.sleep(5)
27
+ progress(0.5, desc="Still processing...")
28
+
29
+ progress(0.8, desc="Downloading generated video...")
30
 
31
  # Download the video
32
  video_url = status.assets.video
33
+ async with aiohttp.ClientSession() as session:
34
+ async with session.get(video_url) as resp:
35
+ if resp.status == 200:
36
+ file_name = f"luma_ai_generated_{generation.id}.mp4"
37
+ with open(file_name, 'wb') as fd:
38
+ while True:
39
+ chunk = await resp.content.read(1024)
40
+ if not chunk:
41
+ break
42
+ fd.write(chunk)
43
 
44
+ progress(1.0, desc="Video generation complete!")
45
  return file_name
46
 
47
+ async def text_to_video(api_key, prompt, loop, aspect_ratio, progress=gr.Progress()):
48
  if not api_key:
49
  return "Please enter your Luma AI API key."
50
 
51
  try:
52
+ video_path = await generate_video(api_key, prompt, loop, aspect_ratio, progress)
53
  return video_path
54
  except Exception as e:
55
  return f"An error occurred: {str(e)}"
56
 
57
+ async def image_to_video(api_key, prompt, image_url, loop, aspect_ratio, progress=gr.Progress()):
58
  if not api_key:
59
  return "Please enter your Luma AI API key."
60
 
61
  try:
62
+ client = AsyncLumaAI(auth_token=api_key)
63
+
64
+ progress(0, desc="Initiating video generation from image...")
65
+ generation = await client.generations.create(
66
  prompt=prompt,
67
  loop=loop,
68
  aspect_ratio=aspect_ratio,
 
74
  }
75
  )
76
 
77
+ progress(0.2, desc="Video generation started. Waiting for completion...")
78
+
79
  # Poll for completion
80
  while True:
81
+ status = await client.generations.get(id=generation.id)
82
  if status.status == "completed":
83
  break
84
+ elif status.status == "failed":
85
+ raise Exception("Video generation failed")
86
+ await asyncio.sleep(5)
87
+ progress(0.5, desc="Still processing...")
88
+
89
+ progress(0.8, desc="Downloading generated video...")
90
 
91
  # Download the video
92
  video_url = status.assets.video
93
+ async with aiohttp.ClientSession() as session:
94
+ async with session.get(video_url) as resp:
95
+ if resp.status == 200:
96
+ file_name = f"luma_ai_generated_{generation.id}.mp4"
97
+ with open(file_name, 'wb') as fd:
98
+ while True:
99
+ chunk = await resp.content.read(1024)
100
+ if not chunk:
101
+ break
102
+ fd.write(chunk)
103
 
104
+ progress(1.0, desc="Video generation complete!")
105
  return file_name
106
  except Exception as e:
107
  return f"An error occurred: {str(e)}"
 
142
  outputs=img_video_output
143
  )
144
 
145
+ demo.queue().launch()