seawolf2357 commited on
Commit
6a9e2b5
·
verified ·
1 Parent(s): 4eada0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -1
app.py CHANGED
@@ -17,7 +17,161 @@ async def get_camera_motions():
17
  print(f"Error fetching camera motions: {str(e)}")
18
  return []
19
 
20
- # 나머지 함수들 (generate_video, text_to_video, image_to_video)은 변경하지 않습니다...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  with gr.Blocks() as demo:
23
  gr.Markdown("# Luma AI Text-to-Video Demo")
 
17
  print(f"Error fetching camera motions: {str(e)}")
18
  return []
19
 
20
+ 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()):
21
+ generation_params = {
22
+ "prompt": prompt,
23
+ "loop": loop,
24
+ "aspect_ratio": aspect_ratio
25
+ }
26
+
27
+ if camera_motion:
28
+ generation_params["prompt"] += f" {camera_motion}"
29
+
30
+ if extend_id:
31
+ generation_params["keyframes"] = {
32
+ "frame0": {
33
+ "type": "generation",
34
+ "id": extend_id
35
+ }
36
+ }
37
+ elif reverse_extend_id:
38
+ generation_params["keyframes"] = {
39
+ "frame1": {
40
+ "type": "generation",
41
+ "id": reverse_extend_id
42
+ }
43
+ }
44
+ elif interpolate_ids:
45
+ generation_params["keyframes"] = {
46
+ "frame0": {
47
+ "type": "generation",
48
+ "id": interpolate_ids[0]
49
+ },
50
+ "frame1": {
51
+ "type": "generation",
52
+ "id": interpolate_ids[1]
53
+ }
54
+ }
55
+
56
+ progress(0, desc="Initiating video generation...")
57
+ generation = await client.generations.create(**generation_params)
58
+
59
+ progress(0.1, desc="Video generation started. Waiting for completion...")
60
+
61
+ # Poll for completion
62
+ start_time = asyncio.get_event_loop().time()
63
+ while True:
64
+ status = await client.generations.get(id=generation.id)
65
+ if status.state == "completed":
66
+ break
67
+ elif status.state == "failed":
68
+ raise Exception("Video generation failed")
69
+
70
+ # Update progress based on time elapsed (assuming 60 seconds total)
71
+ elapsed_time = asyncio.get_event_loop().time() - start_time
72
+ progress_value = min(0.1 + (elapsed_time / 60) * 0.8, 0.9)
73
+ progress(progress_value, desc="Generating video...")
74
+
75
+ await asyncio.sleep(5)
76
+
77
+ progress(0.9, desc="Downloading generated video...")
78
+
79
+ # Download the video
80
+ video_url = status.assets.video
81
+ async with aiohttp.ClientSession() as session:
82
+ async with session.get(video_url) as resp:
83
+ if resp.status == 200:
84
+ file_name = f"luma_ai_generated_{generation.id}.mp4"
85
+ with open(file_name, 'wb') as fd:
86
+ while True:
87
+ chunk = await resp.content.read(1024)
88
+ if not chunk:
89
+ break
90
+ fd.write(chunk)
91
+
92
+ progress(1.0, desc="Video generation complete!")
93
+ return file_name
94
+
95
+ async def text_to_video(prompt, loop, aspect_ratio, camera_motion, extend_id, reverse_extend_id, interpolate_id1, interpolate_id2, progress=gr.Progress()):
96
+ api_key = os.getenv("LMGEN_KEY")
97
+ if not api_key:
98
+ raise gr.Error("LMGEN_KEY environment variable is not set.")
99
+
100
+ client = AsyncLumaAI(auth_token=api_key)
101
+
102
+ try:
103
+ interpolate_ids = None
104
+ if interpolate_id1 and interpolate_id2:
105
+ interpolate_ids = [interpolate_id1, interpolate_id2]
106
+
107
+ video_path = await generate_video(
108
+ client, prompt, loop, aspect_ratio, camera_motion,
109
+ extend_id, reverse_extend_id, interpolate_ids, progress
110
+ )
111
+ return video_path, ""
112
+ except Exception as e:
113
+ return None, f"An error occurred: {str(e)}"
114
+
115
+ async def image_to_video(prompt, image_url, loop, aspect_ratio, camera_motion, progress=gr.Progress()):
116
+ api_key = os.getenv("LMGEN_KEY")
117
+ if not api_key:
118
+ raise gr.Error("LMGEN_KEY environment variable is not set.")
119
+
120
+ client = AsyncLumaAI(auth_token=api_key)
121
+
122
+ try:
123
+ generation_params = {
124
+ "prompt": prompt + (f" {camera_motion}" if camera_motion else ""),
125
+ "loop": loop,
126
+ "aspect_ratio": aspect_ratio,
127
+ "keyframes": {
128
+ "frame0": {
129
+ "type": "image",
130
+ "url": image_url
131
+ }
132
+ }
133
+ }
134
+
135
+ progress(0, desc="Initiating video generation from image...")
136
+ generation = await client.generations.create(**generation_params)
137
+
138
+ progress(0.1, desc="Video generation started. Waiting for completion...")
139
+
140
+ # Poll for completion
141
+ start_time = asyncio.get_event_loop().time()
142
+ while True:
143
+ status = await client.generations.get(id=generation.id)
144
+ if status.state == "completed":
145
+ break
146
+ elif status.state == "failed":
147
+ raise Exception("Video generation failed")
148
+
149
+ # Update progress based on time elapsed (assuming 60 seconds total)
150
+ elapsed_time = asyncio.get_event_loop().time() - start_time
151
+ progress_value = min(0.1 + (elapsed_time / 60) * 0.8, 0.9)
152
+ progress(progress_value, desc="Generating video...")
153
+
154
+ await asyncio.sleep(5)
155
+
156
+ progress(0.9, desc="Downloading generated video...")
157
+
158
+ # Download the video
159
+ video_url = status.assets.video
160
+ async with aiohttp.ClientSession() as session:
161
+ async with session.get(video_url) as resp:
162
+ if resp.status == 200:
163
+ file_name = f"luma_ai_generated_{generation.id}.mp4"
164
+ with open(file_name, 'wb') as fd:
165
+ while True:
166
+ chunk = await resp.content.read(1024)
167
+ if not chunk:
168
+ break
169
+ fd.write(chunk)
170
+
171
+ progress(1.0, desc="Video generation complete!")
172
+ return file_name, ""
173
+ except Exception as e:
174
+ return None, f"An error occurred: {str(e)}"
175
 
176
  with gr.Blocks() as demo:
177
  gr.Markdown("# Luma AI Text-to-Video Demo")