seawolf2357 commited on
Commit
297ade1
Β·
verified Β·
1 Parent(s): f9f982e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -18
app.py CHANGED
@@ -3,6 +3,10 @@ import os
3
  from lumaai import AsyncLumaAI
4
  import asyncio
5
  import aiohttp
 
 
 
 
6
 
7
  async def get_camera_motions():
8
  api_key = os.getenv("LMGEN_KEY")
@@ -53,10 +57,10 @@ async def generate_video(client, prompt, loop=False, aspect_ratio="16:9", camera
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()
@@ -65,16 +69,16 @@ async def generate_video(client, prompt, loop=False, aspect_ratio="16:9", camera
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
@@ -89,39 +93,57 @@ async def generate_video(client, prompt, loop=False, aspect_ratio="16:9", camera
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": {
@@ -132,10 +154,10 @@ async def image_to_video(prompt, image_url, loop, aspect_ratio, camera_motion, p
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()
@@ -144,16 +166,16 @@ async def image_to_video(prompt, image_url, loop, aspect_ratio, camera_motion, p
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
@@ -168,10 +190,10 @@ async def image_to_video(prompt, image_url, loop, aspect_ratio, camera_motion, p
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 ν…μŠ€νŠΈ-λΉ„λ””μ˜€ 생성 데λͺ¨")
 
3
  from lumaai import AsyncLumaAI
4
  import asyncio
5
  import aiohttp
6
+ from transformers import pipeline
7
+
8
+ # λ²ˆμ—­ νŒŒμ΄ν”„λΌμΈ μ΄ˆκΈ°ν™”
9
+ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
10
 
11
  async def get_camera_motions():
12
  api_key = os.getenv("LMGEN_KEY")
 
57
  }
58
  }
59
 
60
+ progress(0, desc="λΉ„λ””μ˜€ 생성 μ‹œμž‘ 쀑...")
61
  generation = await client.generations.create(**generation_params)
62
 
63
+ progress(0.1, desc="λΉ„λ””μ˜€ 생성 μ‹œμž‘λ¨. μ™„λ£Œ λŒ€κΈ° 쀑...")
64
 
65
  # Poll for completion
66
  start_time = asyncio.get_event_loop().time()
 
69
  if status.state == "completed":
70
  break
71
  elif status.state == "failed":
72
+ raise Exception("λΉ„λ””μ˜€ 생성 μ‹€νŒ¨")
73
 
74
  # Update progress based on time elapsed (assuming 60 seconds total)
75
  elapsed_time = asyncio.get_event_loop().time() - start_time
76
  progress_value = min(0.1 + (elapsed_time / 60) * 0.8, 0.9)
77
+ progress(progress_value, desc="λΉ„λ””μ˜€ 생성 쀑...")
78
 
79
  await asyncio.sleep(5)
80
 
81
+ progress(0.9, desc="μƒμ„±λœ λΉ„λ””μ˜€ λ‹€μš΄λ‘œλ“œ 쀑...")
82
 
83
  # Download the video
84
  video_url = status.assets.video
 
93
  break
94
  fd.write(chunk)
95
 
96
+ progress(1.0, desc="λΉ„λ””μ˜€ 생성 μ™„λ£Œ!")
97
  return file_name
98
 
99
+ async def translate_prompt(prompt):
100
+ try:
101
+ translated = translator(prompt, max_length=512)[0]['translation_text']
102
+ return translated
103
+ except Exception as e:
104
+ print(f"λ²ˆμ—­ 쀑 였λ₯˜ λ°œμƒ: {str(e)}")
105
+ return prompt # λ²ˆμ—­ μ‹€νŒ¨ μ‹œ 원본 ν”„λ‘¬ν”„νŠΈ λ°˜ν™˜
106
+
107
  async def text_to_video(prompt, loop, aspect_ratio, camera_motion, extend_id, reverse_extend_id, interpolate_id1, interpolate_id2, progress=gr.Progress()):
108
  api_key = os.getenv("LMGEN_KEY")
109
  if not api_key:
110
+ raise gr.Error("LMGEN_KEY ν™˜κ²½ λ³€μˆ˜κ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.")
111
 
112
  client = AsyncLumaAI(auth_token=api_key)
113
 
114
  try:
115
+ # ν”„λ‘¬ν”„νŠΈ λ²ˆμ—­
116
+ translated_prompt = await translate_prompt(prompt)
117
+ print(f"원본 ν”„λ‘¬ν”„νŠΈ: {prompt}")
118
+ print(f"λ²ˆμ—­λœ ν”„λ‘¬ν”„νŠΈ: {translated_prompt}")
119
+
120
  interpolate_ids = None
121
  if interpolate_id1 and interpolate_id2:
122
  interpolate_ids = [interpolate_id1, interpolate_id2]
123
 
124
  video_path = await generate_video(
125
+ client, translated_prompt, loop, aspect_ratio, camera_motion,
126
  extend_id, reverse_extend_id, interpolate_ids, progress
127
  )
128
  return video_path, ""
129
  except Exception as e:
130
+ return None, f"였λ₯˜ λ°œμƒ: {str(e)}"
131
 
132
  async def image_to_video(prompt, image_url, loop, aspect_ratio, camera_motion, progress=gr.Progress()):
133
  api_key = os.getenv("LMGEN_KEY")
134
  if not api_key:
135
+ raise gr.Error("LMGEN_KEY ν™˜κ²½ λ³€μˆ˜κ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.")
136
 
137
  client = AsyncLumaAI(auth_token=api_key)
138
 
139
  try:
140
+ # ν”„λ‘¬ν”„νŠΈ λ²ˆμ—­
141
+ translated_prompt = await translate_prompt(prompt)
142
+ print(f"원본 ν”„λ‘¬ν”„νŠΈ: {prompt}")
143
+ print(f"λ²ˆμ—­λœ ν”„λ‘¬ν”„νŠΈ: {translated_prompt}")
144
+
145
  generation_params = {
146
+ "prompt": translated_prompt + (f" {camera_motion}" if camera_motion else ""),
147
  "loop": loop,
148
  "aspect_ratio": aspect_ratio,
149
  "keyframes": {
 
154
  }
155
  }
156
 
157
+ progress(0, desc="μ΄λ―Έμ§€λ‘œλΆ€ν„° λΉ„λ””μ˜€ 생성 μ‹œμž‘ 쀑...")
158
  generation = await client.generations.create(**generation_params)
159
 
160
+ progress(0.1, desc="λΉ„λ””μ˜€ 생성 μ‹œμž‘λ¨. μ™„λ£Œ λŒ€κΈ° 쀑...")
161
 
162
  # Poll for completion
163
  start_time = asyncio.get_event_loop().time()
 
166
  if status.state == "completed":
167
  break
168
  elif status.state == "failed":
169
+ raise Exception("λΉ„λ””μ˜€ 생성 μ‹€νŒ¨")
170
 
171
  # Update progress based on time elapsed (assuming 60 seconds total)
172
  elapsed_time = asyncio.get_event_loop().time() - start_time
173
  progress_value = min(0.1 + (elapsed_time / 60) * 0.8, 0.9)
174
+ progress(progress_value, desc="λΉ„λ””μ˜€ 생성 쀑...")
175
 
176
  await asyncio.sleep(5)
177
 
178
+ progress(0.9, desc="μƒμ„±λœ λΉ„λ””μ˜€ λ‹€μš΄λ‘œλ“œ 쀑...")
179
 
180
  # Download the video
181
  video_url = status.assets.video
 
190
  break
191
  fd.write(chunk)
192
 
193
+ progress(1.0, desc="λΉ„λ””μ˜€ 생성 μ™„λ£Œ!")
194
  return file_name, ""
195
  except Exception as e:
196
+ return None, f"였λ₯˜ λ°œμƒ: {str(e)}"
197
 
198
  with gr.Blocks() as demo:
199
  gr.Markdown("# Luma AI ν…μŠ€νŠΈ-λΉ„λ””μ˜€ 생성 데λͺ¨")