akhaliq HF staff commited on
Commit
836daad
1 Parent(s): 6123d43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -14
app.py CHANGED
@@ -14,19 +14,25 @@ async def generate_video(api_key, prompt, loop=False, aspect_ratio="16:9", progr
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
@@ -46,17 +52,17 @@ async def generate_video(api_key, prompt, loop=False, aspect_ratio="16:9", progr
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)
@@ -74,19 +80,25 @@ async def image_to_video(api_key, prompt, image_url, loop, aspect_ratio, progres
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
@@ -102,9 +114,9 @@ async def image_to_video(api_key, prompt, image_url, loop, aspect_ratio, progres
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)}"
108
 
109
  with gr.Blocks() as demo:
110
  gr.Markdown("# Luma AI Text-to-Video Demo")
@@ -115,6 +127,7 @@ with gr.Blocks() as demo:
115
  prompt = gr.Textbox(label="Prompt")
116
  generate_btn = gr.Button("Generate Video")
117
  video_output = gr.Video(label="Generated Video")
 
118
 
119
  with gr.Accordion("Advanced Options", open=False):
120
  loop = gr.Checkbox(label="Loop", value=False)
@@ -123,7 +136,7 @@ with gr.Blocks() as demo:
123
  generate_btn.click(
124
  text_to_video,
125
  inputs=[api_key, prompt, loop, aspect_ratio],
126
- outputs=video_output
127
  )
128
 
129
  with gr.Tab("Image to Video"):
@@ -131,6 +144,7 @@ with gr.Blocks() as demo:
131
  img_url = gr.Textbox(label="Image URL")
132
  img_generate_btn = gr.Button("Generate Video from Image")
133
  img_video_output = gr.Video(label="Generated Video")
 
134
 
135
  with gr.Accordion("Advanced Options", open=False):
136
  img_loop = gr.Checkbox(label="Loop", value=False)
@@ -139,7 +153,7 @@ with gr.Blocks() as demo:
139
  img_generate_btn.click(
140
  image_to_video,
141
  inputs=[api_key, img_prompt, img_url, img_loop, img_aspect_ratio],
142
- outputs=img_video_output
143
  )
144
 
145
  demo.queue().launch()
 
14
  aspect_ratio=aspect_ratio
15
  )
16
 
17
+ progress(0.1, desc="Video generation started. Waiting for completion...")
18
 
19
  # Poll for completion
20
+ start_time = asyncio.get_event_loop().time()
21
  while True:
22
  status = await client.generations.get(id=generation.id)
23
  if status.status == "completed":
24
  break
25
  elif status.status == "failed":
26
  raise Exception("Video generation failed")
27
+
28
+ # Update progress based on time elapsed (assuming 60 seconds total)
29
+ elapsed_time = asyncio.get_event_loop().time() - start_time
30
+ progress_value = min(0.1 + (elapsed_time / 60) * 0.8, 0.9)
31
+ progress(progress_value, desc="Generating video...")
32
+
33
  await asyncio.sleep(5)
 
34
 
35
+ progress(0.9, desc="Downloading generated video...")
36
 
37
  # Download the video
38
  video_url = status.assets.video
 
52
 
53
  async def text_to_video(api_key, prompt, loop, aspect_ratio, progress=gr.Progress()):
54
  if not api_key:
55
+ raise gr.Error("Please enter your Luma AI API key.")
56
 
57
  try:
58
  video_path = await generate_video(api_key, prompt, loop, aspect_ratio, progress)
59
+ return gr.Video.update(value=video_path), ""
60
  except Exception as e:
61
+ return gr.Video.update(value=None), f"An error occurred: {str(e)}"
62
 
63
  async def image_to_video(api_key, prompt, image_url, loop, aspect_ratio, progress=gr.Progress()):
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)
 
80
  }
81
  )
82
 
83
+ progress(0.1, desc="Video generation started. Waiting for completion...")
84
 
85
  # Poll for completion
86
+ start_time = asyncio.get_event_loop().time()
87
  while True:
88
  status = await client.generations.get(id=generation.id)
89
  if status.status == "completed":
90
  break
91
  elif status.status == "failed":
92
  raise Exception("Video generation failed")
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.1 + (elapsed_time / 60) * 0.8, 0.9)
97
+ progress(progress_value, desc="Generating video...")
98
+
99
  await asyncio.sleep(5)
 
100
 
101
+ progress(0.9, desc="Downloading generated video...")
102
 
103
  # Download the video
104
  video_url = status.assets.video
 
114
  fd.write(chunk)
115
 
116
  progress(1.0, desc="Video generation complete!")
117
+ return gr.Video.update(value=file_name), ""
118
  except Exception as e:
119
+ return gr.Video.update(value=None), f"An error occurred: {str(e)}"
120
 
121
  with gr.Blocks() as demo:
122
  gr.Markdown("# Luma AI Text-to-Video Demo")
 
127
  prompt = gr.Textbox(label="Prompt")
128
  generate_btn = gr.Button("Generate Video")
129
  video_output = gr.Video(label="Generated Video")
130
+ error_output = gr.Textbox(label="Error Messages", visible=True)
131
 
132
  with gr.Accordion("Advanced Options", open=False):
133
  loop = gr.Checkbox(label="Loop", value=False)
 
136
  generate_btn.click(
137
  text_to_video,
138
  inputs=[api_key, prompt, loop, aspect_ratio],
139
+ outputs=[video_output, error_output]
140
  )
141
 
142
  with gr.Tab("Image to Video"):
 
144
  img_url = gr.Textbox(label="Image URL")
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)
148
 
149
  with gr.Accordion("Advanced Options", open=False):
150
  img_loop = gr.Checkbox(label="Loop", value=False)
 
153
  img_generate_btn.click(
154
  image_to_video,
155
  inputs=[api_key, img_prompt, img_url, img_loop, img_aspect_ratio],
156
+ outputs=[img_video_output, img_error_output]
157
  )
158
 
159
  demo.queue().launch()